Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4bf4cbe
Adding header files for player and computer.
Sep 9, 2015
da52374
player class
Sep 9, 2015
b33d918
implemented header file
Sep 9, 2015
f6a2f73
test commit
Icebramz Sep 14, 2015
57a86e6
Added in header and cpp files for the computer class.
Sep 14, 2015
59ee3c9
Merge branch 'master' of https://github.com/Icebramz/PigsGame into JT…
Sep 14, 2015
d06038c
made class
Icebramz Sep 14, 2015
ea8adae
made class
Icebramz Sep 14, 2015
2aff0b0
renamed the main file to match others
Icebramz Sep 14, 2015
dc59954
renamed the main file to match others
Icebramz Sep 14, 2015
9515f90
Merge pull request #1 from Icebramz/JTpigsGame
Sep 14, 2015
be1d73b
test commit
cyberjoey Sep 14, 2015
fc93351
added functionality to player_roll function
Icebramz Sep 14, 2015
0a4c7d6
player can roll and hold, adds running total and doesnt add anything …
Icebramz Sep 14, 2015
8343b29
Delete pigsgame.cpp
Icebramz Sep 14, 2015
a8a0ddb
Delete pigsgame.h
Icebramz Sep 14, 2015
db5708a
Delete player.h
Icebramz Sep 14, 2015
40ea87a
Merge branch 'piggame_icebramz'
Icebramz Sep 14, 2015
7c51f29
created Makefile and renamed Person.h and .cpp to person.h and .cpp
Icebramz Sep 14, 2015
c10728c
added functionality to computer_roll() function
Icebramz Sep 15, 2015
3d6299b
Test edit
cyberjoey Sep 15, 2015
e89ecb2
test
Icebramz Sep 15, 2015
3f0ece9
testing
Icebramz Sep 15, 2015
aa9c86a
up to date with master
Icebramz Sep 15, 2015
3733cf1
Delete pigsgame.cpp
Icebramz Sep 15, 2015
fb95ba0
added piggame.cpp
Icebramz Sep 15, 2015
62945ce
made comment person.h
Icebramz Sep 15, 2015
605875a
deleted bad
Icebramz Sep 15, 2015
dfaef41
Completed the computer AI and modified the person and pigsgame files …
Sep 15, 2015
89f7405
int i in person.h
Icebramz Sep 15, 2015
e732d52
changed master to Johnnys branch
cyberjoey Sep 15, 2015
3e92e0c
fixed initialization issue that was causing segfault
Icebramz 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
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
all: pig

pig: pigsgame.o person.o computer.o
g++ pigsgame.o person.o computer.o -o pig

pigsgame.o: pigsgame.cpp
g++ -c pigsgame.cpp

person.o: person.cpp
g++ -c person.cpp

computer.o: computer.cpp
g++ -c computer.cpp

clean:
rm *o pig
57 changes: 57 additions & 0 deletions computer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "computer.h"
#include "person.h"

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

using namespace std;

Computer::Computer()
{
computer_total_score = 0;
target_score = 12;
}

void Computer::play_game()
{

int this_roll;
int this_turn = 0;
bool playing = true;

cout << "Computer is now playing!" << endl;

while(playing){
this_roll = computer_roll();
this_turn += this_roll;

if(this_roll == 1){
this_turn = 0;
cout << "Computer rolled a 1, no points were added for the current turn"
<< endl;
playing = false;
}

else if (this_turn >= target_score){
cout << "Computer rolled a: " << this_roll << endl
<< "Computer decided to hold" << endl;
playing = false;
}
else{
cout << "Computer rolled a: " << this_roll << endl
<< "Computer's score for this round is: " << this_turn << endl;
}
}
computer_total_score += this_turn;
cout << "Computer's total score is: " << computer_total_score << endl;
}

int Computer::computer_roll()
{
int dice_roll;
srand(time(NULL));
dice_roll = (rand()%6) + 1;
return dice_roll;
}
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();
void play_game();
int computer_roll();
int getScore(){return computer_total_score;}
private:
int computer_total_score;
int target_score;

};

#endif
71 changes: 71 additions & 0 deletions person.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "person.h"
#include "computer.h"

#include<iostream>
#include<string>
#include<assert.h>
#include<stdlib.h>
#include<time.h>

using namespace std;


Person::Person()
{
player_total_score = 0;
}

void Person::play_game()
{
string initial_input;
int roll_value = 0;
int current_roll = 0;
int running_total = 0;


cout << "Player, your total score is: " << player_total_score << endl;
cout << "Player, would you like to roll, or will you hold?" << endl;
getline(cin, initial_input);

while(initial_input != "hold" && current_roll == 0)
{
current_roll = player_roll();

cout << "You rolled a: " << current_roll << endl;
if(current_roll == 1)
{
break;
}
running_total += current_roll;

cout << "So far, your score for this round is: " << running_total << endl;
cout << "Player, would you like to roll, or will you hold?" << endl;
getline(cin, initial_input);
current_roll = 0;
continue;
}

if(current_roll == 1)
{
cout << "You rolled a 1, no points added for current turn" << endl;
running_total = 0;
player_total_score += running_total;
cout << "Your score is still: " << player_total_score << endl;
}

if(initial_input == "hold")
{
cout << "player decided to hold" << endl;
player_total_score += running_total;
cout << "Your score is: " << player_total_score << endl;
}
}

int Person::player_roll()
{
int dice_roll;
srand(time(NULL));
dice_roll = (rand()%6) + 1;
return dice_roll;
}

20 changes: 20 additions & 0 deletions person.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef PERSON_H
#define PERSON_H

#include<iostream>


class Person
{
public:
Person();
void play_game();
int player_roll();
int getScore(){return player_total_score;}

private:
int player_total_score;

};

#endif
28 changes: 28 additions & 0 deletions pigsgame.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "person.h"
#include "computer.h"

#include<iostream>
#include<string>

using namespace std;

int main()
{
Person p1;
Computer c1;
bool playing = true;

while(playing){
p1.play_game();
if(p1.getScore() >= 100){
cout << "Congradulations you beat the computer!" << endl;
playing = false;
break;
}
c1.play_game();
if(c1.getScore() >= 100){
cout << "Too bad the computer beat you! Try again!" << endl;
playing = false;
}
}
}