0

我正在尝试创建游戏Chomp。我已经完成了一半,但非常卡住。

游戏将有5个不同的功能。不允许使用指针和结构。

a) initialize()将矩阵中的每个位置初始化为'O'。没有参数或返回值。

b) print_board()打印矩阵。没有参数或返回值。

c) get_move()扫描玩家的移动(行和列)并用数组“返回”它。参数:关于轮到谁的信息(玩家 1 或玩家 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;
}
4

1 回答 1

0

您应该从 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;
}
于 2013-11-04T21:11:29.940 回答