x
或o
(取决于x和o的转弯)。如果x或o填充列,行或对角线 - 它将获胜。
我的代码中遇到了一些问题,
1.矩阵没有正确显示,它将每个选定的位置放在同一位置
2.程序以x应该开始,但跳过o。
如果你能解释我哪里出错了,我会很高兴的 这是我的代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#define COL 3
#define ROW 3
int validity(char board[][COL], int player,int row,int col);
void turns(char board[][COL],int player);
int win(char board[][COL]);
void printBoard(char board[][COL]);
/*
main will determine the turn of x and o until there is a victory
in: none
out: 0
*/
int main(void)
{
char board[ROW][COL] = { { 0 } };
int player = 'o';
do{
if (player == 'x')
{
player = 'o';
turns(board,player);
}
else if (player == 'o')
{
player = 'x';
turns(board,player);
}
} while (win(board) == 0);
getchar();
return 0;
}
/*this func will print the board
in: board
out: none
*/
void printBoard(char board[][COL])
{
int i = 0, j = 0;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("\n");
}
/*
this func will assign 'x' or 'o' to the entered location on the board
in: the board and x or o
out: none
*/
void turns(char board[][COL],int player)
{
int row = 0, col = 0;
printf("It is %c turn's. \n", player);
printf("Enter a location on the board: \n");
scanf("%d", &board[row][col]);
if (validity(board,player,row,col) == 1)
{
board[row][col] = player;
}
else
{
turns(board, player);
}
printBoard(board);
}
/*
this func will determine if x or o has won
in: the board
out: 0 = noone has won
1 = x or o has won
*/
int win(char board[][COL])
{
int flag = 0;
int i = 0, j = 0, k = 0;
for (i = 0; i < ROW; i++) // this loop will check if o or x won by filling a row
{
if (board[i][j] == 'x')
{
printf("X won!");
flag = 1;
}
else if (board[i][j] == 'o')
{
printf("O won!");
flag = 1;
}
else
{
flag = 0;
}
}
for (j = 0; j < COL; j++) // this loop will check if o or x won by filling a column
{
if (board[i][j] == 'x')
{
printf("X won!");
flag = 1;
}
else if (board[i][j] == 'o')
{
printf("O won!");
flag = 1;
}
else
{
flag = 0;
}
}
for (k = 0; k < COL; k++) // this loop will check if x or o won by filling a diagonal
{
if (board[k][k] == 'x')
{
printf("X won!");
flag = 1;
}
else if (board[k][k] == 'o')
{
printf("O won!");
flag = 1;
}
else
{
flag = 0;
}
}
return flag;
}
/*this func will check the validity of a choice that the player has made (if the location is taken by o or x or not).
in: board, x / o, the location
out: 0 = the location is taken
1 = the location is valid
*/
int validity(char board[][COL], int player,int row,int col)
{
int check = 0;
if (board[row][col] == 'o' && player == 'x' || board[row][col] == 'x' && player == 'o')
{
printf("Invalid choice.\n");
check = 0;
}
else
{
check = 1;
}
return check;
}
答案 0 :(得分:1)
在turns
函数中输入位置时存在误解。
该功能应如下
void turns(char board[][COL],int player)
{
int row = 0, col = 0;
printf("It is %c turn's. \n", player);
printf("Enter a location on the board: \n");
scanf("%d %d", &row,&col);
if (validity(board,player,row,col) == 1)
{
board[row][col] = player;
}
else
{
turns(board, player);
}
printBoard(board);
}
你的是
int row=0,col=0;
scanf("%d",&board[row][col])
其中行和列始终为零。