簡體   English   中英

我正在嘗試在 C 中制作一個 5x5 井字游戲程序,但我認為在循環時遇到了一些問題。 程序自動顯示玩家 1 為贏家

[英]I am trying to make a 5x5 tic tac toe program in C but facing some problem in looping I think. The program automatically displays player 1 as winner

#include<stdio.h>
#include <conio.h>

char square[26] = {'o', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
char row[5] = {'1', '2', '3', '4', '5'};
char column[5] = {'1', '2', '3', '4', '5'};
int playingChoice;


int checkWin();
void board();

int main()
{

    
    printf("\n\n");
    printf("\t\t\t**** T I C   T A C   T O E ****\n");
    printf("\n\n");
    
    printf("Press 1 for Player Vs Player \nPress 2 for Player Vs Computer\n\n");
    scanf("%d", &playingChoice);
    
    //Human player Vs Human player Code
    if(playingChoice == 1){
        int player = 1, i, choiceRow, choiceColumn;
        char mark;

    do
    {
        board();
        player = (player % 2) ? 1 : 2;

        printf("Player %d, choose row :  ", player);
        scanf("%d", &choiceRow);
        printf("Player %d, choose column :  ", player);
        scanf("%d", &choiceColumn);

        mark = (player == 1) ? 'X' : 'O';

        if (choiceRow == 1 && choiceColumn == 1 && square[1] == ' ')
            square[1] = mark;
        else if (choiceRow == 1 && choiceColumn == 2 && square[2] == ' ')
            square[2] = mark;
        else if (choiceRow == 1 && choiceColumn == 3 && square[3] == ' ')
            square[3] = mark;   
        else if (choiceRow == 1 && choiceColumn == 4 && square[4] == ' ')
            square[4] = mark;
        else if (choiceRow == 1 && choiceColumn == 5 && square[5] == ' ')
            square[5] = mark;
        else if (choiceRow == 2 && choiceColumn == 1 && square[6] == ' ')
            square[6] = mark;
        else if (choiceRow == 2 && choiceColumn == 2 && square[7] == ' ')
            square[7] = mark;
        else if (choiceRow == 2 && choiceColumn == 3 && square[8] == ' ')
            square[8] = mark;   
        else if (choiceRow == 2 && choiceColumn == 4 && square[9] == ' ')
            square[9] = mark;
        else if (choiceRow == 2 && choiceColumn == 5 && square[10] == ' ')
            square[10] = mark;
        else if (choiceRow == 3 && choiceColumn == 1 && square[11] == ' ')
            square[11] = mark;
        else if (choiceRow == 3 && choiceColumn == 2 && square[12] == ' ')
            square[12] = mark;
        else if (choiceRow == 3 && choiceColumn == 3 && square[13] == ' ')
            square[13] = mark;   
        else if (choiceRow == 3 && choiceColumn == 4 && square[14] == ' ')
            square[14] = mark;
        else if (choiceRow == 3 && choiceColumn == 5 && square[15] == ' ')
            square[15] = mark;
        else if (choiceRow == 4 && choiceColumn == 1 && square[16] == ' ')
            square[16] = mark;
        else if (choiceRow == 4 && choiceColumn == 2 && square[17] == ' ')
            square[17] = mark;
        else if (choiceRow == 4 && choiceColumn == 3 && square[18] == ' ')
            square[18] = mark;   
        else if (choiceRow == 4 && choiceColumn == 4 && square[19] == ' ')
            square[19] = mark;
        else if (choiceRow == 4 && choiceColumn == 5 && square[20] == ' ')
            square[20] = mark;
        else if (choiceRow == 5 && choiceColumn == 1 && square[21] == ' ')
            square[21] = mark;
        else if (choiceRow == 5 && choiceColumn == 2 && square[22] == ' ')
            square[22] = mark;
        else if (choiceRow == 5 && choiceColumn == 3 && square[23] == ' ')
            square[23] = mark;   
        else if (choiceRow == 5 && choiceColumn == 4 && square[24] == ' ')
            square[24] = mark;
        else if (choiceRow == 5 && choiceColumn == 5 && square[25] == ' ')
            square[25] = mark;    
        else
        {
            printf("Invalid move ");

            player--;
            getch();
        }
        i = checkWin();

        player++;
    }while (i ==  - 1);
    
    board();
    
    if (i == 1)
        printf("==>\aPlayer %d win ", --player);
    else
        printf("==>\aGame draw");

    getch();

    return 0;

}//end if
}//end int main
        

void board()
{
        system("cls");
        
        printf("\n\n");
        printf("\t\t\t**** T I C   T A C   T O E ****\n");
        printf("\n\n");
        
        
        printf("\n");
        
        printf("\t\t    Player 1 place [X] | ");
        
        printf("Player 2 place [O]\n\n\n");
         
        printf("\t\t\t     1     2     3     4     5    \n");
        printf("\t\t\t  --------------------------------\n");
        printf("\t\t\t  |     |     |     |     |     | \n");
        
        printf("\t\t\t1 |  %c  |  %c  |  %c  |  %c  |  %c  |\n", square[1], square[2], square[3], square[4], square[5]);
    
        printf("\t\t\t  |_____|_____|_____|_____|_____|\n");
        
        printf("\t\t\t  |     |     |     |     |     | \n");
    
        printf("\t\t\t2 |  %c  |  %c  |  %c  |  %c  |  %c  |\n", square[6], square[7], square[8], square[9], square[10]);
    
        printf("\t\t\t  |_____|_____|_____|_____|_____|\n");
        
        printf("\t\t\t  |     |     |     |     |     | \n");
    
        printf("\t\t\t3 |  %c  |  %c  |  %c  |  %c  |  %c  |\n", square[11], square[12], square[13], square[14], square[15]);
    
        printf("\t\t\t  |_____|_____|_____|_____|_____|\n");
    
        printf("\t\t\t  |     |     |     |     |     | \n");
    
        printf("\t\t\t4 |  %c  |  %c  |  %c  |  %c  |  %c  |\n", square[16], square[17], square[18], square[19], square[20]);
    
        printf("\t\t\t  |_____|_____|_____|_____|_____|\n");
        
        printf("\t\t\t  |     |     |     |     |     | \n");
        
        printf("\t\t\t5 |  %c  |  %c  |  %c  |  %c  |  %c  |\n", square[21], square[22], square[23], square[24], square[25]);
        
        printf("\t\t\t  |     |     |     |     |     | \n");
        printf("\t\t\t  --------------------------------\n");
}

int checkWin()
{

if (square[1] == square[2] && square[2] == square[3] && square[3] == square[4] && square[4] == square[5]){
        return 1;
    }else if (square[6] == square[7] && square[7] == square[8] && square[8] == square[9] && square[9] == square[10]){
        return 1;
    }else if (square[11] == square[12] && square[12] == square[13] && square[13] == square[14] && square[14] == square[15]){
        return 1;
    }else if (square[16] == square[17] && square[17] == square[18] && square[18] == square[19] && square[19] == square[20]){
        return 1;
    }else if (square[21] == square[22] && square[22] == square[23] && square[23] == square[24] && square[24] == square[25]){
        return 1;
    }else if (square[1] == square[6] && square[6] == square[11] && square[11] == square[16] && square[16] == square[21]){
        return 1;
    }else if (square[2] == square[7] && square[7] == square[12] && square[12] == square[17] && square[17] == square[22]){
        return 1;
    }else if (square[3] == square[8] && square[8] == square[13] && square[13] == square[18] && square[18] == square[23]){
        return 1;
    }else if (square[4] == square[9] && square[9] == square[14] && square[14] == square[19] && square[19] == square[24]){
        return 1;
    }else if (square[5] == square[10] && square[10] == square[15] && square[15] == square[20] && square[20] == square[25]){
        return 1;
    }else if (square[1] == square[7] && square[7] == square[13] && square[13] == square[19] && square[19] == square[25]){
        return 1;
    }else if (square[5] == square[9] && square[9] == square[13] && square[13] == square[17] && square[17] == square[21]){
        return 1;
    }else if (square[1] != ' ' && square[2] != ' ' && square[3] != ' ' && square[4] != ' ' && square[5] != ' ' && square[6] != ' ' 
              && square[7] != ' ' && square[8] != ' ' && square[9] != ' ' && square[10] != ' ' && square[11] != ' ' && square[12] != ' ' 
              && square[13] != ' ' && square[14] != ' ' && square[15] != ' ' && square[16] != ' ' && square[17] != ' ' && square[18] != ' ' 
              && square[19] != ' ' && square[20] != ' ' && square[21] != ' ' && square[22] != ' ' && square[23] != ' ' && square[24] != ' ' 
              && square[25] != ' '){
            return 0;
        }else{
            return  - 1;
        }
}

當玩家輸入他想要的行和列時,程序會自動將玩家 1 顯示為獲勝者。 我認為問題可能出在循環中,但我不確定。 請幫助我解決這個問題,如果你覺得我的代碼很幼稚,我很抱歉,因為我是初學者,這是我編程的第二個月

Weather Vane 說明您對“五連勝”的檢查不完整,因為程序無法確保檢查是針對五個 X 還是五個 O。 因此,以代碼中的第一個測試組為例,而不是當前測試

    if (square[1] == square[2] && square[2] == square[3] && square[3] == square[4] && square[4] == square[5])
    {
        return 1;
    }

將對測試組中的一個元素不為空白進行額外測試。

    if (square[1] == square[2] && square[2] == square[3] && square[3] == square[4] && square[4] == square[5] && square[1] != ' ')
    {
        return 1;
    }

檢查其中一個元素以確保它在每個測試塊中不為空白將需要對每個測試組重復進行。

進行這種更改並故意輸入不會導致“連續五次”的選擇會在終端上產生最終的抽獎結果。

            **** T I C   T A C   T O E ****



            Player 1 place [X] | Player 2 place [O]


                 1     2     3     4     5    
              --------------------------------
              |     |     |     |     |     | 
            1 |  X  |  O  |  X  |  O  |  X  |
              |_____|_____|_____|_____|_____|
              |     |     |     |     |     | 
            2 |  O  |  X  |  O  |  X  |  O  |
              |_____|_____|_____|_____|_____|
              |     |     |     |     |     | 
            3 |  X  |  O  |  X  |  O  |  X  |
              |_____|_____|_____|_____|_____|
              |     |     |     |     |     | 
            4 |  O  |  X  |  O  |  X  |  O  |
              |_____|_____|_____|_____|_____|
              |     |     |     |     |     | 
            5 |  O  |  X  |  X  |  X  |  O  |
              |     |     |     |     |     | 
              --------------------------------
==>Game draw

無需接受此答案。 只是想美化之前的評論,以便您清楚。

暫無
暫無

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

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