Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is a game of 21. Is there any way to modify the code to make it more ornam

ID: 3860735 • Letter: B

Question

Below is a game of 21. Is there any way to modify the code to make it more ornamental by implementing the uses of pointers, structures, and files related?

Such as saving each result after each round of the game into a txt file (win/lost). Making it into a record after the game ends, for the user to open the txt file and see how he did.

Furthermore, declare the class/structure dynamically will pointers to make 5 games. If the user or the computer wins with 3 games or more, then the user/oppenont wins.

I am not to sure if I am making this clear enough. Hopfully you get what I am asking for. Thank you for you help. Sincerely.

#include <iostream>

#include <stdlib.h>

#include <time.h>

using namespace std;

class Go{

private:

    int num;

    int num2;

public:

    Go(int = 6);

    int roll();

    int getNum();

    int getNum2();

};

Go::Go(int num3){

    unsigned seed = time(0);

    srand(seed);

    num=num3;

}

int Go::roll(){

    const int minimum = 1;

    num2=(rand()%(num-minimum+1))+minimum;

    return num2;

}

int Go::getNum(){

    return num;

}

int Go::getNum2(){

    return num2;

}

int main(){

    srand(time(0));

    Go go1;

    Go go2;

    char select;

    int user = 0;

    int opponent = 0;

    while(true){

        cout<<"(Y or N)Do you want to roll the dice to accumulate points? ";

        cin>>select;

        if(toupper(select)!='Y')

            break;

        user+=go1.roll()+go2.roll();

        opponent+=go1.roll()+go2.roll();

        cout<<"Your current total is "<<user<<endl;

        if(user > 21)

            break;

    }//end of while loop

    cout<<" Your total: "<<user<<endl;

    cout<<"Opponent total: "<<opponent<<endl;

    if(user<=21&&user==opponent){

        cout<<" Match tie"<<endl;

    }

    else if(user<=21&&user>opponent){

        cout<<" You won!!"<<endl;

    }

    else if(opponent<=21&&user<opponent){

        cout<<" The opponent won!!"<<endl;

    }

    else{

        cout<<" You lost because your total got over 21."<<endl;

    }

return 0;

}

Explanation / Answer

/*

* Game.h

*

* Created on: 26-Jul-2017

* Author: Ramachandra jr

*/

#ifndef GAME_H_

#define GAME_H_

#include <cstdlib>

#include <ctime>

class Game

{

private:

int _score;

int _done;

int _oppo_score;

public:

Game();

bool cat();

int winner();

void endGame();

void roll();

int get_score();

int get_oppo_score();

virtual ~Game();

};

#endif /* GAME_H_ */

/*

* Game.cpp

*

* Created on: 26-Jul-2017

* Author: Ramachandra jr

*/

#include "Game.h"

Game::Game() : _score{ 0 }, _oppo_score{ 0 }, _done{ false }

{}

/**

* Returns true if either of users has hit 21.

*/

bool Game::cat()

{

// check if the any user is over 21.

if ( this->_oppo_score > 21 || this->_score > 21 )

{

return true;

}

else

{

return false;

}

}

/**

* Returns **1** if user wins else returns **2**, **3** in case of tie.

*/

int Game::winner()

{

if (this->_oppo_score > this->_score && this->_oppo_score < 22)

{

return 2;

}

else if (this->_score > this->_oppo_score && this->_score < 22)

{

return 1;

}

else if (this->_oppo_score > 22 && this->_score < 22)

{

return 1;

}

else if (this->_score > 22 && this->_oppo_score < 22)

{

return 2;

}

else

{

return 3;

}

}

/**

* Ends game.

*/

void Game::endGame()

{

}

/**

* Rolls dice

*/

void Game::roll()

{

/* initialize random seed: */

srand (time(NULL));

/* generate between 1 and 12: */

this->_score += rand() % 12 + 1;

this->_oppo_score += rand() % 12 + 1;

}

int Game::get_score()

{

return this->_score;

}

int Game::get_oppo_score()

{

return this->_oppo_score;

}

Game::~Game()

{}

//============================================================================

// Name : 21game.cpp

// Author : Ramachandra jr

// Version :

// Copyright : Your copyright notice

// Description : Hello World in C++, Ansi-style

//============================================================================

#include <iostream>

#include "Game.h"

using namespace std;

int main() {

char user_input{ 'Y' };

Game g1{};

cout << "Roll dice? (Y or N): ";

cin >> user_input;

while (user_input-'Y' != -11 && ! g1.cat() )

{

g1.roll();

cout << "Player1 Score: " << g1.get_score() << endl;

cout << "Roll dice? (Y or N): ";

cin >> user_input;

}

cout << "Winner is: " << g1.winner() << endl;

cout << "Final Scores:" << endl;

cout << "P1 : " << g1.get_score() << endl;

cout << "P2 : " << g1.get_oppo_score() << endl;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote