简体   繁体   English

仅使用If语句在C中对Tic Tac Toe进行编码

[英]Coding Tic Tac Toe in C with only If statements

So the past week I've been attempting to learn some C, I was giving an exercise to code a really simplistic version of tic tac toe. 因此,过去一周我一直在尝试学习C语言,我正在练习编码tic tac toe的一个非常简单的版本。

I have been asked: 我被问到:

  1. Prompt two users to enter a 'naught' or a 'cross' respectively into one of the nine positions on the tic, tac, toe grid 提示两个用户分别在tic,tac和toe网格上的9个位置之一中输入“ naught”或“ cross”
  2. After all 9 inputs have been made display the grid 完成所有9个输入后,显示网格
  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) . 为了简化起见:仅当输入了所有9个网格位置时,才找出是否有赢家(您可以使用一些'if'语句来做到这一点)。

I'm told this can be done with a few if statements, so far I have only learnt up to if 's, including the basic int , char , float , double , ect. 有人告诉我,可以使用一些if语句来完成此操作,到目前为止,我只了解了if ,包括基本的intcharfloatdouble等等。

So, what I'm not truly grasping in how to only use if statements to check if: 因此,我不是真正掌握如何仅使用if语句来检查以下内容:

  1. the position is already taken, if it has prompt the user to try again or place the current persons naught or cross in that position. 如果已经提示用户再次尝试或将当前人员调零或交叉放在该位置,则该位置已被采用。

  2. keep track of the positions the two users enter, so I know which position each of the two users they have entered to check those positions if its empty or not. 跟踪两个用户输入的位置,因此我知道他们输入的两个用户中的每个用户要检查的位置是否为空。

I feel like I am somewhat over thinking this but I am unsure, if anyone has got any help that'd be great. 我觉得我有点想这个问题,但是我不确定,如果有人得到了任何帮助,那将是很大的。

This is what I've written so far: 到目前为止,这是我写的:

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

Note: Could I also ask that all answers be kept to only using if statements as that is as far as I am up too, and it is how the exercise is meant to be completed, although probably this is ten times easier with for/while and arrays. 注意:我是否也可以要求所有答案都只使用if语句,因为这也是我目前为止的情况,这是完成练习的方式,尽管使用for / while可能要容易十倍和数组。 Thank you! 谢谢!

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM