Question

C++ Problem The game starts out with n leprechauns. Each leprechaun starts out with a million...

C++ Problem

The game starts out with n leprechauns. Each leprechaun starts out with a million dollars of gold (i.e., gi = 1,000,000). The player wants to trap as many of these leprechauns in a pit and steal their gold! The leprechauns are all in a row, with each leprechaun at location xi, a double precision floating point number. Initially, the first leprechaun is at x=0, the second at x=1,000,000, ...; i.e., xi = i*gi, i=0,1,2,....
At every iteration of the simulation, the leprechauns are processed in strict order from i=0,1,2,... The processing of a leprechaun involves these steps:

The leprechaun jumps to a new location xi + r * gi, where r is a random number between -1 and 1.

There is a pit located between -1,000 and 1,000. If -1000 < xi < 1000, then that leprechaun is trapped forever (does not participate in the game anymore) and all his gold is added to the player’s score (the player starts with score=0).

If a leprechaun lands right on top of another (k), then the newly arrived leprechaun steals all the gold from k, and the bankrupt leprechaun k exits the game.

The leprechaun then steals half the gold from the nearest leprechauns to his right and left, if he indeed has both neighbours. (If the position moved to is either the largest or smallest value of all xi, then there is only one neighbour that is nearest. In this case he steals half the gold from this nearest neighbour).

The game ends after a fixed period of time (e.g., 20 seconds), at which point the final score is displayed.

You must use a map-based data structure for this project to efficiently access and modify the state of the leprechauns. While it is possible to implement the logic using simpler data structures, your code will run too slow to be competitive. Note that the more iterations you can complete within the specified time limit, the more likely you are to trap leprechauns and get a higher score.

You can use the map container included in the C++ Library

HERE IS THE SAMPLE CODE THAT MUST BE USED, DO NOT CHANGE ANYTHING JUST ADD TO IT:

#include "stdafx.h"
#include <iostream>
#include <chrono>
#include <random>

using namespace std;


class RandomNumberGenerator {
public:
   RandomNumberGenerator(int x) :generator(x) {};
   // return a double between -1 and 1
   double randomBetween1and2() {
       return (2.0*generator()) / generator.max() - 1.0;
   }
private:
   minstd_rand0 generator;
};

int N;
// Use a constant random number seed so behavior is consistent from run to run.
int RANDOM_SEED;

int main()
{

   cout << "Enter seed for random number generator: ";
   cin >> RANDOM_SEED;
   RandomNumberGenerator rng(RANDOM_SEED);

   cout << "Enter number of leprechauns: ";
   cin >> N;

   long playtime;
   cout << "Enter game play time (seconds): ";
   cin >> playtime;
   playtime = playtime * 1000; // convert to milliseconds

   double score = 0;
   int nTrapped = 0;

   //
   // CODE FOR INITIALIZING DATA STRUCTURES GOES HERE
   //

   int t = 0; // keep track of number of iterations
   auto start_time0 = chrono::high_resolution_clock::now();
   auto timeSinceStartMS = 0;
   do {

       //
       // CODE FOR A SINGLE ITERATION GOES HERE
       //

       //// You can use the random number generator like so:
       //   double r = rng.randomBetween1and2();
       // x = x + r*gold;


       t++;
       // code to measure run time
       auto end_time = std::chrono::high_resolution_clock::now();
       auto timeSinceStart = end_time - start_time0;
       timeSinceStartMS = chrono::duration_cast<chrono::milliseconds>(timeSinceStart).count();
   } while (timeSinceStartMS < playtime);

   cout << "Number of iterations = " << t << endl;
   cout << "Number of trapped leprechauns = " << nTrapped << endl;
   cout << "Score = " << (long)score << endl;
   return 0;
}

SAMPLE OUTPUT:

Enter seed for random number generator: 123

Enter number of leprechauns: 10000

Enter game play time (seconds): 20

t=0: Caught a leprechaun!! Score = 1000000

t=5: Caught a leprechaun!! Score = 4729343

t=5: Caught a leprechaun!! Score = 5219710

t=8: Caught a leprechaun!! Score = 6522043

t=8: Caught a leprechaun!! Score = 9111052

t=10: Caught a leprechaun!! Score = 9209047

t=10: Caught a leprechaun!! Score = 9642793

t=11: Caught a leprechaun!! Score = 9967663

Number of iterations = 13

Number of trapped leprechauns = 8

Score = 9967663

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
C++ Problem The game starts out with n leprechauns. Each leprechaun starts out with a million...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Rules of the leprechaun world The game starts out with n leprechauns. Each leprechaun starts out...

    Rules of the leprechaun world The game starts out with n leprechauns. Each leprechaun starts out with a million dollars of gold (i.e., gi = 1,000,000). The player wants to trap as many of these leprechauns in a pit and steal their gold! The leprechauns are all in a row, with each leprechaun at location xi, a double precision floating point number. Initially, the first leprechaun is at x=0, the second at x=1000, the third at 2000, ...; i.e., xi...

  • please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then...

    please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then write each user guess and answer to the file. (on separate lines) Write the number of guesses to the file at the end of the game. After the game is finished, ask the user if they want to play again. If 'n' or 'N' don't play again, otherwise play again! NOTE: your file should have more than one game in it if the user...

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

  • (C++) Hey guys we just went over loops and it got me confused, it has me...

    (C++) Hey guys we just went over loops and it got me confused, it has me scrathcing my head for the past two days. I would appreciate any help you guys have to offer. Few places I have a hard time is get input and determine winner(cummulative part). Write a program that lets the user play the Rock, Paper, Scissors game against the computer. The computer first chooses randomly between rock, paper and scissors, but does not display its choice....

  • For this assignment, your job is to create a simple game called Opoly. The objectives of...

    For this assignment, your job is to create a simple game called Opoly. The objectives of this assignment are to: Break down a problem into smaller, easier problems. Write Java methods that call upon other methods to accomplish tasks. Use a seed value to generate random a sequence of random numbers. Learn the coding practice of writing methods that perform a specific task. Opoly works this way: The board is a circular track of variable length (the user determines the...

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • I need help figuring out how to code this in C++ in Visual Studio. Problem Statement:...

    I need help figuring out how to code this in C++ in Visual Studio. Problem Statement: Open the rule document for the game LCR Rules Dice Game. Develop the code, use commenting to describe your code and practice debugging if you run into errors. Submit source code. Use the document to write rules for the user on how to play the game in a text file. Then, using the information from the resource articles, create a program that will read...

  • In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the...

    In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the code in main to create and sort the array of pointers. The places to make modifications are indicated by TODO: comments. You should not have to make modifications anywhere else. 3- what's big O and runtime for 100000 items. #include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string> #include <cstdlib> #include <cassert> using namespace std; // Set this to false to skip the...

  • Use Dev C++ for program and include all of the following. Im lost. Make sure the...

    Use Dev C++ for program and include all of the following. Im lost. Make sure the user enters a letter of R, P, or S and the computer generates a number between 0-2 and changes that number to a letter of R, P or S, using a switch. Make sure the user knows why he/she has won or lost – print a message like “Paper covers Rock” or “Scissors cuts Paper”. Ask the user how many times he/she wants to...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

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