diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c4ec412 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +pigs: Game.o Die.o Player.o Main.o + +Main.o: main.cpp game.h die.h player.h + g++ -c main.cpp + +Game.o: game.cpp game.h die.h player.h + g++ -c game.cpp + +Die.o: die.cpp die.h + g++ -c die.cpp + +Player.o: player.cpp player.h + g++ -c player.cpp \ No newline at end of file diff --git a/die.cpp b/die.cpp new file mode 100644 index 0000000..9e24839 --- /dev/null +++ b/die.cpp @@ -0,0 +1,21 @@ +/* + * die.cpp + * date - 9/9/2015 + */ +#include "die.h" +#include +#include +#include + + +Die::Die() +{ +} + +int Die::roll() +{ + //srand(time(0)); //needs to be called in main once + //and not in a loop + die_result = (rand() % 6 + 1); + return die_result; +} diff --git a/die.h b/die.h new file mode 100644 index 0000000..8e94aa0 --- /dev/null +++ b/die.h @@ -0,0 +1,19 @@ +#ifndef DIE_H +#define DIE_H +#include +using namespace std; + +class Die +{ + public: + + int roll(); // returns a random number from 1-6 + Die(); // Die ctor + + + private: + + int die_result; // actual die roll value + +}; +#endif diff --git a/game.cpp b/game.cpp new file mode 100644 index 0000000..8626cd7 --- /dev/null +++ b/game.cpp @@ -0,0 +1,42 @@ +#include +#include "game.h" + +using namespace std; + +Game::Game() +{ +} + +void Game::turn(Player current_player) +{ + bool player_keeps_playing; + string userInput; + int roll; + Die *test = new Die(); + + while (player_keeps_playing) + { + cout << "Would you like to roll again? (yes or no)" << endl; + cin >> userInput; + if (userInput == "yes") + { + player_keeps_playing = true; + roll = test->roll(); + turn_score += roll; + } + else + { + current_player.update_score(current_player, turn_score); + player_keeps_playing = false; + } + if (roll == 1) + { + turn_score = 0; + player_keeps_playing = false; + } + else if (current_player.update_score(current_player, turn_score) == true) + { + return; + } + } +} \ No newline at end of file diff --git a/game.h b/game.h new file mode 100644 index 0000000..d32125f --- /dev/null +++ b/game.h @@ -0,0 +1,21 @@ +#ifndef GAME_H +#define GAME_H + +#include "player.h" +#include "die.h" + +#include +using namespace std; + +class Game +{ +public: + Game(); + void turn(Player current_player); + +private: + int turn_score; + +}; + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..deadbda --- /dev/null +++ b/main.cpp @@ -0,0 +1,28 @@ +#include "game.h" +#include "player.h" + +using namespace std; + +int main() +{ + Game game; + + Player *player = new Player(); + Player *computer = new Player(); + Player *current_player = player; + + while (player->get_score() < 100 || computer->get_score() < 100) + { + if (current_player == player) + current_player = computer; + else + current_player = player; + game.turn(current_player); + } + if (current_player == computer) + cout << "Game over! Computer beat you!" << endl; + else + cout << "Game over! You beat a computer, you should be proud of yourself." << endl; + + return 0; +} \ No newline at end of file diff --git a/player.cpp b/player.cpp new file mode 100644 index 0000000..71940bc --- /dev/null +++ b/player.cpp @@ -0,0 +1,23 @@ +#include"player.h" +using namespace std; + +Player::Player() +{ + m_score = 0; +} + +bool Player::update_score(Player ¤t_player, int turn_score) +{ + m_score = m_score + turn_score; + + if(current_player.get_score() >= 100) + return true; + else + return false; +} + +int Player::get_score() +{ + return m_score; +} + diff --git a/player.h b/player.h new file mode 100644 index 0000000..870f230 --- /dev/null +++ b/player.h @@ -0,0 +1,14 @@ +#ifndef PLAYER_H +#define PLAYER_H + +class Player +{ + public: + Player(); + bool update_score(Player ¤t_player, int turn_score); + int get_score(); + private: + int m_score; +}; + +#endif