在C中挣扎着TicTacToe

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在C中挣扎着TicTacToe相关的知识,希望对你有一定的参考价值。

我试图在C中编写一个简单的TicTacToe。程序本身编译并显示没有错误。因此,程序绘制字段,读取名称并让玩家将其符号放在特定字段中。所有这些都发生在一个while循环中,应该运行直到它的平局或某人获胜。但程序只运行循环一次,所以只有一个转,然后程序停止。

所以这是我的代码:

main.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("

");
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;

}

game.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("________________________
");
printf("|       |       |      |
");
printf("|   %c   |   %c   |   %c  |
", field[0], field[1], field[2]);
printf("|_______|_______|______|
");
printf("|       |       |      |
");
printf("|   %c   |   %c   |   %c  |
", field[3], field[4], field[5]);
printf("|_______|_______|______|
");
printf("|       |       |      |
");
printf("|   %c   |   %c   |   %c  |
", field[6], field[7], field[8]);
printf("|_______|_______|______|
");

}

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

    field[p1fieldnumber-1] = 'X';

}
else {

    do {
        printf("
Its Player %s's turn.", names[1]);
        char symbol = 'O';
        printf("
Number of the field which you want to put your symbol in: ");
        scanf("%d", &p2fieldnumber);
        if (field[p2fieldnumber] == 'X' || field[p2fieldnumber] == 'O')
        {
            printf("
Field 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);
}
答案

有很多问题

  1. isWinner返回字符'0','1','2'和'3'。将它们替换为数字0,1,2,3,因为您要与main中的数字进行比较。
  2. main而不是检查if (winner >= 1) {,即使isWinner返回3也是如此,检查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的主要内容,如果未能解决你的问题,请参考以下文章

官方 Android 示例应用程序 tictactoe 接收器代码 (html) 位于何处?

Recycler View 和 DialogFragment 苦苦挣扎(Kotlin)

PHP 代码片段

苦苦挣扎着NSDate,格式和时区

TicTacToe Java 检查抽奖

为什么这个指向指针二维数组的指针会像这样分配新的int