Yahtzee 游戏代码告诉我,尽管没有输入,但我的掷骰无效,并且正在执行第一次掷骰两次

问题描述 投票:0回答:1

我的代码是为了玩 Yahtzee 游戏,一切正常,直到我输入玩家姓名,它进行第一次掷骰并告诉我我的掷骰无效,即使我还没有选择要保留的骰子掷骰然而。对我来说奇怪的是,当它循环到玩家 2 时,它工作得很好。如果你运行代码,你会看到你首先得到两个 roll 1,这就是问题所在。

这是代码

// Include C Libraries
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

// Macros
#define ONE 1
#define TWO 2
#define NAME 20
#define ZERO 0
#define SIX 6
#define ROLLS 3
#define THREE 3
#define FOUR 4
#define FIVE 5
#define DICE 5
#define FALSE 0
#define TRUE 1

// Define enumeration scores
enum scores {one, two, three, four, five, six, three_kind, four_kind, full_house, sm_str, lg_str, yahtzee, chance};

// Function Prototypes
void welcomeScreen();
void playGame();
void displayEmptyCard();
void displayDice();
void displayRandomDice();
int rollDie();
void initDice();
void rollDice();
int selectDice();
void displayRoll();
int isValid();
void trimTrailing();

// Main Function
int main()
{
    // Calls welcomeScreen function
    welcomeScreen();

    //random # generator
    srand((unsigned)time(NULL));
  
    // Calls playGame function
    playGame();

    // Program executed successfully
    return 0;
}

// welcomeScreen function displays YAHTZEE logo/rules
void welcomeScreen()
{
    // Prints Title
    printf("\tY       Y     A         H       H   TTTTTTTTT  ZZZZZZZZ   EEEEEEEE   EEEEEEEE\n");
    printf("\t Y     Y     A A        H       H       T            Z    E          E       \n");
    printf("\t  Y   Y     A   A       H       H       T           Z     E          E       \n");
    printf("\t   Y Y     A     A      HHHHHHHHH       T          Z      EEEE       EEEE    \n");
    printf("\t    Y     AAAAAAAAA     H       H       T         Z       E          E       \n");
    printf("\t    Y    A         A    H       H       T        Z        E          E       \n");
    printf("\t    Y   A           A   H       H       T      ZZZZZZZZ   EEEEEEEE   EEEEEEEE\n");
    printf("\n");

    // Prints Game Rules Title
    printf("YAHTZEE GAME RULES\n");
    printf("\n");

    // Prints Game Rules
    printf("1. Five six-sided dice are rolled.\n");
    printf("2. Players roll all five dice.\n");
    printf("3. Players can roll selected dice three times per turn.\n");
    printf("4. Players must score one of the 13 categories per turn.\n");
    printf("5. Players alternate turns.\n");
    printf("6. Game ends when all players score 13 categories.\n");
    printf("7. Player with the highest score wins!\n");
    printf("\n");
}

// Provides The Actual Game Play
void playGame()
{
  // Declaring Characters
  char playerOne[NAME];
  char playerTwo[NAME];
  int currentPlayer = ONE;
  int loop = ZERO;
  int dice[DICE]; 
  int keep[DICE]; 

  // Player 1 Name Input
  printf("Player One, enter your name: ");
  scanf("%s", playerOne);
  
  // Player 2 Name Input
  printf("Player Two, enter your name: ");
  scanf("%s", playerTwo);
  printf("\n");
  
  // Lets Play Display
  printf("%s and %s, let's play Yahtzee!\n", 
  playerOne, playerTwo);
  printf("\n");

  while(loop < 2)
  {
    if (currentPlayer == ONE)
    {
      // Tells The Player Whos Turn It Is
      printf("%s it's your turn.\n", playerOne);
      printf("\n");

      // Displays Empty Card/Dice
      displayEmptyCard();
      printf("\n");
      initDice(dice);
      initDice(keep);
      fflush(stdin);
      rollDice(dice, keep);
      printf("\n");
      currentPlayer = TWO;
    }
    else if (currentPlayer == TWO)
    {
      // Tells The Player Whos Turn It Is
      printf("%s it's your turn.\n", playerTwo);
      printf("\n");

      // Displays Empty Card/Dice
      displayEmptyCard();
      printf("\n");
      initDice(dice);
      initDice(keep);
      fflush(stdin);
      rollDice(dice, keep);
    }
    //Increment loop control variable
    loop++;
  }
}

