Created
July 1, 2016 22:42
-
-
Save afonsomatos/a4617a5ec80e813c4fa4cb23a6815115 to your computer and use it in GitHub Desktop.
This file contains 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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace PigDice | |
{ | |
class Dice | |
{ | |
private Random random = new Random(); | |
public int Roll() => random.Next(6) + 1; | |
} | |
} |
This file contains 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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace PigDice | |
{ | |
class Game | |
{ | |
public Player[] Players { get; } | |
public int Turn { get; private set; } | |
public Player PlayingPlayer { get; set; } | |
public Game(int numberOfPlayers) | |
{ | |
Players = new Player[numberOfPlayers]; | |
} | |
public void NextTurn() | |
{ | |
Turn += 1; | |
PlayingPlayer = Players[Turn % Players.Length]; | |
} | |
public string GetScoresTable() { | |
string table = "----- Scores -----"; | |
foreach (Player player in Players) | |
table += "\n" + player + ": " + player.OverallScore; | |
return table; | |
} | |
} | |
} |
This file contains 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
How to play PigDice | |
1. Each players has 2 dices. | |
2. Each player rolls the 2 dices and sums up the sum of both to their score. | |
3. However, if one dice gets 1, overall score is set to 0 and it's the next player's turn. | |
4. The player can choose to play or hold (hold = give the turn to the next player) |
This file contains 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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace PigDice | |
{ | |
class Player | |
{ | |
const int pointsToWin = 50; // instead of 100 which is | |
// incredibly hard to get | |
public string PlayerName { get; } | |
public int OverallScore { get; set; } | |
public bool hasWon() => this.OverallScore >= pointsToWin; | |
public Player(string playerName) | |
{ | |
this.PlayerName = playerName; | |
} | |
public override string ToString() | |
{ | |
return this.PlayerName; | |
} | |
} | |
} |
This file contains 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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace PigDice | |
{ | |
class Program | |
{ | |
static void CreatePlayers(Player[] players) | |
{ | |
for (int index = 0; index < players.Length; ++index) | |
{ | |
string defaultName = $"Player #{index + 1}"; | |
Console.Write($"{defaultName}'s name: "); | |
string playerName = Console.ReadLine(); | |
players[index] = new Player(playerName == string.Empty ? defaultName : playerName); | |
} | |
} | |
static void ShowGameStatus(Game game) => Console.WriteLine(game.GetScoresTable()); | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Welcome to PigDice!"); | |
Console.Write("Amount of players: "); | |
// Number of players that are going to play | |
string inputNumber = Console.ReadLine(); | |
int numberOfPlayers = Convert.ToInt32(inputNumber); | |
Game game = new Game(numberOfPlayers); | |
Player[] players = game.Players; | |
// Ask for and assign players' names | |
CreatePlayers(players); | |
Console.WriteLine("All set. Let the games begin!"); | |
// First player starts | |
game.PlayingPlayer = players[0]; | |
// GameLoop returns false if ended | |
while (GameLoop(game)) ; | |
Console.WriteLine(game.PlayingPlayer + " has won!"); | |
Console.ReadKey(); | |
} | |
private static bool GameLoop(Game game) | |
{ | |
Player player = game.PlayingPlayer; | |
ShowGameStatus(game); | |
if (player.hasWon()) | |
return false; | |
Console.Write($"{player}'s turn! Play (no)? "); | |
string playersChoice = Console.ReadLine(); | |
// Handle player's choice to hold | |
if (playersChoice.ToLower() == "no") | |
{ | |
Console.WriteLine($"{player} chose to hold!"); | |
game.NextTurn(); | |
return true; | |
} | |
// Handle player's choice to play | |
Dice dice = new Dice(); | |
Console.WriteLine($"{player} is rolling the dice!"); | |
int number1 = dice.Roll(); | |
int number2 = dice.Roll(); | |
Console.WriteLine($"{player} got {number1} and {number2}"); | |
// If one dice got 1, all score is lost | |
if (number1 == 1 || number2 == 1) | |
{ | |
Console.WriteLine($"Bad luck! {player} lost all points!"); | |
player.OverallScore = 0; | |
game.NextTurn(); | |
return true; | |
} | |
// Player got a score increase | |
int scoreWon = number1 + number2; | |
player.OverallScore += scoreWon; | |
Console.WriteLine($"{player} got {scoreWon} score and now has {player.OverallScore}"); | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment