我正在尝试创建游戏咬伤。我已经走了一半,但还是被困住了。
游戏将有5个不同的功能。指针和结构是不允许的。
( a) 初始化()将矩阵中的每个位置初始化为'O‘。没有参数或返回值。
b) print_board()打印矩阵。没有参数或返回值。
( c) get_move()扫描播放机的移动(行和行),并使用数组“返回”它。参数:关于是谁转动它的信息(Player 1或Player 2),以及一个包含两个元素的数组,其中移动坐标将被存储。没有返回值。
( d) check_move()控制移动是否合法(不在矩阵之外,也不是已经被吃掉的位置)。参数:要检查的移动(行和行)。返回值:控件的结果。<?
( e) update_board()更新矩阵。参数:新移动(行和行)。没有返回值。
这就是我所取得的成就,我被困在了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()
{
for(int row = 0; row < height; row++)
{
for(int col = 0; col < width; col++)
{
printf("%c", matrix[row][col]);
}
printf("\n");
}
printf("\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];
if(row <= height && col <= width)
{
for(row; row <= height; row++)
{
for(col; col <= width; col++)
{
if(matrix[row][col] == ' ');
printf("Invalid move! \n");
}
}
}
}
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();
get_move(player, position);
check_move(position);
update_board(position[0], position[1]);
print_board();
getchar();
getchar();
getchar();
return 0;
}发布于 2013-11-04 21:11:29
您应该从check_move返回一个值,因为它是以这种方式进行原型的,所以我假设您希望在某个地方使用退出状态?:
int check_move(int position[]) 并且在您的;的末尾有一个不必要的if:
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++)
{
if(matrix[row][col] == ' ') //removed ";" from end of line, otherwise,
{ //printf will always be called
printf("Invalid move! \n");
return 0;
}//also added brackets to force return if error (only need one error to return)
}
}
}
return status; //added return status
}所以,基本上,您可以这样做来调用check_move():
int main(void)
{
int player = 1;
int position[2];
initialize();
print_board();
get_move(player, position);
while(check_move(position) != 1)
{
printf("Try again!\n\n");
print_board();
get_move(player, position);
}
update_board(position[0], position[1]);
print_board();
getchar();
getchar();
getchar();
return 0;
}https://stackoverflow.com/questions/19776967
复制相似问题