首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C:创建一个游戏,但在某些情况下,我会因为一个错误和无限循环错误而失败

C:创建一个游戏,但在某些情况下,我会因为一个错误和无限循环错误而失败
EN

Stack Overflow用户
提问于 2012-04-14 10:47:00
回答 1查看 181关注 0票数 0

我在C语言的入门课上,我的教授布置我们为我们现在的作业编写一个叫做“茶话会”的游戏。我已经完成了这个游戏的编码,它在很大程度上是可以工作的,但是有一些我似乎不能解决的问题。

游戏规则很简单:两个玩家轮流旋转旋转器(通过输入"0“并按enter键来模拟),并为茶话会收集所有7件物品。第一个获得全部7个物品的玩家获胜。唯一的问题是,除非你先有一个盘子,否则你不能收集三明治、水果或甜点。如果你落在“输掉一块”方块上,你必须放弃其中一块。这两个错误都来自游戏中的lose a game实例,所以我认为这个错误一定是来自"get_lost_piece“函数。

其中之一是"player“数组中的片段编号奇怪,因为它们比正常值高出1个值。另一个错误是,当玩家试图在他们有一个需要盘子的物品时移走他们的盘子时,它应该打印出“对不起,没有盘子吃饭是不礼貌的。输入另一个选择:”,但是,我得到了一个无限循环,上面写着“你丢失了物品1”。

下面是我的代码:

代码语言:javascript
复制
    #include <stdio.h>
    #include <time.h>

    #define SLOW_MODE 1

    #define NUMPLAYERS 2
    #define NUMPIECES 7
    #define MAXLEN 20
    #define NO_WINNER -1

    const char CHOICES[NUMPIECES+1][MAXLEN] = {"PLATE", "NAPKIN", "TEA CUP", "CREAM AND    SUGAR", "SANDWICH", "FRUIT", "DESSERT", "LOSE A PIECE"};

        void update_player(int player[], int square);
    int get_lost_piece(int player[]);
    int search(int piece_list[], int choice);
    int get_spin();
    void init_player(int player[]);
    int get_winner(int players[][NUMPIECES]);
    int get_next_player(int player_num);
    int count_pieces(int player[]);
    void print_player(int player[], int player_num);

    int main() {

    srand(time(0));

    int players[NUMPLAYERS][NUMPIECES];

    // Initialize each player in the game.
    int i;
    for (i=0; i<NUMPLAYERS; i++)
        init_player(players[i]);

    int player_number = 0;

    // Play until we get a winner.
    int status = get_winner(players);
    while (status == NO_WINNER) {

        int dummy;

        // In slow mode, we stop before every spin.
        if (SLOW_MODE) {
            printf("Player %d, it is your turn. Type 0 and enter to spin.\n", player_number+1);
            scanf("%d", &dummy);
        }

        // Get the current player's spin and print out her pieces.
        int square = get_spin();
        printf("Player %d, have landed on the square %s.\n", player_number+1, CHOICES[square]);
        update_player(players[player_number], square);
        print_player(players[player_number], player_number+1);

        // Update the game status.
        player_number = get_next_player(player_number);
        status = get_winner(players);
        printf("\n\n");
    }

    printf("Congrats player %d, you win!\n", status+1);

    return 0;
}

    // Pre-conditions: player stores the contents of one player and square is in between 0 and 7, inclusive.
    // Post-conditions: The turn for player will be executed with the given square selected.
void update_player(int player[], int square) {

    if (square == 7) {

        if (count_pieces(player) == 0)
        {
            printf("There is no piece for you to lose. Lucky you, sort of.\n");
            return;
        }

        else{
            int q;
            q = get_lost_piece(player);
            player[q]= 0;

        }
        return;
    }

    player[square]=search(player, square);

    // Restricted by having no plate!
    if (player[0] == 0) {

       if(square == 4 || square == 5 ||square == 6){
        printf("Sorry, you can't obtain that item because you don't have a plate yet.\n");}

        else{
            if (player[square] == 0){
                player[square]++;
        }
        }
        }

    // Process a regular case, where the player already has a plate.
    else {
        if (player[square] == 0){
            player[square]++;
        }
        }
    }


    // Pre-conditions: player stores the contents of one player that has at least one piece.
    // Post-conditions: Executes asking a player which item they want to lose, and reprompts them
//                  until they give a valid answer.
int get_lost_piece(int player[]) {


    int choice = -1;



    // Loop until a valid piece choice is made.
    while (1) {
    if (choice == -1){
        printf("Which piece would you like to lose?\n");
        print_player(player,choice);
        scanf("%d", &choice);}
    if (player[choice] == 0 && choice < 7 && choice >= 0){
        printf("Sorry, that was not one of the choices");
        scanf("%d", &choice);
        }
    if (player[0] == 1 && choice == 4 || choice == 5 ||choice == 6){
        printf("Sorry, it is bad manners to eat without a plate. Enter another choice:\n");
        scanf("%d", &choice);
        }

    else{
        printf("You lost piece %d\n", choice);
        }
    }

    return choice;
}

// Pre-conditions: piece_list stores the contents of one player
// Post-conditions: Returns 1 if choice is in between 0 and 6, inclusive and corresponds to
//                  an item in the piece_list. Returns 0 if choice is not valid or if the
//                  piece_list doesn't contain it.
int search(int piece_list[], int choice) {

    int i;
    for (i=0; i<NUMPIECES; i++){
        if(piece_list[i]==choice){
           return 1;}
        else {return 0;}
    }
}
// Pre-condition: None
// Post-condition: Returns a random value in between 0 and 7, inclusive.
int get_spin() {

   return rand() % 8;

}

// Pre-condition: None
// Post-condition: Initializes a player to have no pieces.
void init_player(int player[]) {

  int j;

  for (j=0; j< NUMPIECES; j++)
    player[j]=0;

}

// Pre-condition: players stores the current states of each player in the tea party game.
// Post-condition: If a player has won the game, their 0-based player number is returned.
//                 In the case of no winners, -1 is returned.
int get_winner(int players[][NUMPIECES]) {

   int i =0;
  for (i=0; i<NUMPLAYERS; i++){
    if(count_pieces(players[i]) == NUMPIECES) {
        return i;}
  }
 return -1;


}

// Pre-condition: 0 <= player_num < NUMPLAYERS
// Post-condition: Returns the number of the next player, in numerical order, with
//                 a wrap-around to the beginning after the last player's turn.
int get_next_player(int player_num) {

    player_num++;
    if (player_num == NUMPLAYERS){
        player_num = 0;
    }
    return player_num;

}


// Pre-conditions: player stores the contents of one player
// Post-conditions: Returns the number of pieces that player has.
int count_pieces(int player[]) {

    int y, counter;

    counter = 0;

    for ( y = 0; y < 7; y++){
        if(player[y] == 1){
        counter++;
        }
    }
return counter;
}

// Pre-conditions: player stores the contents of one player and player_num is that
//                 player's 1-based player number.
// Post-conditions: Prints out each item the player has, numbered with the numerical
//                  "codes" for each item.
void print_player(int player[], int player_num) {

    int i;

    printf("\n");
    printf("Player %d\n", player_num);

    for (i=0; i < 7; i++){
        if (player[i] == 1){
            printf("%d. %s\n", i + 1, CHOICES[i]);
        }
    }

}

提前感谢您的帮助。我感觉解决方案就在我面前,但在这上面花了几天时间后,我很难发现问题所在。

EN

回答 1

Stack Overflow用户

发布于 2012-04-14 14:42:43

  • 需要将#include <stdlib.h>用于rand(),将player_number参数传递给get_lost_piece()并将其传递给print_player.

以下只是重构的一个想法。它可以通过许多不同的方式来完成。

  • 在循环开始时获取一次输入。
  • 在每个if语句中使用continue关键字来重做循环。

当我将get_lost_piece()的逻辑更改为:

代码语言:javascript
复制
 while...
     get choice
     if  choice < 1 || choice > 7   "1-7 please..." continue
     if  player[choice - 1] == 0   "sorry..." continue
     if  choice == 1  (and player[] contains foods)   "bad manners..." continue
 return choice - 1;

有用的提示

代码循环应该受到限制,并需要给播放器一个退出代码输出FAQ entryscanf()返回错误,将内容留在输入流上。

  • 尽早测试,经常测试。
  • 出现编译器警告(由catcall建议)

<代码>F233

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10150612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档