diff --git a/computer.cpp b/computer.cpp new file mode 100644 index 0000000..6f331bf --- /dev/null +++ b/computer.cpp @@ -0,0 +1,33 @@ +#include "computer.h" + +Computer::Computer(int diff) + : m_difficulty(diff), m_score(0), m_running_chance(0) { + +} + +bool Computer::turn(int turn_roll, int round_score) { + if (turn_roll == 1) { + m_running_chance = 5.0/6; + return true; + } + + if (round_score + m_score >= 100) { + return false; + } + + if (m_running_chance < 0.5) { + return false; + } + else { + m_running_chance = m_running_chance * (5.0/6); + return true; + } +} + +int Computer::getScore() { + return m_score; +} + +void Computer::setScore(int score) { + m_score = score; +} diff --git a/computer.h b/computer.h new file mode 100644 index 0000000..49afc3a --- /dev/null +++ b/computer.h @@ -0,0 +1,17 @@ + +#ifndef COMPUTER_H +#define COMPUTER_H + +class Computer { + public: + Computer(int diff); + bool turn(int turn_roll, int round_score); + int getScore(); + void setScore(int score); + private: + int m_difficulty; + int m_score; + double m_running_chance; +}; + +#endif diff --git a/dice.cpp b/dice.cpp new file mode 100644 index 0000000..6cb735f --- /dev/null +++ b/dice.cpp @@ -0,0 +1,13 @@ +#include "dice.h" + +Dice::Dice(int size) { + size_ = size; + srand (time(0)); +} + +int Dice::Roll() { + int diceValue = (rand() % size_) + 1; + return diceValue; +} + + diff --git a/dice.h b/dice.h new file mode 100644 index 0000000..fa57e41 --- /dev/null +++ b/dice.h @@ -0,0 +1,16 @@ +#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/game.cpp b/game.cpp new file mode 100644 index 0000000..16bcffa --- /dev/null +++ b/game.cpp @@ -0,0 +1,177 @@ +#include "player.h" +#include "computer.h" +#include "dice.h" +#include "game.h" +#include +#include +using namespace std; + +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(playerName); + + // Start at beginning of player array + m_currentPlayer = 0; + + // Default dice size of 6 + m_die = new Dice(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 + for (int i = 0; igetScore() : 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(); + + Player *player = m_players[m_currentPlayer]; + + cout << player->getName() << " rolled a " << currentRoll << "." << endl; + cout << endl; + + if (currentRoll == 1){ + + cout << player->getName() << "'s turn ends." << endl; + cout << 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; + 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); + + 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; + } + } + } + } + +bool Game::victoryCheck(int score, bool playerTurn, int playerNumber){ + + if (score < 100){ + + return false; + + } + + Player *player = m_players[m_currentPlayer]; + + string victor = playerTurn ? player->getName() : "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 = m_die->Roll(); + turnRolls++; + m_roundScore += currentRoll; + + cout << "The computer rolled a " << currentRoll << "." << endl; + + if (currentRoll == 1) + { + cout << "The computer ended their turn." << endl; + cout << endl; + return; + } + + // Determine if computer is going to roll or hold + 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(); + 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 << endl; + return; + + } + + } + + } diff --git a/game.h b/game.h new file mode 100644 index 0000000..a4f6736 --- /dev/null +++ b/game.h @@ -0,0 +1,30 @@ +#ifndef GAME +#define GAME + +#include "player.h" +#include "computer.h" +#include "dice.h" + +class Game{ + + public: + Game(int, int, string); + ~Game(); + void run(); + + private: + Player **m_players; + Computer *m_computer; + Dice *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 diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..8762e4c --- /dev/null +++ b/main.cpp @@ -0,0 +1,17 @@ +#include +#include +#include "game.h" + +using namespace std; +int main() +{ + string name; + int diceSides; + cout<<"What is your name?"<>name; + cout<<"How many sides does this Die have?"<>diceSides; + + Game game(1, diceSides, name); + game.run(); +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..f6d423c --- /dev/null +++ b/makefile @@ -0,0 +1,29 @@ +# 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 + +clean: + rm -f pigsgame pigsgame.exe pigsgame.o computer.o dice.o game.o player.o diff --git a/player.cpp b/player.cpp new file mode 100644 index 0000000..74db30f --- /dev/null +++ b/player.cpp @@ -0,0 +1,26 @@ +#include "player.h" +#include "dice.h" + +#include + +using namespace std; + +Player::Player(string name) +{ + m_name=name; +} + +int Player::getScore() +{ + return m_score; +} + +void Player::setScore(int player_score) +{ + m_score = player_score; +} + +string Player::getName() +{ +return m_name; +} diff --git a/player.h b/player.h new file mode 100644 index 0000000..9ce9326 --- /dev/null +++ b/player.h @@ -0,0 +1,28 @@ +#ifndef PLAYER_H +#define PLAYER_H + +#include + + +using namespace std; + +class Player +{ + public: + Player(string name); + int getScore(); + void setScore(int player_score); + string getName(); + + + + + private: + int m_score; + string m_name; + + + + +}; +#endif