Last active
April 24, 2018 03:04
-
-
Save ThinkDigitalSoftware/202345d1888a0cd139f6ec2adf246834 to your computer and use it in GitHub Desktop.
Digital Dice Practice Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
import 'dart:io'; | |
main(List<String> arguments) { | |
print("This is a digital dice program. Press enter to roll!"); | |
print("Enter Q or q to quit."); | |
int numOfDieToRoll; | |
do { | |
if (numOfDieToRoll != null || numOfDieToRoll == 0) { | |
print("Sorry, please only enter numbers."); | |
} | |
print("How many dice would you like to roll?"); | |
try { | |
numOfDieToRoll = int.parse(stdin.readLineSync()); | |
} catch (Error) { | |
numOfDieToRoll = 0; | |
continue; | |
} | |
} while (numOfDieToRoll is! int || numOfDieToRoll == 0); | |
print("Rolling $numOfDieToRoll dice."); | |
String input; | |
do { | |
print("Press Enter when ready."); | |
input = stdin.readLineSync(); | |
var diceRoll = gen_random(numOfDieToRoll); | |
int sum = 0; | |
diceRoll.forEach((die) { | |
sum += die; | |
}); | |
for (int i = 0; i < diceRoll.length; i++) { | |
print("Die ${i + 1}: ${diceRoll[i]}"); | |
} | |
print("That totals $sum!"); | |
} while (input.toLowerCase() != "q"); | |
print("Thanks for playing!"); | |
} | |
List<int> gen_random([int numOfRandomIntsToGen]) { | |
if (numOfRandomIntsToGen == null) numOfRandomIntsToGen = 2; // default value | |
List<int> numbers = []; | |
var rGen = new Random(); // this is how you make a random number generator | |
int rand; | |
for (int i = 0; i < numOfRandomIntsToGen; i++) { | |
// looping through the number of "Dice" to roll | |
do { | |
rand = rGen.nextInt(7); // generating the random number | |
} while (rand == | |
0); // this is to check to make sure we didn't get 0, since that's not a valid die roll. | |
numbers.add(rand); | |
} | |
return numbers; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment