仅使用 If 语句在 C 中编写 Tic Tac Toe

标签 c if-statement tic-tac-toe

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

有人问我:

  1. Prompt two users to enter a ‘naught’ or a ‘cross’ respectively into one of the nine positions on the tic, tac, toe grid
  2. After all 9 inputs have been made display the grid
  3. To make it simpler: only when all 9 grid positions have been entered figure out if there is a winner (you can do this with a few ‘if’ statements) .

有人告诉我这可以用一些 if 语句来完成,到目前为止我只学会了 if,包括基本的 int charfloatdouble

所以,我没有真正掌握如何仅使用 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 和数组,这可能要容易十倍。 谢谢!

最佳答案

#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;
        }

    }

关于仅使用 If 语句在 C 中编写 Tic Tac Toe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32677143/

相关文章:

java - 如何使用 if 语句正确格式化文件 .txt 中数组列表的日期

python - 这个功能有问题吗?

c - 如何从汇编调用 C 函数

C - 这个答案的解释

php - NSData(if 语句)

python - 理解循环中的 if 语句

algorithm - 井字游戏启发式 AI

Python - OOP 井字棋

c - 尝试编译 spdif-loop 时出现 "undefined reference"错误消息

c - 从锁定的 SQLite3 数据库中读取