diff --git a/CpuPlayer.cpp b/CpuPlayer.cpp new file mode 100644 index 0000000..853eec3 --- /dev/null +++ b/CpuPlayer.cpp @@ -0,0 +1,37 @@ +#include "CpuPlayer.h" +#include +#include + +CpuPlayer::CpuPlayer() +{ + +} + +bool CpuPlayer::determine_hold(int prev_roll, int cur_score, int roll_num) +{ + // if (!cur_score || (prev_roll < 4 && cur_score < 6)) + // { + // IO.command(COM_ROLL); + // } + // else if ((cur_score > 9 && roll_num > 3) || roll_num > 4) + // { + // IO.command(COM_HOLD); + // } + return false; +} + +int CpuPlayer::get_command(int cur_score) +{ + // int cur_score = 0;// game->getCurrentPlayerScore(); + std::this_thread::sleep_for(std::chrono::seconds(1)); // thinking + + if (cur_score < 20) { + return COM_ROLL; + } + return COM_HOLD; +} + +std::string CpuPlayer::name() +{ + return "CPU"; +} diff --git a/CpuPlayer.h b/CpuPlayer.h new file mode 100644 index 0000000..2257b5e --- /dev/null +++ b/CpuPlayer.h @@ -0,0 +1,19 @@ +//Edward Brown +//PigsGame CPU Player class +#ifndef PLAYER_H +#define PLAYER_H +#include "Player.h" +#include "Io.h" +#include + +//Note for later: implement a difficulty level +class CpuPlayer: public Player +{ +public: + CpuPlayer(); + bool determine_hold(int prev_roll, int cur_score, int roll_num); + virtual int get_command(int); + virtual std::string name(); +}; + +#endif diff --git a/Die.cpp b/Die.cpp new file mode 100644 index 0000000..c4acef6 --- /dev/null +++ b/Die.cpp @@ -0,0 +1,14 @@ +#include // for rand() +#include + +#include "Die.h" + +Die::Die() +{ + srand(time(NULL)); +} + +int Die::roll() +{ + return (rand() % DIE_SIDES) + 1; +} diff --git a/Die.h b/Die.h new file mode 100644 index 0000000..e5d48ee --- /dev/null +++ b/Die.h @@ -0,0 +1,13 @@ +#ifndef DIE_H +#define DIE_H + +#define DIE_SIDES 6 + +class Die +{ + public: + Die(); + static int roll(); +}; + +#endif diff --git a/Game.cpp b/Game.cpp new file mode 100644 index 0000000..667be33 --- /dev/null +++ b/Game.cpp @@ -0,0 +1,119 @@ +#include +#include "Game.h" +#include "Player.h" +#include "CpuPlayer.h" +#include "Io.h" +#include + +using namespace std; + +Game::Game() +{ + m_turn = 0; + // m_pre_roll = 0; + // m_current_player = 1; + + addPlayers(1); + addCpus(1); +} + +//returns which players is currently playing +int Game::whosTurn() +{ + if(m_number_of_players == 0) + { + return 0; + } + return m_current_player; +} + +//gets what the current players score is +int Game::getCurrentPlayerScore() +{ + return m_players[m_current_player]->getScore(); +} + +//takes a number as an argument then adds that number of human players to the game +void Game::addPlayers(int n) +{ + m_number_of_players = n; + for(int i=0; i < m_number_of_players; i++) + { + m_players.push_back(new Player()); + } +} + +//takes a number as an argument and adds that number of computer players to the game +void Game::addCpus(int n) +{ + m_number_of_cpus = n; + for(int i=0; i < m_number_of_cpus; i++) + { + m_players.push_back(new CpuPlayer()); + } +} + +//sends the current turn score to the player so they can add it to their total, then switches player who is taking their turn +void Game::hold() +{ + // if(m_current_player <= m_number_of_players) + // { + m_players[m_current_player]->updateScore(m_current_score); + // return; + // } +} + +//tells the AI player thats currently playing which roll their on, so it can decide wether to hold or roll, returns true if it wants to roll. +bool Game::holdOrRoll() +{ + if(m_number_of_cpus == 0) + { + return false; + } + return true; + // return m_cpus[m_current_player - m_number_of_players - 1]->determine_hold(m_pre_roll,m_current_score,m_turn); +} + +//takes an number for what the dice was rolled. If number is 1, then player looses their turn score. Otherwise roll is added to total +int Game::turnScore(int r) +{ + // if(r == 1) + // { + // // turnChange(); + // m_pre_roll = 0; + // return m_current_score = 0; + // } + m_turn += 1; + m_pre_roll = r; + return m_current_score += r; +} + +void Game::start() +{ + int n = 0; + Io io; + + while(true) { + + m_current_player = n % 2; // change back and forth players (2) + while (io.turn(m_players[m_current_player], m_current_player, m_current_score) != HOLD) + { + int turn = Die::roll(); + if (turn == 1) { + cout << "\t\t\t =( You rolled 1. Nothing scored!" << endl; + m_current_score = 0; + break; + } + cout << "\t\t\tYou rolled: " << turn; + turnScore(turn); + } + hold(); + + if (m_players[m_current_player]->getScore() >= 100) { + io.winner(m_current_player); + return; + } + + n++; + } +} diff --git a/Game.h b/Game.h new file mode 100644 index 0000000..1f508a2 --- /dev/null +++ b/Game.h @@ -0,0 +1,39 @@ +#ifndef GAME_H +#define GAME_H + +#include +using namespace std; + +#include "Player.h" +#include "CpuPlayer.h" +#include "Game.h" +#include "Io.h" + +class Game +{ + public: + Game(); + int whosTurn(); + int getPlayersScore(); + // bool turnChange(); + void addPlayers(int n); + void addCpus(int n); + int getCurrentPlayerScore(); + void setCurrentPlayerScore(int s); + bool holdOrRoll(); + void hold(); + int turnScore(int r); + void start(); + + private: + vector m_players; + // vector m_cpus; + int m_turn; + int m_pre_roll; + int m_current_score; + int m_number_of_players; + int m_number_of_cpus; + int m_current_player; +}; + +#endif diff --git a/Io.cpp b/Io.cpp new file mode 100644 index 0000000..cd1844b --- /dev/null +++ b/Io.cpp @@ -0,0 +1,83 @@ +//Thomas Russel Carrel +#include "Io.h" + +#include +using namespace std; +//////////////////////////////////////////////////////////////////////////////// +// +// Tracks the number of Io objects created, prevents the creation of more than +// one. +// +//////////////////////////////////////////////////////////////////////////////// +// Io::int io_num = 0; + +//////////////////////////////////////////////////////////////////////////////// +//PRIVATE ctor, prevents creation of Io class by any class other than game. +//////////////////////////////////////////////////////////////////////////////// +// +// Io( Game* g ); +// +// +//////////////////////////////////////////////////////////////////////////////// +Io::Io() +{ + // if( io_num > 0 ) //Prevent creation of more than one io object. + // { + // return; + // } + // + // io_num++; + + //########################################################################### + // Code for player's initial setup choices might go here. Things like + // difficulty and number of sides on the die. + //########################################################################### +} + +int Io::turn(Player* player, int player_num, int cur_score) +{ + unsigned char command = 'X'; + + while ( command != 'r' && command != 'h' ) + { + cout << "\t\t\tTotal Score: " << player->getScore() << endl; + cout << "\t\t\tTurn Score: " << cur_score << endl; + cout << player->name() << ":\n\t(R)oll or (H)old? "; + + switch( player->get_command(cur_score) ) //Human player should return COM_NOT_CPU + { // This makes the cpu look like it's entering commands as well. + case COM_HOLD: + cout << "h" << endl; + command = 'h'; + break; + case COM_ROLL: + cout << "r" << endl; + command = 'r'; + break; + case COM_NOT_CPU: + //fallthrough. + default: + cin >> command; + } + + if( 'A' >= command && command <= 'Z' ) //Switch to lowercase, if necessary. + { + command += 32; + } + } + + if( command == 'h' ) + { + return HOLD; + } + + // Die die; // DIIIIIEEEEEEEEEEE!!!!!!!!!!!! + + return COM_ROLL; //die.roll(); // Roll the die and return its result. +} + +void Io::winner(int player_num) +{ + cout << "Player number " << player_num + << " has reached 100 points for the win!"; +} diff --git a/Io.h b/Io.h new file mode 100644 index 0000000..3ea065d --- /dev/null +++ b/Io.h @@ -0,0 +1,43 @@ +// Thomas Russel Carrel +// +// io.h +// +// Class handles input from the player and sends it to the game's main class. +// The game's main class performs its own actions and returns appropriate +// output to the screen + +#ifndef _Io_H +#define _Io_H + +#include +#include "Player.h" +#include "Die.h" + +const int HOLD = -1; + +enum Commands +{ + COM_NOT_CPU, + COM_HOLD, + COM_ROLL +}; + +class Io +{ + public: + + // Returns HOLD if the player decides to hold, otherwise, returns the + //value from the die. + int turn( Player*, int player_num, int score); + + void winner(int); // Displays a victory message. + + friend class Game; + private: + Io(); // Constructor can only be called by the game class. Prevents other + // classes from creating their own instance of this class. + + // static int io_num = 0; // To prevent multiple instances. +}; + +#endif diff --git a/Player.cpp b/Player.cpp new file mode 100644 index 0000000..5d2c05c --- /dev/null +++ b/Player.cpp @@ -0,0 +1,26 @@ +#include +#include "Player.h" + +using namespace std; + +Player::Player() +{ + mScore = 0; +} + +int Player::get_command(int r) { + return -1; // human player +} + +void Player::updateScore(int value) +{ + mScore += value; +} + +int Player::getScore() { + return mScore; +} + +std::string Player::name() { + return "Player"; +} diff --git a/Player.h b/Player.h new file mode 100644 index 0000000..c5adcf9 --- /dev/null +++ b/Player.h @@ -0,0 +1,17 @@ +#ifndef PLAYER_H_INCLUDED +#define PLAYER_H_INCLUDED +#include + +class Player +{ + public: + Player(); + void updateScore(int value); + int getScore(); + virtual int get_command(int); + virtual std::string name(); + private: + int mScore; +}; + +#endif // PLAYER_H_INCLUDED diff --git a/README.md b/README.md index fd09ab5..e49aab7 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,14 @@ -# PigsGame -Pigs dice game +# Pigs dice game -This is a command-line implementation of Pig. The rules of the game as explaned on Wikipedia are:s +This is a command-line implementation of **Pig**. The rules of the game as explaned on Wikipedia are:s 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. +> 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. -From: https://en.wikipedia.org/wiki/Pig_(dice_game) +From [wikipedia](https://en.wikipedia.org/wiki/Pig_(dice_game)) This implementation is a single player game against a computer player. diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..014ce89 --- /dev/null +++ b/main.cpp @@ -0,0 +1,10 @@ +#include "Game.h" + +int main() +{ + Game game; + + game.start(); + + return 0; +}