我在尝试创造游戏Chomp。我已经走了一半,但还是被困住了。
游戏将有5个不同的功能。指针和结构是不允许的。
这就是我所取得的成就,我为一些问题而奋斗了一段时间,但我不知道如何独自解决这些问题,所以我想我可以在这里得到一些帮助。
BUGS
a),如果您首先输入2 2,然后输入2 1,它会说这个位置已经被吃掉了,尽管它是一个完全有效的进食位置。与其检查位置是否为!= 'O',不如检查它是否是== 'O',但这也不起作用,因为在循环中,行和col并不总是一个O.
b)如果您输入一个不在矩阵内的位置(即20,20),您将得到的两行错误。我不明白为什么。当然,我只想显示一个错误,而不是两个错误。
c) (如果您输入一个已经被吃掉的位置,您将得到错误“已经被吃掉!”),这几次是因为循环多次遍历打印。
问题
a) Player 1和Player 2之间的最佳替代方式是什么?我想到了一个整数,当玩家做出有效的移动时,它会增加+1。然后我将检查int的值是奇数还是偶数。奇数= Player 1和偶数= Player 2或副verca。但这是行不通的,因为我不能拥有比现在更多的全局变量。我只能从一个函数(check_move()).返回一个值
#include <stdio.h>
int height = 4;
int width = 10;
char matrix[4][10];
void initialize()
{
for(int row = 0; row < height; row++)
for(int col = 0; col < width; col++)
matrix[row][col] = 'O';
}
void print_board()
{
printf("\n\n");
for(int row = 0; row < height; row++)
{
for(int col = 0; col < width; col++)
{
printf("%c", matrix[row][col]);
}
printf("\n");
}
printf("\n\n");
}
void get_move(int player, int input[])
{
printf("Player %d, make your move: ", player);
scanf("%d %d", &input[0], &input[1]);
}
int check_move(int position[])
{
int row = position[0];
int col = position[1];
int status = 1;
if(row <= height && col <= width)
{
for(row; row <= height; row++)
{
for(col; col <= width; col++)
{
// Checks if position already has been eaten
if(matrix[row-1][col-1] != 'O')
{
printf("Already eaten!\n");
status = 0;
}
}
}
}
else if(row >= height || col >= width)
{
printf("Your move must be inside the matrix!\n");
status = 0;
}
return status;
}
void update_board(int x, int y)
{
for(int xi = x; xi <= 10; ++xi)
{
for(int yi = y; yi <= 10; ++yi)
matrix[xi-1][yi-1] = ' ';
}
}
int main(void)
{
int player = 1;
int position[2];
initialize();
print_board();
while(1){
get_move(player, position);
check_move(position);
while(check_move(position) != 1)
{
printf("Try again!\n\n");
get_move(player, position);
}
update_board(position[0], position[1]);
print_board();
}
getchar();
getchar();
getchar();
return 0;
}发布于 2013-11-05 16:57:28
Bug和c:
您的check_move函数是错误的,您应该只测试所播放的位置是否被吃掉,其他位置的状态与此无关:
int check_move(int pos[])
{
if(pos[0] < 1 || pos[0] > height || pos[1] < 1 || pos[1] > width)
{
printf("Your move must be inside the matrix!\n");
return 0;
}
if(matrix[ pos[0] - 1 ][ pos[1] - 1 ] != 'O' ) {
printf("Already eaten!\n");
return 0;
}
return 1;
}Bug b:
您将收到两次错误消息,因为您在主程序中调用了两次check_move:
check_move(position);
while(check_move(position) != 1)只需删除对check_move()的第一个无用调用即可。
问题a:
通过更新主服务器中的变量player,您可以在播放机之间切换:
player = (player + 1) % maxNumberOfPlayer;这将从0转到maxNumberOfPlayer - 1,因此您可以使用printf("Player %d, make your move: ", player + 1);进行更方便用户的输出。另外,如果是maxNumberOfPlayer = 2,则player = (player + 1) % 2;等同于player = !player。
发布于 2013-11-05 16:31:12
在main中,while循环中只需添加:
player = !player;这将在0和1之间切换player。
https://stackoverflow.com/questions/19793873
复制相似问题