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
= i*1000, 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:
1. The leprechaun jumps to a new location xi + r * gi, where r is a random number between -1 and 1.
2. 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).
3. 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.
4. 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.
Program input
The program initially asks the user for:
A number to initialize the random number generator (that dictates the jumping of the leprechauns)
The number of leprechauns
Game play time, i.e., time to run the simulation in seconds
Program output
At every iteration, only these events should be printed out:
The trapping of a leprechaun in a pit (results in a score increase)
Landing exactly on another leprechaun
At the end of the simulations, print out:
The number of iterations completed
The number of leprechauns trapped in the pit
The score
The following code is the skeleton we were supposed to use with our additions made in bold, but we need help making sure we are doing our code correctly and inputting step #4:
(We are supposed to use a MAP for the project)
#include <iostream>
#include <chrono>
#include <random>
#include <map>
#include <vector>
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
multimap<int, int>
DataStructure;
int gold = 1000000;
for (int i = 0; i < N; i++)
{
//Create N-locations based on the
amount of leprechauns.
//Key: Location Value: What
leprechaun is present.
double location = i * gold;
DataStructure.insert(make_pair(location, i + 1));
}
//Iterator to traverse around
leprechauns:
//Vector for Gold:
//Creates N ints with default value of
gold(1mil)
vector<double>LeprechaunGold(N,
gold);
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
//Iteration - Traverse
through the data structure:
for (std::multimap<int,
int>::iterator it = DataStructure.begin(); it !=
DataStructure.end(); ++it)
{
int
vectorIterator = 0;
//1 The
leprechaun jumps to a new location.
//// You can
use the random number generator like so:
double r =
rng.randomBetween1and2();
double x =
0;
x = x +
r*gold;
DataStructure.insert(make_pair(x, (*it).second));
//Delete old
location.
DataStructure.erase(it);
//2 Determine if
Key is inbetween pit.
if (x >=
-1000 || x <= 1000)
{
//If it IS between -1000 and 1000(It's in the
PIT. "I fell in the pit. You fell in the pit. We all were in the
pit.... THE PIT.")
//Delete this leprechaun AND put goldVector(of
that leprechaun) to 0, and place gold into score.
DataStructure.erase(it);
score +=
LeprechaunGold[vectorIterator];
LeprechaunGold[vectorIterator] =
0;
}
//3
Determine if multiple keys(multiple leprechauns in one spot)
//Count X.
if
(DataStructure.count(x) >= 1)
{
//If there are greater than one, two leprechauns
are occupying the same spot(same key)
std::multimap<int, int>::iterator
toBeDeleted = DataStructure.find(x);
/*range =
DataStructure.equal_range(x);
for (it = range.first; it != range.second;
++it)
{
}*/
}
//4 Leprechaun steals half gold from neighbor(s)
//Move to next leprechaun in Goldvector:
vectorIterator++;
}
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;
}
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.
Rules of the leprechaun world The game starts out with n leprechauns. Each leprechaun starts out...
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...
#include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return...
#include<iostream> #include<cstdlib> using namespace std; int main() { // create array of size 20 int arr[20]; int i; cout<<"scores : "; for( i = 0 ; i < 20 ; i++ ) { // generate a random number i range 70 - 100 an add it to arr arr[i] = rand() % 31 + 70; cout<<arr[i]<<" "; } // store the total score int total = 0;...
#include #include #include #include #include #include // NOLINT (build/c++11) #include class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return type; } }; std::ostream...
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...
Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() { int n; double avg, sum = 0;; string in_file, out_file; cout << "Please enter the name of the input file: "; cin >> in_file; ...
How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...
Three of these function overloads are considered identical by the compiler and would conflict with each other. The other would be considered different- choose the one that is considered different from others. Int foo ( int x, double y ) ; Int foo ( double x, int y ) ; Void foo ( int x, double y ) ; Int foo ( int day, double temp ) ; Consider the following incomplete code: Int f ( int number ) }...
III. ASSIGNMENT 2.1 As discussed in class, the example program enumerates all possible strings (or if we interpret as numbers, numbers) of base-b and a given length, say l. The number of strings enumerated is b l . Now if we interpret the outputs as strings, or lists, rather than base-b numbers and decide that we only want to enumerate those strings that have unique members, the number of possible strings reduces from b l to b!. Furthermore, consider a...
In C++ program Fishing Game Simulation For this assignment, you will write a program that simulates a fishing game. In this game, a six-sided die is rolled to determine what the user has caught. Each possible item is worth a certain number of fishing points. The points will not be displayed until the user has finished fishing, and then a message is displayed congratulating the user depending on the number of fishing points gained. Here are some suggestions for the...