This program must be written in C++_ Battle Dice The following paragraphs descri
ID: 3757036 • Letter: T
Question
This program must be written in C++_
Battle Dice
The following paragraphs describe the rules of the game.
The game begins with m players tossing n dice.
The sum of the spots on the uppermost faces of the dice are a player’s points. The players add their points to their points total.
The player with the most points wins the game. If several players have the most points, then several players win the game. The winning players add 1 to their wins count.
The player with the least points loses the game. If several players have the least points, then the players with the least total points lose the game and are removed from the list of players.
Some players neither win nor lose.
The players play the game until only 1 player remains and that player is the winner.
Write a program that plays battle dice.
Read the number of players and the number dice from a file.
Use the default_random_engine and uniform_int_distribution classes to simulate tossing the dice.
Define a player class. What is the player class’s data?
Declare a vector of players.
Use a while loop to play the game.
read m, n
create m players
while players’ size > 0
for each player
for roll = 1 to n
roll dice
add dice faces to sum
end for
player.setsum(sum)
end for
winners := computeWinners(players)
Add1ToWins(winners)
losers := computeLosers(players)
removeLosers(players, losers)
end while
Explanation / Answer
dependencies.h
#ifndef __dependencies_H_INCLUDED__
#define __dependencies_H_INCLUDED__
using namespace std;
//roll the dice
int rollDice(int diceType) {
//var declaration
int diceSide = 0;
//all the dice types
if (diceType == 4) diceSide = round(rand() % (4));
if (diceType == 6) diceSide = round(rand() % (6));
if (diceType == 8) diceSide = round(rand() % (8));
if (diceType == 10) diceSide = round(rand() % (10));
if (diceType == 12) diceSide = round(rand() % (12));
if (diceType == 20) diceSide = round(rand() % (20));
//return the side the dice landed on
return diceSide;
}
//player class
class Player {
public:
//variable declaration
string name;
string classType;
int strength, endurance;
int id, DT = 20; //DT is dice type by the way
//get players health
int getHP() {
return (10 + ((strength + endurance) * 2));
}
//get players hit
int getHit() {
return rollDice(DT);
} //get damage
};
//Enemies
class Enemy {
public:
//var declaration
string name;
double AC; //armor class ablity to resist hits
int DT; //dice used to attack
int eid; //id for Enemies (Enemy id)
int HP = round(HP*(1+AC)); //should I have done this with the player class? Eh to late now
int getHit() {
return rollDice(DT);
} //get damage
};
//Get context of the situation for enemy fight
string enemyContxt(int option) {
//make random numbers so the fight stuff seems 'random'
int randNum1 = round(rand() % (9) + 1);
int randNum2 = round(rand() % (9) + 1);
int randNum3 = round(rand() % (9) + 1);
int randNum4 = round(rand() % (9) + 1);
int randNum5 = round(rand() % (9) + 1);
//names for enemies
string name1 = " Bruneor the ";
string name2 = " Richard the ";
string name3 = " Filbert the ";
string name4 = " Lodric the ";
string name5 = " Ruuker the ";
string name6 = " Kruger the ";
string name7 = " Charles the ";
string name8 = " Aaarl the " ;
string name9 = " Vasiilk the ";
string name10 = " Gubl the ";
//introduction of situation strings
string intro1 = " hits you with a blunt slinky ";
string intro2 = " whacks you with a feather ";
string intro3 = " pushes you into Tiny Tim ";
string intro4 = " stabs you with a lamp ";
string intro5 = " shoots you with an M16 catapult ";
string intro6 = " summons a spirit to pester you ";
string intro7 = " uses a rune-stothe enchantment ";
string intro8 = " tries to curse you but explodes an unfourtunate chicken, due to a terrible mispronuncation of your name ";
string intro9 = " simply does nothing ";
string intro10 = " burps up a gnerm (a miniature knome) ";
//strings to help transition to next statement
string trans1 = "and says 'Die, filthy swine!' ";
string trans2 = "then trips on a gruubliyth. ";
string trans3 = "and then, snarls! ";
string trans4 = "and then begins to mutter an ancient curse! ";
string trans5 = "then yells 'You hit like a Kerbb hehe!' ";
string trans6 = "and says 'Die, filthy swine!' ";
string trans7 = "then trips on a gruubliyth. ";
string trans8 = "and then, snarls! ";
string trans9 = "and then begins to mutter an ancient curse! ";
string trans10 = "then yells 'You hit like a Uerbb hehe!' ";
//check if user selected an option then use random number var to decide string and return that string
if (option == 1) {
if (randNum1 == 1) return name1;
if (randNum1 == 2) return name2;
if (randNum1 == 3) return name3;
if (randNum1 == 4) return name4;
if (randNum1 == 5) return name5;
if (randNum1 == 6) return name6;
if (randNum1 == 7) return name7;
if (randNum1 == 8) return name8;
if (randNum1 == 9) return name9;
if (randNum1 == 10) return name10;
}
if (option == 2) {
if (randNum2 == 1) return intro1;
if (randNum2 == 2) return intro2;
if (randNum2 == 3) return intro3;
if (randNum2 == 4) return intro4;
if (randNum2 == 5) return intro5;
if (randNum2 == 6) return intro6;
if (randNum2 == 7) return intro7;
if (randNum2 == 8) return intro8;
if (randNum2 == 9) return intro9;
if (randNum2 == 10) return intro10;
}
if (option == 3) {
if (randNum3 == 1) return trans1;
if (randNum3 == 2) return trans2;
if (randNum3 == 3) return trans3;
if (randNum3 == 4) return trans4;
if (randNum3 == 5) return trans5;
if (randNum3 == 6) return trans6;
if (randNum3 == 7) return trans7;
if (randNum3 == 8) return trans8;
if (randNum3 == 9) return trans9;
if (randNum3 == 10) return trans10;
}
}
//Get context of the situation
string playerContxt(Player &player) {
int randNum = round(rand() % (19) + 1);
//pretty much the same thing with the finction above
string name = player.name;
if (randNum == 1) return " " + name + " strikes with an evil Urrgleumbeck ";
if (randNum == 2) return " " + name + " hits, but epicly fails and hits a wall causing a rupture in time itself ";
if (randNum == 3) return " " + name + " trips on an explosive turtle ";
if (randNum == 4) return " " + name + " lunges at his enemy ";
if (randNum == 5) return " " + name + " sneezes violently causing a worldwide pandemic ";
if (randNum == 6) return " " + name + " swiftly hacks at his enemy using a knerm ";
if (randNum == 7) return " " + name + " summons the almighty mega-knerm ";
if (randNum == 8) return " " + name + " summons a crude writhe-golem ";
if (randNum == 9) return " " + name + " casts an ancient curse";
if (randNum == 10) return " " + name + " yells 'AVADA CADABRA!' ";
if (randNum == 11) return " " + name + " falls painfully ";
if (randNum == 12) return " " + name + " throws a strauug gas grenade ";
if (randNum == 13) return " " + name + " fires a portable villkreek mortar ";
if (randNum == 14) return " " + name + " strikes with a pirated knerm sword ";
if (randNum == 15) return " " + name + " drinks a super-enchantment giving him the ability to eat apples 10 times faster than normal ";
if (randNum == 16) return " " + name + " summons Tiny Tim who calls upon his liege ";
if (randNum == 17) return " " + name + " calls upon a skeleton to do his bidding";
if (randNum == 18) return " " + name + " strikes with a molten axe";
if (randNum == 19) return " " + name + " hits a tree with his head causing it to fall ";
if (randNum == 20) return " " + name + " calls upon the ancient curse of Ugaar ";
}
//fight an Enemy (option 1)
int fightEnemy(Player &player, Enemy &enemy) {
//declare all of these variables
int eHit = enemy.getHit();
int pHit = player.getHit();
int eHP = enemy.HP;
int pHP = player.getHP();
int playerLastRoll = pHit;
int enemyLastRoll = eHit;
int counter = 0;
string name = enemyContxt(1);
//welcome the user to the arena!
cout << " ->->->->->->->->->->->->->->->->->->->->->->->->->->->->->-> ";
cout << "Welcome to the Arena! ";
cout << "Starting HP: " << player.name << "'s HP: " << pHP << " " << enemy.name << "'s HP: " << eHP << " ";
cout << "Begin Battle! ";
//for loop to fight until the death
for(;;) {
//as long as both the hits are not the same and it isnt the first round we should be ok
while(playerLastRoll == pHit || enemyLastRoll == eHit && counter != 0){
//if it is keep looping this until these variables are different
eHit = enemy.getHit();
pHit = player.getHit();
}
//pause for a second, give the user some xor_eqconst_cast
sleep(1);
//use contxt function and print out message of hit
cout << name << enemy.name << enemyContxt(2) << enemyContxt(3) << " Dealing " << eHit << " Damage! ";
//subtract damage from health
pHP = pHP - eHit;
//is the player dead if statement
if (pHP <= 0) {
//if they are tell the user and break the loop
cout << " " << player.name << " is Dead! ";
cout << enemy.name << "'s HP left: " << eHP << " ";
cout << player.name << "'s HP left: " << pHP << " ";
break;
} else {
//else keep going
sleep(1);
//the same as above just switched around really
cout << " " << playerContxt(player) << " Dealing " << pHit << " Damage! ";
eHP = eHP - pHit;
if (eHP <= 0) {
cout << " " << enemy.name << " is Dead! ";
cout << enemy.name << "'s HP left: " << eHP << " ";
cout << player.name << "'s HP left: " << pHP << " ";
break;
}
}
//make the last roll be the last roll so the while loop can know in the future
playerLastRoll = pHit;
enemyLastRoll = eHit;
counter++;
}
//return function presumably after someone has died (hopefully)
return 0;
}
//fight a Player (option 2)
int fightPlayer(Player &player1, Player &player2) {
//variable declaration
int pHit1 = player1.getHit();
int pHit2 = player2.getHit();
int pHP1 = player1.getHP();
int pHP2 = player2.getHP();
int playerLastRoll1 = pHit1;
int playerLastRoll2 = pHit2;
int counter = 0;
//same thing as the function above just with two players
cout << " ->->->->->->->->->->->->->->->->->->->->->->->->->->->->->-> ";
cout << "Welcome to the Arena! ";
cout << "Starting HP: " << player1.name << "'s HP: " << pHP1 << " " << player2.name << "'s HP: " << pHP2 << " ";
cout << "Begin Battle! ";
for(;;) {
while(playerLastRoll1 == pHit1 || playerLastRoll2 == pHit2 && counter != 0){
pHit1 = player1.getHit();
pHit2 = player2.getHit();
}
sleep(1);
cout << " " << playerContxt(player1) << " Dealing " << pHit1 << " Damage! ";
pHP2 = pHP2 - pHit1;
if (pHP2 <= 0) {
cout << " " << player2.name << " is Dead! ";
cout << player1.name << "'s HP left: " << pHP1 << " ";
cout << player2.name << "'s HP left: " << pHP2 << " ";
break;
} else {
sleep(1);
cout << " " << playerContxt(player2) << " Dealing " << pHit2 << " Damage! ";
pHP1 = pHP1 - pHit2;
if (pHP1 <= 0) {
cout << " " << player1.name << " is Dead! ";
cout << player1.name << "'s HP left: " << pHP1 << " ";
cout << player2.name << "'s HP left: " << pHP2 << " ";
break;
}
}
playerLastRoll1 = pHit1;
playerLastRoll2 = pHit2;
counter++;
}
//kill the function before it becomes sentient!
return 0;
}
//check for every possible combo in each of the vectors
int enemyComboCheck(int id1, int id2, vector<Player> &allPlayers, vector<Enemy> &allEnemies){
//declare iterators and booleans
vector<Player>::iterator player_iter = allPlayers.begin();
vector<Enemy>::iterator enemy_iter;
bool found = false;
// try to find player
while (player_iter != allPlayers.end() && !found) {
if (player_iter->id == id1)
found = true;
else
player_iter++;
}
// when player has been found, try to find enemy
if (found) {
found = false;
enemy_iter = allEnemies.begin();
while (enemy_iter != allEnemies.end() && !found) {
if (enemy_iter->eid == id2)
found = true;
else
enemy_iter++;
}
// if both have been found call function fightEnemy
if (found)
fightEnemy(*player_iter, *enemy_iter);
return 0;
}
return 0;
}
//same as the function above just for two players though
int playerComboCheck(int id1, int id2, vector<Player> &allPlayers){
vector<Player>::iterator player_iter1 = allPlayers.begin();
vector<Player>::iterator player_iter2;
bool found = false;
// try to find player 1
while (player_iter1 != allPlayers.end() && !found) {
if (player_iter1->id == id1)
found = true;
else
player_iter1++;
}
// when player has been found, try to find player 2
if (found) {
found = false;
player_iter2 = allPlayers.begin();
while (player_iter2 != allPlayers.end() && !found) {
if (player_iter2->id == id2)
found = true;
else
player_iter2++;
}
// if both have been found call function fightPlayer
if (found)
fightPlayer(*player_iter1, *player_iter2);
return 0;
}
return 0;
}
//end of header file!
#endif
main.cpp
//all the libraries
#include <iostream>
#include <string>
#include <time.h>
//#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <vector>
#include "dependencies.h"
//so I dont have to type a million std prefixes
using namespace std;
//main function
int main() {
//set a random seed based on the time
srand(time(NULL));
//vector to add all the players and enemies for scanning later on
vector<Player> allPlayers;
vector<Enemy> allEnemies;
//player declaration below
//yourname1
Player yourname1;
yourname1.name = "yourname1";
yourname1.classType = "Scribe";
yourname1.strength = 3;
yourname1.endurance = 6;
yourname1.id = 1;
//push back this player to the vector
allPlayers.push_back(yourname1);
//------------------------------------------
//yourname2
Player yourname2;
yourname2.name = "yourname2";
yourname2.classType = "Cursed";
yourname2.strength = 3;
yourname2.endurance = 3;
yourname2.id = 2;
//push back this player to the vector
allPlayers.push_back(yourname2);
//------------------------------------------
//yourname3
Player yourname3;
yourname3.name = "yourname3";
yourname3.classType = "Thief";
yourname3.strength = 3;
yourname3.endurance = 1;
yourname3.id = 3;
//push back this player to the vector
allPlayers.push_back(yourname3);
//------------------------------------------
//yourname4
Player yourname4;
yourname4.name = "yourname4";
yourname4.classType = "Paladin";
yourname4.strength = 9;
yourname4.endurance = 5;
yourname4.id = 4;
//push back this player to the vector
allPlayers.push_back(yourname4);
//------------------------------------------
//yourname5
Player yourname5;
yourname5.name = "yourname5";
yourname5.classType = "Hobo";
yourname5.strength = 3;
yourname5.endurance = 7;
yourname5.id = 5;
//push back this player to the vector
allPlayers.push_back(yourname5);
//------------------------------------------
//Enemy declaration below
//AmatuerGuard
Enemy AmatuerGuard;
AmatuerGuard.name = "Amatuer Guard";
AmatuerGuard.HP = 10;
AmatuerGuard.AC = 0.05;
AmatuerGuard.DT = 4;
AmatuerGuard.eid = 1;
//push back this enemy to the vector
allEnemies.push_back(AmatuerGuard);
//MediocreGuard
Enemy MediocreGuard;
MediocreGuard.name = "Mediocre Guard";
MediocreGuard.HP = 15;
MediocreGuard.AC = 0.1;
MediocreGuard.DT = 8;
MediocreGuard.eid = 2;
//push back this enemy to the vector
allEnemies.push_back(MediocreGuard);
//Knight
Enemy Knight;
Knight.name = "Knight";
Knight.HP = 20;
Knight.AC = 0.2;
Knight.DT = 12;
Knight.eid = 3;
//push back this enemy to the vector
allEnemies.push_back(Knight);
//OkachiRaider
Enemy OkachiRaider;
OkachiRaider.name = "Okachi Raider";
OkachiRaider.HP = 10;
OkachiRaider.AC = 0.15;
OkachiRaider.DT = 6;
OkachiRaider.eid = 4;
//push back this enemy to the vector
allEnemies.push_back(OkachiRaider);
//OkachiPaladin
Enemy OkachiPaladin;
OkachiPaladin.name = "Okachi Paladin";
OkachiPaladin.HP = 25;
OkachiPaladin.AC = 0.1;
OkachiPaladin.DT = 10;
OkachiPaladin.eid = 5;
//push back this enemy to the vector
allEnemies.push_back(OkachiPaladin);
//OkachiMage
Enemy OkachiMage;
OkachiMage.name = "Okachi Mage";
OkachiMage.HP = 15;
OkachiMage.AC = 0;
OkachiMage.DT = 20;
OkachiMage.eid = 6;
//push back this enemy to the vector
allEnemies.push_back(OkachiMage);
//FrostGiant
Enemy FrostGiant;
FrostGiant.name = "Frost Giant";
FrostGiant.HP = 30;
FrostGiant.AC = 0.2;
FrostGiant.DT = 10;
FrostGiant.eid = 7;
//push back this enemy to the vector
allEnemies.push_back(FrostGiant);
//DuneViper
Enemy DuneViper;
DuneViper.name = "Dune Viper";
DuneViper.HP = 10;
DuneViper.AC = 0.15;
DuneViper.DT = 10;
DuneViper.eid = 8;
//push back this enemy to the vector
allEnemies.push_back(DuneViper);
//Burrowurm
Enemy Burrowurm;
Burrowurm.name = "Burrowurm";
Burrowurm.HP = 10;
Burrowurm.AC = 0.25;
Burrowurm.DT = 8;
Burrowurm.eid = 9;
//push back this enemy to the vector
allEnemies.push_back(Burrowurm);
//Behemoth
Enemy Behemoth;
Behemoth.name = "Behemoth";
Behemoth.HP = 65;
Behemoth.AC = 0.25;
Behemoth.DT = 6;
Behemoth.eid = 10;
//push back this enemy to the vector
allEnemies.push_back(Behemoth);
//RabidSquirrel
Enemy RabidSquirrel;
RabidSquirrel.name = "Rabid Squirrel";
RabidSquirrel.HP = 10;
RabidSquirrel.AC = 0;
RabidSquirrel.DT = 20;
RabidSquirrel.eid = 11;
//push back this enemy to the vector;
allEnemies.push_back(RabidSquirrel);
//Wolf
Enemy Wolf;
Wolf.name = "Wolf";
Wolf.HP = 15;
Wolf.AC = 0.2;
Wolf.DT = 8;
Wolf.eid = 12;
//push back this enemy to the vector
allEnemies.push_back(Wolf);
//DireWolf
Enemy DireWolf;
DireWolf.name = "Dire Wolf";
DireWolf.HP = 20;
DireWolf.AC = 0.2;
DireWolf.DT = 12;
DireWolf.eid = 13;
//push back this enemy to the vector
allEnemies.push_back(DireWolf);
//InsaneHobo
Enemy InsaneHobo;
InsaneHobo.name = "Insane Hobo";
InsaneHobo.HP = 10;
InsaneHobo.AC = 0.05;
InsaneHobo.DT = 12;
InsaneHobo.eid = 14;
//push back this enemy to the vector
allEnemies.push_back(InsaneHobo);
//ForestArcher
Enemy ForestArcher;
ForestArcher.name = "Forest Archer";
ForestArcher.HP = 15;
ForestArcher.AC = 0.15;
ForestArcher.DT = 10;
ForestArcher.eid = 15;
//push back this enemy to the vector
allEnemies.push_back(ForestArcher);
//ForestKnight
Enemy ForestKnight;
ForestKnight.name = "Forest Knight";
ForestKnight.HP = 20;
ForestKnight.AC = 0.1;
ForestKnight.DT = 8;
ForestKnight.eid = 16;
//push back this enemy to the vector
allEnemies.push_back(ForestKnight);
//ForestKnight
Enemy ForestMage;
ForestMage.name = "Forest Mage";
ForestMage.HP = 10;
ForestMage.AC = 0.15;
ForestMage.DT = 10;
ForestMage.eid = 17;
//push back this enemy to the vector
allEnemies.push_back(ForestMage);
//MurderOfCrows
Enemy MurderOfCrows;
MurderOfCrows.name = "Murder Of Crows";
MurderOfCrows.HP = 10;
MurderOfCrows.AC = 0.1;
MurderOfCrows.DT = 10;
MurderOfCrows.eid = 18;
//push back this enemy to the vector
allEnemies.push_back(MurderOfCrows);
//core
//variable declaration
int choice = 0;
//ask user to select function
cout << "Please select a function: ";
cout << "1) Fight Against an Enemy ";
cout << "2) Fight Against Another Player ";
cout << "Response: ";
cin >> choice; //take in the response to the choice variable
//not sure why I didn't just use an if statement for these
switch (choice) {
//check if choice is 1
case 1:
//uid stands for user input id
int uid1;
int uid2;
//ask for player id
cout << " Enter your Id (1-5): ";
cin >> uid1; //take it in to uid1
//ask for enemy id
cout << " Enter your Enemies Id (1-18): ";
cin >> uid2; //take it in to uid2
//call the ingenious function to check all possiblilities of enemies and players
enemyComboCheck(uid1, uid2, allPlayers, allEnemies);
break;
case 2:
//same thing as above but just with another player instead of an enemy
cout << " Enter Player 1's Id (1-5): ";
cin >> uid1;
cout << " Enter Player 2's Id (1-5): ";
cin >> uid2;
//different function too
playerComboCheck(uid1, uid2, allPlayers);
break;
//if you didn't do anything right resort to this
default:
cout << "Input invalid. Bye! ";
return 0;
break;
}
//ask user if they want to run the program again
string endornot;
sleep(1);
cout << " Run again y/n: ";
cin >> endornot;
if (endornot == "y" || endornot == "Y") return main();
if (endornot == "n" || endornot == "N") cout << "Shutting Down... "; return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.