Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Dice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "Dice.h"

//Upon instansiation we must seed the random number
//generatior.
Dice::Dice()
{
srand (time(NULL));
}
//This returns a random number between 1 and the
//MAX_ROLL_VALUE (which is by default 6).
int Dice::roll()
{
return (rand() % MAX_ROLL_VALUE) + 1;
}

21 changes: 21 additions & 0 deletions Dice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef DICE_H
#define DICE_H

#include <stdlib.h>
#include <time.h>

#define MAX_ROLL_VALUE 6

using namespace std;

//This class creates the random dice roll that will be
//used by all players; this includes human and computer
//players.
class Dice
{
public:
Dice();
int roll();
};

#endif
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CXX = g++
CXXFLAGS = -c -g -std=c++11 -Wall -W -Werror -pedantic

PigsGame : Player.o Dice.o main.o
$(CXX) $(LDFLAGS) -o PigsGame Player.o Dice.o main.o

main.o : main.cpp Dice.h Player.h
$(CXX) $(CXXFLAGS) main.cpp

Dice.o : Dice.cpp Dice.h
$(CXX) $(CXXFLAGS) Dice.cpp

Player.o : Player.cpp Player.h
$(CXX) $(CXXFLAGS) Player.cpp



113 changes: 113 additions & 0 deletions Player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <string>
#include <iostream>
#include "Player.h"

using namespace std;

//Constructor for the Player Class
//requires a name and a boolean that is set to true if the
//player is a cpu.
Player::Player(string new_name, bool new_cpu)
{
total = 0;
current = 0;
cpu = new_cpu;
name = new_name;
}

//This function provides the means to communicate with
//the users by informing them of their current totals, their
//overall totals and the values of their rolls. It also requests
//what actions the players wish to make when it is their turn.
//It also is responsible for controlling the computer . Finnally
//it determines if a certain player has one. The results of
//these actions is reported to the main application which
//monitors the string outputs of this function in order to
//determine if it is the next players turn or if the game is over.
string Player::decision(int amount)
{
string decision;

//Inform the user of their current die roll.
cout << endl;
cout << name << ":" << endl;
cout << "A "<<amount<< " was rolled" << endl;

if(amount == 1) //Determine if the player scored nothing this round.
{
decision = "reset";
update(decision, amount);
cout << "You lost your turn, total = " << total << endl;
}
else if((amount + total + current) >= 100) //Determine if there is a winner.
{
decision = "win";
update(decision, amount);
cout << "WIN with " << total << endl;
}
else if(cpu == false) //If it the current player is not a computer we must ask what they want to do.
{
cout << "Turn total = " << current+amount << endl;
cout << "Game total = " << total+current+amount << endl;
cout << "What would you like to do? [roll, hold] "<< endl;
cin >> decision;

while (decision != "hold" && decision != "roll")
{
cout << "The options are hold or roll, please try again." << endl;
cin >> decision;
}
update(decision, amount);
}
else //This is a computer so we automatically determine what they do.
{
if (amount == 1)
{
decision = "reset";
}
else
{
if (current <= 20)
{
decision = "roll";
}
else
{
decision = "hold";
}
}

//We have to update the current total and game total for this player.
update(decision, amount);

//Inform the user of the results of this round for them.
cout << decision << endl;
cout << "Current total = "<<current << endl;
cout << "Game total = "<<total+current << endl;
}
return decision;
}

//During the process of a player doing his or her turn values
//must be updated such as the current roll total and the total
//a player has scored through a game. The values that are
//updated are based off the decision string, and the current
//roll amount which are the inputs of this function. The output
//of this function tells what the current total for the player is.
void Player::update(std::string decision, int amount)
{
if (decision == "reset")
{
current = 0;
}
else if (decision == "roll")
{
current += amount;
}
else if ((decision == "hold") || (decision == "win"))
{
current += amount;
total += current;
current = 0;
}
}
25 changes: 25 additions & 0 deletions Player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <string>
using namespace std;

#ifndef PLAYER_H
#define PLAYER_H

//The player class handles both humans and computer players. When calling
//the consturctior one must provide a name and if the player is a cpu or not. A
//true input for the bool in the constructor means it is a CPU.
class Player
{
public:
Player(string new_name, bool new_cpu);
string decision(int amount);

private:
void update(string decision, int amount);
string name;
int total;
int current;
bool cpu;
};


#endif
116 changes: 116 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/****************************************************************\
* The Pigs Game *
* Brought to you by: *
* Christian Cleveland *
* Natalie Astorga *
* Crystal Vang *
* Michelle Rodriguez *
* Matthew Daras *
\****************************************************************/

/*The Pigs Game

This is a command-line implementation of the Pigs Game.

The rules of the game as explaned on Wikipedia are:
Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player decides to "hold":
If the player rolls a 1, they score nothing and it becomes the next player's turn. If the player rolls
any other number, it is added to their turn total and the player's turn continues. If a player chooses to
"hold", their turn total is added to their score, and it becomes the next player's turn. The first player to
score 100 or more points wins.
*/

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include "Dice.h"
#include "Player.h"

using namespace std;

//When this command line application is launched it asks the user how many human and Computer
//players wish to play the game. Once thoose numbers are determined it then asks the user to enter
//names for each of the human players. The computer players are automatically given names. Once
//the naming process is complete the game begins. During the naming process a new player class
//is instantiated for each player and the address of that player class is stored into a vector. WIth
//all the players in the vector we are able to cycle through the vector and present each player a
//chance to play the game until a winner is determined. Upon a winner being declared the
//application ends but prior to exit the memory is freed.
int main()
{
//Find out how many human players there are.
int numPlayers;
cout << "How many human players?" << endl;
cin >> numPlayers;

//Find out how many computer players there are.
int numCpu;
cout << "How many cpu players?" << endl;
cin >> numCpu;

vector<Player*> players; // creating vector for players
string playerName;
string cpuName;

//This allows the user to name each of the human players ; once named
//they are added to our players vector.
for(int k = 0; k < numPlayers; k++)
{
cout << "Please enter player " << k << "'s name? " << endl;
cin >> playerName;
players.push_back(new Player(playerName, false));
}

//Each computer player is added to the players vector with a auto
//created name that is based on the current computer player number.
for(int j = 1; j <= numCpu; j++)
{
stringstream ss;
ss << "Cpu" << j;
players.push_back(new Player (ss.str(), true));
}

//Create the random dice that will provide a random roll.
Dice *dice = new Dice;

//default the starting state as a "rolling" action.
string state = "roll";

//Inform the users the game has begun.
cout << "<<<<<<<<<<<<<<<<<<<< Start Game! >>>>>>>>>>>>>>>>>>>>" << endl;

//While there is no winner we cycle though all players allowing each of them to complete their turn
//untill a winner is declared.
for( int i = 0; state != "win"; i = (i+1)%(numPlayers+numCpu))
{
//must always start as a "rolling" state for each player
state = "roll";

//While the current player is rolling we do not move onto the next player.
while( state == "roll")
{
state = players[i]->decision(dice->roll());
}

if (state != "win")
{
cout << endl << "<<<<<<<<<<<<<<< Next Player >>>>>>>>>>>>>>>" << endl;
}
else
{
cout << "<<<<<<<<<<<<<<<<<<<< End Game! >>>>>>>>>>>>>>>>>>>>" << endl;
}
}

//Delete all the created player classes to free memory upon exit.
for(unsigned int i = 0; i < players.size(); ++i)
{
delete players[i];
}

//Delete the dice class to free memory on exit.
delete dice;

return 0;
}