Back to Challenge Set 7

Code Challenge: Dice Duel

Difficulty  

divider

The Brief

What they asked for
Two players each roll a die and the higher roll wins. Ties
get re-rolled in the real game but for now just say it is a
tie. Show both rolls so nobody accuses anyone of cheating.

Write your requirements list before you write any code. Input, calculate, output, and anything you had to decide for yourself.


Notes

  • Two rolls, rolled separately. One variable cannot hold both, and reusing one is the quickest way to a program where the players always tie.
  • Three outcomes, not two. The tie is easy to forget, because it is the one you will not hit while you are testing — it comes up about one run in six.
  • A die is 1 through 6, both included. With Random.Shared.Next(1, 7) the second number is where it stops, so a 7 there is how you get a 6. Activity 1.16 has the whole story.
  • Check that a 1 and a 6 can both actually come up. A die that quietly never rolls one of its faces is the bug this set exists to catch, and it will not announce itself.
  • Once you know the two rolls are different, you only have one question left. That is a nest — and it is the shape Activity 1.16's Task 1 built.

Sample Output

Sample Output
Player 1 rolls: 5
Player 2 rolls: 3
Player 1 wins!

Run it until you have seen all three outcomes — player 1 winning, player 2 winning, and a tie. The tie will take the longest and is the one most likely to be wrong.


Submit

Your requirements list and your program file, named cc-dice-duel.

Commence Challenge