So I got a c++ assignment in which you as a player move through randomly generated locations. The total number of locations is 27 and the movement is performed with a throw of a dice (for example you are at the location 2 and you get 6 on a dice, you move to location 8). And there would be three types of locations randomly assigned to a spot at the start of the game (location 1 - type 1, location 2 - type 3, and so on...). And at the initialization, you need to get exactly 14 locations of type 1, 5 locations of type 2, and the rest are type 3. here is what I came up with so far:
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
// movement and display
string field[28][28];
int moveVal;
int count2;
int count1;
// *** C L A S S E S ***
class location {
};
class monster {
public:
int health;
};
class player
{
public:
int playerPosition = 0;
};
// *** F U N C T I O N S ***
void gameInit(player &Knight)
{
int loc1 = 0;
int loc2 = 0;
int loc3 = 0;
int maxVal = 3;
int minVal = 1;
Knight.playerPosition = 0;
string locStringVal;
for (int a = 0; a < 28; a++)
{
int locVal = 0;
locVal = minVal + rand() % maxVal;
cout << locVal << endl;
if (locVal == 1)
{
loc1 += 1;
}
if (locVal == 3)
{
loc2 += 1;
}
if (locVal == 2)
{
loc3 += 1;
}
if (loc1 > 14)
{
minVal = 2;
}
if (loc2 > 5)
{
maxVal = 2;
}
}
}
// Generates random number to move the player and stores the position of the player
void randNum(player &Knight)
{
srand(time(NULL));
moveVal = rand() % 6 + 1;
Knight.playerPosition += moveVal;
}
void display(player &Knight,int &count2, int &count1)
{
for (count2 = 0; count2 < 28; count2++)
{
field[0][count2] = "*";
}
field[0][27] = "B";
if (Knight.playerPosition > 27)
{
Knight.playerPosition = 27;
}
field[0][Knight.playerPosition] = "P";
for (count1 = 0; count1 < 28; count1++)
{
cout << field[0][count1] << " ";
}
}
void gameLoop(player &Knight)
{
char hui;
bool pidaras = true;
gameInit(Knight);
while(pidaras == true)
{
randNum(Knight);
display(Knight, count1, count2);
cin >> hui;
}
}
int main()
//1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
{
srand(time(NULL));
player Knight;
gameLoop(Knight);
}
The script that I came up with works, but sometimes it generates too many "2s" and messes up the whole code. If you run it you can see the numbers generated.