+1 (315) 557-6473 

Program To Create a Dice Game in C++ Language Assignment Solution.


Instructions

Objective
Write a C++ assignment program to create a dice game.

Requirements and Specifications

Program to create a dice game in C++

Source Code

DICE

#include "Dice.h"

#include

#include

using namespace std;

void Dice ::playDice() {

  for (int i = 0; i < 5; i++) {

    playerDice[i] = 0;

  }

}

void Dice::roll() {

  for (int i = 0; i < 5; i++) {

    playerDice[i] = ((rand() % 6) + 1);

    allDice.push_back(playerDice[i]);

  }

}

void Dice::show() {

  for (int i = 0; i < 5; i++) {

    cout << playerDice[i];

  }

}

void Dice::showAllDice() {

  for (unsigned int i = 0; i < allDice.size(); i++) {

    cout << allDice.at(i);

  }

}

GAME LOOP

#include "GameLoop.h"

#include

using namespace std;

// changes int numPlayers to game

Game::Game(int numPlayers) {

  players = new Player[numPlayers];

  countPlayers = numPlayers;

}

// gets players names

void Game::readPlayerName() {

  string name;

  for (int i = 0; i < countPlayers; i++) {

    cout << "Enter player " << (i + 1) << "'s name : ";

    cin >> name;

    players[i].setName(name);

  }

}

// rolls the dice for all players

void Game::assignDice() {

  for (int i = 0; i < countPlayers; i++) {

    dice.roll();

  }

}

// bet number of dice and the value

void Game::makeBet() {

  int betNumberOfDice;

  int betFaceValueOfDice;

  cout << "Make A Guess!\n";

  cout << "Enter The Number Of Dice You Think There Are : ";

  cin >> betNumberOfDice;

  cout << "Enter The Face Value Of Those Dice : ";

  cin >> betFaceValueOfDice;

  cin.clear();

  cin.ignore();

  int option;

  int countDice = 0;

  int winPoint = 0;

  // asks other player if they accept or call out player

  for (int i = 0; i < sizeof(players[i]); i++) {

    if (i == currentPlayer) {

      continue;

    } else {

      cout << players[i].getName() << ", Do You Accept "

           << players[currentPlayer].getName() << "'s Guess ? \n"

           << "(1) Accept(2) Liar!\n";

      cin >> option;

      if (option == 1) {

        cout << "Accepted\n";

        winPoint = 0;

        players[currentPlayer].setScore(winPoint);

        players[i].setScore(winPoint);

      } else if (option == 2) {

        cout << "LIAR!\n\n";

        int result = 0;

        cout << "All Dice shown : \n";

        dice.showAllDice();

        for (int vecNum : dice.allDice) {

          if (vecNum == betFaceValueOfDice) {

            countDice += 1;

          }

        }

        if (countDice == betNumberOfDice) {

          cout << endl

               << players[currentPlayer].getName() << " Guessed Correctly!\n";

          cout << players[currentPlayer].getName() << "WINS!";

          winPoint = 1;

          players[currentPlayer].setScore(winPoint);

        } else {

          cout << endl

               << players[currentPlayer].getName() << " Is A LIAR!!!!!\n";

          cout << players[i].getName() << " WINS!";

        }

      }

    }

  }

}

// shows who has won the game

bool Game::winner(int *index) {

  for (int i = 0; i < countPlayers; i++) {

    if (players[i].getScore() == 1 || players[currentPlayer].getScore() == 1) {

      *index = i || currentPlayer;

      return true;

    }

  }

  return false;

}

void Game::eachPlayerGame(int i) {

  currentPlayer = i;

  cout << players[i].getName() << " rolled : ";

  dice.show();

  cout << endl;

  makeBet();

  if (players[i].getScore() == 0 && players[currentPlayer].getScore() == 0) {

    cout << "Next Players Turn...";

  }

}

void Game::playEachRound(int index) {

  for (int i = index; i < countPlayers; i++) {

    eachPlayerGame(i);

  }

}

// contenues until a winner is decided

void Game::startGame() {

  int index = 0;

  int count = 0;

  int i = 0;

  while (!winner(&index)) {

    playEachRound(0);

  }

  for (int i = index + 1; i < countPlayers; i++) {

    eachPlayerGame(i);

    count++;

  }

  if (count != countPlayers - 1) {

    for (i = 0; i < index; i++) {

      eachPlayerGame(i);

    }

  }

}

GAME RULES

#include "GameRules.h"

#include

#include

using namespace std;

void Rules::printRules() {

  fstream rulefile;

  string liarsDice;

  rulefile.open("Liar_Dice_Rule.txt");

  // open a file to read

  if (rulefile.is_open())

  // checking to see if the file is open

  {

    string liarsDice;

    while (getline(rulefile, liarsDice))

    // read data from file and put it into string.

    {

      cout << liarsDice << "\n";

      // print the data from the string

    }

    rulefile.close();

    // close the file.

  }

}

LIAR DICE

#include

#include

#include

#include

#include "GameLoop.h"

#include "Player.h"

#include "Dice.h"

#include "GameRules.h"

using namespace std;

 int main()

{

 //generates random numbers

 srand(time(NULL));

 int numPlayers;cout << "\t\t******** WELCOME TO LIAR'S DICE ********\n\n";

 // runs the printRules class to print game rules

 Rules::printRules();

 //gets number of players stores in variable numPlayers

 cout << "How many players will there be? : ";

 cin >> numPlayers;

 // checks for player number, returns message if too few entered

 while (numPlayers < 2)

 {

  cout << "There must be at least 2 players! Try again : ";

  cin >> numPlayers;

 }

 Game game(numPlayers);

 game.assignDice();

 game.readPlayerName();

 game.startGame();

 return 0;

}

PLAYER

#include "Player.h"

#include

using namespace std;

// changes playerName String to Player

Player::Player(string playerName) {

  this->name = playerName;

  point = 0;

}

// changes playerName string setrName

void Player::setName(string playerName) { this->name = playerName; }

string Player::getName() { return name; }

// changes integer point o setScore

void Player::setScore(int point) { this->point = point; }

int Player::getScore() { return point; }