我试着用C语言编写一个简单的TicTacToe程序,程序本身编译后没有显示错误。
因此程序绘制字段,读取名称,并让玩家将其符号放在特定字段中。所有这些都是在一个时间循环中发生的,应该一直运行到它的平局或有人获胜。
但程序只运行一次循环,因此只有一个回合,然后程序停止。
这是我的代码:
主要c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "game.h"


#define NAMELENGTH 20

 int main(void) {
char names[2][NAMELENGTH];
// field positions
char field[9];
int winner = -1;
getnames(names);
printf("\n\n");
initField(field);
// set field positions to 'empty'
char actualPlayer = (char)(get_random_number()*10.0) % 2;

while (1) {
    drawField(field);
    turn(actualPlayer, names, field);
    winner = isWinner(actualPlayer, field);
    drawField(field);
    if (winner >= 1) {
        printwinner(winner, names);
        return 0;
    }
    else if (winner == 0) {
        printDrawGame(names);
        return 0;
    }
    actualPlayer = (actualPlayer + 1) % 2;

}

return 0;

}

游戏h头文件:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define NAMELENGTH 20
#pragma warning(disable:4996)

extern void drawField(char *field);
extern void getnames(char nameField[][NAMELENGTH]);
extern void initField(char *field);
extern void turn(char actualPlayer, char names[][NAMELENGTH], char *field);
extern char isWinner(char actualPlayer, char *field);
extern void printwinner(char winnerNumber, char names[][NAMELENGTH]);
extern void printDrawGame(char names[][NAMELENGTH]);
extern double get_random_number();

然后我的game.c定义了我的函数:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "game.h"
#define NAMELENGTH 20
#pragma warning(disable:4996)

void drawField(char *field) {

printf("________________________\n");
printf("|       |       |      |\n");
printf("|   %c   |   %c   |   %c  |\n", field[0], field[1], field[2]);
printf("|_______|_______|______|\n");
printf("|       |       |      |\n");
printf("|   %c   |   %c   |   %c  |\n", field[3], field[4], field[5]);
printf("|_______|_______|______|\n");
printf("|       |       |      |\n");
printf("|   %c   |   %c   |   %c  |\n", field[6], field[7], field[8]);
printf("|_______|_______|______|\n");

}

void getnames(char nameField[][NAMELENGTH]) {

printf("Name of the first player: ");
scanf("%s", nameField[0]);
printf("Name of the second player: ");
scanf("%s", nameField[1]);

}

void initField(char *field) {

for (int i = 0; i <= 8; i++)
{
    field[i] = i + '1';
}

}

void turn(char actualPlayer, char names[][NAMELENGTH], char *field) {

char symbol = ' ';
int p1fieldnumber;
int p2fieldnumber;


if (actualPlayer == 0)
{
    do {

        printf("\nIts Player %s's turn.", names[0]);
        char symbol = 'X';
        printf("\nNumber of the field which you want to put your symbol in: ");
        scanf("%d", &p1fieldnumber);
        if (field[p1fieldnumber] == 'X' || field[p1fieldnumber] == 'O')
        {
            printf("\nField is already occupied!");
            p1fieldnumber = 10;
        }
    } while (p1fieldnumber < 1 || p1fieldnumber > 9);

    field[p1fieldnumber-1] = 'X';

}
else {

    do {
        printf("\nIts Player %s's turn.", names[1]);
        char symbol = 'O';
        printf("\nNumber of the field which you want to put your symbol in: ");
        scanf("%d", &p2fieldnumber);
        if (field[p2fieldnumber] == 'X' || field[p2fieldnumber] == 'O')
        {
            printf("\nField is already occupied!");
            p2fieldnumber = 10;
        }
    } while (p2fieldnumber < 1 || p2fieldnumber > 9);

    field[p2fieldnumber-1] = 'O';

}

}

