我一直得到这个错误(类型为“void”的值不能分配给类型为“char”的实体)我如何解决它?[关闭]

m3eecexj  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(140)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
23天前关闭
Improve this question
我想做一个井字游戏,玩家将在C中面对计算机,但我在第28行和第35行将CheckWinner()分配给winner时遇到问题,有什么解决方案吗?
这是我一直得到的消息:`类型为“void”的值不能赋给类型为“char”的实体)。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

char board[3][3];
const char PLAYER = 'X';
const char COMPUTER = 'O';

void resetBoard();
void printBoard();
int checkFreeSpaces();
void PlayerMove();
void ComputerMove();
void CheckWinner();
void printWinner(char);

int main()
{
   char winner = ' ';

   resetBoard();
   while (winner == ' ' && checkFreeSpaces() != 0)
   {
      printBoard();

      PlayerMove();
      winner = CheckWinner();
      if (winner != ' ' || checkFreeSpaces() == 0)
      {
         break;
      }

      ComputerMove();
      winner = CheckWinner();
      if (winner != ' ' || checkFreeSpaces() == 0)
      {
         break;
      }
   }
   printBoard();
   printWinner(winner);

   return 0;
}

void resetBoard()
{
   for (int i = 0; i < 3; i++)
   {
      for (int j = 0; j < 3; j++)
      {
         board[i][j] = ' ';
      }
   }
}

void printBoard()
{
   printf(" %c | %c | %c ", board[0][0], board[0][1], board[0][2]);
   printf("\n---|---|---\n");
   printf(" %c | %c | %c ", board[1][0], board[1][1], board[1][2]);
   printf("\n---|---|---\n");
   printf(" %c | %c | %c ", board[2][0], board[2][1], board[2][2]);
   printf("\n");
}

int checkFreeSpaces()
{
   int freeSpaces = 9;
   for (int i = 0; i < 3; i++)
   {
      for (int j = 0; j < 3; j++)
      {
         if (board[i][j] != ' ')
         {
            freeSpaces--;
         }
      }
   }
   return freeSpaces;
}

void PlayerMove()
{
   int x;
   int y;

   do
   {
      printf("Enter row #(1-3): ");
      scanf("%d", &x);
      x--;
      printf("Enter column #(1-3): ");
      scanf("%d", &y);
      y--;

      if (board[x][y] != ' ')
      {
         printf("Invalid move\n");
      }
      else
      {
         board[x][y] = PLAYER;
         break;
      }
   } while (board[x][y] != ' ');
}

void ComputerMove()
{
   // creates a seed based on curent time
   srand(time(0));
   int x;
   int y;

   if (checkFreeSpaces() > 0)
   {
      do
      {
         x = rand() % 3;
         y = rand() % 3;

      } while (board[x][y] != ' ');
      board[x][y] = COMPUTER;
   }
   else
   {
      printWinner(' ');
   }
}

void CheckWinner()
{
   // check rows
   for (int i = 0; i < 3; i++)
   {
      if (board[i][0] == board[i][1] && board[i][0] == board[i][2])
      {
         return board[i][0];
      }
   }

   // check columns
   for (int i = 0; i < 3; i++)
   {
      if (board[0][i] == board[1][i] && board[0][i] == board[2][i])
      {
         return board[0][i];
      }
   }

   // check diagonals
   if (board[0][0] == board[1][1] && board[0][0] == board[2][2])
   {
      return board[0][0];
   }

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

   return ' ';
}

void printWinner(char winner)
{
   if (winner == PLAYER)
   {
      printf("YOU WIN!");
   }
   else if (winner == COMPUTER)
   {
      printf("YOU LOSE!");
   }
   else
   {
      printf("IT'S A TIE!");
   }
}

字符串

v440hwme

v440hwme1#

CheckWinner()的返回类型应该声明和定义为char,而不是void
还请注意这些评论:

  • 你应该在函数printWinner()中输出一个尾随的换行符。
  • 主循环while (winner == ' ' && checkFreeSpaces() != 0)中的测试是多余的。您应该使用for ever循环来代替:for (;;)
  • PlayerMove()中也有同样的问题:do/while循环测试是冗余的,只需使用一个for ever循环for (;;)
  • 你应该检查scanf()转换失败,以避免无效和丢失输入的未定义行为。
  • checkFreeSpaces()不需要计算空闲空间的数量,返回10作为布尔指示符就足够了,并且更有效。
    ***[major]**函数CheckWinner()将返回' '作为获胜方,如果有3个空格对齐。这是不正确的,可能会阻止正确检测获胜方。您应该修改函数以检查给定方是否获胜,作为参数传递。
  • 伪随机数生成器应该在main()函数中初始化一次。2每次计算机移动时重新初始化它是浪费的,并且会引入很强的偏差。
  • 你在ComputerMove中检查checkFreeSpaces(),但在PlayerMove()中没有。考虑到这些函数是如何被调用的,应该总是有至少一个空闲空间。然而,在这些函数中检查空格似乎很有用,如果移动不能完成,返回0并简化main()函数循环。
  • 你应该检查xy是否在13的范围内,否则无效的值将导致具有未定义行为的越界访问。
  • 数组board不应该是全局对象,在main中定义它并将其作为参数传递是一种更好的方法。

以下是修改后的版本:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

const char PLAYER = 'X';
const char COMPUTER = 'O';

void resetBoard(char board[3][3]);
void printBoard(const char board[3][3]);
int checkFreeSpaces(const char board[3][3]);
int playerMove(char board[3][3]);
int computerMove(char board[3][3]);
int checkWinner(const char board[3][3], char player);
void printWinner(char winner);

int main(void) {
    char board[3][3];
    char winner = ' ';

    // creates a seed based on curent time
    srand(time(0));

    resetBoard(board);
    for (;;) {
        printBoard(board);
        playerMove(board);
        if (checkWinner(board, PLAYER)) {
            winner = PLAYER;
            break;
        }
        if (!checkFreeSpaces(board)) {
            break;
        }

        computerMove(board);
        if (checkWinner(board, COMPUTER)) {
            winner = COMPUTER;
            break;
        }
        if (!checkFreeSpaces(board)) {
            break;
        }
    }
    printBoard(board);
    printWinner(winner);

    return 0;
}

void resetBoard(char board[3][3]) {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            board[i][j] = ' ';
        }
    }
}

void printBoard(const char board[3][3]) {
    printf(" %c | %c | %c \n", board[0][0], board[0][1], board[0][2]);
    printf("---|---|---\n");
    printf(" %c | %c | %c \n", board[1][0], board[1][1], board[1][2]);
    printf("---|---|---\n");
    printf(" %c | %c | %c \n", board[2][0], board[2][1], board[2][2]);
}

int checkFreeSpaces(const char board[3][3]) {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (board[i][j] == ' ')
                return 1;
        }
    }
    return 0;
}

int getNumber(const char *prompt, int min, int max) {
    for (;;) {
        int val;  // value read from the user
        int rc;   // scanf return code
        int c;    // variable to store bytes read from stdin
        printf("Enter %s #(%d-%d): ", prompt, min, max);
        // read a number from the user
        rc = scanf("%d", &val);
        // read and discard the rest of the input line
        while ((c = getchar()) != EOF && c != '\n')
            continue;
        if (rc == 1) {
            if (val >= min && val <= max)
                return val;
            printf("invalid value %d, must be between %d and %d\n", val, min, max);
        } else
        if (rc == 0) {
            printf("invalid input, must be a number\n");
        } else {
            printf("unexpected end of file, exiting\n");
            exit(1);
        }
    }
}

int playerMove(char board[3][3]) {
    if (!checkFreeSpaces(board))
        return 0;

    for (;;) {
        int x = getNumber("row", 1, 3) - 1;
        int y = getNumber("column", 1, 3) - 1;

        if (board[x][y] != ' ') {
            printf("Invalid move\n");
        } else {
            board[x][y] = PLAYER;
            return 1;
        }
    }
}

int computerMove(char board[3][3]) {
    if (!checkFreeSpaces(board))
        return 0;

    for (;;) {
        // try random cells until an available one is found
        int x = rand() % 3;
        int y = rand() % 3;
        if (board[x][y] == ' ') {
            board[x][y] = COMPUTER;
            return 1;
        }
    }
}

int checkWinner(const char board[3][3], char player) {
    // check rows and columns
    for (int i = 0; i < 3; i++) {
        if (board[i][0] == player && board[i][1] == player && board[i][2] == player)
            return 1;
        if (board[0][i] == player && board[1][i] == player && board[2][i] == player)
            return 1;
    }

    // check diagonals
    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 printWinner(char winner) {
    if (winner == PLAYER) {
        printf("YOU WIN!\n");
    } else
    if (winner == COMPUTER) {
        printf("YOU LOSE!\n");
    } else {
        printf("IT'S A TIE!\n");
    }
}

字符串

c2e8gylq

c2e8gylq2#

在你的代码中有两个相同类型的错误:第一,你在函数声明和函数定义中声明了void checkWinner(),但试图从包含字符类型值的char board[][]数组中返回char值。

**解决方案:-**您需要将void checkWinner()更改为char checkWinner(),它将正常工作。

相关问题