Level 5 - One-dimension arrays and lists 0 van 9 activiteiten opgelost
  1. RPG inventory Nog niet opgelost
  2. Search for life Nog niet opgelost
  3. Two-dice pig Nog niet opgelost
  4. Random name generator Nog niet opgelost
  5. Cup draw Nog niet opgelost
  6. Feud Nog niet opgelost
  7. Darts Nog niet opgelost
  8. Cut the deck Nog niet opgelost
  9. London underground Nog niet opgelost
Volgende reeks Level 6 - Count controlled iterations and scope
Level 5 - One-dimension arrays and lists

Two-dice pig

Meld je aan om je oplossing in te dienen en feedback te krijgen.

// Two-dice pig program using System; class Submission { // ------------------------- // Globals // ------------------------- static Random random_generator = new Random(); // ------------------------- // Subprograms // ------------------------- static int[] roll() { int[] dice = { 0, 0 }; dice[0] = random_generator.Next(1, 7); dice[1] = random_generator.Next(1, 7); return dice; } static int points(int[] dice) { if (dice[0] == 1 && dice[1] == 1) { return -1; } else if (dice[0] == 1 || dice[1] == 1) { return 0; } else { return dice[0] + dice[1]; } } static void play_game() { int[] score = { 0, 0 }; int player = 0; bool new_player = true; int total = 0; bool turn_end = false; string choice = ""; while (score[0] <= 100 && score[1] <= 100) { if (new_player) { Console.WriteLine(); Console.WriteLine("------------------------------------"); Console.WriteLine($"Player {player + 1} it's your turn"); Console.WriteLine($"Your score is currently {score[player]}"); total = 0; new_player = false; turn_end = false; } Console.WriteLine("Press Enter to roll the dice."); Console.ReadLine(); int[] dice = roll(); int result = points(dice); Console.WriteLine($"You rolled a {dice[0]} and {dice[1]}"); if (result > 0) { Console.WriteLine($"You scored {result}"); total += result; Console.WriteLine($"Your total is {total}"); choice = ""; while (choice != "y" && choice != "n") { Console.WriteLine("Do you want to continue y/n? :"); choice = Console.ReadLine(); } if (choice == "n") { score[player] = score[player] + total; turn_end = true; } } else { Console.WriteLine("Oh no, that's a pig out!"); turn_end = true; } if (turn_end) { Console.WriteLine("Press Enter to hand the dice to the next player."); Console.ReadLine(); player = (player + 1) % 2; new_player = true; } } } // ------------------------- // Main program // ------------------------- public static void Main(string[] args) { play_game(); } }