This is the verification from a connect four game prototype, but it seems I've done something wrong.
I want that everytime the player is making a move, the function will verify if there he won or not, by verifying vertically, horizontally, and eventually, on the diagonal.
But it seems that it does not verify correctly, because in some cases, even though there are only 2 moves made, the functions returns 1.
int verifyGame(int gamePosition, int gameVariable, char gameArray[HEIGTH][WIDTH])
{
if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition + 1][gameVariable] == gameArray[gamePosition + 2][gameVariable] == gameArray[gamePosition + 3][gameVariable]) //verify vertically
return 1;
else
if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition][gameVariable - 3] == gameArray[gamePosition][gameVariable - 2] == gameArray[gamePosition][gameVariable - 1]) //verify horizontally
return 1;
else
if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition][gameVariable - 2] == gameArray[gamePosition][gameVariable - 1] == gameArray[gamePosition][gameVariable + 1])
return 1;
else
if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition][gameVariable - 1] == gameArray[gamePosition][gameVariable + 1] == gameArray[gamePosition][gameVariable + 2])
return 1;
else
if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition][gameVariable + 1] == gameArray[gamePosition][gameVariable+ 2] == gameArray[gamePosition][gameVariable + 3])
return 1;
//verify diagonally
else return 0;
};
This is where the function is called. The switch verifies the users input, and then it places the value in the matrix, and then verifies for won
printf("playerPick is : %d\n", playerPick);
fflush(stdout);
switch(playerPick)
{
case 1:
if(gameVariables[0] >0 && gameVariables[0] < 7)
{
--gameVariables[0];
gameArray[gameVariables[0]][0] = (char) 82;
ifWon = verifyGame(gameVariables[0], 0, gameArray);
}
printArray(gameArray);
break;
case 2:
if(gameVariables[1] >0 && gameVariables[1] < 7)
{
--gameVariables[1];
gameArray[gameVariables[1]][1] = (char) 82;
ifWon = verifyGame(gameVariables[1], 1, gameArray);
}
printArray(gameArray);
break;
case 3:
if(gameVariables[2] >0 && gameVariables[2] < 7)
{
--gameVariables[2];
gameArray[gameVariables[2]][2] = (char) 82;
ifWon = verifyGame(gameVariables[2], 2, gameArray);
}
printArray(gameArray);
break;
case 4:
if(gameVariables[3] >0 && gameVariables[3] < 7)
{
--gameVariables[3];
gameArray[gameVariables[3]][3] = (char) 82;
ifWon = verifyGame(gameVariables[3], 3, gameArray);
}
printArray(gameArray);
break;
case 5:
if(gameVariables[4] >0 && gameVariables[4] < 7)
{
--gameVariables[4];
gameArray[gameVariables[4]][4] = (char) 82;
ifWon = verifyGame(gameVariables[4], 4, gameArray);
}
printArray(gameArray);
break;
case 6:
if(gameVariables[5] >0 && gameVariables[5] < 7)
{
--gameVariables[5];
gameArray[gameVariables[5]][5] = (char) 82;
ifWon = verifyGame(gameVariables[5], 5, gameArray);
}
printArray(gameArray);
break;
case 7:
if(gameVariables[6] >0 && gameVariables[6] < 7)
{
--gameVariables[6];
gameArray[gameVariables[6]][6] = (char) 82;
ifWon = verifyGame(gameVariables[6], 6, gameArray);
}
printArray(gameArray);
break;
}
printf("%d %d %d %d %d %d %d\n", gameVariables[0], gameVariables[1], gameVariables[2], gameVariables[3], gameVariables[4], gameVariables[5], gameVariables[6]);
printf("ifwon : %d\n", ifWon);
#Weather Vane's answer is correct. The logic used in your original post is not correct for a verification.
One reason you may not have caught it yourself may be the complicated way it was written. Try simplifying the user input verification code: (Range checking the user input values is all that is necessary.)
//User input range checking:
if((gamePosition >= x)&& //where `x` is minimum for gamePosition
(gamePosition <= y)&& //where `y` is maximum for gamePosition
(gameVariable >= z)&& //where `z` is minimum for gameVariable
(gameVariable <= w)) //where `w` is maximum for gameVariable
{//continue }
else
{
printf("Invalid value. Please re-enter");
return -1;
}
Another opportunity for simplification is to note that each of your case statements contain identical code, with the exception of the value of the case. Because of this the entire switch(...){...} can be replaced with a single if statement:
//assuming playerPick >= 1
if(gameVariables[playerPick-1] >0 && gameVariables[playerPick-1] < 7)
{
--gameVariables[playerPick-1];
gameArray[gameVariables[playerPick-1]][playerPick-1] = (char) 82;
ifWon = verifyGame(gameVariables[playerPick-1], playerPick-1, gameArray);
}
printArray(gameArray);
Also note that although the statement:
gameArray[gameVariables[0][0] = (char) 82; //what is 82?
is perfectly legal, the variable gameArray[0][0] is just a char, so casting the value 82 is not necessary. Also, C syntax provides a way to pull out the ASCII decimal value of the character by surrounding it with the graves symbol, allowing the following form, which is more readable:
gameArray[gameVariables[0]][0] = `R`; //intuitive
You cannot chain equality testing as you are attempting. The code will execute, but not as you suppose. Your code
if(gameArray[gamePosition][gameVariable] ==
gameArray[gamePosition + 1][gameVariable] ==
gameArray[gamePosition + 2][gameVariable] ==
gameArray[gamePosition + 3][gameVariable])
must be split up into individual tests, such as:
if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition + 1][gameVariable] &&
gameArray[gamePosition][gameVariable] == gameArray[gamePosition + 2][gameVariable] &&
gameArray[gamePosition][gameVariable] == gameArray[gamePosition + 3][gameVariable])
and on the other lines too.
Related
i want to find the alphabet charachter that is 7 charachters before mine ,so i wrote this function to do so and it works fine :
char find7_before(char letter){
switch (letter){
case 'g':
return 'z';
break;
case 'f':
return 'y';
break;
case 'e':
return 'x';
break;
case 'd':
return 'w';
break;
case 'c':
return 'v';
break;
case 'b':
return 'u';
break;
case 'a':
return 't';
break;
default:
return (char)(((int)letter) - 7);
}
}
but i think i can do it in a smarter way without all of these cases but i just can't figure it out ! (i figured how to find 7 letters after in a cyclic way ) any help or an idea or a hint ?
thank you :)
Assuming ASCII with continuous ['a' , 'z']...
Simply "mod 26".
letter = ((letter - 'a' - 7) mod 26) + 'a';
Yet C does not have a Euclidean mod operator.
See What's the difference between “mod” and “remainder”?
So create a Euclidean mod function - save for later use.
int modulo_Euclidean(int a, int b) {
int m = a % b;
if (m < 0) {
// m += (b < 0) ? -b : b; // avoid this form: it is UB when b == INT_MIN
m = (b < 0) ? m - b : m + b;
}
return m;
}
letter = modulo_Euclidean(letter - 'a' - 7, 26) + 'a';
Alternately code can take advantage that 'a' has a value of 97 and not subtract so much that letter - ('a'%26) - 7 becomes negative.
letter = (letter - ('a'%26) - 7)%26 + 'a';
Pedantic code would not assume continuous ['a' , 'z'] and perform more elaborate code.
subtract 'a' (so now it's in 0-25), subtract 7, and mod 26. Then add 'a' again so it's back to a char.
In my opinion, the clearest and simplest way is to use an if statement:
char find7_before(char letter) {
char value = letter - 7;
if (value < 'a') {
value += 26;
}
return value;
}
The precondition here is the letter is between 'a' and 'z', inclusive.
This technique generalizes as well:
char findn_before(char letter, int n) {
char value = letter - n;
if (value < 'a') {
value += 26;
}
return value;
}
Precondition on letter is the same as before; n must be between 0 and 26, inclusive.
I have created an array full of letters and I'm stuck on implementing the function for the cell to randomly move in one of 8 directions (N, NE, E, SE, S, SW, W, NW). I've used a switch statement for the basic 4 direction but couldn't figure out the other 4 directions.
void randomStep()
{
if ((island[ro][co + 1] != ('B'||'L') || co == NUMROWS - 1 )&& (island[ro + 1][co] != ('B'||'L') || ro == NUMCOLS -1) && (island[ro - 1][co] != ('B'||'L') || ro == 0)
&& (island[ro][co - 1] != ('B'||'L') || co == 0))
break;
int direction = rand() % 8;
switch (direction) {
case 0: if (co < NUMROWS - 1 && island[ro][co + 1] == 'B'||'L'){ //move right
co++;
break;
}
case 1: if (ro < NUMCOLS -1 && island[co + 1][ro] == 'B'||'L') { //move down
ro++;
break;
}
case 2: if (ro > 0 && island[ro - 1][co] == 'B'||'L'){ //move up
ro--;
break;
}
case 3: if (co > 0 && island[ro][co - 1] == 'B'||'L') { //move left
co--;
break;
}
You can simply combine the conditions and results of two other cases. Here is an example for NW (left and up)
case 4: if (ro > 0 && co > 0 && island[ro - 1][co - 1] == 'B'||'L') { //move up & left
ro--;
co--;
}
break;
Note that I have moved the position of the break; to be outside the if code block.
I think you may also have an error with
... island[ro - 1][co - 1] == 'B'||'L' ...
which I guess should be
... island[ro - 1][co - 1] == 'B' || island[ro - 1][co - 1] == 'L' ...
and similarly in the other cases.
You can move parts of your code into functions:
bool mayGoUp(int ro, int co)
{
return ro > 0;
}
bool mayGoDown(int ro, int co)
{
return ro < NUMROWS - 1;
}
bool mayGoLeft(int ro, int co)
{
return co > 0;
}
bool mayGoRight(int ro, int co)
{
return co < NUMCOLS - 1;
}
Note: I changed the logic a bit: from co < NUMROWS - 1 to co < NUMCOLS - 1; not sure which one is the correct one.
Then you can combine them in a straightforward way:
bool mayGoUpLeft(int ro, int co)
{
return mayGoUp(ro, co) && mayGoLeft(ro, co);
}
Then, using them in your code will make your code clearer:
switch (direction) {
case 0:
if (MayGoRight(ro, co) && island[ro][co + 1] == ...
{ //move right
co++;
break;
}
...
case 99:
if (MayGoUpRight(ro, co) && ...
{ // move up and right
ro--;
co++;
break;
}
I'm doing homework for UNI and I got to do a Tic-Tac-Toe without any decision taken by player, the moves are all chosen randomly. So if the character on matrix is ' ' it means it's free, while if it's 'X' or 'O' it should generate another move. This is the code (language C):
if (playerTurn == 1){
playerSymb = 'X';
}
else if (playerTurn == 2){
playerSymb = 'O';
}
if (matrix[rand1][rand2] == ' '){
matrix[rand1][rand2] = playerSymb;
} else if(matrix[rand1][rand2] == 'X' || matrix[rand1][rand2] == 'O'){
do{
randAlt1 = MINRND + rand()%(MAXRND - MINRND +1);
randAlt2 = MINRND + rand()%(MAXRND - MINRND +1);
}while (matrix[randAlt1][randAlt2] != 'X' && matrix[randAlt1][randAlt2] != 'O');
matrix[randAlt1][randAlt2] = playerSymb;
}
I did not copied the whole code because it's not finished at all, i just need help solving this. But if I try to run this, the Symbols can be overwritten, like if I have a 'X' at matrix[1][2], it's possible that it will be a 'O' after some turns. So how can I make moves do not overwrite? (sorry for bad english).
Just put correct condition:
while (matrix[randAlt1][randAlt2] == 'X' || matrix[randAlt1][randAlt2] == 'O')
(i.e. try again if this cell is not empty)
Also it is easy to simplify your code without loosing of anything:
randAlt1 = rand1;
randAlt2 = rand2;
while (matrix[randAlt1][randAlt2] != ' ') {
randAlt1 = MINRND + rand()%(MAXRND - MINRND +1);
randAlt2 = MINRND + rand()%(MAXRND - MINRND +1);
}
matrix[randAlt1][randAlt2] = (playerTurn == 1) ? 'X' : 'O';
And it is better to add loop guard to prevent infinite loop (or to add special checks for this case):
randAlt1 = rand1;
randAlt2 = rand2;
int nbAttempts = 0;
while (matrix[randAlt1][randAlt2] != ' ' && nbAttempts < 100) {
randAlt1 = MINRND + rand()%(MAXRND - MINRND +1);
randAlt2 = MINRND + rand()%(MAXRND - MINRND +1);
nbAttempts++;
}
if (matrix[randAlt1][randAlt2] != ' ') {
// show error message and stop the game
}
matrix[randAlt1][randAlt2] = (playerTurn == 1) ? 'X' : 'O';
You choose an arbitrary position and then test if it is free – possibly multiple times. But you can also choose a number of a free position and then find it.
First set up a turn counter
int turnNo = 0;
then make a loop for alternate moves, which chooses one of 9-turnNo unused positions, finds it, marks is with a player mark and tests if the move made a line of three:
while(turnNo < 9)
{
char currPlayerMark = ...choose 'X' or 'O';
int freePos = 9 - turnNo;
int currPos = rand() % freePos; // 0 .. freePos-1
for(x=0; x<3; x++)
{
for(y=0; y<3; y++)
{
if(matrix[x][y] == ' ') // a free position
if(--currPos < 0) // the sought one
break; // break the inner loop
}
if(currPos < 0)
break; // break the outer loop
}
matrix[x][y] = currPlayerMark;
if(test_for_win_position(x,y))
{
message_a_win_of_player(currPlayerMark);
break; // turnNo < 9 here
}
turnNo ++;
}
Finally test if the loop terminated with no 'win':
if(turnNo == 9)
message_its_a_draw(); // no-one wins
A function to test the win position might look like this:
int test_for_win_position(int x, int y)
{
char mark = matrix[x][y];
// check a column
if(matrix[x][0] == mark && matrix[x][1] == mark && matrix[x][2] == mark)
return 1;
// check a row
if(matrix[0][y] == mark && matrix[1][y] == mark && matrix[2][y] == mark)
return 1;
// check one diagonal
if(x==y)
if(matrix[0][0] == mark && matrix[1][1] == mark && matrix[2][2] == mark)
return 1;
// check another diagonal
if(x+y==2)
if(matrix[0][2] == mark && matrix[1][1] == mark && matrix[2][0] == mark)
return 1;
// current player has not won (yet)
return 0;
}
I have made this data.txt file and imported it into matlab using import wizard.
this is the result :
This table is my data base and I want to search into it column wise (each variable). I used strread as this lets me to search columns, for example :
var1=strread(VarName12(1,1),'%d',"delimiter','|')
I wrote several lines of code using strread in a script and then run it. when I check the workspace var1 and other variables that included strread result are missing and typing them in command window gives me this : Undefined function or variable 'End'. I typed sttread in command window and used variables in data.txt and got the result but it doesn't work when I run my script. I can't understand what's the problem? ( I'm using matlab R2013a and there is warning that says : using strread is not recommende. use TEXTSCAN instead. I can't do the same with textscan also strread works properly when written in workspace ). I don't know what is the problem. can give a clue?
EDIT :
this is the code
(Note: I'm writing a "biometric recognition system based on ear" and the database is data extracted from images1 to 5). This is the classification section of the code:
Nbcoordinates = vertcat(BEcell{:,1});
Necoordinates = vertcat(BEcell{:,2});
import DATA.txt.*; % import our data set
for i=1:5
token1 = 0; token2 = 0; token3 = 0; token4 = 0; token5 = 0; token6 = 0; token7 = 0;
if(VarName1(i, 1) == V{1,1}(1,2) && VarName3(i, 1) == V{1,2}(1,2) && VarName5(i,1) == V{1,3}(1,2) && VarName2(i,1) == V{1,1}(1,3) && VarName4(i,1) == V{1,2}(1,3) && VarName6(i,1) == V{1,3}(1,3))
% Check number of endings for each component
numberofendings = strread(VarName10{i,1}, '%d', 'delimiter', '|');
for j=1:size(Ne,1)
if(numberofendings(j,1)~=Ne(j,1))
break;
end
end
if(j >= size(Ne,1))
token1 = 1;
end
% check number of bifurcations for each components
numberofbifurcations = strread(VarName11{i,1}, '%d', 'delimiter', '|');
for j=1:size(Nb,1)
if(numberofbifurcations(j,1) ~= Nb(j,1))
break;
end
end
if(j >= Nb)
token2 = 1;
end
% Check Intersections1,2,3 Cordinates
Intercoordinate1 = strread(VarName7{i,1}, '%f', 'delimiter', '|');
m = 1;
for j=1:NI1
if((Intersection1(j, 1) ~= Intercoordinate1(m,1)) || (Intersection1(j, 2) ~= Intercoordinate1(m+1,1)))
break;
end
m = m + 2;
end
if(j >= NI1)
token3 = 1;
end
Intercoordinate2 = strread(VarName8{i,1}, '%f', 'delimiter', '|');
m = 1;
for j=1:NI2
if((Intersection2(j, 1) ~= Intercoordinate2(m,1)) || (Intersection2(j, 2) ~= Intercoordinate2(m+1,1)))
break;
end
m = m + 2;
end
if(j >= NI2)
token4 = 1;
end
Intercoordinate3 = strread(VarName9{i,1}, '%s', 'delimiter', '|');
m = 1;
for j=1:NI3
bi1 = cell2mat(Intercoordinate3(m, 1));
bi2 = cell2mat(Intercoordinate3(m + 1, 1));
if((Intersection3(j, 1) ~= str2num(bi2)) || (Intersection3(j, 2) ~= str2num(bi2)))
break;
end
m = m + 2;
end
if(j >= NI3)
token5 = 1;
end
% Check endings coordinate of each component
Endcoor = strread(VarName12{i,1}, '%d', 'delimiter', '|');
m = 1;
for j=1:sum(Ne)
en1 = Endcoor(m, 1);
en2 = Endcoor(m + 1, 1);
if((Necoordinates(j, 1) ~= en1) || (Necoordinates(j, 2) ~= en2))
break;
end
m = m + 2;
end
if(j >= sum(Ne))
token6 = 1;
end
% check bifurcation coordinates of each component
Bifcoor = strread(VarName13{i,1}, '%d', 'delimiter', '|');
m = 1;
for j=1:sum(Nb)
en1 = Bifcoor(m, 1);
en2 = Bifcoor(m + 1, 1);
if((Nbcoordinates(j, 1) ~= en1) || (Nbcoordinates(j, 2) ~= en2))
break;
end
m = m + 2;
end
if(j >= sum(Nb))
token7 = 1;
end
if(token1 ==1 && token2 == 1 && token3== 1 && token4 == 1 && token5 == 1 && token6== 1 && token7==1 )
disp(['This image is a member of Class ',num2str(i)]);
break;
end
end
end
if(token1 == 0 || token2 == 0 || token3 == 0 || token4 == 0 || token5 == 0 || token6== 0 || token7==0)
disp('Doesn;t exist in the data base ');
end
end
i am having a problem figuring out an algorithm for this problem,been trying for few days without success,here is a pic of what im trying to obtain:
http://i.stack.imgur.com/X70nX.png
Here is my code tried many differents solutions but always get stuck at the same point:(Sorry for mixed language the important part is in english)
ps
im not supposed to use functions to solve this problem only loops and array.
EDIT
after much fixing it does the walk but seldomly crashes
any idea?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void){
char box[10][10];
int i,j;
int move,row,col;
char letter='A';
srand(time(NULL));
printf("\n\tSTART\n\n");
for(i=0;i < 10 ;i++)/* righe */
{
for(j=0;j < 10;j++) /* colonne */
{
box[i][j] = '.'; /* assegno . a tutti gli elementi dell array */
if(j == 9)
printf("%c%c\n", box[i][j]); /* giustifico ogni 10 elementi dell array j(0-9) */
else
printf("%c%c", box[i][j]);
}
}
/* LETS START */
printf("\n\n Inizia il gioco\n\n");
/* random place to start */
row = rand() % 9;
col = rand() % 9;
box[row][col]= 'A';
while(letter <= 'Z')
{
if(box[row+1][col] == '.' || box[row-1][col] == '.' || box[row][col+1] == '.' || box[row][col-1] == '.' )
{
move=rand() % 4;
switch(move){
case 0: /* Going UP */
if((row != 0) && (box[row-1][col] == '.'))
{
box[row-1][col]=++letter;
box[row--][col];
}else{
move=rand() % 4;
}
case 1:/* Going Down */
if((row != 9) && (box[row+1][col] == '.'))
{
box[row+1][col]=++letter;
box[row++][col];
}else{
move=rand() % 4;
}
case 2: /*Going Left */
if((col != 0) && (box[row][col-1] == '.'))
{
box[row][col-1]=++letter;
box[row][col--];
}else{
move=rand() % 4;
}
case 3: /* Going Right */
if((col != 9) && (box[row][col+1] == '.') )
{
box[row][col+1]=++letter;
box[row][col++];
}else{
move=rand() % 4;
}
}
}else{
printf("\n\nBloccato a %c\n\n", letter);
break;
}
}
/* FINE */
for(i=0;i<10;i++)/* righe */
{
for(j=0;j<10;j++) /* colonne */
{
if(j == 9)
printf("%c%c\n", box[i][j]); /* giustifico ogni 10 elementi dell array j(0-9) */
else
printf("%c%c", box[i][j]);
}
}
return 0;
}
You need to update row and col inside the loop.
Otherwise you'll always attempt to walk from the position of the 'A'.
... and once all 4 directions are filled, you're stuck in a infinite loop
. . . . .
. . B . .
. E A C .
. . D . .
Even when you update row and col inside the loop (and correct the == mistake), you have to handle a problem: suppose the first spot (the 'A') is the top left corner and the next random directions are East, South, South, West, and North. ... now what? :)
A B .
F C .
E D .
. . .
It's not a good idea to "reroll" the random number when you discover that you cannot go in some direction, because if you have bad luck, you get the same number twice (or even 3 or 4 or more times) - so even if you generated 4 random numbers and they all failed, that doesn't mean that you're stuck.
You can solve this problem by generating one number, and trying all 4 possible directions starting from it:
If the random number generator returned 0: check 0, 1, 2, 3
If the random number generator returned 1: check 1, 2, 3, 0
If the random number generator returned 2: check 2, 3, 0, 1
If the random number generator returned 3: check 3, 0, 1, 2
Implemented by the following code:
desired_move = rand();
success = 0;
for (i = 0; i < 4 && !success; ++i)
{
move = (desired_move + i) % 4;
switch (move)
{
case 0: // Go up
if (row > 0 && box[row - 1][col] == '.')
{
row = row - 1;
success = 1;
}
break;
case 1: // Go down
...
}
}
if (!success) // Tried all 4 directions but failed! You are stuck!
{
goto START_OVER; // or whatever else
}
Note that this algorithm is not very random: if you cannot go up, there is a greater chance that you go down than right or left. If you want to fix it, you can pick a random permutation of 4 directions instead of checking the directions sequentially:
const int permutation_table[24][4] = {
{0, 1, 2, 3},
{0, 1, 3, 2},
{0, 2, 1, 3},
...
{3, 2, 1, 0}
};
index = rand() % 24;
for (i = 0; i < 4; ++i)
{
move = permutation_table[index][i];
switch (move) {
... // As above
}
}
When you're in for loop.
Draw a possible direction
int direction = rand()%4;
Check all possible directions if the drawed one is invalid (not in array or not a ".")
int i=-1;
while( ++i < 4 )
{
switch(direction)
{
case 0:
if( row-1 >= 0 && box[row-1][col] == '.' ) {
--row;
i = -1;
}
break;
case 1:
if( col+1 < 10 && box[row][col+1] == '.' ) {
++col;
i = -1;
}
break;
case 2:
if( row+1 < 10 && box[row+1][col] == '.' ) {
++row;
i = -1;
}
break;
case 3:
if( col-1 >= 0 && box[row][col-1] == '.' ) {
--col;
i = -1;
}
break;
}
if( i != -1 ) {
direction = (direction+1)%4;
}
else {
break;
}
}
If there's no valid move end the for loop>
if( i == 4 ) {
break;
}
Otherwise write a letter to the table cell and update row/col position.
box[row][col] = letter;
And... that's all I guess. This is greedy algorithm so you don't need any optimizations (at least I don't see any in exercise requirements.
It looks like you are breaking out of your switch statement if you try to go in a direction that isn't valid, but you increment your counter anyway. Try to check another random direction if that happens.
where exactly does it break?
from what I can see at a glance is that you have a chance that It_that_walks gets in position from witch it cant go anywhere:
A B C D .
. I J E .
. H G F .
where after J?
There is no need for the && (box[row][col-1]= '.')
Allso, it is wrong (assignment instead of comparison), it should be: && (box[row][col-1]== '.') (but you dont need it alltogether)