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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
int g_gridSizeX;
int g_gridSizeY;
int** g_grid; // = new int *[];
int g_userInput;
int g_guessX = -1;
int g_guessY = -1;
int g_answerX = -2;
int g_answerY = -2;
bool dataCheck(int userI, int checkNum) // function for checking the input is a number value
{
bool returnVal = false; // this is the boolean variable that will return true if a number was entered
int testV = 0;
testV = userI;
{
switch (checkNum)
{
case 1:
if (testV >= 2 && testV <= 10) // this is used for stopping the user from having a grid size smaller than 2 x 2
{
returnVal = true; //If this is the case then it will return the grid to the user. If false, an error message
}
break;
case 2:
if (testV >= 0 && testV < g_gridSizeX)
{
returnVal = true;
}
break;
case 3:
if (testV >= 0 && testV < g_gridSizeY)
{
returnVal = true;
}
break;
}
}
return returnVal; // states that either true or false will be returned to the main
}
void drawGrid()
{
for (int y = 0; y < g_gridSizeY; y++) // step through the rows in the grid
{
for (int x = 0; x < g_gridSizeX; x++) // step through each column in the grid
{
g_grid[y][x] = 1;
std::cout << "* ";
}
std::endl(std::cout);
}
}
void findTreasure()
{
bool test = false;
while (g_guessX != g_answerX || g_guessY != g_answerY) // so long as the user guess is incorrect, this will repeat
{
do
{
test = false;
g_userInput = -2;
std::cout << "Please input your X co-ord guess between 1 and " << g_gridSizeX << std::endl;
std::cin >> g_userInput;
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
test = true;
}
} while (!dataCheck(g_userInput - 1, 2) && test);
g_guessX = int(g_userInput) - 1;
do
{
test = false;
g_userInput = -2;
std::cout << "Please input your Y co-ord guess between 1 and " << g_gridSizeY << std::endl;
std::cin >> g_userInput;
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
test = true;
}
} while (!dataCheck(g_userInput - 1, 3) && test);
g_guessY = int(g_userInput) - 1;
if (g_guessX != g_answerX || g_guessY != g_answerY) // so long as the guess is incorrect, the error message will be displayed and the loop will repeat
{
std::cout << "Sorry, that is incorrect. Please try again..." << std::endl;
}
}
}
int main()
{
std::cout << "Welcome to Treasure Hunt 2!" << std::endl;
bool test = false; // test is started as false. Going through the do while will proceed the program
do
{
test = false;
g_userInput = -2;
std::cout << "Please enter the X co-ordinate of your grid you wish to play on: " << std::endl;
std::cin >> g_userInput;
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
test = true;
}
} while (!dataCheck(g_userInput, 1) && test);
g_gridSizeX = int(g_userInput); // the X co-ordinate of the grid will be what the user has entered
do
{
test = false;
g_userInput = -2;
std::cout << "Please enter the Y co-ordinate of your grid you wish to play on: " << std::endl;
std::cin >> g_userInput;
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
test = true;
}
} while (!dataCheck(g_userInput, 1) && test);
g_gridSizeY = int(g_userInput); // the Y co-ordinate of the grid will be what the user has entered
std::cout << "Here is your grid" << std::endl;
g_grid = new int*[g_gridSizeY];
for (int i = 0; i < g_gridSizeY; i++)
{
g_grid[i] = new int[g_gridSizeX];
}
drawGrid();
srand(time(NULL)); // after the grid has been created, this is used for setting one of the locations as the answer
g_answerX = rand() % (g_gridSizeX - 1);
g_answerY = rand() % (g_gridSizeY - 1);
findTreasure();
std::cout << "Well done, you have found the treasure! It is at location: " << g_guessX+1 << ", " << g_guessY+1;
std::endl (std::cout);
system("pause");
for (int i = 0; i < g_gridSizeY; i++)
{
delete[] g_grid[i];
}
delete[] g_grid;
return 0;
}
|