【问题标题】:Coding Tic Tac Toe in C with only If statements仅使用 If 语句在 C 中编码井字游戏
【发布时间】:2015-09-20 07:59:50
【问题描述】:

所以过去一周我一直在尝试学习一些 C,我正在做一个练习来编写一个非常简单的井字游戏。

有人问我:

  1. 提示两个用户分别在 tic、tac、toe 网格的九个位置之一中输入“naught”或“cross”
  2. 在所有 9 个输入完成后显示网格
  3. 为了简单起见:只有当所有 9 个网格位置都输入后,才能确定是否有赢家(您可以使用一些“if”语句来做到这一点) .

有人告诉我这可以用几个if 语句来完成,到目前为止我只学到了if 的,包括基本的intcharfloat、@987654326 @,等等。

所以,我没有真正掌握如何仅使用 if 语句来检查是否:

  1. 该位置已被占用,如果它提示用户重试或将当前人员置零或交叉在该位置。

  2. 跟踪两个用户输入的位置,因此我知道他们输入的两个用户的每个位置,以检查这些位置是否为空。

我觉得我有点想太多了,但我不确定,如果有人能得到任何帮助,那就太好了。

这是我目前写的:

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

int main()
{
    char board[9]; 
    int num;
    printf("Today we are playing a game of Tic Tac Toe\n");
    printf("To play the game you need to line up 3 of the same type 'x's or o's' in a line to win\n");
    printf("Before we start, whoever wants to be 'X' starts first, and whoever wants to be 'O' starts second\n");
    printf("The board looks like this\n");
    printf("%d%c%d%c%d\n", 1, 124, 2, 124, 3);
    printf("%d%c%d%c%d\n", 4, 124, 5, 124, 6);
    printf("%d%c%d%c%d\n", 7, 124, 8, 124, 9);
    printf("Lets begin");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'X';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'O';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'X';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'O';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'X';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'O';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'X';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'O';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'X';
    return 0;
}

注意:我也可以要求所有答案都只使用if 语句,因为这也是我所知道的,这就是练习的完成方式,尽管这可能是十次使用 for/while 和数组更容易。 谢谢!

【问题讨论】:

  • 你真的不需要spot0spot1等等。您可以简单地创建一个数组int spot[9];
  • if((temp = 1)){ spot0 == "X"; --> if((temp == 1)){ spot0 = 'X';
  • 感谢 ForceBru 和 BLUEPIXY 为我提供有用的信息。 arash kordi,你显然不明白这个网站是关于什么的,我来这里是为了了解更多信息,看看我犯了什么错误,以及我可以学习什么来改进代码..
  • @Collwyr 另外,你不需要你的位置是int 类型,你可以使用char,因为你只存储字符而不是一些非常大的数字。
  • Colwyr 你还有一个if((temp=2)。致命错误(因为它语法正确),== 用于测试相等性,= 用于赋值,正如@BLUEPIXY 已经指出的那样。

标签: c if-statement tic-tac-toe


【解决方案1】:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
 #define ROWS 3
 #define COLUMNS 3
 #define PLAYER_ONE 'X'
 #define PLAYER_TWO 'O'

void board();//calling class required by C
int checkForWinner();//calling class required by C
char tic_tac_toe[ROWS][COLUMNS] =
    {
        { '1', '2', '3'},
        { '4', '5', '6'},
        { '7', '8', '9'}
    };
int main()
{

    int isGameOn=1, currentPlayer=1, pick;
    char checked;

    do
    {
        board();
        currentPlayer = (currentPlayer % 2) ? 1 : 2;
        printf("Player %d, pick a number: ",  currentPlayer);
        scanf("%d", &pick);
        checked = (currentPlayer == 1) ? 'X' : 'O';
        isGameOn = checkForWinner();
        if(pick == 1 && tic_tac_toe[0][0] == '1'){
            tic_tac_toe[0][0] = checked;
            if(isGameOn == 1)break;
        }else if(pick == 2 && tic_tac_toe[0][1] == '2'){
            tic_tac_toe[0][1] = checked;
            if(isGameOn == 1){break;}
        }else if(pick == 3 && tic_tac_toe[0][2] == '3'){
            tic_tac_toe[0][2] = checked;
            if(isGameOn == 1){break;}
        }else if(pick == 4 && tic_tac_toe[1][0] == '4'){
            tic_tac_toe[1][0] = checked;
            if(isGameOn == 1){break;}
        }else if(pick == 5 && tic_tac_toe[1][1] == '5'){
            tic_tac_toe[1][1] = checked;
            if(isGameOn == 1){break;}
        }else if(pick == 6 && tic_tac_toe[1][2] == '6'){
            tic_tac_toe[1][2] = checked;
            if(isGameOn == 1){break;}
        }else if(pick == 7 && tic_tac_toe[2][0] == '7'){
            tic_tac_toe[2][0] = checked;
            if(isGameOn == 1){break;}
        }else if(pick == 8 && tic_tac_toe[2][1] == '8'){
            tic_tac_toe[2][1] = checked;
            if(isGameOn == 1){break;}
        }else if(pick == 9 && tic_tac_toe[2][2] == '9'){
            tic_tac_toe[2][2] = checked;
            if(isGameOn == 1){break;}
        }
        else{
            printf("Invalid moved");
        }
        isGameOn = checkForWinner();
        if(isGameOn == 1){
            printf("***************************************\n");
            printf("The winner is player %d\n", currentPlayer);
            printf("***************************************");
            break;
         }
        currentPlayer++;
    }while(isGameOn == -1);
    board();

    return(0);
}
/*Functions*/

    /*Board display*/
    void board()
    {
        printf("\n");
        printf("%c\t %c\t %c\n", tic_tac_toe[0][0], tic_tac_toe[0][1], tic_tac_toe[0][2]);
        printf("%c\t %c\t %c\n", tic_tac_toe[1][0], tic_tac_toe[1][1], tic_tac_toe[1][2]);
        printf("%c\t %c\t %c\n", tic_tac_toe[2][0], tic_tac_toe[2][1], tic_tac_toe[2][2]);

    }
    /*Checking for winner*/
    int checkForWinner()
    {
        /*checkign vertical line*/
        for(int col=0; col<COLUMNS; col++){
            int row=0;
            if(tic_tac_toe[row][col] == tic_tac_toe[row+1][col] && tic_tac_toe[row+1][col] == tic_tac_toe[row+2][col]){
                return 1;
            }
        }
        /*checking horizontal line*/
        if(tic_tac_toe[0][0] == tic_tac_toe[0][1] && tic_tac_toe[0][1] == tic_tac_toe[0][2]){
            return 1;
        }
        else if(tic_tac_toe[1][0] == tic_tac_toe [1][1] && tic_tac_toe[1][1] == tic_tac_toe[1][2])
        {
            return 1;
        }
        else if(tic_tac_toe[2][0] == tic_tac_toe [2][1] && tic_tac_toe[2][1] == tic_tac_toe[2][2])
        {
            return 1;
        }
        else if(tic_tac_toe[0][0] == tic_tac_toe[1][1] && tic_tac_toe[1][1] == tic_tac_toe[2][2])/*diagonal*/
        {
            return 1;
        }
        else {
            return -1;
        }

    }

【讨论】:

    猜你喜欢
    • 2020-06-16
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多