我的程序的逻辑有什么问题?

时间:2013-04-20 06:29:36

标签: c while-loop logic

我的程序逻辑出了什么问题。 出于某种原因,当我在玩家之间轮流转弯时,当它到达第二回合时,它会跳过它,并且游戏似乎被拖了一步。 即使我在主函数上面声明它,数组也会改变吗? tictactoe游戏的位置是

1 | 2 | 3


4 | 5 | 6


6 | 8 | 9

#include <stdio.h>
int check(char player);
void move(char player);
void outprint(void);
char board[3][3] ;



int main(void)
{
int first;
char player1, player2;

        printf("Player 1: Choose your symbol: \n");
        player1 = getchar();
        getchar();

        printf("Player 2: Choose your symbol: \n");
        player2 = getchar();
        getchar();



int i=0;
int win;
char turn;
while(win == 0)
{
        if((i%2) == 0){
                turn = player1;
                move(player1);
                win = check(player1);
                outprint();}
        else {
                turn = player2;
                move(player2);
                win = check(player2);
                outprint();}
        i++;
}

        if (i == 8)
                printf("its a tie");
        else
                printf("the winner is %c", turn);

return 0;
}

/*printing the board that takes in a placement int*/
void outprint(void)
{
        int r;
        printf("\n");
        for (r = 0; r < 3; r++){
                printf(" %c | %c | %c \n" , board[r][0], board[r][2], board[r][3]);
        if (r != 2)
                printf("___________\n");
        } printf("\n");
return;
}

/*check to see if someone won*/
int check(char player)
{
        int r, c;

        for ( r = 0 ; r <3 ; r++)
        {
                if ((board[r][0] == player) && (board[r][1] == player) && (board[r][2] == player))
                        return 1;
        }

        for ( c = 0 ; c <3 ; c++)
        {
                if ((board[0][c] == player) && (board[1][c] == player) && (board[2][c] == player))
                        return 1;
        }

        if((board[0][0] == player) && (board[1][1] == player) && (board[2][2] == player))
                return 1;

        if((board[0][2] == player) && (board[1][1] == player) && (board[2][0] == player))
                return 1;

        return 0;
}


void move(char player)
{
        int place;
        printf("player %c, enter placement: \n", player);
        scanf("%d", &place);

        if (place == 1)
                board[0][0] = player;
        else if (place == 2)
                board[0][1] = player;
        else if (place == 3)
                board[0][2] = player;

        else if (place == 4)
                board[1][0] = player;
        else if (place == 5)
                board[1][1] = player;
        else if (place == 6)
                board[1][2] = player;

        else if (place == 7)
                board[2][0] = player;
        else if (place == 8)
                board[2][1] = player;
        else if (place == 9)
                board[2][2] = player;
}

输出

Player 1: Choose your symbol: 
x
Player 2: Choose your symbol: 
o
player x, enter placement: 
1

 x |  |  
___________
  |  |  
___________
  |  |  

player o, enter placement: 
2

 x |  |  
___________
  |  |  
___________
  |  |  

player x, enter placement: 
3

 x | x |  
___________
  |  |  
___________
  |  |  

player o, enter placement: 
4

 x | x | o 
___________
 o |  |  
___________
  |  |  

player x, enter placement: 
5

 x | x | o 
___________
 o |  |  
___________
  |  |  

player o, enter placement: 
6

 x | x | o 
___________
 o | o |  
___________
  |  |  

player x, enter placement: 
7

 x | x | o 
___________
 o | o | x 
___________
 x |  |  

the winner is x

3 个答案:

答案 0 :(得分:2)

1,在void outprint(void)中,更改

printf(" %c | %c | %c \n" , board[r][0], board[r][2], board[r][3]);

printf(" %c | %c | %c \n" , board[r][0], board[r][1], board[r][2]);

2,在int main(void)中,将int win;更改为int win = 0;

答案 1 :(得分:1)

看起来你需要将电路板初始化为空格。

char board[3][3] = { "   ", "   ", "    " };

看起来你什么都没打印。可能是来自未初始化数据的不可打印的垃圾。

答案 2 :(得分:0)

尝试在你的move()函数中添加getf()你的move()函数中的getchar()就像你在第一个输入条目中所做的那样:

void move(char player)
{
    int place;
    printf("player %c, enter placement: \n", player);
    scanf("%d", &place);
    getchar();