gpt4 book ai didi

c - 为什么我的十五游戏程序打印的是 '_'而不是替换后的数字?

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

我正在学习 cs50,pset3 则为 15。游戏看起来不错,除非输入一个数字来替换“_”。在游戏开始时,当游戏要求移动数字/图 block 时,我输入要移动到的“_”数字,但它不会用所选数字替换“_”。仅当维度为偶数并在游戏开始时选择“_”左侧的数字时才会发生这种情况。谢谢!

#define _XOPEN_SOURCE 500

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

// constants
#define DIM_MIN 3
#define DIM_MAX 9

// board
int board[DIM_MAX][DIM_MAX];

// dimensions
int d;

// prototypes
void clear(void);
void greet(void);
void init(void);
void draw(void);
bool move(int tile);
bool won(void);

int main(int argc, string argv[])
{
// ensure proper usage
if (argc != 2)
{
printf("Usage: fifteen d\n");
return 1;
}

// ensure valid dimensions
d = atoi(argv[1]);
if (d < DIM_MIN || d > DIM_MAX)
{
printf("Board must be between %i x %i and %i x %i, inclusive.\n",
DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
return 2;
}

// open log
FILE *file = fopen("log.txt", "w");
if (file == NULL)
{
return 3;
}

// greet user with instructions
greet();

// initialize the board
init();

// accept moves until game is won
while (true)
{
// clear the screen
clear();

// draw the current state of the board
draw();

// log the current state of the board (for testing)
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
fprintf(file, "%i", board[i][j]);
if (j < d - 1)
{
fprintf(file, "|");
}
}
fprintf(file, "\n");
}
fflush(file);

// check for win
if (won())
{
printf("ftw!\n");
break;
}

// prompt for move
printf("Tile to move: ");
int tile = get_int();

// quit if user inputs 0 (for testing)
if (tile == 0)
{
break;
}

// log move (for testing)
fprintf(file, "%i\n", tile);
fflush(file);

// move if possible, else report illegality
if (!move(tile))
{
printf("\nIllegal move.\n");
usleep(500000);
}

// sleep thread for animation's sake
usleep(500000);
}

// close log
fclose(file);

// success
return 0;
}

/**
* Clears screen using ANSI escape sequences.
*/
void clear(void)
{
printf("\033[2J");
printf("\033[%d;%dH", 0, 0);
}

/**
* Greets player.
*/
void greet(void)
{
clear();
printf("WELCOME TO GAME OF FIFTEEN\n");
usleep(2000000);
}

/**
* Initializes the game's board with tiles numbered 1 through d*d - 1
* (i.e., fills 2D array with values but does not actually print them).
*/
void init(void)
{
int dimensionNum = (d * d) - 1;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
board[i][j] = dimensionNum--;
}
}

if (d % 2 == 0)
{
board[d - 1][d - 2] = 2;
board[d - 1][d - 3] = 1;
}
}

/**
* Prints the board in its current state.
*/
void draw(void)
{
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
if (board[i][j] == 0)
{
printf(" _");
}
else
{
printf("%3i", board[i][j]);
}
}
printf("\n");
}
}

/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
bool move(int tile)
{
int _i, _j;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
if (board[i][j] == tile)
{
_i = i;
_j = j;
}
}
}

if (board[_i + 1][_j] == 0)
{
board[_i + 1][_j] = board[_i][_j];
board[_i][_j] = 0;
return true;
}
else if (board[_i - 1][_j] == 0)
{
board[_i - 1][_j] = board[_i][_j];
board[_i][_j] = 0;
return true;
}
else if (board[_i][_j - 1] == 0)
{
board[_i][_j - 1] = board[_i][_j];
board[_i][_j] = 0;
return true;
}
else if (board[_i][_j + 1] == 0)
{
board[_i][_j + 1] = board[_i][_j];
board[_i][_j] = 0;
return true;
}

return false;
}

最佳答案

尝试在移动函数中找到棋盘中的图 block 后,您应该检查获得的数组索引是否有效。因为,如果用户输入超出允许值的数字,则会使用一些垃圾值进行进一步的条件检查。因此,在开始时将 _i 和 _j 变量设置为 -1 或其他值,并在 move 函数中的 for 循环之后检查它们是否为 - 1。如果是,则说明输入非法,可以直接返回 false。否则,您可以继续进行条件检查。其他代码似乎没问题。您还可以尝试使用调试器或完成演练以更好地理解。

如果这回答了您的问题,请点击绿色复选标记关闭此问题。

关于c - 为什么我的十五游戏程序打印的是 '_'而不是替换后的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45259190/

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