Skip to content

Instantly share code, notes, and snippets.

@afonsomatos
Created Jul 1, 2016
Embed
What would you like to do?
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;
}
}
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;
}
}
}
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)
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;
}
}
}
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