// Function to Display The Empty Card
void displayEmptyCard()
{
  printf("|---------------------------------------|\n");
  printf("|  UPPER SECTION   |    LOWER SECTION   |\n");
  printf("|---------------------------------------|\n");
  printf("|---------------------------------------|\n");
  printf("| Aces   |         | 3 Kind  |          |\n");
  printf("| Twos   |         | 4 Kind  |          |\n");
  printf("| Threes |         | Full Hs |          |\n");
  printf("| Fours  |         | Sm Str  |          |\n");
  printf("| Fives  |         | LG Str  |          |\n");
  printf("| Sixes  |         | Yahtzee |          |\n");
  printf("| Total  |         | Chance  |          |\n");
  printf("| Bonus  |         | Total   |          |\n");
  printf("|---------------------------------------|\n");
}

//Function to Display Random Dice
void displayRoll(int dice[DICE])
{
  printf("\n");
  printf("+---------+ +---------+ +---------+ +---------+ +---------+\n");
  for (int i = 0; i < 5; ++i){
    if (i == 0){
      printf("|    %d    |", dice[i]);
    }
    else{
      printf(" |    %d    |", dice[i]);
    }
  } 
  printf("\n");
  printf("+---------+ +---------+ +---------+ +---------+ +---------+\n");
  printf("    (1)         (2)         (3)         (4)         (5)    \n");
  printf("\n");
}

//Function for Rolling Dice
int rollDie()
{
  int dieValue = rand() % 6 + 1;
  return dieValue;
}

//Function to Initialize Dice
void initDice(int dice[DICE]){
    for (int i = 0; i < DICE; ++i){
      dice[i] = 0;
  }
}

//Function to Roll Dice
void rollDice(int dice[DICE], int keep[DICE]){
  int roll = 0;
  while (roll != 3){
    printf("Roll %d", roll + 1);
    
    for (int i = 0; i < DICE; ++i){
      if (keep[i] == 0){
        dice[i] = rollDie();
        }
      }
    
    displayRoll(dice);
    if (selectDice(dice, keep) == 0){
      printf("The dice you have selected are invalid. Please try again.\n");
      printf("\n");
    }
    else{
      ++roll;
    }
  }
}

//Function to Select Dice
int selectDice(int dice[DICE], int keep[DICE]){
  char input[NAME];
  char data[NAME];
  char * value;
  int valid = 0;

  printf("Enter the dice you would like to keep, enter values 1 through 5 with spaces between numbers: \n");
  
  fgets(input, NAME, stdin);
  trimTrailing(input);
  strcpy(data, input);
  valid = isValid(data);
  
  printf("\n");
  
  if (valid == 0){
    return 0;
  }

  initDice(keep);
  value = strtok(input, " ");
  
  while (value != NULL){
    int number = atoi(value);
    if (number >= 1 && number <= 5){
      keep[number - 1] = 1;
      valid = TRUE;
    }
    value = strtok(NULL, " ");
  }
  return valid;
}

//Function to determine if Valid
int isValid(char data[NAME]){
  char * value;
  int valid = FALSE;
  value = strtok(data, " ");
  
  while (value != NULL){
    //printf("%d", valid);
    int number = atoi(value);
    if (number >= 1 && number <= 5){
      valid = TRUE;
    }
    else{
      valid = FALSE;
    }
    break;
  }
  value = strtok(NULL, " ");
  return valid;
}

//Function to Trim Trailing
void trimTrailing(char * str){
  
// Set default index to invalid number
  int index = -1;
  
// loop control variable
  int i = 0;
  
// Find last index of non-white space character
  while(str[i] != '\0'){
    if(str[i] != ' ' && str[i] != '\t' && str[i] != '\n'){
      index = i;
    }
    i++;
  }
  
// Mark next character to last non-white space character as NULL
  str[index + 1] = '\0';
}

我尝试跟踪我设置的值以确定何时通知用户无效输入,但我似乎无法确定它。

c loops repeat
1个回答
0
投票

scanf("%s", playerTwo);
将尾随换行符留在输入缓冲区中,
fgets(input, NAME, stdin)
会读入
selectDice()
,并且您将其作为无效输入拒绝。 您可以使用
getchar()
删除换行符。 更稳健的方法是将 I/O 和解析分开,一次读取一行(例如使用
fgets()
)。

© www.soinside.com 2019 - 2024. All rights reserved.