C程序骰子游戏输出结果不符合预期。

huangapple go评论60阅读模式
英文:

C program Dice game output not expected

问题

I have reviewed your code and the desired output. It looks like there are several issues in your code that need to be addressed to achieve the desired output. Here are some of the problems I've identified:

  1. In your main function, you are not storing the values returned by getRoundType(), getRandomNumber(1, 6), and getRoundPoints(roundtype) in variables. Instead, you are calling these functions without storing their results, which leads to incorrect calculations.

  2. You have a loop condition in your for loop that is not set up correctly. The loop should continue as long as i is less than totalRnd, so the condition should be i < totalRnd instead of i == totalRnd.

  3. You are not updating the dice variable inside the loop. You should generate a new random number for dice in each round.

  4. You are not updating the pTurn variable correctly to switch between players.

  5. The way you are calculating and updating points for each player is incorrect. You should accumulate points for each player separately.

  6. You have a typo in the printf statements where you print the final points for both players. It should be "Player 2 had" instead of "Player 1 had" for the second player.

Here is a corrected version of your main function:

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 totalRnd;
    int dice;
    int points;

    // Ask the user for the number of rounds to run the game
    printf("How many rounds would you like to play: ");
    scanf("%d", &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++)
    {
        // Call the corresponding functions to get the information for this round - gamemode, dice, points
        roundtype = getRoundType();
        dice = getRandomNumber(1, 6);
        points = getRoundPoints(roundtype);

        // Print round number
        printf("ROUND %d\n", i + 1);
        printf("--------\n");
        // Print current player
        printf("Player  : %d\n", pTurn);
        
        // Update points for the current player based on the dice roll and round type
        if ((pTurn == 1 && dice % 2 == 1) || (pTurn == 2 && dice % 2 == 0))
        {
            p1 += points;
        }
        else
        {
            p2 += points;
        }

        // Call printRoundInfo() to print the round information
        printRoundInfo(roundtype, dice, points);
        // Call printPlayerPoints() to print the player information at the end of the round
        printPlayerPoints(p1, p2);

        // Switch to the other player for the next round
        pTurn = (pTurn == 1) ? 2 : 1;
    }

    printf("\nGAME OVER!!\n");
    // Compare the final points for player-1 and player-2
    printf("Player 1 had: %d points\n", p1);
    printf("Player 2 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;
}

This corrected code should produce the desired output you mentioned.

英文:

I am trying to create a dicegame using two c files and a header file however I believe I have an issue with main when trying to execute it as the output doesnt come out right so I put the other functions in case however they work as intended though I might be accidently calling it in main yeilding the incorrect output

Dicegame.c File

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
#include &lt;string.h&gt;
#include &quot;dicegame.h&quot;
// 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 &lt;= 2)
{  
// Bonus: replace “points” to be equal to 200 as calculated above using a random number generator.
roundtype = Bonus;
printf(&quot;\nType    : Bonus&quot;);
}
else if (rT &gt;= 5)
{  
// Regular: keep the “points” equal to the number of points as calculated above using a random number generator.
roundtype = Regular;
printf(&quot;\nType    : Regular&quot;);
}
else
{  
// Double: update “points” to be equal to DOUBLE the number of points
roundtype = Double;
printf(&quot;\nType    : Double&quot;);
}
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(&quot;%d&quot;, points);
return points; // add to return points
}
void printPlayerPoints( int p1, int p2)
{  
printf(&quot;%d\n&quot;,p1);
printf(&quot;%d\n\n&quot;,p2);
}
void printRoundInfo(enum ROUNDTYPE roundtype, int dice, int points)
{  
printf(&quot;%d\n&quot;,roundtype);
printf(&quot;Dice    : %d\n&quot;,dice);
printf(&quot;Points  : %d\n&quot;,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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
#include &lt;string.h&gt;
#include &quot;dicegame.h&quot;
// 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(&quot;How many rounds would you like to play:\t&quot;);
scanf(&quot;%d\n&quot;, &amp;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++ &amp;&amp; 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(&quot;Round %d of %d\n&quot;, round, totalRnd);
printf(&quot;----------------\n&quot;);
// 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(&quot;Player		: 1&quot;);
// 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(&quot;Player		: 2&quot;);
// 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&#195;&#162;&#226;‚&#172;&#226;„&#162;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(&quot;\nGAME OVER!!\n&quot;);
// Compare the final points for player-1 and player-2
printf(&quot;Player 1 had: %d points\n&quot;, p1);
printf(&quot;Player 1 had: %d points\n&quot;, p2);
// Print the winner as the one with higher points
if (p1 &gt; p2)
{
printf(&quot;Player 1 wins!!!!\n&quot;);
}
else if(p2 &gt; p1)
{
printf(&quot;Player 2 wins!!!!\n&quot;);
}
else
{
printf(&quot;It&#39;s a tie!!!!\n&quot;);
}
// Return from the main() function to end the program successfully
return 0;
}

(My output)

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&#39;s a tie!!!!

I dont know how to make it properly function

(Desired output)

Enter the number of rounds: 5
P1&#160;     : 0
P2&#160;     : 0
ROUND 1
--------
Player&#160; : 1
Type&#160;   : REGULAR
Dice&#160;   : 5
Points&#160; : 70
P1&#160;     : 70
P2&#160;     : 0
ROUND 2
--------
Player&#160; : 1
Type&#160;   : REGULAR
Dice&#160;   : 2
Points&#160; : 50
P1&#160;     : 20
P2&#160;     : 0
ROUND 3
--------
Player&#160; : 2
Type&#160;   : DOUBLE
Dice&#160;   : 4
Points&#160; : 180
P1&#160;     : 20
P2&#160;     : 180
GAME OVER!!
P2 Won

答案1

得分: 2

你的问题可能出在main函数中的for循环部分。

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

根据你的示例输入,totalRnd 的值是 5。这个循环只在 i 等于 totalRnd 时执行,而这永远不会发生。循环根本不会执行。你可能本意是要这样的:

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

即使你将输入的 totalRnd 设为 0,行为也是不正确的,因为循环将执行一次,而不是零次。

你还希望在更新中使用逗号而不是 &&&& 布尔运算符仅在第一个表达式为真时才评估第二个表达式。当 i0 时,i++ 返回 0,在 C 中这是假的。因此,在第一次迭代时,round 永远不会增加。

通过使用逗号,两个表达式都在所有迭代中被评估。

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

Your issue likely lies with your for loop in main.

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

With you example input, totalRnd is 5. This loop only executes while i is equal to totalRnd, which never happens. The loop never executes. This was likely meant to be:

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

Even if you enter 0 as your input for totalRnd the behavior is incorrect as the loop will execute once rather than zero times.

You also want to use a comma in your update rather than &amp;&amp;. The &amp;&amp; boolean operator only evaluates the second expression if the first is true. When i is 0, i++ returns 0, which is false in C. Thus on the first iteration, round is never incremented.

By using a comma, both expressions are evaluated on all iterations.

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

huangapple
  • 本文由 发表于 2023年6月22日 06:46:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定