Question

Write a c++ program to stimulate gameplay. Randomly select two players to attack or restore. A...

Write a c++ program to stimulate gameplay.

Randomly select two players to attack or restore.

A player starts off with 300 points. A player dies when his points is reduced to 0.

A player can either attack or restore another player.

Points:

If a player’s points are between 200 and 300 inclusive an attack can decrease a player points by a random number between 20 and 80 and a restore and increase a player points by a random number between 20 and 80.

200 ≤ POINTS ≤ 300 (Attack 80%, Restore 20%)

100 ≤ POINTS < 200 (Attack 70%, Restore 30%)

0 ≤ POINTS < 100 (Attack 40%, Restore 60%)

Create a struct for Players and Moves.

struct Player{

                string name;

                int points;

};

struct Move{

                int amount;

                char move; // ‘A’- Attack, ‘R’-Restore

};

Read from a file (input.txt) the following information:

20 20

paul

peter

kim

The first integer is the random number generator. The second number is a number of moves per player. After the integers there can be up to 50 names in the text file with each name on a separate line.

Sample Output:

paul attacks kim -50

peter restores paul +30

After 20 moves

paul 100

peter 150

kim 200

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution to your question. I tried my best to solve your doubt, however, if you find it is not as good as expected by you. Please do write your further doubts regarding this question in the comment section, I will try to resolve your doubts regarding the submitted solution as soon as possible.

Please give proper indentation as shown in the screenshot.

If you think, the solution provided by me is helpful to you please do an upvote.

Either you have forgotten to upload some files or you have provided incomplete questions. I am not able to find input and output files also some functions of your question are very confusing. Please do clarify in the comments

  1. How to decide who will attack?
  2. How to decide who will be get attacked?
  3. What is the use of random number generator?

However, based on your provided information below is the solution that might help you.

1. 2. 3. #include <iostream> #include<fstream> #include<cstdlib> #include<ctime> #include<cctype> using namespace std; 4. 5.
#include <iostream>
#include<fstream>
#include<cstdlib>
#include<ctime>
using namespace std;

struct Player 
{
    string name;
    int points;
};

struct Move 
{
    int amount;
    char move;
};



