每当我运行代码时,我都会收到一条消息:Segmentation fault: 11
我的问题是为什么会弹出这条消息。在此之前,我已经做了一些研究并尝试修复它,但我仍然弹出相同的消息。请查看我的代码!
为我的代码提供一些上下文:该程序的目标是在计算机和人类玩家之间实现Connect Four游戏。函数将在文件connect4_functions.c中实现。这些函数的前向声明位于文件 connect4_functions.h 中。connect4.c 包含主要的 () 函数。
代码 (connect4_functions.c) 如下:
#include "connect4_functions.h"
int print_welcome(void) // PROBLEM: returnerer alltid 1
{
printf ("*** Welcome to the Connect Four game!!! ***n");
printf ("Would you like to make the first move [y/n]: ");
if (getchar() == 'N' || getchar() == 'n') return 2;
else return 1;
while (getchar()!= 'n');
}
void display_board(int board[][BOARD_SIZE_VERT])
{
int i, j;
for (i = BOARD_SIZE_VERT-1; i >= 0; i--)
{
for (j = 0; j < BOARD_SIZE_HORIZ; j++) printf ("+---");
printf ("+n");
for (j = 0; j < BOARD_SIZE_HORIZ; j++)
{
switch (board[j][i])
{
case 0: printf ("| "); break;
case 1: printf ("| O "); break;
case 2: printf ("| X "); break;
}
}
printf ("|n");
} // end for
for (j = 0; j < BOARD_SIZE_HORIZ; j++) printf ("+---");
printf ("+n");
for (j = 1; j <= BOARD_SIZE_HORIZ; j++) printf (" %d ", j);
printf ("n");
} //end function display_board
int random_move(int board[][BOARD_SIZE_VERT], int computer_num)
{
int m = (rand() % BOARD_SIZE_HORIZ) + 1;
if (!is_column_full(board,m))
{
update_board(board,m,computer_num);
return m;
}
else return random_move(board,computer_num);
}
int player_move(int board[][BOARD_SIZE_VERT], int player_num)
{
int m;
printf ("Please enter your move: ");
scanf ("%d", &m);
while (getchar() != 'n');
if ( 0 > m || m > BOARD_SIZE_HORIZ)
{
printf ("Not a valid move. Enter a column number!n");
return player_move(board, player_num);
}
if (is_column_full(board, m))
{
printf ("This column is full. Try again!n");
return player_move(board, player_num);
}
update_board(board,m,player_num);
return m;
}
bool check_win_or_tie(int board[][BOARD_SIZE_VERT], int last_move)
{
int m, count = 0;
if (check_winner(board, last_move))
{
printf("Player %c won!n", ( check_winner(board,last_move) == 1 ? '1' : '2' ) );
return true;
}
for (m = 0; m < BOARD_SIZE_HORIZ; m++) if ( is_column_full(board, m) ) count++;
if (count == BOARD_SIZE_HORIZ)
{
printf ("Tie game!n");
return true;
}
else return false;
}
bool is_column_full(int board[][BOARD_SIZE_VERT], int m)
{
return (board[m-1][BOARD_SIZE_VERT-1]);
}
void update_board(int board[][BOARD_SIZE_VERT], int m, int player_num)
{
int i;
for ( i = 0; i < BOARD_SIZE_VERT ; i++)
{
if (!board[m-1][i])
{
board[m-1][i] = player_num;
return;
}
}
}
int check_winner(int board[][BOARD_SIZE_VERT], int last_move)
{
int i, row, count;
// Find row
for (row = 0; row < BOARD_SIZE_VERT; row++)
{
if (board[last_move-1][row]) count++;
}
row = count;
printf ("row = %dn", row);
// Vertical
for (i = 0; i < BOARD_SIZE_VERT; i++)
{
if (board[last_move-1][i] == board[last_move-1][row]) count++;
else count = 0;
if (count == 4) return board[last_move-1][row];
else return 0;
}
count = 0; // reset
// Horizontal
for (i = 0; i < BOARD_SIZE_HORIZ; i++)
{
if (board[i][row] == board[last_move-1][row]) count++;
else count = 0;
if (count == 4) return board[last_move-1][row];
else return 0;
}
count = 0; // reset
return 0;
}
connect4_functions.h(无法更改)的代码如下:
#ifndef CONNECT4_FUNCTIONS
#define CONNECT4_FUNCTIONS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define BOARD_SIZE_HORIZ 7
#define BOARD_SIZE_VERT 6
int print_welcome(void);
void display_board(int board[][BOARD_SIZE_VERT]);
int random_move(int board[][BOARD_SIZE_VERT], int computer_num);
int player_move(int board[][BOARD_SIZE_VERT], int player_num);
bool check_win_or_tie(int board[][BOARD_SIZE_VERT], int last_move);
bool is_column_full(int board[][BOARD_SIZE_VERT], int m);
void update_board(int board[][BOARD_SIZE_VERT], int m, int player_num);
int check_winner(int board[][BOARD_SIZE_VERT], int last_move);
int best_move(int board[][BOARD_SIZE_VERT], int computer_num);
#endif
connect4.c的代码(也无法更改)如下:
#include "connect4_functions.h"
int main()
{
int board[BOARD_SIZE_HORIZ][BOARD_SIZE_VERT] = { {0} };
int player_num, computer_num;
int last_move;
/* Ask Alice if she wants to go first */
player_num = print_welcome();
if (player_num == 1) computer_num = 2;
else computer_num = 1;
/* If Alice wants to go first, let her make a move */
if (player_num == 1)
{
display_board(board);
last_move = player_move(board,player_num);
display_board(board);
}
/* The main loop */
while (1)
{
/* Make a computer move, then display the board */
last_move = random_move(board,computer_num);
printf("Computer moved in column: %dn", last_move);
display_board(board);
/* Check whether the computer has won */
if (check_win_or_tie(board,last_move)) return 0;
/* Let Alice make a move, then display the board */
last_move = player_move(board,player_num);
display_board(board);
/* Check whether Alice has won */
if (check_win_or_tie(board,last_move)) return 0;
} /* end of while (1) */
} /* end of main() */
另外,如果您想查看原始pdf,请在下面链接:
链接到 pdf
输出为:
*** Welcome to the Connect Four game!!! ***
Would you like to make the first move [y/n]: y
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
1 2 3 4 5 6 7
Please enter your move: 4
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | O | | | |
+---+---+---+---+---+---+---+
1 2 3 4 5 6 7
Computer moved in column: 1
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| X | | | O | | | |
+---+---+---+---+---+---+---+
1 2 3 4 5 6 7
row = -355898527
Segmentation fault: 11
(如果您对上下文或目的有任何疑问,请随时询问!
没有实际运行代码,我相信问题可能是计数没有在check_winner中初始化:
int check_winner(int board[][BOARD_SIZE_VERT], int last_move)
{
int i, row, count;
// Find row
for (row = 0; row < BOARD_SIZE_VERT; row++)
{
if (board[last_move-1][row]) count++;
}
row = count;
// WHAT IF IT EXITS THE for () WITHOUT board[last_move-1][row] being non-zero?
// HINT: count can be anything!
printf ("row = %dn", row);
然后,您将row
用作对阵列的访问,然后轰隆隆!您访问了超出权限的内存。
在check_winner函数中,您不初始化变量计数。我得到一个单位化变量运行时错误,但如果我稍后继续前进几行,然后设置:
row = count;
在我的例子中,将行设置为 -858993459;
在你写的同一个函数中,有几行:
if (board[last_move - 1][i] == board[last_move - 1][row]) count++;
// You use "row", an initialised variable as the index
// for the second subscript operator of board.
这就是我得到的地方:
Access violation reading location 0x0000005663E82BB4. occurred
我认为应该对这个单位化变量发出警告。老实说,我不知道这是否是它的全部问题,但它看起来是一款相当不错的游戏。
首先,为了能够解决您的问题,您应该确保在崩溃之前可能打印的所有内容都已打印出来。在此目标中,您应该在每个打印指令之后添加一条fflush(stdout);
指令
其次,如果这不能明确编程崩溃的位置,则应在发生崩溃的代码段中添加补充跟踪指令。
然后,当您确定哪个指令导致 de 崩溃时,您应该打印它的变量以检查它们的值是否是您所期望的。
或者,您可以使用assert
指令来确保变量值都在预期范围内。