Level 5 - One-dimension arrays and lists 0 of 9 activities solved
  1. RPG inventory Not yet solved
  2. Search for life Not yet solved
  3. Two-dice pig Not yet solved
  4. Random name generator Not yet solved
  5. Cup draw Not yet solved
  6. Feud Not yet solved
  7. Darts Not yet solved
  8. Cut the deck Not yet solved
  9. London underground Not yet solved
Next series Level 6 - Count controlled iterations and scope
Level 5 - One-dimension arrays and lists

Two-dice pig

Sign in to hand in your solution and get feedback.

// 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(); } }