Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
55 changes: 55 additions & 0 deletions computer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "computer.h"
#include <iostream>
using namespace std;



Computer::Computer()
{
totalScore = 0;
}

void Computer::sumScore(int turnScore){
totalScore = totalScore + turnScore;
}

void Computer::printScore(){
cout << "Computer's total score is: " << totalScore << endl;
}

bool Computer::gameOver(){
if(totalScore >= 100){
cout << "Game over, Computer wins!" << endl;
return true;
}
return false;
}

void Computer::turn(){

cout << endl << endl << endl;

d.clearTurnScore();

int roll, runningTotal;
int rollCount = 0;

while(rollCount < 3 && roll != 1)
{
roll = d.roll();

rollCount++;

cout << "Computer rolled: " << roll << endl;

runningTotal = totalScore + d.addToTurnScore(0);

cout << "Computer's running total score is: " << runningTotal << endl;
}

sumScore(d.hold());

cout << "Computer's total score is: " << totalScore << ", and its turn is over." << endl;

cout << endl << endl << endl;
}
23 changes: 23 additions & 0 deletions computer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef COMPUTER_H
#define COMPUTER_H

#include "dice.h"


class Computer
{

public:
Computer();

void sumScore(int turnScore);
void printScore();
bool gameOver();
void turn();

private:
Dice d;
int totalScore;

};
#endif
37 changes: 37 additions & 0 deletions dice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "dice.h"
#include <stdlib.h>
#include <iostream>

using namespace std;

int Dice::roll(){
int curRoll = rand() % 6 +1;

if(curRoll == 1){
clearTurnScore();
rolledOne();
return 1;
}
else{
addToTurnScore(curRoll);
}
return curRoll;
}

int Dice::hold(){
return turnScore;
}

void Dice::clearTurnScore(){
turnScore = 0;
}

int Dice::addToTurnScore(int roll){
turnScore += roll;
return turnScore;
}

void Dice::rolledOne(){
turnScore = 0;
cout << "A 'one' was rolled! Score for turn lost" << endl;
}
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

class Dice{
public:
int roll();
int hold();
void clearTurnScore();
int addToTurnScore(int roll);
int turnScore;
void rolledOne();
private:

};

#endif
31 changes: 31 additions & 0 deletions pigGame.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include "player.cpp"
#include "dice.cpp"
#include "computer.cpp"
using namespace std;

int main(){
Player p1;
Computer c1;

p1.totalScore = 0;
//Computer c1;
Dice d;
string initIn;
cout << "Welcome to pigs game! Type 'ready' to play" << endl;
cin >> initIn;
if(initIn == "ready"){
p1.turn();

c1.turn();
}

while(!p1.gameOver() && !c1.gameOver()){
p1.turn();

if(p1.gameOver())
break;

c1.turn();
}
}
48 changes: 48 additions & 0 deletions player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include "player.h"
using namespace std;


void Player::sumScore(int turnScore){
totalScore = totalScore + turnScore;
}

void Player::printScore(){
cout << "Your score is: " << totalScore << endl;
}

bool Player::gameOver(){
if(totalScore >= 100){
cout << "Game over, Player wins!" << endl;
return true;
}
return false;
}

void Player::turn(){
d.turnScore = 0;
string x;
int roll, runningTotal;
cout << "Roll or hold?" << endl;
cin >> x;
while(x!="hold"){
if(x == "roll"){
roll = d.roll();

if(roll == 1)
break;

cout << "You rolled: " << roll << endl;
runningTotal = totalScore + d.addToTurnScore(0);
cout << "Your running total score is: " << runningTotal << endl;
cout << "Roll or hold?" << endl;
}
if(x!="hold" && x!= "roll"){
cout << "Invalid input. Enter 'roll' or 'hold'.";
}
cin >> x;
}
int score = d.hold();
sumScore(score);
cout << "Your total score is: " << totalScore << ", your turn is now over." << endl;
}
16 changes: 16 additions & 0 deletions player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef PLAYER_H
#define PLAYER_H
#include "dice.h"

class Player{
public:
void sumScore(int turnScore);
void printScore();
bool gameOver();
void turn();
int totalScore;
private:
Dice d;
};

#endif