C程序骰子游戏输出不预期

wfsdck30  于 2023-06-28  发布在  其他
关注(0)|答案(1)|浏览(113)

我试图创建一个骰子游戏使用两个c文件和一个头文件,但我相信我有一个问题,主要当试图执行它的输出不出来的权利,所以我把其他功能的情况下,但他们的工作,虽然我可能会不小心调用它在主yielding不正确的输出
Dicegame.c文件

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "dicegame.h"

// Generates a psuedo random number with parameters of max and min
int getRandomNumber(int min, int max)
{  
    //  random number of determined max and min
    int rndmNum = (rand() % (max-min+1)) + min;
    return rndmNum;
}
// Will determine the type of Round that will take place
enum ROUNDTYPE getRoundType()
{  
    // declaring roundtype
    enum ROUNDTYPE roundtype;
    // random number of 1-10
    int max = 10;
    int min = 1;

    int rT = (getRandomNumber(1, 10));
    // conditionals for roundtype

    if (rT <= 2)
    {  
        // Bonus: replace “points” to be equal to 200 as calculated above using a random number generator.
        roundtype = Bonus;
        printf("\nType    : Bonus");
    }
    else if (rT >= 5)
    {  
        // Regular: keep the “points” equal to the number of points as calculated above using a random number generator.
        roundtype = Regular;
        printf("\nType    : Regular");
    }
    else
    {  
        // Double: update “points” to be equal to DOUBLE the number of points
        roundtype = Double;
        printf("\nType    : Double");
    }
    return roundtype;
}

int getRoundPoints(enum ROUNDTYPE roundtype)
{  
    // points variable
    int points;
    // conditionals to calculate points by roundtype
    if (roundtype == Bonus)
    {
        points = 200;
    }
    // print out points (20 - 200) in multiples of 20
    else if (Double == roundtype)
    {  
        points = getRandomNumber(1, 10) * 20;    
    }
    else
    {
        // print out points (10 - 100) in multiples of 10
        points = getRandomNumber(1, 10) * 10;
    }
    //printf("%d", points);
    return points; // add to return points
}

void printPlayerPoints( int p1, int p2)
{  
    printf("%d\n",p1);
    printf("%d\n\n",p2);
}

