Post
by **Subeen** » Sat Aug 14, 2004 5:21 pm

I got WA with my code. some one please post some tests. or you can help me to find the bug in my program. here it is:

[cpp]#include <stdio.h>

char board[6][6];

char *str = "xo";

int game_over()

{

int i, j;

for(i=0; i<4; i++)

for(j=0; j<4; j++)

if(board*[j]=='.') return 0;*

return 1;

}

int wins(int player)

{

int i, j, count;

char ch = str[player];

count = 0;

for(i=0, j=0; i<4 && j<4; i++, j++)

if(board*[j] == ch)*

count++;

if(count==4) return 1;

count = 0;

for(i=0, j=3; i<4 && j>=0; i++, j--)

if(board*[j] == ch)*

count++;

if(count==4) return 1;

for(i=0; i<4; i++)

{

count = 0;

for(j=0; j<4; j++)

if(board*[j] == ch)*

count++;

if(count==4) return 1;

}

for(i=0; i<4; i++)

{

count = 0;

for(j=0; j<4; j++)

if(board[j]* == ch)*

count++;

if(count==4) return 1;

}

return 0;

}

int try_to_win(int player)

{

int i, j;

/* check if board is full and no one wins */

if(game_over())

return 1;

/* now find a position where player wins (if any) */

for(i=0; i<4; i++)

{

for(j=0; j<4; j++)

{

if(board*[j]=='.')*

{

board*[j] = str[player];*

if( wins(player) )

return player;

board*[j] = '.';*

}

}

}

/* now find a position where opponent player wins (if any) */

for(i=0; i<4; i++)

{

for(j=0; j<4; j++)

{

if(board*[j]=='.')*

{

board*[j] = str[!player];*

if( wins(!player) )

{

board[i][j] = str[player];

if( !try_to_win(!player))

return !player;

}

board[i][j] = '.';

}

}

}

/* otherwise try to put player in next available position */

for(i=0; i<4; i++)

{

for(j=0; j<4; j++)

{

if(board[i][j]=='.')

{

board[i][j] = str[player];

if( try_to_win(!player) )

return 1;

board[i][j] = '.';

}

}

}

return 0;

}

int main()

{

char in[10];

int i, j, flag, x, o;

while(gets(in) && in[0]!='$')

{

for(i=0; i<4; i++) gets(board[i]);

x = o = 0;

for(i=0; i<4; i++)

for(j=0; j<4; j++)

if(board[i][j]=='x') x++;

else if(board[i][j]=='o') o++;

if(x<=2 && o<=2)

{

puts("#####");

continue;

}

flag = 1;

for(i=0; i<4; i++)

{

for(j=0; j<4; j++)

{

if(board[i][j]=='.')

{

board[i][j] = 'x';

if( !try_to_win(1) )

{

flag = 0;

break;

}

}

}

if(!flag) break;

}

if(flag) puts("#####");

else

printf("(%d,%d)\n", i, j);

}

return 0;

}[/cpp]

thank u.