From 786b1c3f5a4b5081487b83ff3a1d944bc1a81f55 Mon Sep 17 00:00:00 2001 From: Christian Cleveland Date: Wed, 9 Sep 2015 12:34:13 -0700 Subject: [PATCH 01/16] created the dice class --- dice.cpp | 7 +++++++ dice.h | 14 ++++++++++++++ main.cpp | 6 ++++++ 3 files changed, 27 insertions(+) create mode 100644 dice.cpp create mode 100644 dice.h create mode 100644 main.cpp diff --git a/dice.cpp b/dice.cpp new file mode 100644 index 0000000..1fddee0 --- /dev/null +++ b/dice.cpp @@ -0,0 +1,7 @@ +#include "dice.h" + +int dice::roll() +{ + return 1; +} + diff --git a/dice.h b/dice.h new file mode 100644 index 0000000..69dbc4f --- /dev/null +++ b/dice.h @@ -0,0 +1,14 @@ +#ifndef DICE_H +#define DICE_H + +#define MAX_ROLL_VALUE 6 + +using namespace std; + +class dice +{ + public: + int roll(); +}; + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..7cd89f0 --- /dev/null +++ b/main.cpp @@ -0,0 +1,6 @@ +#include "dice.h" + +int main() +{ + return 0; +} From 42982476b538a783d7d90bc6671196d996af8f08 Mon Sep 17 00:00:00 2001 From: Christian Cleveland Date: Wed, 9 Sep 2015 12:44:03 -0700 Subject: [PATCH 02/16] made the dice roll random --- dice.cpp | 6 +++++- dice.h | 4 ++++ main.cpp | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/dice.cpp b/dice.cpp index 1fddee0..563053e 100644 --- a/dice.cpp +++ b/dice.cpp @@ -1,7 +1,11 @@ #include "dice.h" +dice::dice() +{ + srand (time(NULL)); +} int dice::roll() { - return 1; + return (rand() % MAX_ROLL_VALUE) + 1; } diff --git a/dice.h b/dice.h index 69dbc4f..43134bf 100644 --- a/dice.h +++ b/dice.h @@ -1,6 +1,9 @@ #ifndef DICE_H #define DICE_H +#include +#include + #define MAX_ROLL_VALUE 6 using namespace std; @@ -8,6 +11,7 @@ using namespace std; class dice { public: + dice(); int roll(); }; diff --git a/main.cpp b/main.cpp index 7cd89f0..19609a2 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,14 @@ +#include #include "dice.h" +using namespace std; + int main() { + dice *firstDice = new dice; + + cout << firstDice->roll() << endl; + + delete firstDice; return 0; } From 8f6384d94eb3edb45dde23af28c303684fafdeed Mon Sep 17 00:00:00 2001 From: Crystal Vang Date: Wed, 9 Sep 2015 12:48:43 -0700 Subject: [PATCH 03/16] Created the player class --- Player.cpp | 31 +++++++++++++++++++++++++++++++ Player.h | 20 ++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 Player.cpp create mode 100644 Player.h diff --git a/Player.cpp b/Player.cpp new file mode 100644 index 0000000..26957e0 --- /dev/null +++ b/Player.cpp @@ -0,0 +1,31 @@ +#include +#include "Player.h" + +Player::Player() +{ + total = 0; + current = 0; + cpu = false; + level = 1; +} + +int Player::update(string decision, int amount) +{ + if (decision == "reset") + { + current = 0; + } + else if (decision == "roll") + { + current += amount; + } + else if (decision == "hold") + { + current += amount; + total += current; + current = 0; + } + + return total; +} + diff --git a/Player.h b/Player.h new file mode 100644 index 0000000..cb2a5fe --- /dev/null +++ b/Player.h @@ -0,0 +1,20 @@ +#ifndef PLAYER_H +#define PLAYER_H + +class Player +{ + private: + Player(); + string name; + int total; + int current; + bool cpu; + int level; + + public: + int action(string decision, int amount); + int getScore(return total;); + +}; + +#endif From 8f3d3039c7fc02bcf6e942ccfeb6a9d0d8a55fcf Mon Sep 17 00:00:00 2001 From: SDMatt619 Date: Mon, 14 Sep 2015 11:57:29 -0700 Subject: [PATCH 04/16] Created Makefile --- makefile | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 makefile diff --git a/makefile b/makefile new file mode 100644 index 0000000..70cdabd --- /dev/null +++ b/makefile @@ -0,0 +1,18 @@ +CXX = g++ +CXXFLAGS = -c -g -std=c++11 -Wall -W -Werror -pedantic +LDFLAGS = + +PigGame : player.o dice.o main.o + $(CXX) $(LDFLAGS) -o PigGame 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 + + + From 6c4c5a06e7d4e64f5540a2dcef8d9b643508fedc Mon Sep 17 00:00:00 2001 From: SDMatt619 Date: Tue, 15 Sep 2015 10:46:45 -0700 Subject: [PATCH 05/16] Fixed Errors and Added Computer AI Computer.cpp and Computer.h added which controls the AI. Started work on main. Fixed errors in player.cpp and player.h. --- Computer.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Computer.h | 20 +++++++++++++++++ Player.cpp | 9 +++++++- Player.h | 7 +++--- main.cpp | 48 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 Computer.cpp create mode 100644 Computer.h diff --git a/Computer.cpp b/Computer.cpp new file mode 100644 index 0000000..c8ff386 --- /dev/null +++ b/Computer.cpp @@ -0,0 +1,63 @@ +#include +#include +#include "Computer.h" + +//Constructor to inialize values. +Computer::Computer() +{ + total = 0; + current = 0; +} + +//Returns true of false if the computer should continue rolling. +bool Computer::continueRolling(int currentTotal) +{ + //Used 20 as a stop point due to the fact after 20 points + //the probability of scoring more is signficantly reduced. + if (currentTotal > 20) + return false; + else + return true; +} + +//Returns if the computer holds on a roll or not or if it is a reset. +//Depending on the choice the relevant total and current values are adjusted. +//It also checks if the computer hits 100; +std::string Computer::decision(int rollAmount) +{ + if (rollAmount == 0) + { + current = 0; + return "reset"; + } + else if((rollAmount + total) >= 100) + { + return "win"; + } + else + { + if (continueRolling(current)) + { + current += rollAmount; + return "roll"; + } + else + { + total += current; + current = 0; + return "hold"; + } + } +} + +//Returns the current roll amount. +int Computer::getCurrent() +{ + return current; +} + +//Returns the total the computer has. +int Computer::getTotal() +{ + return total; +} \ No newline at end of file diff --git a/Computer.h b/Computer.h new file mode 100644 index 0000000..362eea1 --- /dev/null +++ b/Computer.h @@ -0,0 +1,20 @@ +#include +#ifndef COMPUTER_H +#define COMPUTER_H + + +class Computer +{ + private: + int total; + int current; + bool continueRolling(int currentTotal); + + public: + Computer(); + std::string decision(int rollAmount); + int getCurrent(); + int getTotal(); +}; + +#endif \ No newline at end of file diff --git a/Player.cpp b/Player.cpp index 26957e0..87b8727 100644 --- a/Player.cpp +++ b/Player.cpp @@ -1,3 +1,4 @@ +#include #include #include "Player.h" @@ -9,7 +10,7 @@ Player::Player() level = 1; } -int Player::update(string decision, int amount) +int Player::update(std::string decision, int amount) { if (decision == "reset") { @@ -29,3 +30,9 @@ int Player::update(string decision, int amount) return total; } +//Not Sure what you want returned +int Player::getScore() +{ + return current; + //return total; +} \ No newline at end of file diff --git a/Player.h b/Player.h index cb2a5fe..17e70e1 100644 --- a/Player.h +++ b/Player.h @@ -1,3 +1,4 @@ +#include #ifndef PLAYER_H #define PLAYER_H @@ -5,15 +6,15 @@ class Player { private: Player(); - string name; + std::string name; int total; int current; bool cpu; int level; public: - int action(string decision, int amount); - int getScore(return total;); + int update(std::string decision, int amount); + int getScore(); }; diff --git a/main.cpp b/main.cpp index 19609a2..1b0dc5d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,14 +1,60 @@ #include #include "dice.h" +#include "Computer.h" using namespace std; int main() { dice *firstDice = new dice; + Computer *comp = new Computer; + int coinFlip, temp, currentPlayer; + bool winner = false; + + + cout << "It is time to play Pigs Game!\n"; + cout << "Choose Heads(1) or Tails(2) to see who goes first\n"; + cin >> coinFlip; + + temp = firstDice->roll(); + + if(temp > 3 && coinFlip == 2) + { + cout << "You won the coin toss!\n"; + currentPlayer = 1; + } + else if (temp < 4 && coinFlip == 1) + { + cout << "You won the coin toss!\n"; + currentPlayer = 1; + } + else + { + cout << "The computer won the coin toss!\n"; + currentPlayer = 0; + } + + while (!winner) + { + if (currentPlayer == 0) + { + + } + else + { + + } + + } + - cout << firstDice->roll() << endl; + //cout << firstDice->roll() << endl; + + + //Cleanup + delete comp; delete firstDice; + return 0; } From 3647b8d07cd1b5b449afaf67c5228d805c9d58cf Mon Sep 17 00:00:00 2001 From: nastorga24 Date: Tue, 15 Sep 2015 11:49:22 -0700 Subject: [PATCH 06/16] added computer type player --- Player.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++------ Player.h | 1 - main.cpp | 4 +--- makefile | 5 ++--- 4 files changed, 58 insertions(+), 14 deletions(-) diff --git a/Player.cpp b/Player.cpp index 87b8727..26953a3 100644 --- a/Player.cpp +++ b/Player.cpp @@ -2,12 +2,60 @@ #include #include "Player.h" -Player::Player() +Player::Player(string new_name, bool new_cpu) { - total = 0; + total = 0; current = 0; - cpu = false; - level = 1; + cpu = new_cpu; + name = new_name; +} + +int Player::decision(int amount) +{ + string decision; + + cout << endl; + cout << name << ":" << endl; + cout << "A "<= 100) + { + decision = "win"; + update(decision, amount); + cout << "WIN with " << total << endl; + } + else if(cpu == false) + { + cout << "Current total = "<> decision; + update(decision, amount); + } + else + { + if (amount == 1) + { + decision = "reset"; + } + else + { + if (current <= 20) + { + decision = "roll"; + } + else + { + decision = "hold"; + } + } + + update(decision, amount); + + cout << decision << endl; + cout << "Current total = "< Date: Tue, 15 Sep 2015 12:12:05 -0700 Subject: [PATCH 07/16] Implemented the main function --- main.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index 19609a2..ad4529c 100644 --- a/main.cpp +++ b/main.cpp @@ -1,14 +1,65 @@ #include -#include "dice.h" +#include +#include +#include "Dice.h" +#include "Player.h" using namespace std; + + int main() -{ - dice *firstDice = new dice; - - cout << firstDice->roll() << endl; +{ + int numPlayers; + cout << " How many human players? " << endl; + cin >> numPlayers; + + int numCpu; + cout << " How many cpu players? " << endl; + cin >> numCpu; + + vector players; // creating vector for players + + string player_name; + string temp; + + for(int k=0; k> player_name; + Player player(player_name, false); + players.push_back(player); + } + + for(int j=0; j Date: Tue, 15 Sep 2015 12:24:16 -0700 Subject: [PATCH 08/16] Improve spacing of main --- main.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/main.cpp b/main.cpp index ad4529c..e330ab1 100644 --- a/main.cpp +++ b/main.cpp @@ -7,7 +7,6 @@ using namespace std; - int main() { int numPlayers; From 8ae13c896384620efa320ab3b89abae62b82ebc6 Mon Sep 17 00:00:00 2001 From: nastorga24 Date: Tue, 15 Sep 2015 12:27:51 -0700 Subject: [PATCH 09/16] fixed issues with the Player --- Player.cpp | 5 ++++- Player.h | 18 +++++++++++------- main.cpp | 48 +++++++++++++++--------------------------------- makefile | 8 ++++---- 4 files changed, 34 insertions(+), 45 deletions(-) diff --git a/Player.cpp b/Player.cpp index 26953a3..3be6f91 100644 --- a/Player.cpp +++ b/Player.cpp @@ -2,6 +2,8 @@ #include #include "Player.h" +using namespace std; + Player::Player(string new_name, bool new_cpu) { total = 0; @@ -10,7 +12,7 @@ Player::Player(string new_name, bool new_cpu) name = new_name; } -int Player::decision(int amount) +string Player::decision(int amount) { string decision; @@ -56,6 +58,7 @@ int Player::decision(int amount) cout << "Current total = "<> coinFlip; - temp = firstDice->roll(); + Player *player1 = new Player("Natalie", false); + Player *player2 = new Player("Chris", false); - if(temp > 3 && coinFlip == 2) - { - cout << "You won the coin toss!\n"; - currentPlayer = 1; - } - else if (temp < 4 && coinFlip == 1) - { - cout << "You won the coin toss!\n"; - currentPlayer = 1; - } - else - { - cout << "The computer won the coin toss!\n"; - currentPlayer = 0; - } + Player *array[2]; + array[0] = player1; + array[1] = player2; + + string state = "roll"; - while (!winner) + for(int i = 0; state != "win"; i = (i+1)%2) { - if (currentPlayer == 0) - { - - } - else - { - - } - + state = "roll"; + while(state == "roll") + { + state = array[i]->decision(firstDice->roll()); + } } - - //cout << firstDice->roll() << endl; //Cleanup delete firstDice; + delete player1; + delete player2; return 0; } diff --git a/makefile b/makefile index ebab40e..7a7a1b0 100644 --- a/makefile +++ b/makefile @@ -1,8 +1,8 @@ CXX = g++ CXXFLAGS = -c -g -std=c++11 -Wall -W -Werror -pedantic -PigGame : player.o dice.o main.o - $(CXX) $(LDFLAGS) -o PigGame player.o dice.o main.o +PigGame : Player.o dice.o main.o + $(CXX) $(LDFLAGS) -o PigGame Player.o dice.o main.o main.o : main.cpp dice.h Player.h $(CXX) $(CXXFLAGS) main.cpp @@ -10,8 +10,8 @@ main.o : main.cpp dice.h Player.h dice.o : dice.cpp dice.h $(CXX) $(CXXFLAGS) dice.cpp -player.o : Player.cpp Player.h - $(CXX) $(CXXFLAGS) player.cpp +Player.o : Player.cpp Player.h + $(CXX) $(CXXFLAGS) Player.cpp From 98d9c0ca07efa1dd3389a4b0aa67aca18b81d0b8 Mon Sep 17 00:00:00 2001 From: Christian Cleveland Date: Tue, 15 Sep 2015 14:15:33 -0700 Subject: [PATCH 10/16] fixed the seg fault --- Player.cpp | 8 +++++++- main.cpp | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Player.cpp b/Player.cpp index 7e9a7ed..bb58fb4 100644 --- a/Player.cpp +++ b/Player.cpp @@ -28,7 +28,7 @@ string Player::decision(int amount) } else if(amount == 1) { - decision = "win"; + decision = "reset"; update(decision, amount); cout << "You lost your turn, total = " << total << endl; } @@ -83,6 +83,12 @@ int Player::update(std::string decision, int amount) total += current; current = 0; } + else if (decision == "win") + { + current += amount; + total += current; + current = 0; + } return total; } diff --git a/main.cpp b/main.cpp index faadef0..71cdcd3 100644 --- a/main.cpp +++ b/main.cpp @@ -40,16 +40,16 @@ int main() string state = "roll"; - for( int i = 0; state != "win"; i = (i+1)%(numPlayers+numCpu+1)) + cout << "<<<<<<<<<<<<<<<<<<<< Start Game! >>>>>>>>>>>>>>>>>>>>" << endl; + for( int i = 0; state != "win"; i = (i+1)%(numPlayers+numCpu)) { state = "roll"; while( state == "roll") { state = players[i]->decision(diceRoll->roll()); - } - + cout << endl << "<<<<<<<<<<<<<<< Next Player >>>>>>>>>>>>>>>" << endl; } for(unsigned int i = 0; i < players.size(); ++i) From 8a22fead4eafcd68c07b168b8bddee07236f0561 Mon Sep 17 00:00:00 2001 From: SDMatt619 Date: Tue, 15 Sep 2015 15:25:52 -0700 Subject: [PATCH 11/16] Updated Comments Updated comments for all the class and header files along with commenting the main. --- Computer.h | 2 +- Dice.cpp | 4 ++ Dice.h | 3 ++ Player.cpp | 31 +++++++++-- Player.h | 4 +- main.cpp | 147 ++++++++++++++++++++++++++++++++++------------------- 6 files changed, 132 insertions(+), 59 deletions(-) diff --git a/Computer.h b/Computer.h index 362eea1..83a985c 100644 --- a/Computer.h +++ b/Computer.h @@ -2,7 +2,7 @@ #ifndef COMPUTER_H #define COMPUTER_H - +//This class controls computer players decision making process. class Computer { private: diff --git a/Dice.cpp b/Dice.cpp index 7930dec..de27f7b 100644 --- a/Dice.cpp +++ b/Dice.cpp @@ -1,9 +1,13 @@ #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 index 0af432e..094b763 100644 --- a/Dice.h +++ b/Dice.h @@ -8,6 +8,9 @@ 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: diff --git a/Player.cpp b/Player.cpp index bb58fb4..8712ef9 100644 --- a/Player.cpp +++ b/Player.cpp @@ -4,6 +4,9 @@ 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; @@ -12,27 +15,37 @@ Player::Player(string new_name, bool 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 "<= 100) + if((amount + total + current) >= 100) //Determine if there is a winner. { decision = "win"; update(decision, amount); cout << "WIN with " << total << endl; } - else if(amount == 1) + else 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(cpu == false) + else if(cpu == false) //If it the current player is not a computer we must ask what they want to do. { cout << "Current total = "< #include #include @@ -6,58 +28,79 @@ 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() { - int numPlayers; - cout << " How many human players? " << endl; - cin >> numPlayers; - - int numCpu; - cout << " How many cpu players? " << endl; - cin >> numCpu; - - vector players; // creating vector for players - - string player_name; - string temp; - - for(int k=0; k> player_name; - players.push_back(new Player(player_name, false)); - } - - for(int j=0; j>>>>>>>>>>>>>>>>>>>" << endl; - for( int i = 0; state != "win"; i = (i+1)%(numPlayers+numCpu)) - { - state = "roll"; - - while( state == "roll") - { - state = players[i]->decision(diceRoll->roll()); - } - cout << endl << "<<<<<<<<<<<<<<< Next Player >>>>>>>>>>>>>>>" << endl; - } - - for(unsigned int i = 0; i < players.size(); ++i) - { - delete players[i]; - } - - delete diceRoll; - - return 0; + //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 player_name; + string temp; + + //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> player_name; + players.push_back(new Player(player_name, 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=0; j>>>>>>>>>>>>>>>>>>>" << 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(diceRoll->roll()); + } + cout << endl << "<<<<<<<<<<<<<<< Next Player >>>>>>>>>>>>>>>" << 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 diceRoll; + + return 0; } From 1c69d9ebf94fbd17b983d972c8c302448d23288a Mon Sep 17 00:00:00 2001 From: SDMatt619 Date: Tue, 15 Sep 2015 16:10:52 -0700 Subject: [PATCH 12/16] Fixed Incorrect Win Condition It was possible for someone to have a score of 99 and then roll 1 and win. Changed the order of the if-else block to account for this. --- Player.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Player.cpp b/Player.cpp index 8712ef9..d9238e0 100644 --- a/Player.cpp +++ b/Player.cpp @@ -33,17 +33,17 @@ string Player::decision(int amount) cout << name << ":" << endl; cout << "A "<= 100) //Determine if there is a winner. + if(amount == 1) //Determine if the player scored nothing this round. { - decision = "win"; + decision = "reset"; update(decision, amount); - cout << "WIN with " << total << endl; + cout << "You lost your turn, total = " << total << endl; } - else if(amount == 1) //Determine if the player scored nothing this round. + else if((amount + total + current) >= 100) //Determine if there is a winner. { - decision = "reset"; + decision = "win"; update(decision, amount); - cout << "You lost your turn, total = " << total << endl; + 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. { From d9098964a05307ceecc9eb140813617047aa99b5 Mon Sep 17 00:00:00 2001 From: Crystal Vang Date: Tue, 15 Sep 2015 16:27:01 -0700 Subject: [PATCH 13/16] Updated variable names and removed Computer class --- Computer.cpp | 63 ---------------- Computer.h | 20 ------ makefile => Makefile | 4 +- main.cpp | 168 +++++++++++++++++++++---------------------- 4 files changed, 86 insertions(+), 169 deletions(-) delete mode 100644 Computer.cpp delete mode 100644 Computer.h rename makefile => Makefile (74%) diff --git a/Computer.cpp b/Computer.cpp deleted file mode 100644 index c8ff386..0000000 --- a/Computer.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include -#include -#include "Computer.h" - -//Constructor to inialize values. -Computer::Computer() -{ - total = 0; - current = 0; -} - -//Returns true of false if the computer should continue rolling. -bool Computer::continueRolling(int currentTotal) -{ - //Used 20 as a stop point due to the fact after 20 points - //the probability of scoring more is signficantly reduced. - if (currentTotal > 20) - return false; - else - return true; -} - -//Returns if the computer holds on a roll or not or if it is a reset. -//Depending on the choice the relevant total and current values are adjusted. -//It also checks if the computer hits 100; -std::string Computer::decision(int rollAmount) -{ - if (rollAmount == 0) - { - current = 0; - return "reset"; - } - else if((rollAmount + total) >= 100) - { - return "win"; - } - else - { - if (continueRolling(current)) - { - current += rollAmount; - return "roll"; - } - else - { - total += current; - current = 0; - return "hold"; - } - } -} - -//Returns the current roll amount. -int Computer::getCurrent() -{ - return current; -} - -//Returns the total the computer has. -int Computer::getTotal() -{ - return total; -} \ No newline at end of file diff --git a/Computer.h b/Computer.h deleted file mode 100644 index 83a985c..0000000 --- a/Computer.h +++ /dev/null @@ -1,20 +0,0 @@ -#include -#ifndef COMPUTER_H -#define COMPUTER_H - -//This class controls computer players decision making process. -class Computer -{ - private: - int total; - int current; - bool continueRolling(int currentTotal); - - public: - Computer(); - std::string decision(int rollAmount); - int getCurrent(); - int getTotal(); -}; - -#endif \ No newline at end of file diff --git a/makefile b/Makefile similarity index 74% rename from makefile rename to Makefile index 7898cb6..b3551aa 100644 --- a/makefile +++ b/Makefile @@ -1,8 +1,8 @@ CXX = g++ CXXFLAGS = -c -g -std=c++11 -Wall -W -Werror -pedantic -PigGame : Player.o Dice.o main.o - $(CXX) $(LDFLAGS) -o PigGame Player.o Dice.o main.o +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 diff --git a/main.cpp b/main.cpp index 17224f1..8f76f1e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,23 +1,23 @@ /****************************************************************\ -* The Pig Game * -* Brought to you by: * -* Christian Cleveland * -* Natalie Astorga * -* Crystal Vang * -* Michelle Rodriguez * -* Matthew Daras * -\****************************************************************/ - -/*The Pig Game - -This is a command-line implementation of the Pig 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. + * 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 @@ -38,69 +38,69 @@ using namespace std; //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 player_name; - string temp; - - //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> player_name; - players.push_back(new Player(player_name, 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=0; j>>>>>>>>>>>>>>>>>>>" << 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(diceRoll->roll()); - } - cout << endl << "<<<<<<<<<<<<<<< Next Player >>>>>>>>>>>>>>>" << 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 diceRoll; - - return 0; + //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 = 0; j < numCpu; j++) + { + cpuName = ("Cpu" + j); + players.push_back(new Player (cpuName, 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()); + } + cout << endl << "<<<<<<<<<<<<<<< Next Player >>>>>>>>>>>>>>>" << 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; } From 487d6d7309d6144d027a21570004ec8df1186b6e Mon Sep 17 00:00:00 2001 From: Christian Cleveland Date: Tue, 15 Sep 2015 16:45:55 -0700 Subject: [PATCH 14/16] Added the ability to change decision if spelled wrong --- Player.cpp | 21 ++++++++++----------- Player.h | 1 - main.cpp | 10 +++++++++- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Player.cpp b/Player.cpp index d9238e0..5650059 100644 --- a/Player.cpp +++ b/Player.cpp @@ -47,10 +47,16 @@ string Player::decision(int amount) } else if(cpu == false) //If it the current player is not a computer we must ask what they want to do. { - cout << "Current total = "<> decision; + + while (decision != "hold" && decision != "roll") + { + cout << "The options are hold are roll, please try again." << endl; + cin >> decision; + } update(decision, amount); } else //This is a computer so we automatically determine what they do. @@ -98,7 +104,7 @@ int Player::update(std::string decision, int amount) { current += amount; } - else if (decision == "hold") + else if ((decision == "hold") || (decision == "win")) { current += amount; total += current; @@ -113,10 +119,3 @@ int Player::update(std::string decision, int amount) return total; } - -//Returns the current total of the current player. -int Player::getScore() -{ - return current; - //return total; -} diff --git a/Player.h b/Player.h index 9b6e8e1..60918a6 100644 --- a/Player.h +++ b/Player.h @@ -12,7 +12,6 @@ class Player public: Player(string new_name, bool new_cpu); string decision(int amount); - int getScore(); private: int update(string decision, int amount); diff --git a/main.cpp b/main.cpp index 17224f1..f957e0d 100644 --- a/main.cpp +++ b/main.cpp @@ -90,7 +90,15 @@ int main() { state = players[i]->decision(diceRoll->roll()); } - cout << endl << "<<<<<<<<<<<<<<< Next Player >>>>>>>>>>>>>>>" << endl; + + if (state != "win") + { + cout << endl << "<<<<<<<<<<<<<<< Next Player >>>>>>>>>>>>>>>" << endl; + } + else + { + cout << "<<<<<<<<<<<<<<<<<<<< End Game! >>>>>>>>>>>>>>>>>>>>" << endl; + } } //Delete all the created player classes to free memory upon exit. From 6eeefb5c545e0f192d09c2caa8ad085f6fad0eb5 Mon Sep 17 00:00:00 2001 From: Christian Cleveland Date: Tue, 15 Sep 2015 17:29:03 -0700 Subject: [PATCH 15/16] Correctly converted the cpu int to a string --- Player.cpp | 10 +--------- Player.h | 2 +- main.cpp | 8 +++++--- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/Player.cpp b/Player.cpp index 5650059..1cde6b2 100644 --- a/Player.cpp +++ b/Player.cpp @@ -94,7 +94,7 @@ string Player::decision(int amount) //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. -int Player::update(std::string decision, int amount) +void Player::update(std::string decision, int amount) { if (decision == "reset") { @@ -110,12 +110,4 @@ int Player::update(std::string decision, int amount) total += current; current = 0; } - else if (decision == "win") - { - current += amount; - total += current; - current = 0; - } - - return total; } diff --git a/Player.h b/Player.h index 60918a6..e3aa4d9 100644 --- a/Player.h +++ b/Player.h @@ -14,7 +14,7 @@ class Player string decision(int amount); private: - int update(string decision, int amount); + void update(string decision, int amount); string name; int total; int current; diff --git a/main.cpp b/main.cpp index a6312fa..9085b94 100644 --- a/main.cpp +++ b/main.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include "Dice.h" #include "Player.h" @@ -63,10 +64,11 @@ int main() //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 = 0; j < numCpu; j++) + for(int j = 1; j <= numCpu; j++) { - cpuName = ("Cpu" + j); - players.push_back(new Player (cpuName, true)); + stringstream ss; + ss << "Cpu" << j; + players.push_back(new Player (ss.str(), true)); } //Create the random dice that will provide a random roll. From 491470bafbd51faecfa3e1c49581eff0319482b1 Mon Sep 17 00:00:00 2001 From: Crystal Vang Date: Tue, 15 Sep 2015 18:51:29 -0700 Subject: [PATCH 16/16] Updated the statement when roll or hold is not entered --- Player.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Player.cpp b/Player.cpp index 1cde6b2..79208f9 100644 --- a/Player.cpp +++ b/Player.cpp @@ -54,7 +54,7 @@ string Player::decision(int amount) while (decision != "hold" && decision != "roll") { - cout << "The options are hold are roll, please try again." << endl; + cout << "The options are hold or roll, please try again." << endl; cin >> decision; } update(decision, amount);