如何使我的if语句在我的tic tac toe程序中更好地工作

时间:2013-11-29 07:33:20

标签: c

我的问题是代码中“检查重复移动”部分下的if语句。有时if语句有效,但有时它不会这样做。 if语句应该检查用户的输入并将其与当前坐标中的字符进行比较。如果字符是'X'或'O',则它不应该覆盖当前在该数组部分中的字符,告诉用户它是无效的移动,并从计数器中减去1,因此它仍然是X或O的转向。

 #include <stdio.h>
struct coordinate{
    int i;
    int j;

};

//function that prints the tic-tac-toe board
int printTable( char array[3][3]){

    printf(" X's first O's second\n");
    printf("\n     0   1   2\n");
    printf("\n       |   |   \n");
    printf(" 0    %c | %c | %c \n", array[0][0], array[0][1], array[0][2]);
    printf("      ___|___|___\n");
    printf("         |   |   \n");
    printf(" 1    %c | %c | %c \n", array[1][0], array[1][1], array[1][2]);
    printf("      ___|___|___\n");
    printf("         |   |   \n");
    printf(" 2    %c | %c | %c \n", array[2][0], array[2][1], array[2][2]);
    printf("         |   |   \n\n");
    return 0;
}

int main (void){
    struct coordinate move;
    char game;
    int counter;
    char ttt[3][3] = {
        {'-', '-', '-'},
        {'-', '-', '-'},
        {'-', '-', '-'}
    };

    printf("\nWelcome to tic tac toe!\n");

    //Checks if you want to play
    printf("Would you like to play? y/n\n");
    scanf("%c", &game);

    //stops game by returning zero
    if(game == 'n'){
        printf("Have a pleasant day");
        return 0;
    }

    //starts game! =)
    if(game == 'y'){
        for(counter = 1; counter <= 9; counter++){
            printTable(ttt);

            //asks player where they want to move
            printf("Where would you like to move?");
            scanf("%i %i", &move.i, &move.j);

            //Checks for duplicate move
            if( ttt[move.i][move.j] == 'O' || ttt[move.i][move.j] == 'X' ){
                ttt[move.i][move.j] = ttt[move.i][move.j];
                printf("\n\n\nInvalid move. Try again\n\n\n");
                counter = counter - 1;
            }

            //O wins
            if( ttt[0][0] == 'O' && ttt[0][1] == 'O' && ttt[0][2] == 'O' || ttt[1][0] == 'O' && ttt[1][1] == 'O' && ttt[1][2] == 'O' || ttt[2][0] == 'O' && ttt[2][1] == 'O' && ttt[2][2] == 'O' || ttt[0][0] == 'O' && ttt[1][0] == 'O' && ttt[2][0] == 'O' || ttt[0][1] == 'O' && ttt[1][1] == 'O' && ttt[2][1] == 'O' || ttt[0][2] == 'O' && ttt[1][2] == 'O' && ttt[2][2] == 'O' || ttt[0][0] == 'O' && ttt[1][1] == 'O' && ttt[2][2] == 'O' || ttt[2][0] == 'O' && ttt[1][1] == 'O' && ttt[0][2] == 'O'){
                printf("O WINS!!!!!");
                return 0;
            }

            //X wins
            if( ttt[0][0] == 'X' && ttt[0][1] == 'X' && ttt[0][2] == 'X' || ttt[1][0] == 'X' && ttt[1][1] == 'X' && ttt[1][2] == 'X' || ttt[2][0] == 'X' && ttt[2][1] == 'X' && ttt[2][2] == 'X' || ttt[0][0] == 'X' && ttt[1][0] == 'X' && ttt[2][0] == 'X' || ttt[0][1] == 'X' && ttt[1][1] == 'X' && ttt[2][1] == 'X' || ttt[0][2] == 'X' && ttt[1][2] == 'X' && ttt[2][2] == 'X' || ttt[0][0] == 'X' && ttt[1][1] == 'X' && ttt[2][2] == 'X' || ttt[2][0] == 'X' && ttt[1][1] == 'X' && ttt[0][2] == 'X'){
                printf("X WINS!!!!!");
                return 0;
            }

            //X's turns
            if( counter == 0 || counter == 2 ||counter == 4 ||counter == 6 ||counter == 8){
                ttt[move.i][move.j] = 'X';
            }

            //Y's turns
            if( counter == 1 || counter == 3 ||counter == 5 ||counter == 7 ){
                ttt[move.i][move.j] = 'O';
            }

            //CATS GAME
            if( counter == 9){
                printf("Cat's Game =(");
                return 0;
            }
        }
    }
    return 0;
}

3 个答案:

答案 0 :(得分:1)

首先,你可以写一下,如果轮到Y,那么if语句可能看起来像

if(counter%2 == 1)

分别为X转:

 if(counter%2 == 0) 

更重要的是,这部分代码

//O wins
        if( ttt[0][0] == 'O' && ttt[0][1] == 'O' && ttt[0][2] == 'O' || ttt[1][0] == 'O' && ttt[1][1] == 'O' && ttt[1][2] == 'O' || ttt[2][0] == 'O' && ttt[2][1] == 'O' && ttt[2][2] == 'O' || ttt[0][0] == 'O' && ttt[1][0] == 'O' && ttt[2][0] == 'O' || ttt[0][1] == 'O' && ttt[1][1] == 'O' && ttt[2][1] == 'O' || ttt[0][2] == 'O' && ttt[1][2] == 'O' && ttt[2][2] == 'O' || ttt[0][0] == 'O' && ttt[1][1] == 'O' && ttt[2][2] == 'O' || ttt[2][0] == 'O' && ttt[1][1] == 'O' && ttt[0][2] == 'O'){
            printf("O WINS!!!!!");
            return 0;

        }
