Trying to determine who wins in a game of tic tac toe,I am new to programming. Currently getting user has won after only entering 1 X or O input. Inputs must be entered with 2 int, row and coloumn. Any help is greatly appreciated!

#include <stdio.h>
#include <stdlib.h>

void drawBoard(char board[][3])
{
    int rows, columns;
    for ( rows = 0 ; rows < 3 ; rows++ )
    {
            for ( columns = 0 ; columns < 3 ; columns++ )
            {
               if(board[rows][columns]){
                    printf( "|%c", board[rows][columns] );
                }else{
                   printf("| ");
                }
            }
            printf("|\n");
        }
}


 int main()
{
    char game[3][3]={{0}};
    int totalEntry =0,row,column;
    char value;

    while(totalEntry<=9){
         printf("Please choose x or o: ");
          scanf("%c",&value);
          getchar();
          printf("Enter row number: ");
          scanf("%d",&row);
          getchar();
          printf("Enter Column number: ");
          scanf("%d",&column);
          getchar();
          game[row][column] = value;
          drawBoard(game);

    if((game[0][0] == game[0][1]) && (game[0][1] == game[0][2]) && game[0][0] != 'x')
    if((game[1][0] == game[1][1]) && (game[1][1] == game[1][2]) && game[1][0] != 'x')
    if((game[2][0] == game[2][1]) && (game[2][1] == game[2][2]) && game[2][0] != 'x')
    if((game[0][0] == game[1][0]) && (game[1][0] == game[2][0]) && game[0][0] != 'x')
    if((game[0][1] == game[1][1]) && (game[1][1] == game[2][1]) && game[0][1] != 'x')
    if((game[0][2] == game[1][2]) && (game[1][2] == game[2][2]) && game[0][2] != 'x')
    if((game[0][0] == game[1][1]) && (game[1][1] == game[2][2]) && game[0][0] != 'x')
    if((game[2][0] == game[1][1]) && (game[1][1] == game[0][2]) && game[2][0] != 'x')
        printf("User x has won!");


    if((game[0][0] == game[0][1]) && (game[0][1] == game[0][2]) && game[0][0] != 'o')
    if((game[1][0] == game[1][1]) && (game[1][1] == game[1][2]) && game[1][0] != 'o')
    if((game[2][0] == game[2][1]) && (game[2][1] == game[2][2]) && game[2][0] != 'o')
    if((game[0][0] == game[1][0]) && (game[1][0] == game[2][0]) && game[0][0] != 'o')
    if((game[0][1] == game[1][1]) && (game[1][1] == game[2][1]) && game[0][1] != 'o')
    if((game[0][2] == game[1][2]) && (game[1][2] == game[2][2]) && game[0][2] != 'o')
    if((game[0][0] == game[1][1]) && (game[1][1] == game[2][2]) && game[0][0] != 'o')
    if((game[2][0] == game[1][1]) && (game[1][1] == game[0][2]) && game[2][0] != 'o');
        printf("User o has won!");
        break;


    }




    return 0;
}
有帮助吗?

解决方案

I would have used something like following, using a loop. Note that since the board is initialized to zero, assigning winner to a board position which still has zero means that a winner isn't found even if the test for all equal passes.

//declare this outside the main while-loop:
char winner=0;

for(i=0;i<3 && !winner;i++) {
  if(game[i][0]==game[i][1] && game[i][1]==game[i][2])
    winner=game[i][0]; // across
  else if(game[0][i]==game[1][i] && game[1][i]==game[2][i])
    winner=game[0][i]; // down
}
if( !winner && ( game[0][0]==game[1][1] && game[1][1]==game[2][2] ||
                 game[0][2]==game[1][1] && game[1][1]==game[2][0] ) )
  winner = game[1][1]; // diagonal

if(winner) {
  printf( "Winner is %c!\n",winner );
  break;
}

其他提示

IckIckIck.

First, it'll make you life much easier (at least for determining the winner), if you use a one-dimenisional array of 9 elements.

Then, write a function which test one possible win and returns true or false.

bool Test(char board[9], char player, int a, int b, int c)
{  
     return  board[a] == player
          && board[b] == player
          && board[c] == player;
}

Then Test(game, 'x', 0,3,6); -- tests of x wins down left side.

The logic in your conditional is incorrect. Imagine the board looks like this. 0 denotes an empty spot.

0 0 0 
0 0 x 
0 0 0 

The statement if((game[0][0] == game[0][1]) && (game[0][1] == game[0][2]) && game[0][0] != 'x') will evaluate to true because game[0][0] is empty game[0][1] is empty and game[0][2] is empty so they are all equal.

Also since game[0][0] is empty, it is not equal to 'x'.

So changing your != to == will help in that regard.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top