gpt4 book ai didi

c - 创建 connect 4 board c 程序时出现段错误

转载 作者:行者123 更新时间:2023-11-30 16:49:25 25 4
gpt4 key购买 nike

我正在创建一个 connect-4 游戏...我已经完成了很多工作;然而,我创建电路板的方式是静态的,并且需要是动态的,所以在我的主程序中实现它之前,我制作了一个辅助程序来解决这个问题。由于某种原因,这段代码中的 if 和 else-if 条件会产生段错误,我不明白为什么......

  // for the rows/columns of the board
for(row = num_rows - 1; row >= 0; row--){
printf("|");
for(col = 0; col < num_columns; col++){
if(aPtr[row][col] == '0') {
printf("| X ");
}
else if(aPtr[row][col] == '1') {
printf("| O ");
}
else {
printf("| ");
}
}
puts("||");
}

当我评论这些条件时,板打印得很好并且看起来像这样

------ Connect *Four ------
Connect X Command Line Game
&&===================&&
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
&&===================&&
1 2 3 4 5

这个副程序的全部内容如下,任何关于为什么会发生这种段错误的见解都将不胜感激。

    #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>


void initialize(int num_rows, int num_cols, char **aPtr) {
int i, r, c;

// create the space for the board
aPtr = malloc(num_rows * sizeof(char*));

for (i = 0; i < num_rows; i++){
aPtr[i] = malloc(num_cols * sizeof (char));
}

// go through the board and set all values equal to -1
for (r = 0; r < num_rows; r++) {
for (c = 0; c < num_cols; c++) {
aPtr[r][c] = '9';
printf("%c", aPtr[r][c]);
}
printf("\n");
}
}


void printBoard(int num_rows, int num_columns, char **aPtr) {
int row, col;

printf("\n");
puts("------ Connect *Four ------");
puts("Connect X Command Line Game");

// for fancy top of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");

// for the rows/columns of the board
for(row = num_rows - 1; row >= 0; row--){
printf("|");
for(col = 0; col < num_columns; col++){
// if(aPtr[row][col] == '0') {
// printf("| X ");
// }
// else if(aPtr[row][col] == '1') {
// printf("| O ");
// }
// else {
printf("| ");
// }
}
puts("||");
}

// for fancy bottom of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");
printf(" ");
if (col < 100){
for(col = 0; col < num_columns; col++) {
if (col < 10) {
printf(" %d ", col + 1);
}
else {
printf("%d ", col + 1);
}
}
puts("\n");
}
}

// *******************************************************************************************************
// *******************************************************************************************************

int main (int argc, char *argv[]) {

char **aPtr;
int height = 10;
int width = 5;
int i;


initialize(height, width, aPtr);
printBoard(height, width, aPtr);
}

最佳答案

这是您代码的修改,也许会有帮助。请注意,我传递了 &aPtr*aPtr = (char*) malloc(...)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>


void initialize(int num_rows, int num_cols, char **aPtr) {
int i, r, c;

// create the space for the board
*aPtr = (char*) malloc(num_rows * sizeof(char*));

if(*aPtr == NULL)
{
free(*aPtr);
printf("Memory allocation failed");
}

for (i = 0; i < num_rows; i++){
aPtr[i] = (char *) malloc(num_cols * sizeof (char));
}

// go through the board and set all values equal to -1
for (r = 0; r < num_rows; r++) {
for (c = 0; c < num_cols; c++) {
aPtr[r][c] = '9';
printf("%c", aPtr[r][c]);
}
printf("\n");
}
}


void printBoard(int num_rows, int num_columns, char **aPtr) {
int row, col;

printf("\n");
puts("------ Connect *Four ------");
puts("Connect X Command Line Game");

// for fancy top of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");

// for the rows/columns of the board
for(row = num_rows - 1; row >= 0; row--){
printf("|");
for(col = 0; col < num_columns; col++){
if(aPtr[row][col] == '0') {
printf("| X ");
}
else if(aPtr[row][col] == '1') {
printf("| O ");
}
else {
printf("| ");
}
}
puts("||");
}

// for fancy bottom of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");
printf(" ");
if (col < 100){
for(col = 0; col < num_columns; col++) {
if (col < 10) {
printf(" %d ", col + 1);
}
else {
printf("%d ", col + 1);
}
}
puts("\n");
}
}

// *******************************************************************************************************
// *******************************************************************************************************

int main (int argc, char *argv[]) {

char *aPtr;
int height = 10;
int width = 5;
int i;


initialize(height, width, &aPtr);
printBoard(height, width, &aPtr);
}

请注意,您正在执行:aPtr[r][c] = '9';,因此您的板是空的,但如果您将其更改为 0,您会得到类似的内容:

------ Connect *Four ------
Connect X Command Line Game
&&===================&&
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
&&===================&&
1 2 3 4 5

我假设这就是您所期望的?

关于c - 创建 connect 4 board c 程序时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42590796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com