請問C/C++高手, 有個IF condition我不知道是check什麽的, 謝謝?

來源: iwant2know 2006-09-25 08:15:59 [] [舊帖] [給我悄悄話] 本文已被閱讀: 次 (2972 bytes)
請問C/C++高手, 以下的program是一個simple tic tac toe game. 其中的void get_computer_move(void) function裏, 我有一點看不懂, 有一個condition 是
if(i*j==9) {
printf("draw\n");
exit(0);}
請哪位高人告訴, 這個 if(i*j==9)是在check什麽, 我看不出來它的存在意義. 小弟這裏多謝了! 以下是完整的code.



#include
#include

char matrix[3][3]; /* the tic tac toe matrix */

char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);

int main(void)
{
char done;

printf("This is the game of Tic Tac Toe.\n");
printf("You will be playing against the computer.\n");

done = ' ';
init_matrix();
do{
disp_matrix();
get_player_move();
done = check(); /* see if winner */
if(done!= ' ') break; /* winner!*/
get_computer_move();
done = check(); /* see if winner */
} while(done== ' ');
if(done=='X') printf("You won!\n");
else printf("I won!!!!\n");
disp_matrix(); /* show final positions */

return 0;
}

/* Initialize the matrix. */
void init_matrix(void)
{
int i, j;

for(i=0; i<3; i++)
for(j=0; j<3; j++) matrix[i][j] = ' ';
}

/* Get a player's move. */
void get_player_move(void)
{
int x, y;

printf("Enter X,Y coordinates for your move: ");
scanf("%d%*c%d", &x, &y);

x--; y--;

if(matrix[x][y]!= ' '){
printf("Invalid move, try again.\n");
get_player_move();
}
else matrix[x][y] = 'X';
}

/* Get a move from the computer. */
void get_computer_move(void)
{
int i, j;
for(i=0; i<3; i++){
for(j=0; j<3; j++)
if(matrix[i][j]==' ') break;
if(matrix[i][j]==' ') break;
}

if(i*j==9) {
printf("draw\n");
exit(0);
}
else
matrix[i][j] = 'O';
}

/* Display the matrix on the screen. */
void disp_matrix(void)
{
int t;

for(t=0; t<3; t++) {
printf(" %c | %c | %c ",matrix[t][0],
matrix[t][1], matrix [t][2]);
if(t!=2) printf("\n---|---|---\n");
}
printf("\n");
}

/* See if there is a winner. */
char check(void)
{
int i;

for(i=0; i<3; i++) /* check rows */
if(matrix[i][0]==matrix[i][1] &&
matrix[i][0]==matrix[i][2]) return matrix[i][0];

for(i=0; i<3; i++) /* check columns */
if(matrix[0][i]==matrix[1][i] &&
matrix[0][i]==matrix[2][i]) return matrix[0][i];

/* test diagonals */
if(matrix[0][0]==matrix[1][1] &&
matrix[1][1]==matrix[2][2])
return matrix[0][0];

if(matrix[0][2]==matrix[1][1] &&
matrix[1][1]==matrix[2][0])
return matrix[0][2];

return ' ';
}

所有跟帖: 

那個if condition?俺看一切都很clear啊,除非你不知道 -衷吏- 給 衷吏 發送悄悄話 (14 bytes) () 09/25/2006 postreply 08:27:50

最傻的一個算法,根本不能稱為算法 -德州老外- 給 德州老外 發送悄悄話 (184 bytes) () 09/25/2006 postreply 08:56:51

回複:請問C/C++高手, 有個IF condition我不知道是check什麽的, 謝謝 -天邊的星星- 給 天邊的星星 發送悄悄話 (228 bytes) () 09/25/2006 postreply 09:05:30

多謝各位高手的指點. -iwant2know- 給 iwant2know 發送悄悄話 (0 bytes) () 09/25/2006 postreply 11:35:48

請您先登陸,再發跟帖!

發現Adblock插件

如要繼續瀏覽
請支持本站 請務必在本站關閉/移除任何Adblock

關閉Adblock後 請點擊

請參考如何關閉Adblock/Adblock plus

安裝Adblock plus用戶請點擊瀏覽器圖標
選擇“Disable on www.wenxuecity.com”

安裝Adblock用戶請點擊圖標
選擇“don't run on pages on this domain”