gpt4 book ai didi

c - 调试时断言错误

转载 作者:太空宇宙 更新时间:2023-11-04 04:18:30 24 4
gpt4 key购买 nike

我目前正在编写允许两个人玩骰子游戏的代码。您“掷”了两个骰子,如果掷出双骰,您将获得 5 分,除非: 1) 双骰是两个 6,此时您将获得 25 分;或 2) double 是两个三分,此时整个玩家的得分都被删除。

目前,我在调试时遇到了断言错误。具体来说,它表示格式 != nullptr。

我不太确定这里发生了什么。任何帮助将不胜感激!

这是我的代码。

/*This program allows users to play the dice game Fifty, where they roll a pair of dice to obtain doubles.
Any set of doubles except for threes or sixes earn the player 5 points. Sixes earn the player 25 points.
If the player rolls a pair of threes, their entrie score will be reset to 0. Play continues until one
player reaches fifty points. At this point, the game ends and the players have the chance to start a new
game.*/

#include <stdio.h>
#include <stdlib.h>

//This function will generate a random number for the dice roll.
int roll(void);
//This function will compare the two dice rolls to see if they are doubles and compute the correct score change.
int compareDiceRoll(int diceRollOne, int diceRollTwo, int turnNumber, int playerOneScore, int playerTwoScore);
//This function will print out the players' current scores.
void printPlayerScores(int playerOneScore, int playerTwoScore);

int main(void)
{
/*//Declaring variables
//These variables will be storing the dice rolls to compare the numbers to see if the player receives points or if points are deducted.
int diceRollOne, diceRollTwo;
//These variables track the players' scores.
int playerOneScore, playerTwoScore;
//These variables determine the amount by which a player's score is incremented.
int playerOneScoreIncrement, playerTwoScoreIncrement;
//This variable determines which turn the game is on.
int turnNumber;*/
//This variable determines whether the users will quit the game or not.
char quit;

//Initializing the variables
int diceRollOne = 0, diceRollTwo = 0, playerOneScore = 0, playerTwoScore = 0, playerOneScoreIncrement = 0, playerTwoScoreIncrement = 0;
int turnNumber = 1;

//This seeds the random number function with the current internal system time to keep the numbers as random as possible.
srand((unsigned)time(NULL));

for (; (playerOneScore < 15 && playerTwoScore < 15);)
{
//This section "rolls" the dice.
diceRollOne = roll();
diceRollTwo = roll();

//Prints the dice roll.
printf("\nYour dice roll is: %d %d\n\n", diceRollOne, diceRollTwo);

//Compare the dice roll to determine the score increment.
if (turnNumber = 1)
{
scanf_s(compareDiceRoll(diceRollOne, diceRollTwo, turnNumber, playerOneScore, playerTwoScore), &playerOneScoreIncrement);
}
else
scanf_s(compareDiceRoll(diceRollOne, diceRollTwo, turnNumber, playerOneScore, playerTwoScore), &playerTwoScoreIncrement);
//Increment the score.
playerOneScore = playerOneScore + playerOneScoreIncrement;
playerTwoScore = playerTwoScore + playerTwoScoreIncrement;

//Print the scores.
printPlayerScores(playerOneScore, playerTwoScore);

printf("Would you like to continue playing: Y/N ?\n\n");
scanf_s(" %c", &quit);

if (quit == 'N')
{
printf("\nThank you for playing!\n\n");
break;
}
else
{
turnNumber++;
turnNumber = (turnNumber + 1) % 2;
}
}
}

//This creates a random number between 1 and 6.
int roll(void)
{
int randomNumber = (rand() % 6) + 1;

return randomNumber;
}

//This function compares the dice rolls and determines the increase in the players' scores.
int compareDiceRoll(int diceRollOne, int diceRollTwo, int turnNumber, int playerOneScore, int playerTwoScore)
{
int playerOneScoreIncrement, playerTwoScoreIncrement;

//This section determines if the player has rolled doubles and if so, what should happen to their score.
if (diceRollOne == diceRollTwo)
{
if (turnNumber == 1)
{
if ((diceRollOne == 1) || (diceRollOne == 2) || (diceRollOne == 4) || (diceRollOne == 5))
{
playerOneScoreIncrement = 5;
return playerOneScoreIncrement;
}
else if (diceRollOne == 6)
{
playerOneScoreIncrement = 25;
return playerOneScoreIncrement;
}
else
{
playerOneScoreIncrement = -1 * playerOneScore;
}
}

else
{
if ((diceRollOne == 1) || (diceRollOne == 2) || (diceRollOne == 4) || (diceRollOne == 5))
{
playerTwoScoreIncrement = 5;
return playerTwoScoreIncrement;
}
else if (diceRollOne == 6)
{
playerTwoScoreIncrement = 25;
return playerTwoScoreIncrement;
}
else
{
playerTwoScoreIncrement = -1 * playerTwoScore;
return playerTwoScoreIncrement;
}
}
}
//This section is for the non-double rolls.
else
{
if (turnNumber == 1)
{
playerOneScoreIncrement = 0;
return playerOneScoreIncrement;
}
else
{
playerTwoScoreIncrement = 0;
return playerTwoScoreIncrement;
}
}
}

//This function prints the current player scores.
void printPlayerScores(int playerOneScore, int playerTwoScore)
{
printf("The current scores are:\nPlayer 1: %d\nPlayer 2: %d\n\n",
playerOneScore, playerTwoScore);
}

最佳答案

我弄乱了我的 scanf_s 参数,所以错误已修复!

关于c - 调试时断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49309724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com