//X wins
    if( ttt[0][0] == 'X' && ttt[0][1] == 'X' && ttt[0][2] == 'X' || ttt[1][0] == 'X' && ttt[1][1] == 'X' && ttt[1][2] == 'X' || ttt[2][0] == 'X' && ttt[2][1] == 'X' && ttt[2][2] == 'X' || ttt[0][0] == 'X' && ttt[1][0] == 'X' && ttt[2][0] == 'X' || ttt[0][1] == 'X' && ttt[1][1] == 'X' && ttt[2][1] == 'X' || ttt[0][2] == 'X' && ttt[1][2] == 'X' && ttt[2][2] == 'X' || ttt[0][0] == 'X' && ttt[1][1] == 'X' && ttt[2][2] == 'X' || ttt[2][0] == 'X' && ttt[1][1] == 'X' && ttt[0][2] == 'X'){
        printf("X WINS!!!!!");
        return 0;

    }

可以安排为函数checkWin(char c)

顺便说一下:

ttt[move.i][move.j] = ttt[move.i][move.j];

此行没有任何用处(它将元素的值写入同一元素,因此没有任何更改) - 您只需删除此行

答案 1 :(得分:0)

你应该在重复检查后添加一个'continue'(见下文),这样你的代码就不会在重复移动后检查谁赢了。我相信这是您问题的主要来源。然而,如果你能够详细阐述你的意思,那么它可能会更有用“有时它有效,有时它不会”

  if( ttt[move.i][move.j] == 'O' || ttt[move.i][move.j] == 'X' ){

                    printf("\n\n\nInvalid move. Try again\n\n\n");
                    counter = counter - 1;
                    continue;
                }

答案 2 :(得分:0)

您的代码中存在一些问题 - 我将解释其他问题尚未指出的问题。

你的'if'条件很长很混乱 - 你不必分别检查'X'和'O' - 你可以只比较它们,无论结果如何,它都会赢。但是需要对“ - ”进行特殊检查,但在我看来,它更易于维护。然而,这不是主要观点。

你的错误是,你确实在实际设置当前单元格之前是否是胜利者。所以获胜者只会出现一轮到晚。考虑改进的代码(我还应用了其他评论中的一些改进):

#include <stdio.h>

struct coordinate{
  int i;
  int j;
};

//function that prints the tic-tac-toe board
int printTable( char array[3][3]){
  printf(" X's first O's second\n");
  printf("\n     0   1   2\n");
  printf("\n       |   |   \n");
  printf(" 0   %c | %c | %c \n", array[0][0], array[0][1], array[0][2]);
  printf("    ___|___|___\n");
  printf("       |   |   \n");
  printf(" 1   %c | %c | %c \n", array[1][0], array[1][1], array[1][2]);
  printf("    ___|___|___\n");
  printf("       |   |   \n");
  printf(" 2   %c | %c | %c \n", array[2][0], array[2][1], array[2][2]);
  printf("       |   |   \n\n");
  return 0;
}


int main (void){
  struct coordinate move;
  char game;
  int counter, x, y;
  char ttt[3][3] = {
    {'-', '-', '-'},
    {'-', '-', '-'},
    {'-', '-', '-'}
  };
  printf("\nWelcome to tic tac toe!\n");
  //Checks if you want to play
  printf("Would you like to play? y/n\n");
  scanf("%c", &game);
  //stops game by returning zero
  if(game == 'n'){
    printf("Have a pleasant day");
  }else /*if(game == 'y')*/{   //starts game! =) 
    for(counter = 1; counter <= 9; counter++){
      printTable(ttt);
      //asks player where they want to move
      printf("Where would you like to move?");
      scanf("%i %i", &move.i, &move.j);
      //Checks for duplicate move
      if( ttt[move.i][move.j] == 'O' || ttt[move.i][move.j] == 'X' ){
        printf("\n\n\nInvalid move. Try again\n\n\n");
        counter = counter - 1;
        continue;
      }
      //X's turns
      if( counter % 2 == 0){
        ttt[move.i][move.j] = 'X';
      }else if( counter % 2 == 1 ){
        ttt[move.i][move.j] = 'O';
      }
      /* horizontal checks */
      for(x = 0; x < 3; x++){
        if(ttt[x][0] != '-' && ttt[x][0] == ttt[x][1] && ttt[x][1] == ttt[x][2]){
          printf("%c WINS!!!!!", ttt[x][0]);
          return 0;
        }
      }
       /* vertical checks */
      for(y = 0; y < 3; y++){
       if(ttt[0][y] != '-' && ttt[0][y] == ttt[1][y] && ttt[1][y] == ttt[2][y]){
          printf("%c WINS!!!!!", ttt[0][y]);
          return 0;
        }
      }
       /* diagonal checks */
      if(ttt[0][0] != '-' && ttt[0][0] == ttt[1][1] && ttt[1][1]  == ttt[2][2]){
         printf("%c WINS!!!!!", ttt[0][0]);
         return 0;
      }else if(ttt[0][2] != '-' && ttt[0][2] == ttt[1][1] && ttt[1][1] == ttt[2][0]){
         printf("%c WINS!!!!!", ttt[0][2]);
         return 0;
      }
      if( counter >= 9){
        printf("Cat's Game =(");
        return 0;
      }
    }
  }
  return 0;
}