diff --git a/Dice.cpp b/Dice.cpp new file mode 100644 index 0000000..de27f7b --- /dev/null +++ b/Dice.cpp @@ -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; +} + diff --git a/Dice.h b/Dice.h new file mode 100644 index 0000000..094b763 --- /dev/null +++ b/Dice.h @@ -0,0 +1,21 @@ +#ifndef DICE_H +#define DICE_H + +#include +#include + +#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 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b3551aa --- /dev/null +++ b/Makefile @@ -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 + + + diff --git a/Player.cpp b/Player.cpp new file mode 100644 index 0000000..79208f9 --- /dev/null +++ b/Player.cpp @@ -0,0 +1,113 @@ +#include +#include +#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 "<> 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 = "< +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 diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..9085b94 --- /dev/null +++ b/main.cpp @@ -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 +#include +#include +#include +#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 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; +}