Task: Pig the dice game


C++: #include <windows.h>
#include <iostream>
#include <string>


using namespace std;


const int PLAYERS = 2, MAX_POINTS = 100;


class player
{
public:
    player() { reset(); }
    void reset()
    {
	name = "";
	current_score = round_score = 0;
    }
    string getName()             { return name; }
    void setName( string n )     { name = n; }
    int getCurrScore()           { return current_score; }
    void addCurrScore()          { current_score += round_score; }
    int getRoundScore()          { return round_score; }
    void addRoundScore( int rs ) { round_score += rs; }
    void zeroRoundScore()        { round_score = 0; }

private:
    string name;
    int current_score, round_score;
};

class pigGame
{
public:
    pigGame() { resetPlayers(); }

    void play()
    {
	while( true )
	{
	    system( "cls" );
	    int p = 0;
	    while( true )
	    {
		if( turn( p ) )
		{
		    praise( p );
		    break;
		}

		++p %= PLAYERS;
	    }

	    string r;
	    cout << "Do you want to play again ( y / n )? "; cin >> r;
	    if( r != "Y" && r != "y" ) return;
	    resetPlayers();
	}
    }

private:
    void resetPlayers()
    {
	system( "cls" );
	string n;
	for( int p = 0; p < PLAYERS; p++ )
	{
	    _players[p].reset();
	    cout << "Enter name player " << p + 1 << ": "; cin >> n;
	    _players[p].setName( n );
	}

    }

    void praise( int p )
    {
	system( "cls" );
	cout << "CONGRATULATIONS " << _players[p].getName() << ", you are the winner!" << endl << endl;
	cout << "Final Score" << endl;
	drawScoreboard();
	cout << endl << endl;
    }

    void drawScoreboard()
    {
	for( int p = 0; p < PLAYERS; p++ )
	    cout << _players[p].getName() << ": " << _players[p].getCurrScore() << " points" << endl;
	cout << endl;
    }

    bool turn( int p )
    {
	system( "cls" );
	drawScoreboard();
	_players[p].zeroRoundScore();
	string r;
	int die;
	while( true )
	{
	    cout << _players[p].getName() << ", your round score is: " << _players[p].getRoundScore() << endl;
	    cout << "What do you want to do (H)old or (R)oll? "; cin >> r;
	    if( r == "h" || r == "H" )
	    {
		_players[p].addCurrScore();
		return _players[p].getCurrScore() >= MAX_POINTS;
	    }
	    if( r == "r" || r == "R" )
	    {
		die = rand() % 6 + 1;
		if( die == 1 )
		{
	    	    cout << _players[p].getName() << ", your turn is over." << endl << endl;
		    system( "pause" );
		    return false;
		}
		_players[p].addRoundScore( die );
	    }
	    cout << endl;
	}
	return false;
    }

    player	_players[PLAYERS];
};

int main( int argc, char* argv[] )
{
    srand( GetTickCount() );
    pigGame pg;
    pg.play();
    return 0;
}



C#: using System;
using System.IO;

namespace Pig {

	class Roll {
		public int TotalScore{get;set;}
		public int RollScore{get;set;}
		public bool Continue{get;set;}
	}

	class Player {
		public String Name{get;set;}
		public int Score {get;set;}
		Random rand;

		public Player() {
			Score = 0;
			rand = new Random();
		}

		public Roll Roll(int LastScore){
			Roll roll = new Roll();
			roll.RollScore = rand.Next(6) + 1;

			if(roll.RollScore == 1){
				roll.TotalScore = 0;
				roll.Continue = false;
				return roll;
			}

			roll.TotalScore = LastScore + roll.RollScore;
			roll.Continue = true;
			return roll;
		}

		public void FinalizeTurn(Roll roll){
			Score = Score + roll.TotalScore;
		}
	}

	public class Game {		
		public static void Main(String[] argv){
			String input = null;
			Player[] players = new Player[2];

			
			while(true){								
				Console.Write("Greetings! Would you like to play a game (y/n)?");
				while(input == null){
					input = Console.ReadLine();
					if(input.ToLowerInvariant() == "y"){
						players[0] = new Player();
						players[1] = new Player();
						Console.Write("Player One, what's your name?");
						input = Console.ReadLine();
						players[0].Name = input;
						Console.Write("Player Two, what's your name?");
						input = Console.ReadLine();
						players[1].Name = input;
						Console.WriteLine(players[0].Name + " and " + players[1].Name + ", prepare to do battle!");
					} else if (input.ToLowerInvariant() == "n"){
						goto Goodbye; 
					} else {
						input = null;
						Console.Write("I'm sorry, I don't understand. Play a game (y/n)?");
					}
				}

				
				int currentPlayer = 0;
				Roll roll = null;
				bool runTurn = true;
				while(runTurn){
					Player p = players[currentPlayer];
					roll = p.Roll( (roll !=null) ? roll.TotalScore : 0 );
					if(roll.Continue){
						if(roll.TotalScore + p.Score > 99){
							Console.WriteLine("Congratulations, " + p.Name + "! You rolled a " + roll.RollScore + " for a final score of " + (roll.TotalScore + p.Score) + "!");
							runTurn = false;
						} else {
							Console.Write(p.Name + ": Roll " + roll.RollScore + "/Turn " + roll.TotalScore + "/Total " + (roll.TotalScore + p.Score) + ". Roll again (y/n)?");
							input = Console.ReadLine();
							if(input.ToLowerInvariant() == "y"){
								
							} else if (input.ToLowerInvariant() == "n"){
								p.FinalizeTurn(roll);
								currentPlayer = Math.Abs(currentPlayer - 1);			
								Console.WriteLine();
							Console.WriteLine(players[0].Name + ": " + players[0].Score + "    " + players[1].Name + ": " + players[1].Score);					
								Console.WriteLine(players[currentPlayer].Name + ", your turn begins.");
								roll = null;
							} else {
								input = null;
								Console.Write("I'm sorry, I don't understand. Play a game (y/n)?");
							}
						}
					} else {
						Console.WriteLine(p.Name + @", you rolled a 1 and lost your points for this turn.
Your current score:	" + p.Score);
						Console.WriteLine();
						Console.WriteLine(players[0].Name + ": " + players[0].Score + "    " + players[1].Name + ": " + players[1].Score);
						currentPlayer = Math.Abs(currentPlayer - 1);
					}
				}


				input = null;
			}
			Goodbye:
			Console.WriteLine("Thanks for playing, and remember: the house ALWAYS wins!");
			System.Environment.Exit(0);
		}
	}
}