簡體   English   中英

僅使用If語句在C中對Tic Tac Toe進行編碼

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

因此,過去一周我一直在嘗試學習C語言,我正在練習編碼tic tac toe的一個非常簡單的版本。

我被問到:

  1. 提示兩個用戶分別在tic,tac和toe網格上的9個位置之一中輸入“ naught”或“ cross”
  2. 完成所有9個輸入后,顯示網格
  3. 為了簡化起見:僅當輸入了所有9個網格位置時,才找出是否有贏家(您可以使用一些'if'語句來做到這一點)。

有人告訴我,可以使用一些if語句來完成此操作,到目前為止,我只了解了if ,包括基本的intcharfloatdouble等等。

因此,我不是真正掌握如何僅使用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;
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM