繁体   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