char isWinner(char actualPlayer, char *field) {

char pwinner = '3';

if (((field[0] == 'O') && (field[1] == 'O') && (field[2] == 'O')) ||
    (field[3] == 'O') && (field[4] == 'O') && (field[5] == 'O') ||
    (field[6] == 'O') && (field[7] == 'O') && (field[8] == 'O') ||
    (field[0] == 'O') && (field[4] == 'O') && (field[8] == 'O') ||
    (field[2] == 'O') && (field[4] == 'O') && (field[6] == 'O') ||
    (field[0] == 'O') && (field[3] == 'O') && (field[6] == 'O') ||
    (field[1] == 'O') && (field[4] == 'O') && (field[7] == 'O') ||
    (field[2] == 'O') && (field[5] == 'O') && (field[8] == 'O'))

{
    pwinner = '2';
}

else if (((field[0] == 'X') && (field[1] == 'X') && (field[2] == 'X')) ||
    (field[3] == 'X') && (field[4] == 'X') && (field[5] == 'X') ||
    (field[6] == 'X') && (field[7] == 'X') && (field[8] == 'X') ||
    (field[0] == 'X') && (field[4] == 'X') && (field[8] == 'X') ||
    (field[2] == 'X') && (field[4] == 'X') && (field[6] == 'X') ||
    (field[0] == 'X') && (field[3] == 'X') && (field[6] == 'X') ||
    (field[1] == 'X') && (field[4] == 'X') && (field[7] == 'X') ||
    (field[2] == 'X') && (field[5] == 'X') && (field[8] == 'X'))

{
    pwinner = '1';
}

else if (((field[0] == 'X') || (field[0] == 'O')) && ((field[1] == 'X') || (field[1] == 'O')) && ((field[2] == 'X') || (field[2] == 'O')) ||
    ((field[3] == 'X') || (field[3] == 'O')) && ((field[4] == 'X') || (field[4] == 'O')) && ((field[5] == 'X') || (field[5] == 'O')) ||
    ((field[6] == 'X') || (field[6] == 'O')) && ((field[7] == 'X') || (field[7] == 'O')) && ((field[8] == 'X') || (field[8] == 'O')) ||
    ((field[0] == 'X') || (field[0] == 'O')) && ((field[4] == 'X') || (field[4] == 'O')) && ((field[8] == 'X') || (field[8] == 'O')) ||
    ((field[2] == 'X') || (field[2] == 'O')) && ((field[4] == 'X') || (field[4] == 'O')) && ((field[6] == 'X') || (field[6] == 'O')) ||
    ((field[0] == 'X') || (field[0] == 'O')) && ((field[3] == 'X') || (field[3] == 'O')) && ((field[6] == 'X') || (field[6] == 'O')) ||
    ((field[1] == 'X') || (field[1] == 'O')) && ((field[4] == 'X') || (field[4] == 'O')) && ((field[7] == 'X') || (field[7] == 'O')) ||
    ((field[2] == 'X') || (field[2] == 'O')) && ((field[5] == 'X') || (field[5] == 'O')) && ((field[8] == 'X') || (field[8] == 'O')))
{
    pwinner = '0';
}

return pwinner;
}

void printwinner(char winnerNumber, char names[][NAMELENGTH]) {

if (winnerNumber == '1') {
    printf("Player %s won!", names[0]);
}
else if (winnerNumber == '2') {
    printf("Player %s won!", names[1]);
}

}

void printDrawGame(char names[][NAMELENGTH]) {
printf("Draw!");
}

static int _initialized;

double get_random_number() {
if (!_initialized) {
    srand(time(NULL));
    _initialized = 1;
}
return (double)rand() / ((double)(RAND_MAX)+1);
}

最佳答案

有很多问题
isWinner返回字符“0”、“1”、“2”和“3”。将它们替换为数字0、1、2、3,因为您正在与main中的数字进行比较。
main中,检查if (winner >= 1) {,而不是检查isWinner,即使if (winner == 1 || winner == 2) {返回3也是如此。
建议在每个由isWinner分隔的条件周围使用||中的括号。
第一个条件的括号如下所示

if (((field[0] == 'O') && (field[1] == 'O') && (field[2] == 'O')) ||
    ((field[3] == 'O') && (field[4] == 'O') && (field[5] == 'O')) ||
    ((field[6] == 'O') && (field[7] == 'O') && (field[8] == 'O')) ||
    ((field[0] == 'O') && (field[4] == 'O') && (field[8] == 'O')) ||
    ((field[2] == 'O') && (field[4] == 'O') && (field[6] == 'O')) ||
    ((field[0] == 'O') && (field[3] == 'O') && (field[6] == 'O')) ||
    ((field[1] == 'O') && (field[4] == 'O') && (field[7] == 'O')) ||
    ((field[2] == 'O') && (field[5] == 'O') && (field[8] == 'O')))

关于c - 在TicTacToe中挣扎,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33783995/

10-12 12:29