diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c3f79b2 --- /dev/null +++ b/Makefile @@ -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 diff --git a/computer.cpp b/computer.cpp new file mode 100644 index 0000000..07d8854 --- /dev/null +++ b/computer.cpp @@ -0,0 +1,57 @@ +#include "computer.h" +#include "person.h" + +#include +#include +#include +#include + +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; +} diff --git a/computer.h b/computer.h new file mode 100644 index 0000000..74e31bc --- /dev/null +++ b/computer.h @@ -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 diff --git a/person.cpp b/person.cpp new file mode 100644 index 0000000..1bcdaff --- /dev/null +++ b/person.cpp @@ -0,0 +1,71 @@ +#include "person.h" +#include "computer.h" + +#include +#include +#include +#include +#include + +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; +} + diff --git a/person.h b/person.h new file mode 100644 index 0000000..b23b08f --- /dev/null +++ b/person.h @@ -0,0 +1,20 @@ +#ifndef PERSON_H +#define PERSON_H + +#include + + +class Person +{ + public: + Person(); + void play_game(); + int player_roll(); + int getScore(){return player_total_score;} + + private: + int player_total_score; + +}; + +#endif diff --git a/pigsgame.cpp b/pigsgame.cpp new file mode 100644 index 0000000..1eab380 --- /dev/null +++ b/pigsgame.cpp @@ -0,0 +1,28 @@ +#include "person.h" +#include "computer.h" + +#include +#include + +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; + } + } +}