Random Number Game

Hi,

I am creating a dice game where the user will play against the computer. The goal is to collect more points than the opponent but no more than 6 points. If the sum of either player goes over 6 the game ends and the player has lost.

The game starts by showing the instructions to User. First Bot generates a random number (1-6) to User and to itself. Then it displays the numbers and asks whether User wants to continue. If User wants to continue Bot generates a random number to User and then decides if itself will continue and generate a random numbers to itself. The game goes on until User decides to stop rolling after which Bot may still have one roll unless it has decided to stop earlier. The game stops by displaying the winner and generated numbers of all rounds.

The problems I currenty am working are:

  1. After the user enters his name, the name is not displayed in the following message box.
  2. Once both have rolled the dice, and decide if they want to continue or not, how can I compare the results to see who won and display the winner and generated numbers of all rounds?
  3. If both players have under 6 after the first turn and continue, the results of the second
    roll should be added to the first one.

After the second roll, should I add a Do While loop to continue the process or a flow decision?
Dice Game.xaml (7.7 KB)

Thanks and regards,
Röggi

You need a couple of variables:
userName: string consisting of the input given
currentRoll: integer with the value of the current roll
totalRolls: integer which starts as zero and is incremented by the current roll (totalRolls = totalRolls + currentRoll)
numberOfRolls: integer to keep track of the number of rolls
allRolls: string, which after the first roll (if numberOfRolls = 1) should be set to the current roll. For the next rolls, make the string longer: allRolls = allRolls + ", " + currentRoll.tostring

To do 2., make a check after each roll, to see if totalRolls > 6, if thats the case, then that player has lost

1 Like

Thanks for the answer, i´ve added the variables.

The check should come after both players have rolled, how do you compare both rolls to see which is higher? Is there some wasy to do an IF where we “if roll1 < roll2 then roll1 wins” for example? How would that look like?

Then, if neither has scored a 6, I want to ask them if they want to continue, if I use an “IF” loop, can I ask the user to input yes or no and take the loop from there? In that case do I need varaibles for both yes and no options?

//Röggi

The check should come after both players have rolled, how do you compare both rolls to see which is higher? Is there some wasy to do an IF where we “if roll1 < roll2 then roll1 wins” for example? How would that look like?

Yes, just compare the two variables containing the players current score, in this way.

Then, if neither has scored a 6, I want to ask them if they want to continue, if I use an “IF” loop, can I ask the user to input yes or no and take the loop from there? In that case do I need varaibles for both yes and no options?

You can use an input to know if the game should continue. Use that information in an IF-statement.

Okay,

so once the dice has been rolled for both players I´m using an “IF” loop to see if both scored under 6, which is the precondition for the game to continue. I have another “IF” loop to see if either player scored a 6, then they won.

In the ifrst “IF” if the player selects “yes” when asked if he wants to continue, what action is needed to go back to the beginning of the game?

If the player selects “No”, I want to show the name of the winner and what numbers each player got in each round. I.e. Computer wins. Round 1 3 points. Round two 3 points or something similar.

Im using a seuence now but Im wondering if I should switch to a flowchart and a Do While loop?
Dice Game.xaml (9.8 KB)

You are going to need a while-loop, since you don’t know how many iterations is needed. Have a boolean variable as the while condition, when this becomes false, it means that the game should not continue any more. Outside the loop, you should annonce the winner.

The post is very useful and I’ve been using it to be great.

I didn´t get the game to work, I would appreciate more clearer instructions regarding the structure and flow. I.e. a bit more step by step guide.