2013-11-04 41 views
0

我正在尝试创建游戏Chomp。我半路过,但相当卡住。C - 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; 
} 
+0

我假设你会返回true/fale(1/0)并对此采取行动。 – OldProgrammer

+1

耶,好缩进! (真的。珍稀鸟:-)) – 2013-11-04 21:00:16

+0

将'check_move'重命名为'valid_move()';当建议的移动有效时使其返回'1',当它无效时返回'0'。然后你可以写下如下内容:'do getmove(...); while(!valid_move(...));'。代码中还需要更多的循环。 –

回答

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; 
} 
+0

好吧,所以在** update_board()**我现在必须检查if ** status == 1 **,如果它是1,我将执行代码。否则我会做什么? – user2952320

+0

通常情况下,除非函数的返回状态显示成功,否则不执行流中的下一项,您将指示用户再次尝试该输入(可能在while循环中)。请参阅我的编辑:) – ryyker

+0

但是我不能检查** status == 1 **是否在其他函数中,因为** status **不是全局变量,并且我不允许再有全局变量。但**状态**不必是全球性的,因为我将它返回,对吧?但其他函数不知道** status **是什么。 – user2952320