Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NathanWood_Final.cpp : Defines the entry point for the console application.
- //
- #include "Player.h"
- #include "PlayerOptions.h"
- #include "OtherPlayerOptions.h"
- #include "stdafx.h"
- #include "GameLoop.h"
- #include <string>
- #include <fstream>
- #include <iostream>
- #include <ctime>
- using namespace std;
- void Game::WelcomePlayer()
- {
- std::string line;
- std::ifstream myfile("Rules.txt");
- if (myfile.is_open())
- {
- while (getline(myfile, line))
- {
- std::cout << line << '\n';
- }
- myfile.close();
- }
- else std::cout << "Unable to open file";
- std::cout << "\nWelcome to liars dice. How many players are playing?";
- int noOfPlayers;
- std::cin >> noOfPlayers;
- m_noOfPlayers = noOfPlayers;
- if (noOfPlayers > 0) m_player = new Player[noOfPlayers];
- else
- std::cout << "That is not a valid choice";
- }
- void Game::RollDice()
- {
- srand((unsigned int)time(NULL)); //seed the random # generator
- for (int i = 0; i < m_noOfPlayers; ++i)
- {
- for (int j = 0; j < 5; ++j)
- {
- m_player[i].dice[j] = (rand() % 6) + 1;
- }
- }
- }
- void Game::ShowDice(int i)
- {
- string temp;
- std::cout << "Player " << i + 1 << ", your dice are: ";
- std::cout << m_player[i].dice[0] << m_player[i].dice[1];
- std::cout << m_player[i].dice[2] << m_player[i].dice[3];
- std::cout << m_player[i].dice[4] << endl << endl;
- std::cin.ignore();
- std::cout << "Press Enter to Continue..." << endl;
- std::cin.get();
- std::cout << string(40, '\n');
- }
- void Game::GiveOptions(int i) const
- {
- std::cout << string(40, '\n');
- std::cout << "Player " << i + 1 << " choose an option:" << endl << endl;
- std::cout << "1: Show Dice" << endl;
- std::cout << "2: Enter Guess" << endl;
- std::cout << "3: Quit" << endl;
- }
- void Game::GiveOtherOptions(int i)
- {
- std::cout << string(40, '\n');
- std::cout << "the current guess is " << m_prevAmountGuess << " " << m_prevValueGuess << "'s" << endl;
- std::cout << "Player " << (i % m_noOfPlayers) + 1 << " choose an option:" << endl << endl;
- std::cout << "1: Show Dice" << endl;
- std::cout << "2: Pass" << endl;
- std::cout << "3: Call Liar" << endl;
- std::cout << "4: Quit" << endl;
- }
- void Game::GetPlayerInput(string& playerInput) const
- {
- std::cin >> playerInput;
- }
- void Game::GetOtherPlayerInput(string& otherPlayerInput) const
- {
- std::cin >> otherPlayerInput;
- }
- PlayerOptions Game::EvaluateInput(string& playerInput) const
- {
- PlayerOptions chosenOption = PlayerOptions::None;
- if (playerInput.compare("1") == 0)
- {
- std::cout << "You have chosen to view your dice" << endl << endl;
- chosenOption = PlayerOptions::ShowDice;
- }
- else if (playerInput.compare("2") == 0)
- {
- cout << "You have chosen to enter a guess" << endl << endl;
- chosenOption = PlayerOptions::EnterGuess;
- }
- else if (playerInput.compare("3") == 0)
- {
- cout << "You have chosen to quit." << endl << endl;
- chosenOption = PlayerOptions::Quit;
- }
- else
- {
- cout << "I do not recognise that option, try again!" << endl << endl;
- }
- return chosenOption;
- }
- OtherPlayerOptions Game::EvaluateOtherInput(string& otherPlayerInput) const
- {
- OtherPlayerOptions otherChosenOption = OtherPlayerOptions::None;
- if (otherPlayerInput.compare("1") == 0)
- {
- std::cout << "You have chosen to view your dice" << endl << endl;
- otherChosenOption = OtherPlayerOptions::ShowDice;
- }
- else if (otherPlayerInput.compare("2") == 0)
- {
- cout << "You have chosen to enter a guess" << endl << endl;
- otherChosenOption = OtherPlayerOptions::Pass;
- }
- else if (otherPlayerInput.compare("3") == 0)
- {
- cout << "You have Called the previous player a Liar!" << endl << endl;
- otherChosenOption = OtherPlayerOptions::CallLiar;
- }
- else if (otherPlayerInput.compare("4") == 0)
- {
- cout << "You have chosen to quit." << endl << endl;
- otherChosenOption = OtherPlayerOptions::Quit;
- }
- else
- {
- cout << "I do not recognise that option, try again!" << endl << endl;
- }
- return otherChosenOption;
- }
- void Game::EnterGuess()
- {
- do
- {
- if (m_turn > 1) {
- std::cout << "previous guess is " << m_prevAmountGuess << " " << m_prevValueGuess << endl;
- }
- std::cout << "Enter a number of dice" << endl;
- std::cin >> m_currentAmountGuess;
- std::cout << "Enter a value of dice ( 1 through 6 )" << endl;
- std::cin >> m_currentValueGuess;
- if (m_currentAmountGuess <= m_prevAmountGuess) {
- std::cout << "You must guess a number of Dice greater than the previous player. Try again.";
- }
- } while (m_currentAmountGuess <= m_prevAmountGuess);
- m_prevAmountGuess = m_currentAmountGuess;
- m_prevValueGuess = m_currentValueGuess;
- }
- void Game::Liar(int i)
- {
- int counter = 0;
- for (int i = 0; i < m_noOfPlayers; ++i)
- {
- std::cout << "player " << i + 1 << ": ";
- std::cout << m_player[i].dice[0] << m_player[i].dice[1];
- std::cout << m_player[i].dice[2] << m_player[i].dice[3];
- std::cout << m_player[i].dice[4] << endl;
- }
- for (int i = 0; i < m_noOfPlayers; ++i)
- {
- for (int j = 0; j < 5; ++j)
- {
- if (m_player[i].dice[j] == m_prevValueGuess)
- {
- ++counter;
- if (counter == m_prevAmountGuess)
- {
- std::cout << "You win!";
- return;
- }
- }
- }
- }
- std::cout << "You lose!";
- }
- void Game::RunGame()
- {
- WelcomePlayer();
- RollDice();
- m_turn = 1;
- int i = 0;
- bool bShouldEnd = false;
- while (bShouldEnd == false)
- {
- GiveOptions(i);
- string playerInput;
- GetPlayerInput(playerInput);
- PlayerOptions tmpOption = EvaluateInput(playerInput);
- if (tmpOption == PlayerOptions::ShowDice)
- {
- if ( i > m_noOfPlayers ) i = 0;
- ShowDice(i);
- }
- else if (tmpOption == PlayerOptions::EnterGuess)
- {
- string otherPlayerInput;
- int callcounter = 0;
- m_CurrentPlayer = i;
- EnterGuess();
- //m_CurrentPlayer % m_noOfPlayers
- while (callcounter < m_noOfPlayers - 1)
- {
- std::cout << "entered other while loop" << endl;
- if (i != m_CurrentPlayer)
- {
- std::cout << "give other options" << endl;
- GiveOtherOptions(i);
- GetOtherPlayerInput(otherPlayerInput);
- OtherPlayerOptions OthertmpOption = EvaluateOtherInput(otherPlayerInput);
- if (OthertmpOption == OtherPlayerOptions::ShowDice)
- {
- ShowDice(i);
- }
- else if (OthertmpOption == OtherPlayerOptions::Pass)
- {
- ++i;
- ++callcounter;
- }
- else if (OthertmpOption == OtherPlayerOptions::CallLiar)
- {
- std::cout << "A liar has been called!" << endl;
- Liar(i);
- bShouldEnd = true;
- }
- else if (OthertmpOption == OtherPlayerOptions::Quit)
- {
- break;
- bShouldEnd = true;
- }
- }
- else if (i == m_CurrentPlayer)
- {
- ++i;
- }
- if (i == m_noOfPlayers) i = 0;
- }
- //Liar(i);
- i = i + 1;
- if (i >= m_noOfPlayers) {
- i = 0;
- }
- m_turn = m_turn + 1; //increment the turn
- }
- /*else if (tmpOption == PlayerOptions::CallLiar)
- {
- if (i = 1) i = i + m_noOfPlayers;
- std::cout << "You called player " << i - 1 << " a liar! The dice are: " << endl;
- Liar(i);
- bShouldEnd = true;
- }*/
- else if (tmpOption == PlayerOptions::Quit)
- {
- bShouldEnd = true;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement