调试

时间:2018-03-15 21:42:26

标签: c assert

我目前正在编写代码,允许两个人玩骰子游戏。你" roll"两个骰子,如果你掷双打,你获得5分,除非:1)双打是两个六分,此时你获得25分;或者2)双打是两个三分,此时整个玩家的点数都被删除。

当我调试时,我现在有些东西会让我产生断言错误。具体来说,它表示格式!= 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);
}

1 个答案:

答案 0 :(得分:0)

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