int main() 
{
    string filename="input.txt";
    fstream ifile;
    ifile.open(filename.c_str());

    int randgen,moves;
    ifile>>randgen>>moves;

    struct Player players[50];
    int n=0;
    while(ifile>>players[n].name)
    {
        players[n].points=300;
        n+=1;
    }

    srand(time(0)); 

    
    for(int mv=0;mv<moves;mv+=1)
    {
        for(int plcnt=0;plcnt<n;plcnt+=1)
        {
            if(players[plcnt].points==0)
                continue;
            int opl;
            do 
            {
                opl=rand()%n;
            }while(opl==plcnt);

            if(players[opl].points>=200 && players[opl].points<=300) 
            {
                int pblt=80;
                Move m;
                m.amount=(rand()%(80-20))+20;
                int r=(rand()%100)+1;
                if(r<pblt)
                {
                    m.move='A';
                }
                else
                {
                    m.move='R';
                }
                if(m.move=='A')
                {
                    players[opl].points-=m.amount;
                    cout<<players[plcnt].name<<" attacks "<<players[opl].name<<" -"<<m.amount<<endl;
                }
                else 
                {
                    players[opl].points+=m.amount;
                    cout<<players[plcnt].name<<" restores "<<players[opl].name<<" "<<m.amount<<endl;
                }
                if(players[opl].points<=0)
                {
                    players[opl].points=0;
                    cout<<players[opl].name<<" died"<<endl;
                }

            }

            else if(players[opl].points>=100 && players[opl].points<=200) 
            {
                int pblt=70;
                Move m;
                m.amount=(rand()%(80-20))+20;
                int r=(rand()%100)+1;
                if(r<pblt)
                {
                    m.move='A';
                }
                else
                {
                    m.move='R';
                }
                if(m.move=='A')
                {
                    players[opl].points-=m.amount;
                    cout<<players[plcnt].name<<" attacks "<<players[opl].name<<" -"<<m.amount<<endl;
                }
                else 
                {
                    players[opl].points+=m.amount;
                    cout<<players[plcnt].name<<" restores "<<players[opl].name<<" "<<m.amount<<endl;
                }
                if(players[opl].points<=0)
                {
                    players[opl].points=0;
                    cout<<players[opl].name<<" died"<<endl;
                }

            }

            else if(players[opl].points>=0 && players[opl].points<=100) 
            {
                int pblt=40;
                Move m;
                m.amount=(rand()%(80-20))+20;
                int r=(rand()%100)+1;
                if(r<pblt)
                {
                    m.move='A';
                }
                else
                {
                    m.move='R';
                }
                if(m.move=='A')
                {
                    players[opl].points-=m.amount;
                    cout<<players[plcnt].name<<" attacks "<<players[opl].name<<" -"<<m.amount<<endl;
                }
                else 
                {
                    players[opl].points+=m.amount;
                    cout<<players[plcnt].name<<" restores "<<players[opl].name<<" "<<m.amount<<endl;
                }
                if(players[opl].points<=0)
                {
                    players[opl].points=0;
                    cout<<players[opl].name<<" died"<<endl;
                }

            }
        }

    }
    cout<<"After "<<moves<<" moves"<<endl;
    for(int plcnt=0;plcnt<n;plcnt+=1)
    {
        cout<<players[plcnt].name<<" "<<players[plcnt].points<<endl;
    }
    ifile.close();
    return 0;
}
stdin 20 20 paul peter kim ostdout paul attacks kim - 76 peter restores paul 39 kim attacks peter -51 paul attacks kim -62 pe
Add a comment
Know the answer?
Add Answer to:
Write a c++ program to stimulate gameplay. Randomly select two players to attack or restore. A...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players...

    The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 (”pig”) is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player’s turn, the player is faced with two decisions: roll If the player rolls a 1: the player scores nothing and it becomes the...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • python question Question 1 Write a Python program to play two games. The program should be...

    python question Question 1 Write a Python program to play two games. The program should be menu driven and should use different functions to play each game. Call the file containing your program fun games.py. Your program should include the following functions: • function guess The Number which implements the number guessing game. This game is played against the computer and outputs the result of the game (win/lose) as well as number of guesses the user made to during the...

  • C++ Can someone please help with this code... even when the players number isnt a winning number the program always says the player wins: #include <stdio.h> #include <time.h> //main functi...

    C++ Can someone please help with this code... even when the players number isnt a winning number the program always says the player wins: #include <stdio.h> #include <time.h> //main function int main() {    //seeding a random number    srand(time(0));    //define the arrays for the wagered, winning amount, percent amount    double amounts[7] = { 1, 5, 10, 20, 50, 100, 1000 };    double payoff[7] = { 100, 500, 1000, 2000, 5000, 10000, 100000 };    double percent[7] = { 0.01, 0.05, 1, 2,...

  • C++ programming: Write a program to score a two player game of scrabble. The program reads...

    C++ programming: Write a program to score a two player game of scrabble. The program reads the player number, word played and the squares on the board used for each word played then reports the scores for each word and the total score for each player. • All information is read from the file, moves.in. • Each line of the file contains a player number (1 or 2), the word and the board spaces used. • The words are entered...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • Please, I need help with program c++. This is a chutes and ladders program. The code...

    Please, I need help with program c++. This is a chutes and ladders program. The code must be a novel code to the specifications of the problem statement. Thank you very much. Assignment Overview This program will implement a variation of the game “chutes and ladders” or “snakes and ladders:” https://en.wikipedia.org/wiki/Snakes_and_Ladders#Gameplay. Just like in the original game, landing on certain squares will jump the player ahead or behind. In this case, you are trying to reach to bottom of the...

  • the card game Acey Deucey, which is also known by several other names. In general, the...

    the card game Acey Deucey, which is also known by several other names. In general, the game is played with three or more people that continue to gamble for a pot of money. The pot grows and shrinks depending on how much General description of the game ● Two cards are dealt face up to a player from a shuffled deck of cards. ○ If the face of each card is the same then the player adds $1 into the...

  • Requirements: 1. You are not allowed to use global variables. 2. Must declare the following two...

    Requirements: 1. You are not allowed to use global variables. 2. Must declare the following two constants public static final int POINTS = 30; public static final int FORFEIT_POINTS = 20; 3. You can implement your own solution but you are required to break down the problem into smaller pieces. 4. You must provide the output for two rounds of game minimum. 5. Since we are using the Random number generator your output will not be exactly like mine. However,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT