英文:
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:
-
In your
main
function, you are not storing the values returned bygetRoundType()
,getRandomNumber(1, 6)
, andgetRoundPoints(roundtype)
in variables. Instead, you are calling these functions without storing their results, which leads to incorrect calculations. -
You have a loop condition in your
for
loop that is not set up correctly. The loop should continue as long asi
is less thantotalRnd
, so the condition should bei < totalRnd
instead ofi == totalRnd
. -
You are not updating the
dice
variable inside the loop. You should generate a new random number fordice
in each round. -
You are not updating the
pTurn
variable correctly to switch between players. -
The way you are calculating and updating points for each player is incorrect. You should accumulate points for each player separately.
-
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 <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 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's a tie!!!!
I dont know how to make it properly function
(Desired output)
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
答案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
,行为也是不正确的,因为循环将执行一次,而不是零次。
你还希望在更新中使用逗号而不是 &&
。&&
布尔运算符仅在第一个表达式为真时才评估第二个表达式。当 i
为 0
时,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++ && 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 < totalRnd; i++ && 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 &&
. The &&
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 < totalRnd; i++, round++)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论