From a6796915dc5767e0ab5eca88c61cfea4bd37b414 Mon Sep 17 00:00:00 2001 From: Tyler Gering Date: Wed, 9 Sep 2015 12:30:31 -0700 Subject: [PATCH 1/6] adding files --- dice.cpp | 4 ++++ dice.h | 12 ++++++++++++ pigGame.cpp | 8 ++++++++ player.cpp | 20 ++++++++++++++++++++ player.h | 12 ++++++++++++ 5 files changed, 56 insertions(+) create mode 100644 dice.cpp create mode 100644 dice.h create mode 100644 pigGame.cpp create mode 100644 player.cpp create mode 100644 player.h diff --git a/dice.cpp b/dice.cpp new file mode 100644 index 0000000..1988fbc --- /dev/null +++ b/dice.cpp @@ -0,0 +1,4 @@ +#include "dice.h" +#include + +using namespace std; \ No newline at end of file diff --git a/dice.h b/dice.h new file mode 100644 index 0000000..12a2833 --- /dev/null +++ b/dice.h @@ -0,0 +1,12 @@ +#ifndef DICE_H +#define DICE_H + +class Dice{ + public: + void roll(); + void hold(); + void clearTurnScore(); + void addToTurnScore(int roll); + private: + int turnScore; +} \ No newline at end of file diff --git a/pigGame.cpp b/pigGame.cpp new file mode 100644 index 0000000..684bc41 --- /dev/null +++ b/pigGame.cpp @@ -0,0 +1,8 @@ +#include +#include "player.cpp" +#include "dice.cpp" +using namespace std; + +int main{ + +} \ No newline at end of file diff --git a/player.cpp b/player.cpp new file mode 100644 index 0000000..c7877bb --- /dev/null +++ b/player.cpp @@ -0,0 +1,20 @@ +#include "player.h" +#include + +using namespace std; + +void addTurnScore(int turnScore){ + +} + +void printScore(){ + +} + +void checkScore(){ + +} + +void gameOver(){ + +} \ No newline at end of file diff --git a/player.h b/player.h new file mode 100644 index 0000000..bf8f112 --- /dev/null +++ b/player.h @@ -0,0 +1,12 @@ +#ifndef PLAYER_H +#define PLAYER_H + +class Player{ + public: + void addTurnScore(int turnScore); + void printScore(); + void checkScore(); + void gameOver(); + private: + int totalScore; +} \ No newline at end of file From 7c282874944feef9bde7e9fa7e651ff68bb1327c Mon Sep 17 00:00:00 2001 From: Rogelio Date: Mon, 14 Sep 2015 11:22:27 -0700 Subject: [PATCH 2/6] finished header files for pigsgame --- dice.h | 21 +++++++++++++-------- player.h | 21 +++++++++++++-------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/dice.h b/dice.h index 12a2833..4a8ca88 100644 --- a/dice.h +++ b/dice.h @@ -1,12 +1,17 @@ #ifndef DICE_H #define DICE_H +#include + +using namespace std; class Dice{ - public: - void roll(); - void hold(); - void clearTurnScore(); - void addToTurnScore(int roll); - private: - int turnScore; -} \ No newline at end of file +public: + void roll(); + int hold(); + void clearTurnScore(); + void addToTurnScore(int roll); +rivate: + int turnScore; +}; + +#endif diff --git a/player.h b/player.h index bf8f112..ed5a220 100644 --- a/player.h +++ b/player.h @@ -1,12 +1,17 @@ #ifndef PLAYER_H #define PLAYER_H +#include + +using namespace std; class Player{ - public: - void addTurnScore(int turnScore); - void printScore(); - void checkScore(); - void gameOver(); - private: - int totalScore; -} \ No newline at end of file +public: + void addTurnScore(int turnScore); + void printScore(); + void checkScore(); + void gameOver(); +private: + int totalScore; +}; + +#endif From b8f3618084b41f3193cbf4ef7b3e7b4c9e831ab0 Mon Sep 17 00:00:00 2001 From: Tyler Gering Date: Tue, 15 Sep 2015 01:07:22 -0700 Subject: [PATCH 3/6] files that compile, but have multiple errors. Also missing computer player files. --- dice.cpp | 34 +++++++++++++++++++++++++++++++++- dice.h | 21 ++++++++++----------- pigGame.cpp | 21 +++++++++++++++++++-- player.cpp | 43 +++++++++++++++++++++++++++++++++++-------- player.h | 21 ++++++++++----------- 5 files changed, 107 insertions(+), 33 deletions(-) diff --git a/dice.cpp b/dice.cpp index 1988fbc..73dd209 100644 --- a/dice.cpp +++ b/dice.cpp @@ -1,4 +1,36 @@ #include "dice.h" #include -using namespace std; \ No newline at end of file +using namespace std; + +int Dice::roll(){ + int curRoll = rand() % 6 +1; + + if(curRoll == 1){ + clearTurnScore(); + rolledOne(); + return 1; + } + else{ + addToTurnScore(curRoll); + } + return curRoll; +} + +int Dice::hold(){ + return turnScore; +} + +void Dice::clearTurnScore(){ + turnScore = 0; +} + +int Dice::addToTurnScore(int roll){ + turnScore += roll; + return turnScore; +} + +void Dice::rolledOne(){ + turnScore = 0; + cout << "You rolled a one! Score for turn lost, your turn is over" << endl; +} \ No newline at end of file diff --git a/dice.h b/dice.h index 4a8ca88..262d6a2 100644 --- a/dice.h +++ b/dice.h @@ -1,17 +1,16 @@ #ifndef DICE_H #define DICE_H -#include - -using namespace std; class Dice{ -public: - void roll(); - int hold(); - void clearTurnScore(); - void addToTurnScore(int roll); -rivate: - int turnScore; + public: + int roll(); + int hold(); + void clearTurnScore(); + int addToTurnScore(int roll); + int turnScore; + void rolledOne(); + private: + }; -#endif +#endif \ No newline at end of file diff --git a/pigGame.cpp b/pigGame.cpp index 684bc41..4b50044 100644 --- a/pigGame.cpp +++ b/pigGame.cpp @@ -3,6 +3,23 @@ #include "dice.cpp" using namespace std; -int main{ - +int main(){ + Player p1; + p1.totalScore = 0; + //Computer c1; + Dice d; + string initIn; + cout << "Welcome to pigs game! Type 'roll' to play" << endl; + cin >> initIn; + if(initIn == "roll"){ + int x = d.roll(); + cout << "You rolled: " << x << endl; + cout << "Your total score is: " << x << endl; + } + while(!p1.gameOver() /*|| !c1.gameOver()*/){ + p1.turn(); + p1.printScore(); + //c1.turn(); + //cout << "Computer's total score is: " << c1.totalScore << endl; + } } \ No newline at end of file diff --git a/player.cpp b/player.cpp index c7877bb..895031b 100644 --- a/player.cpp +++ b/player.cpp @@ -1,20 +1,47 @@ #include "player.h" #include - +#include "player.h" using namespace std; -void addTurnScore(int turnScore){ +Player p1; +void Player::sumScore(int turnScore){ + totalScore = totalScore + turnScore; } -void printScore(){ - +void Player::printScore(){ + cout << "Your score is: " << totalScore << endl; } -void checkScore(){ - +bool Player::gameOver(){ + if(totalScore >= 100){ + cout << "Game over, Player wins!" << endl; + return true; + } + return false; } -void gameOver(){ - +void Player::turn(){ + d.turnScore = 0; + string x; + int roll, runningTotal; + cout << "Roll or hold?" << endl; + cin >> x; + while(x!="hold"){ + if(x == "roll"){ + d.roll(); + roll = d.roll(); + cout << "You rolled: " << roll << endl; + runningTotal = totalScore + d.addToTurnScore(0); + cout << "Your running total score is: " << runningTotal << endl; + cout << "Roll or hold?" << endl; + } + if(x!="hold" && x!= "roll"){ + cout << "Invalid input. Enter 'roll' or 'hold'."; + } + cin >> x; + } + int score = d.hold(); + p1.sumScore(score); + cout << "Your total score is: " << totalScore << ", your turn is now over." << endl; } \ No newline at end of file diff --git a/player.h b/player.h index ed5a220..a797621 100644 --- a/player.h +++ b/player.h @@ -1,17 +1,16 @@ #ifndef PLAYER_H #define PLAYER_H -#include - -using namespace std; +#include "dice.h" class Player{ -public: - void addTurnScore(int turnScore); - void printScore(); - void checkScore(); - void gameOver(); -private: - int totalScore; + public: + void sumScore(int turnScore); + void printScore(); + bool gameOver(); + void turn(); + int totalScore; + private: + Dice d; }; -#endif +#endif \ No newline at end of file From acdafccddb51ab33a21e5a5182bff17185729ef2 Mon Sep 17 00:00:00 2001 From: SethMcFarland Date: Tue, 15 Sep 2015 16:20:32 -0700 Subject: [PATCH 4/6] Created Computer class and implemented a couple of the easier functions. dice.cpp: Added the cstdlib file as it is needed for the rand() function. --- computer.cpp | 22 ++++++++++++++++++++++ computer.h | 21 +++++++++++++++++++++ dice.cpp | 1 + pigGame.cpp | 2 +- 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 computer.cpp create mode 100644 computer.h diff --git a/computer.cpp b/computer.cpp new file mode 100644 index 0000000..3d5cd42 --- /dev/null +++ b/computer.cpp @@ -0,0 +1,22 @@ +#include "computer.h" +#include +using namespace std; + + + +void Computer::sumScore(int turnScore){ + totalScore = totalScore + turnScore; +} + +void Computer::printScore(){ + cout << "Computer's total score is: " << totalScore << endl; +} + +bool Computer::gameOver(){ + if(totalScore >= 100){ + cout << "Game over, Computer wins!" << endl; + return true; + } + return false; +} + diff --git a/computer.h b/computer.h new file mode 100644 index 0000000..1e5c0f4 --- /dev/null +++ b/computer.h @@ -0,0 +1,21 @@ +#ifndef COMPUTER_H +#define COMPUTER_H + +#include "dice.h" + + +class Computer +{ + + public: + void sumScore(int turnScore); + void printScore(); + bool gameOver(); + void turn(); + + private: + Dice d; + int totalScore; + +}; +#endif \ No newline at end of file diff --git a/dice.cpp b/dice.cpp index 73dd209..4d4d084 100644 --- a/dice.cpp +++ b/dice.cpp @@ -1,4 +1,5 @@ #include "dice.h" +#include #include using namespace std; diff --git a/pigGame.cpp b/pigGame.cpp index 4b50044..315d6ad 100644 --- a/pigGame.cpp +++ b/pigGame.cpp @@ -20,6 +20,6 @@ int main(){ p1.turn(); p1.printScore(); //c1.turn(); - //cout << "Computer's total score is: " << c1.totalScore << endl; + //c1.printScore(); } } \ No newline at end of file From 13bb58b27d319fc8a736533f914fc9b90ceefe40 Mon Sep 17 00:00:00 2001 From: SethMcFarland Date: Tue, 15 Sep 2015 16:50:03 -0700 Subject: [PATCH 5/6] Implemented Computer::Turn() function Fixed error of every roll counting twice Fixed error of player's turn not ending when a '1' is rolled Made minor changes to text that is displayed to the player --- computer.cpp | 33 +++++++++++++++++++++++++++++++++ computer.h | 2 ++ dice.cpp | 2 +- pigGame.cpp | 22 +++++++++++++++------- player.cpp | 5 ++++- 5 files changed, 55 insertions(+), 9 deletions(-) diff --git a/computer.cpp b/computer.cpp index 3d5cd42..427b57e 100644 --- a/computer.cpp +++ b/computer.cpp @@ -4,6 +4,11 @@ using namespace std; +Computer::Computer() +{ + totalScore = 0; +} + void Computer::sumScore(int turnScore){ totalScore = totalScore + turnScore; } @@ -20,3 +25,31 @@ bool Computer::gameOver(){ return false; } +void Computer::turn(){ + + cout << endl << endl << endl; + + d.clearTurnScore(); + + int roll, runningTotal; + int rollCount = 0; + + while(rollCount < 3 && roll != 1) + { + roll = d.roll(); + + rollCount++; + + cout << "Computer rolled: " << roll << endl; + + runningTotal = totalScore + d.addToTurnScore(0); + + cout << "Computer's running total score is: " << runningTotal << endl; + } + + sumScore(d.hold()); + + cout << "Computer's total score is: " << totalScore << ", and its turn is over." << endl; + + cout << endl << endl << endl; +} \ No newline at end of file diff --git a/computer.h b/computer.h index 1e5c0f4..47234a0 100644 --- a/computer.h +++ b/computer.h @@ -8,6 +8,8 @@ class Computer { public: + Computer(); + void sumScore(int turnScore); void printScore(); bool gameOver(); diff --git a/dice.cpp b/dice.cpp index 4d4d084..d159ba4 100644 --- a/dice.cpp +++ b/dice.cpp @@ -33,5 +33,5 @@ int Dice::addToTurnScore(int roll){ void Dice::rolledOne(){ turnScore = 0; - cout << "You rolled a one! Score for turn lost, your turn is over" << endl; + cout << "A 'one' was rolled! Score for turn lost" << endl; } \ No newline at end of file diff --git a/pigGame.cpp b/pigGame.cpp index 315d6ad..9e34050 100644 --- a/pigGame.cpp +++ b/pigGame.cpp @@ -1,25 +1,33 @@ #include #include "player.cpp" #include "dice.cpp" +#include "computer.cpp" using namespace std; int main(){ Player p1; + Computer c1; + p1.totalScore = 0; //Computer c1; Dice d; string initIn; - cout << "Welcome to pigs game! Type 'roll' to play" << endl; + cout << "Welcome to pigs game! Type 'ready' to play" << endl; cin >> initIn; - if(initIn == "roll"){ - int x = d.roll(); + if(initIn == "ready"){ + + p1.turn(); + + c1.turn(); + + /*int x = d.roll(); cout << "You rolled: " << x << endl; - cout << "Your total score is: " << x << endl; + cout << "Your total score is: " << x << endl;*/ } while(!p1.gameOver() /*|| !c1.gameOver()*/){ p1.turn(); - p1.printScore(); - //c1.turn(); - //c1.printScore(); + + c1.turn(); + c1.printScore(); } } \ No newline at end of file diff --git a/player.cpp b/player.cpp index 895031b..ddea586 100644 --- a/player.cpp +++ b/player.cpp @@ -29,8 +29,11 @@ void Player::turn(){ cin >> x; while(x!="hold"){ if(x == "roll"){ - d.roll(); roll = d.roll(); + + if(roll == 1) + break; + cout << "You rolled: " << roll << endl; runningTotal = totalScore + d.addToTurnScore(0); cout << "Your running total score is: " << runningTotal << endl; From a973980e6c92baf92f2927eafc711da1cefe892f Mon Sep 17 00:00:00 2001 From: SethMcFarland Date: Tue, 15 Sep 2015 17:01:17 -0700 Subject: [PATCH 6/6] Removed part where player.cpp makes an instantiation of itself. Fixed the end game flag so that it would end at the proper time. --- pigGame.cpp | 12 +++++------- player.cpp | 4 +--- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/pigGame.cpp b/pigGame.cpp index 9e34050..0859bbb 100644 --- a/pigGame.cpp +++ b/pigGame.cpp @@ -15,19 +15,17 @@ int main(){ cout << "Welcome to pigs game! Type 'ready' to play" << endl; cin >> initIn; if(initIn == "ready"){ - p1.turn(); c1.turn(); - - /*int x = d.roll(); - cout << "You rolled: " << x << endl; - cout << "Your total score is: " << x << endl;*/ } - while(!p1.gameOver() /*|| !c1.gameOver()*/){ + + while(!p1.gameOver() && !c1.gameOver()){ p1.turn(); + if(p1.gameOver()) + break; + c1.turn(); - c1.printScore(); } } \ No newline at end of file diff --git a/player.cpp b/player.cpp index ddea586..d02c144 100644 --- a/player.cpp +++ b/player.cpp @@ -1,9 +1,7 @@ -#include "player.h" #include #include "player.h" using namespace std; -Player p1; void Player::sumScore(int turnScore){ totalScore = totalScore + turnScore; @@ -45,6 +43,6 @@ void Player::turn(){ cin >> x; } int score = d.hold(); - p1.sumScore(score); + sumScore(score); cout << "Your total score is: " << totalScore << ", your turn is now over." << endl; } \ No newline at end of file