From 5cb317f79e858af8500b5ddef92de659d4ad5b10 Mon Sep 17 00:00:00 2001 From: albertoarmienta Date: Wed, 9 Sep 2015 13:33:02 -0700 Subject: [PATCH 01/30] added a main to take in the number of sides --- main.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..e624c1b --- /dev/null +++ b/main.cpp @@ -0,0 +1,9 @@ +#include + +using namespace std; +int main() +{ + int count; + cout<<"How many sides does this Die have?"<>count; +} From 4645d7863dd3a53c01fcba17ba63e6ca08c1ffb4 Mon Sep 17 00:00:00 2001 From: "Adam J. Penn" Date: Wed, 9 Sep 2015 13:35:28 -0700 Subject: [PATCH 02/30] Created dice.cpp and .h as well as created dice class. --- dice.cpp | 9 +++++++++ dice.h | 9 +++++++++ 2 files changed, 18 insertions(+) create mode 100644 dice.cpp create mode 100644 dice.h diff --git a/dice.cpp b/dice.cpp new file mode 100644 index 0000000..336ac10 --- /dev/null +++ b/dice.cpp @@ -0,0 +1,9 @@ +#include "dice.h" + +Dice::Dice(int size) { + size_ = size; +} + +int Dice::Roll() { + +} diff --git a/dice.h b/dice.h new file mode 100644 index 0000000..1ccbcea --- /dev/null +++ b/dice.h @@ -0,0 +1,9 @@ +#include + +class Dice { + public: + Dice(int size); + int Roll(); + private: + int size_; +} From 4cb37fbbc8b2617431f3984540aac3f9f562182d Mon Sep 17 00:00:00 2001 From: mymercurialsky Date: Wed, 9 Sep 2015 13:38:21 -0700 Subject: [PATCH 03/30] Set up the header file for the Game class --- game.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 game.h diff --git a/game.h b/game.h new file mode 100644 index 0000000..532d025 --- /dev/null +++ b/game.h @@ -0,0 +1,25 @@ +#include "player.h" +#include "computer.h" +#include "die.h" + +class Game{ + + public: + Game(); + ~Game(); + void run(); + + private: + Player players[]; + Computer computer; + Die die; + bool isPlayerTurn; + int roundScore; + + + + + + + +} \ No newline at end of file From a6d174801a29d1c0d7f41b2d660b574f38512d67 Mon Sep 17 00:00:00 2001 From: atheri Date: Wed, 9 Sep 2015 13:39:11 -0700 Subject: [PATCH 04/30] Initial pass of basic computer class functionality --- computer.cpp | 19 +++++++++++++++++++ computer.h | 15 +++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 computer.cpp create mode 100644 computer.h diff --git a/computer.cpp b/computer.cpp new file mode 100644 index 0000000..0c19e54 --- /dev/null +++ b/computer.cpp @@ -0,0 +1,19 @@ + +#include "computer.h" + +Computer::Computer(int diff) + : m_difficulty(diff), m_score(0) { + +} + +bool Computer::turn() { + return true; +} + +int Computer::getScore() { + return m_score; +} + +int Computer::setScore(int score) { + m_score = score; +} diff --git a/computer.h b/computer.h new file mode 100644 index 0000000..d14a6ed --- /dev/null +++ b/computer.h @@ -0,0 +1,15 @@ + +#ifndef COMPUTER_H +#define COMPUTER_H + +class Computer { + public: + Computer(int diff); + int turn(); + int getScore(); + void setScore(int score); + private: + int m_difficulty; + int m_score; +} + From 081d97d398fe3bd87258bf174b14d39d3fa4cef5 Mon Sep 17 00:00:00 2001 From: "Adam J. Penn" Date: Wed, 9 Sep 2015 13:44:12 -0700 Subject: [PATCH 05/30] Finished the dice class, just need a main to finish testing it. --- dice.cpp | 4 +++- dice.h | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/dice.cpp b/dice.cpp index 336ac10..1cb5a1c 100644 --- a/dice.cpp +++ b/dice.cpp @@ -2,8 +2,10 @@ Dice::Dice(int size) { size_ = size; + srand (time(0)); } int Dice::Roll() { - + int diceValue = rand() % 6; + return diceValue; } diff --git a/dice.h b/dice.h index 1ccbcea..f2e8839 100644 --- a/dice.h +++ b/dice.h @@ -1,4 +1,7 @@ #include +#include +#include +#include class Dice { public: @@ -6,4 +9,4 @@ class Dice { int Roll(); private: int size_; -} +}; From 8897d9e34cc4d58da2c172ffdfa8f784b0149dbd Mon Sep 17 00:00:00 2001 From: atheri Date: Wed, 9 Sep 2015 13:44:29 -0700 Subject: [PATCH 06/30] Fixed compilation errors in the computer class --- computer.cpp | 2 +- computer.h | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/computer.cpp b/computer.cpp index 0c19e54..6c00535 100644 --- a/computer.cpp +++ b/computer.cpp @@ -14,6 +14,6 @@ int Computer::getScore() { return m_score; } -int Computer::setScore(int score) { +void Computer::setScore(int score) { m_score = score; } diff --git a/computer.h b/computer.h index d14a6ed..f898966 100644 --- a/computer.h +++ b/computer.h @@ -5,11 +5,12 @@ class Computer { public: Computer(int diff); - int turn(); + bool turn(); int getScore(); void setScore(int score); private: int m_difficulty; int m_score; -} +}; +#endif From b6f19f69cb27e651bb4932b0eea41c0960966744 Mon Sep 17 00:00:00 2001 From: albertoarmienta Date: Wed, 9 Sep 2015 13:45:08 -0700 Subject: [PATCH 07/30] added make file --- makefile | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 makefile diff --git a/makefile b/makefile new file mode 100644 index 0000000..409b2ac --- /dev/null +++ b/makefile @@ -0,0 +1,5 @@ +PigsGame: + g++ main.cpp -o PigsGame + +clean: + rm PigsGame *.o From d19d3c0aade8cc750a346f61fe688564a37e3d53 Mon Sep 17 00:00:00 2001 From: mymercurialsky Date: Wed, 9 Sep 2015 13:50:26 -0700 Subject: [PATCH 08/30] Started implementation of game.cpp and modified game.h --- game.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 game.cpp diff --git a/game.cpp b/game.cpp new file mode 100644 index 0000000..4b7b178 --- /dev/null +++ b/game.cpp @@ -0,0 +1,25 @@ +#include "player.h" +#include "computer.h" +#include "die.h" +/* +class Game{ + + public: + Game(); + ~Game(); + void run(); + + private: + Player players[]; + Computer computer; + Die die; + bool isPlayerTurn; + int roundScore; + */ + + + + + + +} \ No newline at end of file From dbede126761ff172c192def244e7d535ca18b707 Mon Sep 17 00:00:00 2001 From: atheri Date: Wed, 9 Sep 2015 13:51:41 -0700 Subject: [PATCH 09/30] Added parameters to Computer's turn function. turn(num of dice rolled in round, current round score) --- computer.cpp | 2 +- computer.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/computer.cpp b/computer.cpp index 6c00535..6022d19 100644 --- a/computer.cpp +++ b/computer.cpp @@ -6,7 +6,7 @@ Computer::Computer(int diff) } -bool Computer::turn() { +bool Computer::turn(int num_dice, int round_score) { return true; } diff --git a/computer.h b/computer.h index f898966..da7f45c 100644 --- a/computer.h +++ b/computer.h @@ -5,7 +5,7 @@ class Computer { public: Computer(int diff); - bool turn(); + bool turn(int num_dice, int round_score); int getScore(); void setScore(int score); private: From 25600bdd2accae3dc17623ab34114753ff4a634b Mon Sep 17 00:00:00 2001 From: Shirley Date: Wed, 9 Sep 2015 15:01:34 -0700 Subject: [PATCH 10/30] Incomplete PlayerClass --- Player.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Player.h diff --git a/Player.h b/Player.h new file mode 100644 index 0000000..4d919a9 --- /dev/null +++ b/Player.h @@ -0,0 +1,23 @@ +#ifndef PLAYER_H +#define PLAYER_H + +#include + + +using namespace std; + +class Player +{ + public: + player(int score, int name); + int total(); + + + private: + + + + + +}; +#endif From 79f443b7c9ecc60c1e739c05325bac6f28986745 Mon Sep 17 00:00:00 2001 From: mymercurialsky Date: Thu, 10 Sep 2015 12:58:24 -0700 Subject: [PATCH 11/30] Fleshed out text interface mechanics, score reporting and victory check. --- game.cpp | 186 ++++++++++++++++++++++++++++++++++++++++++++++++------- game.h | 37 ++++++----- 2 files changed, 185 insertions(+), 38 deletions(-) diff --git a/game.cpp b/game.cpp index 4b7b178..a96e672 100644 --- a/game.cpp +++ b/game.cpp @@ -1,25 +1,167 @@ #include "player.h" #include "computer.h" #include "die.h" -/* -class Game{ - - public: - Game(); - ~Game(); - void run(); - - private: - Player players[]; - Computer computer; - Die die; - bool isPlayerTurn; - int roundScore; - */ - - - - - - -} \ No newline at end of file +#include "game.h" +#include +#include +using namespace std; + +Game::Game(int numPlayers, int dieSize){ + + m_numPlayers = numPlayers; + + // Initialize array of players + m_players = new Player[m_numPlayers]; + for (int i = 0; i < m_numPlayers; i++ ) m_players[i] = new Player(); + + // Start at beginning of player array + m_currentPlayer = 0; + + // Default dice size of 6 + m_die = new Die(dieSize); + + // Default computer difficulty of 1 + m_computer = new Computer(1); + + // Start with the player + m_isPlayerTurn = true; + + // Initialize round score to zero + m_roundScore = 0; + + m_gameWon = false; + + } + +Game::~Game(){ + + // Free allocated memory + delete[] m_players; + + // TODO do you need to delete each individual player? + delete m_die; + delete m_computer; + + } + +void Game::run(){ + + while (!m_gameWon){ + + m_roundScore = 0; + + if (m_isPlayerTurn) playerTurn(); + else computerTurn(); + + int score = m_isPlayerTurn ? m_players[m_currentPlayer].getScore() : m_computer.getScore(); + + // Check for victory! + if (victoryCheck(score, m_isPlayerTurn, m_currentPlayer)) m_gameWon = true; + + // Change turns - player vs computer + m_isPlayerTurn = !m_isPlayerTurn; + + // Change number of player (player 1, player 2, etc) + m_currentPlayer = (m_currentPlayer + 1) % m_numPlayers; + + // TODO Reset game to play again? + + } + } + +void Game::playerTurn(){ + + while (true){ + + int currentRoll = m_die.roll(); + + cout << "Player rolled a " << currentRoll << "." << endl; + + if (currentRoll == 1){ + + cout << "Player's turn ends." << endl; + return; + + } + + m_roundScore += currentRoll; + + char answer = '0'; + + cout << "Your current round score is " << m_roundScore << "." << endl; + + while (answer != 'H' && answer != 'R'){ + + cout << "Do you want to roll or hold? 'R' or 'H'" << endl; + cin >> answer; + + if (answer == 'H'){ + + // Add round score to player score + int playerScore = m_players[m_currentPlayer].getScore(); + m_players[m_currentPlayer].setScore(m_roundScore + playerScore); + + cout << "Player " << m_currentPlayer << "added a total of " << m_roundScore; + cout << " points to their score. They now have " << playerScore + m_roundScore << " points." << endl; + + return; + } + } + } + } + +bool Game::victoryCheck(int score, bool player, int playerNumber){ + + if (score < 100){ + + return false; + + } + + string victor = player ? "Player " + to_string(playerNumber) : "The computer"; + + cout << victor << " wins the game with a total of " << score << " points!" << endl; + + return true; + +} + +void Game::computerTurn(){ + + int turnRolls = 0; + + while (true){ + + int currentRoll = die.roll(); + turnRolls++; + m_roundScore += currentRoll; + + cout << "The computer rolled a " << currentRoll << "." << endl; + + if (currentRoll == 1) + { + cout << "The computer ended their turn." << endl; + return; + } + + // Determine if computer is going to roll or hold + if (!computer.turn(turnRolls, m_roundScore)){ + + // computer.turn() returns false if they decide to hold + + // Add round score to computer score + int computerScore = m_computer.getScore(); + computer.setScore(m_roundScore + computerScore); + + // TODO separate out reporting into one function? + cout << "The computer added a total of " << m_roundScore; + cout << " points to their score. They now have " << m_computer.getScore() << " points." << endl; + return; + + } + + } + + } + +} diff --git a/game.h b/game.h index 532d025..35d9fd2 100644 --- a/game.h +++ b/game.h @@ -1,25 +1,30 @@ +#ifndef GAME +#define GAME + #include "player.h" #include "computer.h" #include "die.h" class Game{ - + public: - Game(); + Game(int, int); ~Game(); void run(); - + private: - Player players[]; - Computer computer; - Die die; - bool isPlayerTurn; - int roundScore; - - - - - - - -} \ No newline at end of file + Player m_players[]; + Computer m_computer; + Die m_die; + bool m_isPlayerTurn; + bool m_gameWon; + int m_roundScore; + int m_currentPlayer; + int m_numPlayers; + void playerTurn(); + void computerTurn(); + bool victoryCheck(int, bool, int); + +}; + +#endif // GAME From e4c6f38a619acd46a244b36be5cdf9197e15e676 Mon Sep 17 00:00:00 2001 From: mymercurialsky Date: Sun, 13 Sep 2015 13:02:19 -0700 Subject: [PATCH 12/30] Fixed compilation errors in game.cpp and added new lines to text reporting. --- game.cpp | 54 ++++++++++++++++++++++++++++++++---------------------- game.h | 10 +++++----- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/game.cpp b/game.cpp index a96e672..16bcffa 100644 --- a/game.cpp +++ b/game.cpp @@ -1,24 +1,24 @@ #include "player.h" #include "computer.h" -#include "die.h" +#include "dice.h" #include "game.h" #include #include using namespace std; -Game::Game(int numPlayers, int dieSize){ +Game::Game(int numPlayers, int dieSize, string playerName){ m_numPlayers = numPlayers; // Initialize array of players - m_players = new Player[m_numPlayers]; - for (int i = 0; i < m_numPlayers; i++ ) m_players[i] = new Player(); + m_players = new Player*[m_numPlayers]; + for (int i = 0; i < m_numPlayers; i++ ) m_players[i] = new Player(playerName); // Start at beginning of player array m_currentPlayer = 0; // Default dice size of 6 - m_die = new Die(dieSize); + m_die = new Dice(dieSize); // Default computer difficulty of 1 m_computer = new Computer(1); @@ -36,9 +36,9 @@ Game::Game(int numPlayers, int dieSize){ Game::~Game(){ // Free allocated memory + for (int i = 0; igetScore() : m_computer->getScore(); // Check for victory! if (victoryCheck(score, m_isPlayerTurn, m_currentPlayer)) m_gameWon = true; @@ -73,13 +75,17 @@ void Game::playerTurn(){ while (true){ - int currentRoll = m_die.roll(); + int currentRoll = m_die->Roll(); + + Player *player = m_players[m_currentPlayer]; - cout << "Player rolled a " << currentRoll << "." << endl; + cout << player->getName() << " rolled a " << currentRoll << "." << endl; + cout << endl; if (currentRoll == 1){ - cout << "Player's turn ends." << endl; + cout << player->getName() << "'s turn ends." << endl; + cout << endl; return; } @@ -94,15 +100,17 @@ void Game::playerTurn(){ cout << "Do you want to roll or hold? 'R' or 'H'" << endl; cin >> answer; + cout << endl; if (answer == 'H'){ // Add round score to player score - int playerScore = m_players[m_currentPlayer].getScore(); - m_players[m_currentPlayer].setScore(m_roundScore + playerScore); + int playerScore = m_players[m_currentPlayer]->getScore(); + m_players[m_currentPlayer]->setScore(m_roundScore + playerScore); - cout << "Player " << m_currentPlayer << "added a total of " << m_roundScore; + cout << player->getName() << " added a total of " << m_roundScore; cout << " points to their score. They now have " << playerScore + m_roundScore << " points." << endl; + cout << endl; return; } @@ -110,15 +118,17 @@ void Game::playerTurn(){ } } -bool Game::victoryCheck(int score, bool player, int playerNumber){ +bool Game::victoryCheck(int score, bool playerTurn, int playerNumber){ if (score < 100){ return false; } + + Player *player = m_players[m_currentPlayer]; - string victor = player ? "Player " + to_string(playerNumber) : "The computer"; + string victor = playerTurn ? player->getName() : "The computer"; cout << victor << " wins the game with a total of " << score << " points!" << endl; @@ -132,7 +142,7 @@ void Game::computerTurn(){ while (true){ - int currentRoll = die.roll(); + int currentRoll = m_die->Roll(); turnRolls++; m_roundScore += currentRoll; @@ -141,21 +151,23 @@ void Game::computerTurn(){ if (currentRoll == 1) { cout << "The computer ended their turn." << endl; + cout << endl; return; } // Determine if computer is going to roll or hold - if (!computer.turn(turnRolls, m_roundScore)){ + if (!m_computer->turn(turnRolls, m_roundScore)){ // computer.turn() returns false if they decide to hold // Add round score to computer score - int computerScore = m_computer.getScore(); - computer.setScore(m_roundScore + computerScore); + int computerScore = m_computer->getScore(); + m_computer->setScore(m_roundScore + computerScore); // TODO separate out reporting into one function? cout << "The computer added a total of " << m_roundScore; - cout << " points to their score. They now have " << m_computer.getScore() << " points." << endl; + cout << " points to their score. They now have " << m_computer->getScore() << " points." << endl; + cout << endl; return; } @@ -163,5 +175,3 @@ void Game::computerTurn(){ } } - -} diff --git a/game.h b/game.h index 35d9fd2..a4f6736 100644 --- a/game.h +++ b/game.h @@ -3,19 +3,19 @@ #include "player.h" #include "computer.h" -#include "die.h" +#include "dice.h" class Game{ public: - Game(int, int); + Game(int, int, string); ~Game(); void run(); private: - Player m_players[]; - Computer m_computer; - Die m_die; + Player **m_players; + Computer *m_computer; + Dice *m_die; bool m_isPlayerTurn; bool m_gameWon; int m_roundScore; From b446b3a0b746dbdd097f08fa658e67a0d40fcbe4 Mon Sep 17 00:00:00 2001 From: Shirley Date: Sun, 13 Sep 2015 18:22:36 -0700 Subject: [PATCH 13/30] Updated with functions of counting scores in Player.cpp. --- Player.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Player.cpp diff --git a/Player.cpp b/Player.cpp new file mode 100644 index 0000000..b959dc8 --- /dev/null +++ b/Player.cpp @@ -0,0 +1,50 @@ +#include "Player.h" +#include "dice.h" + +#include + +using namespace std; + +Player::Player(int m_score, m_string name) +{ +score=m_score; +name=m_string; +} + +bool Player::roll() +{ + return true; +} + +int Player::PlayerTurn(int TotalScore) +{ + int all_score=0; + int score=0; + string roll; + + //if the player decide to roll + cout << "Do you want to roll or hold? Choose R(Roll) or H(Hold) "<< endl; + cin >> roll; + + while(roll == 'R') + { + score = Dice::Roll(); //roll a dice for this turn; + if (score == 1) + { + cout << "I am sorry, you just rolled a 1"<< endl; + //roll a 1, stop, break + break; + } + return all_score=all_score+score; + + } + + return TotalScore; + +} + + +int Player::total() +{ + return score; +} From 57871de4a7a77938824dc5bd138b42d5e8246bfc Mon Sep 17 00:00:00 2001 From: Shirley Date: Mon, 14 Sep 2015 11:09:59 -0700 Subject: [PATCH 14/30] Player.h with new functions --- Player.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Player.h b/Player.h index 4d919a9..b5d2aa1 100644 --- a/Player.h +++ b/Player.h @@ -11,10 +11,15 @@ class Player public: player(int score, int name); int total(); + bool roll(int roll_num); + PlayerTurn(int TotalScore); + + private: - + int score; + string name; From 6fbf88d3f3ed3bd40cb0bebf2501a3eba9bfd528 Mon Sep 17 00:00:00 2001 From: Shirley Date: Mon, 14 Sep 2015 11:11:42 -0700 Subject: [PATCH 15/30] Player.h with new functions --- Player.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Player.h b/Player.h index b5d2aa1..e332550 100644 --- a/Player.h +++ b/Player.h @@ -9,7 +9,7 @@ using namespace std; class Player { public: - player(int score, int name); + player(int score, string name); int total(); bool roll(int roll_num); PlayerTurn(int TotalScore); From 5f56c2aca314f9ddcc6223e92865734d8f3668f0 Mon Sep 17 00:00:00 2001 From: "Adam J. Penn" Date: Mon, 14 Sep 2015 11:13:35 -0700 Subject: [PATCH 16/30] added define's to dice.cpp and made rand genreate correct number --- dice.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dice.cpp b/dice.cpp index 1cb5a1c..fe141fb 100644 --- a/dice.cpp +++ b/dice.cpp @@ -1,11 +1,16 @@ #include "dice.h" +#ifndef DICE_H +#define DICE_H + Dice::Dice(int size) { size_ = size; srand (time(0)); } int Dice::Roll() { - int diceValue = rand() % 6; + int diceValue = rand() % size_ + 1; return diceValue; } + +#endif //DICE_H From 5b5417911701663adc1a9a719566f8361bb84cb6 Mon Sep 17 00:00:00 2001 From: "Adam J. Penn" Date: Mon, 14 Sep 2015 11:17:57 -0700 Subject: [PATCH 17/30] adding () to dice value --- dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dice.cpp b/dice.cpp index fe141fb..1d7037e 100644 --- a/dice.cpp +++ b/dice.cpp @@ -9,7 +9,7 @@ Dice::Dice(int size) { } int Dice::Roll() { - int diceValue = rand() % size_ + 1; + int diceValue = (rand() % size_)++; return diceValue; } From e9e0685de5bd0e4ee1ef9262eb206381ee359b4b Mon Sep 17 00:00:00 2001 From: atheri Date: Mon, 14 Sep 2015 11:18:39 -0700 Subject: [PATCH 18/30] Added a simple algorithm for determining whether the computer should continue rolling or hold --- computer.cpp | 15 +++++++++++++-- computer.h | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/computer.cpp b/computer.cpp index 6022d19..ea2d8d7 100644 --- a/computer.cpp +++ b/computer.cpp @@ -2,12 +2,23 @@ #include "computer.h" Computer::Computer(int diff) - : m_difficulty(diff), m_score(0) { + : m_difficulty(diff), m_score(0), m_running_chance(0) { } bool Computer::turn(int num_dice, int round_score) { - return true; + if (round_score == 0) { + m_running_chance = 5/6; + return true; + } + + if (m_running_chance < 1/2) { + return false; + } + else { + m_running_chance = m_running_chance * 5/6; + return true; + } } int Computer::getScore() { diff --git a/computer.h b/computer.h index da7f45c..bec0a51 100644 --- a/computer.h +++ b/computer.h @@ -11,6 +11,7 @@ class Computer { private: int m_difficulty; int m_score; + double m_running_chance; }; #endif From 9fdaf950d01e111b67790e727ed845672b675d17 Mon Sep 17 00:00:00 2001 From: albertoarmienta Date: Mon, 14 Sep 2015 11:19:11 -0700 Subject: [PATCH 19/30] main now creates a Game object to run the actual game --- main.cpp | 12 ++++++++++-- makefile | 4 +++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index e624c1b..08980b0 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,17 @@ #include +#include +#include using namespace std; int main() { - int count; + string name; + int diceSides; + cout<<"What is your name?"<>name cout<<"How many sides does this Die have?"<>count; + cin>>diceSides; + + Game game(1, diceSides, name); + game.run(); } diff --git a/makefile b/makefile index 409b2ac..889cb99 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,7 @@ -PigsGame: +PigsGame:game.o g++ main.cpp -o PigsGame +game.o: game.cpp game.h + g++ -c game.cpp clean: rm PigsGame *.o From 2be5474dbe1c9890105885c8fe3e12ec45393509 Mon Sep 17 00:00:00 2001 From: Shirley Date: Mon, 14 Sep 2015 11:21:08 -0700 Subject: [PATCH 20/30] updated with the requested functions --- Player.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Player.h b/Player.h index e332550..cff6359 100644 --- a/Player.h +++ b/Player.h @@ -9,17 +9,17 @@ using namespace std; class Player { public: - player(int score, string name); - int total(); - bool roll(int roll_num); - PlayerTurn(int TotalScore); + Player(string name); + int getScore(); + void setScore(int); + string getName(); private: - int score; - string name; + int m_score; + string m_name; From a042ada29e162d9e91fc8f714aa05a3d916e74e2 Mon Sep 17 00:00:00 2001 From: Shirley Date: Mon, 14 Sep 2015 11:21:36 -0700 Subject: [PATCH 21/30] updated with the requested functions --- Player.cpp | 42 +++++++++--------------------------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/Player.cpp b/Player.cpp index b959dc8..acb28d9 100644 --- a/Player.cpp +++ b/Player.cpp @@ -1,50 +1,26 @@ #include "Player.h" -#include "dice.h" +//#include "dice.h" #include using namespace std; -Player::Player(int m_score, m_string name) +Player::Player(string name) { -score=m_score; -name=m_string; + name=m_name; } -bool Player::roll() +int Player::getScore() { - return true; + return m_score; } -int Player::PlayerTurn(int TotalScore) +void Player::setScore(int) { - int all_score=0; - int score=0; - string roll; - - //if the player decide to roll - cout << "Do you want to roll or hold? Choose R(Roll) or H(Hold) "<< endl; - cin >> roll; - - while(roll == 'R') - { - score = Dice::Roll(); //roll a dice for this turn; - if (score == 1) - { - cout << "I am sorry, you just rolled a 1"<< endl; - //roll a 1, stop, break - break; - } - return all_score=all_score+score; - - } - - return TotalScore; - + int score = m_score; } - -int Player::total() +string Player::getName() { - return score; +return m_name; } From cd5917fac95a084da6561a5ad5e6a58d454bf79d Mon Sep 17 00:00:00 2001 From: Shirley Date: Mon, 14 Sep 2015 11:22:03 -0700 Subject: [PATCH 22/30] updated with the requested functions --- Player.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Player.cpp b/Player.cpp index acb28d9..1b50476 100644 --- a/Player.cpp +++ b/Player.cpp @@ -1,5 +1,5 @@ #include "Player.h" -//#include "dice.h" +#include "dice.h" #include From b4c91d104c4ae70a13880d944e6ed958f3226b5c Mon Sep 17 00:00:00 2001 From: Shirley Date: Mon, 14 Sep 2015 11:28:39 -0700 Subject: [PATCH 23/30] updated with requested player functions --- Player.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Player.cpp b/Player.cpp index 1b50476..4043513 100644 --- a/Player.cpp +++ b/Player.cpp @@ -15,7 +15,7 @@ int Player::getScore() return m_score; } -void Player::setScore(int) +void Player::setScore(int player_score) { int score = m_score; } From c68df7568a0c9a4c6a0600bfbeb4d9a9be64f741 Mon Sep 17 00:00:00 2001 From: Shirley Date: Mon, 14 Sep 2015 11:29:31 -0700 Subject: [PATCH 24/30] updated with requested player functions --- Player.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Player.h b/Player.h index cff6359..9ce9326 100644 --- a/Player.h +++ b/Player.h @@ -11,7 +11,7 @@ class Player public: Player(string name); int getScore(); - void setScore(int); + void setScore(int player_score); string getName(); From 92b6b5d47dd7a826f7a3388a8f6bd488ec6e6928 Mon Sep 17 00:00:00 2001 From: Shirley Date: Mon, 14 Sep 2015 11:29:48 -0700 Subject: [PATCH 25/30] updated with requested player functions --- Player.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Player.cpp b/Player.cpp index 4043513..66e3ad7 100644 --- a/Player.cpp +++ b/Player.cpp @@ -17,7 +17,7 @@ int Player::getScore() void Player::setScore(int player_score) { - int score = m_score; + int player_score = m_score; } string Player::getName() From 31e6bd53d17cc7aa271a8b34804ceab476db34af Mon Sep 17 00:00:00 2001 From: mymercurialsky Date: Mon, 14 Sep 2015 12:01:26 -0700 Subject: [PATCH 26/30] Merged all branches and checked for compiler errors. --- Player.cpp | 6 +++--- dice.cpp | 7 ++----- dice.h | 28 ++++++++++++++++------------ main.cpp | 4 ++-- makefile | 32 +++++++++++++++++++++++++++----- 5 files changed, 50 insertions(+), 27 deletions(-) diff --git a/Player.cpp b/Player.cpp index 66e3ad7..74db30f 100644 --- a/Player.cpp +++ b/Player.cpp @@ -1,4 +1,4 @@ -#include "Player.h" +#include "player.h" #include "dice.h" #include @@ -7,7 +7,7 @@ using namespace std; Player::Player(string name) { - name=m_name; + m_name=name; } int Player::getScore() @@ -17,7 +17,7 @@ int Player::getScore() void Player::setScore(int player_score) { - int player_score = m_score; + m_score = player_score; } string Player::getName() diff --git a/dice.cpp b/dice.cpp index 1d7037e..6cb735f 100644 --- a/dice.cpp +++ b/dice.cpp @@ -1,16 +1,13 @@ #include "dice.h" -#ifndef DICE_H -#define DICE_H - Dice::Dice(int size) { size_ = size; srand (time(0)); } int Dice::Roll() { - int diceValue = (rand() % size_)++; + int diceValue = (rand() % size_) + 1; return diceValue; } -#endif //DICE_H + diff --git a/dice.h b/dice.h index f2e8839..fa57e41 100644 --- a/dice.h +++ b/dice.h @@ -1,12 +1,16 @@ -#include -#include -#include -#include - -class Dice { - public: - Dice(int size); - int Roll(); - private: - int size_; -}; +#ifndef DICE_H +#define DICE_H + +#include +#include +#include +#include + +class Dice { + public: + Dice(int size); + int Roll(); + private: + int size_; +}; +#endif //DICE_H \ No newline at end of file diff --git a/main.cpp b/main.cpp index 08980b0..8762e4c 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include "game.h" using namespace std; int main() @@ -8,7 +8,7 @@ int main() string name; int diceSides; cout<<"What is your name?"<>name + cin>>name; cout<<"How many sides does this Die have?"<>diceSides; diff --git a/makefile b/makefile index 889cb99..f6d423c 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,29 @@ -PigsGame:game.o - g++ main.cpp -o PigsGame +# Compiler options +# the arguments after g++ specify specific options for the compiler +# -Wall ==> print all warnings +# -pedantic ==> print warnings when non-standard C++ is used +# -std=c++11 ==> use the C++11 standard +# -g ==> keep additional information to aid in debugging +# -o ==> specify the output filename +# -c ==> compile only (create a .o file) + +pigsgame: main.o computer.o dice.o game.o player.o + g++ -Wall -pedantic -g -std=c++11 -o pigsgame main.o computer.o dice.o game.o player.o + +main.o: main.cpp + g++ -Wall -pedantic -g -std=c++11 -c main.cpp + +computer.o: computer.cpp computer.h + g++ -Wall -pedantic -g -std=c++11 -c computer.cpp + +dice.o: dice.cpp dice.h + g++ -Wall -pedantic -g -std=c++11 -c dice.cpp + +game.o: game.cpp game.h + g++ -Wall -pedantic -g -std=c++11 -c game.cpp + +player.o: player.cpp player.h + g++ -Wall -pedantic -g -std=c++11 -c player.cpp -game.o: game.cpp game.h - g++ -c game.cpp clean: - rm PigsGame *.o + rm -f pigsgame pigsgame.exe pigsgame.o computer.o dice.o game.o player.o From 382efa84e9585d887a5236c6754998721be83983 Mon Sep 17 00:00:00 2001 From: atheri Date: Mon, 14 Sep 2015 12:08:24 -0700 Subject: [PATCH 27/30] Fixed the computer not intializing m_running_chance. Added logic for the computer to stop if holding will result in a win --- computer.cpp | 10 +++++++--- computer.h | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/computer.cpp b/computer.cpp index ea2d8d7..190fad9 100644 --- a/computer.cpp +++ b/computer.cpp @@ -6,17 +6,21 @@ Computer::Computer(int diff) } -bool Computer::turn(int num_dice, int round_score) { - if (round_score == 0) { +bool Computer::turn(int turn_roll, int round_score) { + if (turn_roll == 1) { m_running_chance = 5/6; return true; } + + if (round_score + m_score >= 100) { + return false; + } if (m_running_chance < 1/2) { return false; } else { - m_running_chance = m_running_chance * 5/6; + m_running_chance = m_running_chance * (5/6); return true; } } diff --git a/computer.h b/computer.h index bec0a51..49afc3a 100644 --- a/computer.h +++ b/computer.h @@ -5,7 +5,7 @@ class Computer { public: Computer(int diff); - bool turn(int num_dice, int round_score); + bool turn(int turn_roll, int round_score); int getScore(); void setScore(int score); private: From bc180d882d72449a567133b7880d7ccb8a7eeac7 Mon Sep 17 00:00:00 2001 From: mymercurialsky Date: Mon, 14 Sep 2015 12:11:43 -0700 Subject: [PATCH 28/30] Renamed Player.cpp and Player.h to be consistent with other file names in lowercase. --- Player.cpp => player.cpp | 0 Player.h => player.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename Player.cpp => player.cpp (100%) rename Player.h => player.h (100%) diff --git a/Player.cpp b/player.cpp similarity index 100% rename from Player.cpp rename to player.cpp diff --git a/Player.h b/player.h similarity index 100% rename from Player.h rename to player.h From 253f747752fb6739fc32072b2f90681455bea2c9 Mon Sep 17 00:00:00 2001 From: atheri Date: Mon, 14 Sep 2015 12:26:29 -0700 Subject: [PATCH 29/30] Fixed the computer never holding. Integer division was causing the stop condition to be lower than intended --- computer.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/computer.cpp b/computer.cpp index 190fad9..1eb6bcc 100644 --- a/computer.cpp +++ b/computer.cpp @@ -1,4 +1,4 @@ - +#include #include "computer.h" Computer::Computer(int diff) @@ -8,7 +8,8 @@ Computer::Computer(int diff) bool Computer::turn(int turn_roll, int round_score) { if (turn_roll == 1) { - m_running_chance = 5/6; + std::cout << "Init turn" << std::endl; + m_running_chance = 5.0/6; return true; } @@ -16,11 +17,11 @@ bool Computer::turn(int turn_roll, int round_score) { return false; } - if (m_running_chance < 1/2) { + if (m_running_chance < 0.5) { return false; } else { - m_running_chance = m_running_chance * (5/6); + m_running_chance = m_running_chance * (5.0/6); return true; } } From 412efd61813fa2f67fa355d5afc6a50f250ff1e9 Mon Sep 17 00:00:00 2001 From: mymercurialsky Date: Mon, 14 Sep 2015 12:36:22 -0700 Subject: [PATCH 30/30] Fixed bug where computer never held in computer.cpp. --- computer.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/computer.cpp b/computer.cpp index ea2d8d7..e223a4b 100644 --- a/computer.cpp +++ b/computer.cpp @@ -7,18 +7,21 @@ Computer::Computer(int diff) } bool Computer::turn(int num_dice, int round_score) { - if (round_score == 0) { - m_running_chance = 5/6; + double fraction = 0.83; + double check = 0.5; + + if (num_dice == 1) { + m_running_chance = fraction; return true; } - if (m_running_chance < 1/2) { + if (m_running_chance < check) { return false; } else { - m_running_chance = m_running_chance * 5/6; + m_running_chance = m_running_chance * fraction; return true; - } + } } int Computer::getScore() {