2017-03-11 145 views
1

我写了一个tic tac toe游戏,它不是针对电脑的。简而言之,用户输入板上的位置(3X3),并且该位置被转换为xo(取决于x和o的转向)。如果x或o填充一列,一行或一个对角线 - 它会胜出。为什么打印时矩阵(2d阵列)显示不正确

我在我的代码中遇到了一些问题,
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; 
} 

实施例:
enter image description here

+0

请问您可以转储您的输出样本以了解您的意思吗?也许是一张图片,我想帮忙。 –

回答

1

有一个misktake同时服用的位置的输入在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]) 

,其中的行和列始终为零。