Opgepast! Het lijkt erop dat je Dodona gebruikt binnen een andere webpagina waardoor mogelijk niet alles goed werkt. Laat dit weten aan de lesgever zodat die het probleem kan oplossen door een instelling in de leeromgeving aan te passen. Ondertussen kan je op deze link klikken om Dodona te openen in een nieuw venster.
Opgepast! De pagina kon niet volledig ingeladen worden, waarschijnlijk door een netwerkprobleem. Het kan zijn dat niet alle functionaliteit beschikbaar is. Probeer de pagina te verversen.
Two-dice pig
Log in om je oplossingen te testen.
// 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();
}
}