Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
c76c77e
Updates README.md for better formatting
kbuffardi Sep 9, 2015
e68cfa7
Created Die class header and declared its members/methods
ebracho Sep 9, 2015
daa0f10
Implemented Die::roll()
ebracho Sep 9, 2015
e2af2cf
added structure Player class
Sep 9, 2015
15a8c2b
added structure Player class
Sep 9, 2015
d95136f
created outline for game class
JordanSalvagno Sep 9, 2015
b64d00e
Update Player.h
pedrosobral Sep 9, 2015
a36af16
Die constructor now seeds the rng.
ebracho Sep 14, 2015
f59fe54
added getScore
Sep 14, 2015
ad71a03
Merge remote-tracking branch 'origin/player' into player
Sep 14, 2015
caddbda
Update Player.cpp
pedrosobral Sep 14, 2015
6bbdc08
Created CPU class, a derivative at player that has decision-making ca…
Sep 15, 2015
9ed9629
created functions to handle what I believe the game class is doing, u…
JordanSalvagno Sep 15, 2015
b50c9ce
Merge remote-tracking branch 'origin/player' into merge_all
Sep 15, 2015
37f7551
Merge remote-tracking branch 'origin/eddie' into merge_all
Sep 15, 2015
a99cb79
Merge remote-tracking branch 'origin/JordanSalvagno' into merge_all
Sep 15, 2015
6eedf60
Merge remote-tracking branch 'origin/formatting' into merge_all
Sep 15, 2015
518758d
Minor changes: formating coding to CamelCase
Sep 15, 2015
89de580
Added Header Guard
Sep 15, 2015
d2debfa
Remaned class CPU -> CpuPlayer
Sep 15, 2015
5c7c31f
updated Player and Cpu classes and functions
JordanSalvagno Sep 15, 2015
cd541a7
Remaned files, add library vector
Sep 15, 2015
10e84b2
Merge remote-tracking branch 'origin/JordanSalvagno' into merge_all
Sep 15, 2015
bf9b7de
Remaned CPU_Player -> CpuPlayer
Sep 15, 2015
9a85fac
Fixed some typos
Sep 15, 2015
ab1e306
Added undeclared method in .h file
Sep 15, 2015
f480b6d
Fixed undefined class
Sep 15, 2015
8a73f66
Made determine_hold public as it is required in Game
Sep 15, 2015
a54587c
Rename die.cpp to Die.cpp
pedrosobral Sep 15, 2015
adfbe51
Rename die.h to Die.h
pedrosobral Sep 15, 2015
1dd1362
Created main file/function
Sep 15, 2015
44f0963
Created prototype method Start in Game
Sep 15, 2015
88bd4e8
CpuPlayer is a Player, so that we just need one vector of players
Sep 15, 2015
4fac8e7
Fixed typo
Sep 15, 2015
39e6037
Now adding CpuPlayer to vector of players
Sep 15, 2015
0425d23
Fixed getCurrentPlayerScore to use only one vector
Sep 15, 2015
924cdb9
We don't a function to do that
Sep 15, 2015
56bd1c2
Don't need m_cpus anymore
Sep 15, 2015
19fc318
Added Turn method to player
Sep 15, 2015
0c04955
Pushing/Merging Thomas Carrel branch
Sep 16, 2015
7db1a10
Merge branch 'development' into improve_game
Sep 16, 2015
c6a884c
Get rid of compiler errors by brutal force
Sep 16, 2015
23c8801
Made roll static
Sep 16, 2015
9404cb5
Basic IA to decide to hold or die and name method added
Sep 16, 2015
ad64051
Added default get_comand and name
Sep 16, 2015
dfdd974
Defined start method
Sep 16, 2015
c406fba
Basicaly remove Die from IO and get_command(cur_score)
Sep 16, 2015
07f460a
Merge branch 'improve_game' into development
Sep 16, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions CpuPlayer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "CpuPlayer.h"
#include <chrono>
#include <thread>

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";
}
19 changes: 19 additions & 0 deletions CpuPlayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//Edward Brown
//PigsGame CPU Player class
#ifndef PLAYER_H
#define PLAYER_H
#include "Player.h"
#include "Io.h"
#include <string>

//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
14 changes: 14 additions & 0 deletions Die.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdlib.h> // for rand()
#include <time.h>

#include "Die.h"

Die::Die()
{
srand(time(NULL));
}

int Die::roll()
{
return (rand() % DIE_SIDES) + 1;
}
13 changes: 13 additions & 0 deletions Die.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef DIE_H
#define DIE_H

#define DIE_SIDES 6

class Die
{
public:
Die();
static int roll();
};

#endif
119 changes: 119 additions & 0 deletions Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include <vector>
#include "Game.h"
#include "Player.h"
#include "CpuPlayer.h"
#include "Io.h"
#include <iostream>

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++;
}
}
39 changes: 39 additions & 0 deletions Game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef GAME_H
#define GAME_H

#include <vector>
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<Player*> m_players;
// vector<CpuPlayer*> 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
83 changes: 83 additions & 0 deletions Io.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//Thomas Russel Carrel
#include "Io.h"

#include <iostream>
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!";
}
43 changes: 43 additions & 0 deletions Io.h
Original file line number Diff line number Diff line change
@@ -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 <cstdlib>
#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
Loading