A classic game of chance.
Watch this video to learn about the new concepts shown in the program:
Whenever you want to use more than one variable with the same name, such as dice1, dice2, you should consider if an array or a List<T> would be a better approach. It is more scalable which means the program can be extended without much if any additional code for new situations.
An array must already exist with the correct number of elements before you can refer to its indexes. E.g. int[] dice = { 0, 0 } declares that dice is an array with two indexes, both assigned the number zero. That enables you to use dice[0] = and dice[1] = because the original data can now be over-written. Remember empty arrays have no indexes.
Using modulus is a good way of looping a variable back to zero when it goes past a maximum value. E.g. x = x % 6 would prevent the variable x storing more than the number six.
Questions to think about with this program to check your understanding:
At the end of the player’s turn the line player = (player + 1) % 2 is executed. What does this achieve, and how does it relate to declaring int[] score = new int[2] initialised to {0, 0}?
If it is player 1’s turn it becomes player 2’s turn. If it is player 2’s turn it becomes player 1’s turn.
Player 1 is actually player 0 in the code, and player 2 is player 1 in the code.
score is an array storing the score for both players. You can use score[player] to change or output the score for any player without the need for additional selection statements to determine whose turn it is.
It also means you can increase the number of players without having to add any additional code. This means the program is more “scalable”.
The condition while (score[0] <= 100 && score[1] <= 100) works for a two player game, but does not scale well if you wanted a 3-6 player game. What is a better approach here?
Use a flag to indicate if the game has been won. E.g.
int winner = -1; // Nobody has won the game.
while (winner == -1)
{
if (score[player] >= 100)
{
winner = player;
}
}
This code is much more scalable because it allows any number of players without any additional code.
Change the program so that:
The program prompts for the number of players (
How many players? 1-4:) and whether to continue (Do you want to continue y/n? :); the press-Enter pause reads a line of stdin without printing a prompt.
roll, points
// Function to return the outcome of the roll of two dice as an array
static int[] roll()
{
---
int[] dice = { 0, 0 };
dice[0] = random_generator.Next(1, 7);
dice[1] = random_generator.Next(1, 7);
return dice;
---
}
---
// Function to calculate the points scored from the dice roll
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];
---
}
}
play_game, main program
// Procedure to play the game
static void play_game(int players)
{
---
int[] scores = new int[players];
int player = 0;
bool new_player = true;
int total = 0;
bool turn_end = false;
bool game_won = false;
while (!game_won)
{
---
if (new_player)
{
---
Console.WriteLine();
Console.WriteLine("------------------------------------");
Console.WriteLine($"Player {player + 1} it's your turn.");
Console.WriteLine($"Your score is currently: {scores[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 = total + result;
Console.WriteLine($"Your total is {total}");
string choice = "";
while (choice != "y" && choice != "n")
{
---
Console.WriteLine("Do you want to continue y/n? :");
choice = Console.ReadLine();
---
}
---
if (choice == "n")
{
---
scores[player] = scores[player] + total;
turn_end = true;
---
}
---
}
---
else if (result == 0)
{
---
Console.WriteLine("Oh no, that's a pig out!");
turn_end = true;
---
}
---
else
{
---
Console.WriteLine("Oh no, that's a double pig out, back to zero!");
scores[player] = 0;
turn_end = true;
---
}
---
if (scores[player] > 100)
{
---
game_won = true;
Console.WriteLine($"Player {player + 1} WINS!");
---
}
---
else if (turn_end)
{
---
Console.WriteLine("Press Enter to hand the dice to the next player.");
Console.ReadLine();
player = (player + 1) % players;
new_player = true;
---
}
---
}
---
}
---
public static void Main(string[] args)
{
---
Console.WriteLine("How many players? 1-4:");
int players = Convert.ToInt32(Console.ReadLine());
play_game(players);
}