Created
October 17, 2019 19:31
-
-
Save peterthorsteinson/c962d2935334cc5b7e19a14a83cdc025 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Pig Dice"); | |
Game game = new Game(); | |
game.Play(); | |
} | |
} | |
class Game | |
{ | |
public void Play() | |
{ | |
Player sally = new Player("Sally"); | |
Player james = new Player("James"); | |
while (true) | |
{ | |
sally.TakeTurn(); | |
Console.WriteLine(sally.name + " score: " + sally.score + "\n"); | |
james.TakeTurn(); | |
Console.WriteLine(james.name + " score: " + james.score + "\n"); | |
} | |
} | |
} | |
class Player | |
{ | |
public string name; | |
public int score; | |
Random random = new Random(); | |
public Player(string name) | |
{ | |
this.name = name; | |
} | |
public void TakeTurn() | |
{ | |
int scoreForTurn = 0; | |
while (true) | |
{ | |
Console.Write(name + ": Enter h for hold or r for role: "); | |
ConsoleKeyInfo cki = Console.ReadKey(); | |
Console.WriteLine(); | |
if (cki.Key != ConsoleKey.H && cki.Key != ConsoleKey.R) Console.Beep(); // Error: must enter h or r | |
if (cki.Key == ConsoleKey.H) break; | |
if (cki.Key == ConsoleKey.R) | |
{ | |
int scoreForThrow = ThrowDice(); | |
if (scoreForThrow == 0) { scoreForTurn = 0; break; } // loose all wins this turn | |
scoreForTurn += scoreForThrow; | |
} | |
} | |
score += scoreForTurn; | |
} | |
public int ThrowDice() | |
{ | |
int die1 = random.Next(1, 7); | |
int die2 = random.Next(1, 7); | |
if (die1 == 1 || die2 == 1) | |
{ | |
Console.WriteLine("ThrowDice -> " + die1 + ", " + die2 + " -> throw score: 0"); | |
return 0; | |
} | |
Console.WriteLine("ThrowDice -> " + die1 + ", " + die2 + " -> throw score: " + (die1 + die2)); | |
return die1 + die2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment