diff --git a/computer.cpp b/computer.cpp new file mode 100644 index 0000000..427b57e --- /dev/null +++ b/computer.cpp @@ -0,0 +1,55 @@ +#include "computer.h" +#include +using namespace std; + + + +Computer::Computer() +{ + totalScore = 0; +} + +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; +} + +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 new file mode 100644 index 0000000..47234a0 --- /dev/null +++ b/computer.h @@ -0,0 +1,23 @@ +#ifndef COMPUTER_H +#define COMPUTER_H + +#include "dice.h" + + +class Computer +{ + + public: + Computer(); + + 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 new file mode 100644 index 0000000..d159ba4 --- /dev/null +++ b/dice.cpp @@ -0,0 +1,37 @@ +#include "dice.h" +#include +#include + +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 << "A 'one' was rolled! Score for turn lost" << endl; +} \ No newline at end of file diff --git a/dice.h b/dice.h new file mode 100644 index 0000000..262d6a2 --- /dev/null +++ b/dice.h @@ -0,0 +1,16 @@ +#ifndef DICE_H +#define DICE_H + +class Dice{ + public: + int roll(); + int hold(); + void clearTurnScore(); + int addToTurnScore(int roll); + int turnScore; + void rolledOne(); + private: + +}; + +#endif \ No newline at end of file diff --git a/pigGame.cpp b/pigGame.cpp new file mode 100644 index 0000000..0859bbb --- /dev/null +++ b/pigGame.cpp @@ -0,0 +1,31 @@ +#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 'ready' to play" << endl; + cin >> initIn; + if(initIn == "ready"){ + p1.turn(); + + c1.turn(); + } + + while(!p1.gameOver() && !c1.gameOver()){ + p1.turn(); + + if(p1.gameOver()) + break; + + c1.turn(); + } +} \ No newline at end of file diff --git a/player.cpp b/player.cpp new file mode 100644 index 0000000..d02c144 --- /dev/null +++ b/player.cpp @@ -0,0 +1,48 @@ +#include +#include "player.h" +using namespace std; + + +void Player::sumScore(int turnScore){ + totalScore = totalScore + turnScore; +} + +void Player::printScore(){ + cout << "Your score is: " << totalScore << endl; +} + +bool Player::gameOver(){ + if(totalScore >= 100){ + cout << "Game over, Player wins!" << endl; + return true; + } + return false; +} + +void Player::turn(){ + d.turnScore = 0; + string x; + int roll, runningTotal; + cout << "Roll or hold?" << endl; + cin >> x; + while(x!="hold"){ + if(x == "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; + cout << "Roll or hold?" << endl; + } + if(x!="hold" && x!= "roll"){ + cout << "Invalid input. Enter 'roll' or 'hold'."; + } + cin >> x; + } + int score = d.hold(); + 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 new file mode 100644 index 0000000..a797621 --- /dev/null +++ b/player.h @@ -0,0 +1,16 @@ +#ifndef PLAYER_H +#define PLAYER_H +#include "dice.h" + +class Player{ + public: + void sumScore(int turnScore); + void printScore(); + bool gameOver(); + void turn(); + int totalScore; + private: + Dice d; +}; + +#endif \ No newline at end of file