Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5cb317f
added a main to take in the number of sides
albertoarmienta Sep 9, 2015
4645d78
Created dice.cpp and .h as well as created dice class.
adampenn Sep 9, 2015
4cb37fb
Set up the header file for the Game class
mymercurialsky Sep 9, 2015
a6d1748
Initial pass of basic computer class functionality
atheri Sep 9, 2015
081d97d
Finished the dice class, just need a main to finish testing it.
adampenn Sep 9, 2015
8897d9e
Fixed compilation errors in the computer class
atheri Sep 9, 2015
b6f19f6
added make file
albertoarmienta Sep 9, 2015
d19d3c0
Started implementation of game.cpp and modified game.h
mymercurialsky Sep 9, 2015
dbede12
Added parameters to Computer's turn function. turn(num of dice rolled…
atheri Sep 9, 2015
25600bd
Incomplete PlayerClass
Sep 9, 2015
79f443b
Fleshed out text interface mechanics, score reporting and victory check.
mymercurialsky Sep 10, 2015
e4c6f38
Fixed compilation errors in game.cpp and added new lines to text repo…
mymercurialsky Sep 13, 2015
b446b3a
Updated with functions of counting scores in Player.cpp.
Sep 14, 2015
57871de
Player.h with new functions
Sep 14, 2015
6fbf88d
Player.h with new functions
Sep 14, 2015
5f56c2a
added define's to dice.cpp and made rand genreate correct number
adampenn Sep 14, 2015
5b54179
adding () to dice value
adampenn Sep 14, 2015
e9e0685
Added a simple algorithm for determining whether the computer should …
atheri Sep 14, 2015
9fdaf95
main now creates a Game object to run the actual game
albertoarmienta Sep 14, 2015
2be5474
updated with the requested functions
Sep 14, 2015
a042ada
updated with the requested functions
Sep 14, 2015
cd5917f
updated with the requested functions
Sep 14, 2015
a77b47a
adding dice to master
adampenn Sep 14, 2015
b4c91d1
updated with requested player functions
Sep 14, 2015
3e1eab3
Merge branch 'berto-main'
mymercurialsky Sep 14, 2015
c68df75
updated with requested player functions
Sep 14, 2015
92b6b5d
updated with requested player functions
Sep 14, 2015
29c8a55
Merge branch 'computer'
mymercurialsky Sep 14, 2015
bbb2024
Merge branch 'PlayerClass'
mymercurialsky Sep 14, 2015
31e6bd5
Merged all branches and checked for compiler errors.
mymercurialsky Sep 14, 2015
382efa8
Fixed the computer not intializing m_running_chance. Added logic for …
atheri Sep 14, 2015
bc180d8
Renamed Player.cpp and Player.h to be consistent with other file name…
mymercurialsky Sep 14, 2015
2d48cb7
Merge branch 'computer'
atheri Sep 14, 2015
253f747
Fixed the computer never holding. Integer division was causing the st…
atheri Sep 14, 2015
412efd6
Fixed bug where computer never held in computer.cpp.
mymercurialsky Sep 14, 2015
c813258
Removed test couts from computer.cpp.
mymercurialsky Sep 14, 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
33 changes: 33 additions & 0 deletions computer.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
17 changes: 17 additions & 0 deletions computer.h
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions dice.cpp
Original file line number Diff line number Diff line change
@@ -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;
}


16 changes: 16 additions & 0 deletions dice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef DICE_H
#define DICE_H

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

class Dice {
public:
Dice(int size);
int Roll();
private:
int size_;
};
#endif //DICE_H
Expand Down
177 changes: 177 additions & 0 deletions game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#include "player.h"
#include "computer.h"
#include "dice.h"
#include "game.h"
#include <iostream>
#include <string>
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; i<m_numPlayers; i++) delete m_players[i];
delete[] m_players;

delete m_die;
delete m_computer;

}

void Game::run(){

while (!m_gameWon){

m_roundScore = 0;

Player *player = m_players[m_currentPlayer];

if (m_isPlayerTurn) playerTurn();
else computerTurn();

int score = m_isPlayerTurn ? player->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();

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;

}

}

}
30 changes: 30 additions & 0 deletions game.h
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<iostream>
#include<string>
#include "game.h"

using namespace std;
int main()
{
string name;
int diceSides;
cout<<"What is your name?"<<endl;
cin>>name;
cout<<"How many sides does this Die have?"<<endl;
cin>>diceSides;

Game game(1, diceSides, name);
game.run();
}
29 changes: 29 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "player.h"
#include "dice.h"

#include<iostream>

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

#include <iostream>


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