Warning! It seems that you are using Dodona within another webpage, so not everything may work properly. Let your teacher know so that he can solve the problem by adjusting a setting in the learning environment. In the meantime, you can click
this link to open Dodona in a new window.
Warning! The page was not fully loaded, probably because of a network issue. It could be that not all functionalities work as expected. Please try reloading the page.
Level 5 - One-dimension arrays and lists
0/9 0 of 9 activities solved
RPG inventory
Not yet solved
Search for life
Not yet solved
Two-dice pig
Not yet solved
Random name generator
Not yet solved
Cup draw
Not yet solved
Feud
Not yet solved
Darts
Not yet solved
Cut the deck
Not yet solved
London underground
Not yet solved
Next series
Level 6 - Count controlled iterations and scope
Previous activity
Search for life
Next activity
Random name generator
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();
}
}
Feedback