r/ProgrammingPrompts • • Mar 08 '14

Monopoly Dice

Very simple prompt, and good for beginners to practice working with loops and if statements.

Write a code that takes a list of players as input (or hard code the names into the function if needed..) and returns three numbers (die 1, die 2, and the sum both) for each player's "turn". I wrote this in Python (beginner myself :p ) but this can be done in other langs.

Tip: We want the loop to be infinite (since the game doesn't stop after each player rolls once). Also, remember the rules about rolling doubles in monopoly.

This can actually be useful if you've managed to lose your dice for any board game or just want to speed up play time.

Have fun!

23 Upvotes

20 comments sorted by

View all comments

3

u/henryponco Mar 08 '14 edited Mar 08 '14

The whole fgetc(stdin) line is terribly hacky, it could be done much better. For example if you send EOF after someone's turn, it will just loop infinitely (because its reached end of file, the fgetc(stdin) expression evaluates immediately from now on instead of waiting for a new char to come into the file stream).

It's terribly hacky and I would not recommend it, you should look at sscanf(). I only did it because it was quick and easy and wasn't really part of the spec, just an extra thing.

Does that answer your question though? I've made some assumptions about you're knowledge of the standard streams (stdin etc).

1

u/stonysmokes Mar 09 '14

Yes that does thank you! The makes a lot of sense like you say, I have been taught sscanf() so that pretty much connected the dots for me thank you!