void printRoundInfo(enum ROUNDTYPE roundtype, int dice, int points)
{  
    printf("%d\n",roundtype);
    printf("Dice    : %d\n",dice);
    printf("Points  : %d\n",points);
}`

dicegame.h

#ifndef DICEGAME_H
#define DICEGAME_H

enum ROUNDTYPE{ Bonus, Double, Regular};

int getRandomNumber(int min, int max);
enum ROUNDTYPE getRoundType();
int getRoundPoints(enum ROUNDTYPE roundtype);
void printPlayerPoints( int p1, int p2);
void printRoundInfo(enum ROUNDTYPE roundtype, int dice, int points); 

#endif // DICEGAME

main.c

// Add all the includes for the required header files
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "dicegame.h"
// Start the main() function with both the parameters

int main()
{
    // Initialize the srand() to start the random generator
    srand(time(0));
    // Initialize the player-1 and player-2 points to 0
    int p1 = 0;
    int p2 = 0;
    // Initialize all other required variables
    enum ROUNDTYPE roundtype;
    int pTurn = getRandomNumber(1, 2); 
    int round = 0;  // round++ and the end of each round in loop
    int totalRnd;   
    int dice = ((rand() % 6)+1);// getRandomNumber(1, 6);
    int points;//getRoundPoints(roundtype);

    // Ask the user for the number of rounds to run the game
    printf("How many rounds would you like to play:\t");
    scanf("%d\n", &totalRnd);
    // Call printPlayerPoints() function to print the initial player points which will be 0
    printPlayerPoints(p1, p2);
    // Set up the loop to go through all the rounds one at a time
    for (int i = 0; i == totalRnd; i++ && round++)
    {
        // Call the corresponding functions to get the information for this round - gamemode, dice, points
        getRoundType();
        getRandomNumber(1, 6);
        getRoundPoints(roundtype);
        // Print round number
        printf("Round %d of %d\n", round, totalRnd);
        printf("----------------\n");
        // Print current player
        if (pTurn % 2 == 1)
        // MAIN GAME LOGIC
        // Write code here to get the main game logic using branches
        {
            //player 1 Turn
            printf("Player      : 1");
            // Success: Player-1 (odd player) is the current player and the dice rolled is odd 
            if (dice % 2 == 1)
            {
                p1 = (points += getRoundPoints(roundtype));
            }
            // Failure: Player-1 (odd player) is the current player and the dice rolled is even
            else if(dice % 2 == 0)
            {
                p1 = (points -= getRoundPoints(roundtype));
                pTurn++;
            }
        }
        else
        {   
            //player 2 Turn
            printf("Player      : 2");
            // Success: Player-2 (even player) is the current player and the dice rolled is even.
            if (dice % 2 == 0)
            {
                p2 = (points += getRoundPoints(roundtype));
            }
        // Failure: Player-2 (even player) is the current player and the dice rolled is odd.
            else if(dice % 2 == 1)
            {
                p2 = (points -= getRoundPoints(roundtype));
                pTurn++;
            }
        }
        // Call printRoundInfo() to print the round information
        printRoundInfo(roundtype, dice, points);
        // Current player gains the points, based on the type of the round (see above). The current player’s turn continues in the next round. ()
        // Current player incurs penalty of the same number of points, based on the type of the round (see above). In the next round, the current player is changed to the other player.
        // Call printPlayerPoints() to print the player information at the end of the round
        printPlayerPoints(p1, p2);
    }
    printf("\nGAME OVER!!\n");
    // Compare the final points for player-1 and player-2
    printf("Player 1 had: %d points\n", p1);
    printf("Player 1 had: %d points\n", p2);
    // Print the winner as the one with higher points
    if (p1 > p2)
    {
        printf("Player 1 wins!!!!\n");
    }
    else if(p2 > p1)
    {
        printf("Player 2 wins!!!!\n");
    }
    else
    {
        printf("It's a tie!!!!\n");
    }
    // Return from the main() function to end the program successfully
    return 0;
}

(My输出)

Type    : RegularHow many rounds would you like to play:5
5
0
0

GAME OVER!!
Player 1 had: 0 points
Player 1 had: 0 points
It's a tie!!!!

我不知道如何使它正常工作
(所需输出)

Enter the number of rounds: 5
P1      : 0
P2      : 0

ROUND 1
--------
Player  : 1
Type    : REGULAR
Dice    : 5
Points  : 70
P1      : 70
P2      : 0

ROUND 2
--------
Player  : 1
Type    : REGULAR
Dice    : 2
Points  : 50
P1      : 20
P2      : 0

ROUND 3
--------
Player  : 2
Type    : DOUBLE
Dice    : 4
Points  : 180
P1      : 20
P2      : 180

GAME OVER!!
P2 Won
hec6srdp

hec6srdp1#

您的问题可能在于main中的for循环。

for (int i = 0; i == totalRnd; i++ && round++)

对于您的示例输入,totalRnd5。这个循环只会在i等于totalRnd时执行,而这种情况永远不会发生。循环永远不会执行。这可能意味着:

for (int i = 0; i < totalRnd; i++ && round++)

即使您输入0作为totalRnd的输入,行为也是不正确的,因为循环将执行一次而不是零次。
您还希望在更新中使用逗号而不是&&&&boolean 运算符仅在第一个表达式为true时计算第二个表达式。当i0时,i++返回0,这在C中为false。因此,在第一次迭代中,round永远不会递增。
通过使用逗号,在所有迭代中计算这两个表达式。

for (int i = 0; i < totalRnd; i++, round++)

相关问题