This is a game of dice code; It needs to be able to print result of the game for
ID: 3662767 • Letter: T
Question
This is a game of dice code; It needs to be able to print result of the game for both players. Everytime I run the code it shows draw, I cannot figure out how to make a pointer to throwdice() function. in this program LoadedDie suppose to inhereted from Die. Please Help.
#include "gameAdmin.hpp"
#include <iostream>
#include <ctime>
int main()
{
srand(std::time(0));
std::cout << "Do you want Player 1 to use a loaded die? press 1 for yes 2 for no"<<endl;
int c;
std::cin >> c;
std::cout << "Do you want Player 2 to use a loaded die? press 1 for yes 2 for no"<<endl;
int a;
int rounds=-1;
std::cin>>a;
std::cout << "How many rounds would you like to play? ";
std:cin>>rounds;
Game p(rounds, 6, c, a);
p.play();
return 0;
}
#include "gameAdmin.hpp"
#include "die.hpp"
#include "loadedDie.hpp"
Game::Game(int rds, int sides, int c, int a)
{
int rd=1;
rd=1;
s1=0;
s2=0;
if (c==1&&a==1)
{
p1= new LoadedDie(sides);
p2= new LoadedDie(sides);
}
else if (c==2&&a==1)
{
p1=new Die(sides);
p2=new LoadedDie(sides);
}
else if(c==1&&a==2)
{
p1=new LoadedDie(sides);
p2=new Die(sides);
}
else(c==2&&a==2)
{
p1=new Die(sides);
p2=new Die(sides);
}
}
Game::~Game()
{
delete p1, p2;
}
void Game::round()
{
cout << "Round " << rd << ": Player 1: "<<endl;
while(rd<=rds)
{
int r1 = p1->throwdice();
cout<<r1<<endl;
cout << " Player 2: "<<endl;
int r2 = p2->throwdice();
cout<<r2<<endl;
++rds;
}
if (r1 > r2)
{
++s1;
}
else if (r1 < r2)
{
++s2;
}
cout << "Player 1 score: " << s1 << ' ' <<
"Player 2 score: " << s2 << ' ';
}
void Game::play()
{
cout << "New game. ";
while (rd <= 10)
{
round();
}
print();
}
void Game::print()
{
cout << "Result: ";
if (s1 > s2)
{
cout << "Player 1 wins! ";
}
else if (s1 < s2)
{
cout << "Player 2 wins! ";
}
else
{
cout << "It's a draw! ";
}
}
#include "die.hpp"
#include <cstdlib>
using namespace std;
Die::Die(int sides)
{
int numsides1=sides;
}
int Die::throwdice()
{
int o = rand() % numsides1 + 1;
std::cout << " " << o << ' ';
return o;
}
#include <iostream>
#include <cstdlib>
#include <ctime>
#ifndef DIE_HPP
#define DIE_HPP
using namespace std;
class Die
{
private:
int numsides1;
public:
Die(int sides);
virtual int throwdice();
};
#endif
#ifndef GAMEADMIN_HPP
#define GAMEADMIN_HPP
#include "die.hpp"
#include "loadedDie.hpp"
using namespace std;
class Game
{
private:
int s1;
int s2;
Die *p1;
Die *p2;
int rd;
int r1;
int r2;
public:
Game(int, int, int, int);
~Game();
int rds;
void play();
void round();
void print();
};
#endif
#ifndef LOADEDDIE_HPP
#define LOADEDDIE_HPP
#include "die.hpp"
#pragma once
class LoadedDie: public Die
{
private:
int numsides;
public:
LoadedDie(int sides);
virtual int throwdice();
};
#endif
#include "loadedDie.hpp"
#include <cstdlib>
#include <iostream>
using namespace std;
LoadedDie::LoadedDie(int sides):Die(sides)
{
int numsides=sides;
}
int LoadedDie::throwdice()
{
if (rand()%2==0)
{
int j=0;
j=rand()%2+1;
cout<< "roll was "<<j<<endl;
return j;
}
else
{
int u=0;
u=rand()%numsides+1;
std::cout<<"roll was "<<u<<endl;
return u;
}
}
Explanation / Answer
Answer:
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main()
{
int gamescore1=0;
int gamescore2=0;
int temperoryscore=0;
int playdice=0;
int chance =1;
char characterdata = 'y';
char player;
bool lastchance = false;
bool lastPlay = false;
srand(time(NULL));
cout << "Do you want to play against a player (p) if yes press p.Thank you :) : ";
cin >> player;
do {
while (characterdata == 'y'){
cout << "Player " << chance << ": Your Turn" << endl;
playdice = rand()%6 + 1;
cout << "playdice :" << playdice << endl;
if (playdice != 1 ){
temperoryscore = temperoryscore + playdice;
if (player == 'p'){
cout << "Your current score is : " << temperoryscore << endl << "Would you like to continue (y/n)?: "<< endl;
cin >> characterdata;
}else if (player == 'b' && chance == 2){
if((rand()%2 + 1) == 1){
characterdata = 'y';
}else if ((rand()%2 + 1) == 2){
cout << "Bot is ending turn..." << endl;
characterdata = 'n';
}
}
}else if (playdice == 1){
temperoryscore = 0;
cout << "Sorry, your score is 0 and you must end your turn." << endl;
characterdata = 'n';
}
}
if (chance == 1){
gamescore1 = gamescore1 + temperoryscore;
temperoryscore = 0;
chance = 2;
}else if (chance == 2){
gamescore2 = gamescore2 + temperoryscore;
temperoryscore = 0;
chance = 1;
}
cout <<endl<< "Score [Player: " << gamescore1 << " Player2: " << gamescore2 <<"]"<< endl <<endl;
characterdata = 'y';
if (gamescore1 >= 100 || gamescore2 >= 100){
if (lastchance == true){
lastPlay = true;
}
if (lastchance == false){
lastchance = true;
if (gamescore1 >= 100){
cout << "Player1 is winning! Player2, you have one last chance to take the game in !!!!!!!!" << endl;
}else if (gamescore2 >= 100){
cout << "Player2 is winning! Player1, you have last chance to take the game in !!!!!!!!" << endl;
}
}
}
}while (lastPlay == false);
if (gamescore1 > gamescore2){
cout << endl << " Player1, you won the game!" << endl << endl;
}else if (gamescore1 < gamescore2){
cout << endl << "Player2, you won the game!" << endl << endl;
}else if (gamescore1 == gamescore2){
cout << endl << "Tie Break !!!!!!!!! Player1 and Player2, you have tied the game!" << endl << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.