the program randomly walks through from element to element always going up, down, left, right by one element. the elements visited by the program will be labeled with the letters a through z in the order visited. If no place for a letter to go then the program terminates.

My problem is that when it reached end of row, it goes to next row and column 0 and populates. I don't want it to go out of bounds or go to next row if at end of row but don't know how to fix it. Otherwise the code works fine.

I would like some help in fixing that and after fixed, please provide some ideas to simplify the code if possible. tks
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void) {

int i, j, k, direction;
char board[10][10];
const char letters[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

srand ((unsigned) time(NULL));

for (i = 0; i < 10; i++)
  for (j = 0; j < 10; j++)
    board[i][j] = '.';

i = 0;
j = 0;    
k = 1;
board[i][j] = letters[0];
while (k < 26) {
  direction = rand() % 4;
    switch (direction) {
      case 0: if (board[i][j + 1] == '.'){
              board[i][j +1] = letters[k];  //move right 
              k++; j++;}      
              if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
              k = 27;
              break;     
      case 1: if (board[i + 1][j] == '.') {
              board[i + 1][j] = letters[k];  //move down
              k++; i++; }
              if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
              k = 27;
              break;
      case 2: if (board[i - 1][j] == '.'){
              board[i - 1][j] = letters[k];  //move up
              k++; i--; }
              if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
              k = 27;
              break;      
      case 3: if (board[i][j - 1] == '.') {
              board[i][j - 1] = letters[k];  //move left
              k++; j--;}
              if (board[i][j + 1] != '.' && board[i + 1][j] != '.' && board[i - 1][j] != '.' && board[i][j - 1] != '.')
              k = 27;
              break;
}
}
for (i = 0; i < 10; i++) {
  for (j = 0; j < 10; j++)
    printf ("%4c", board[i][j]);
  printf ("\n");
}

return 0;

}