#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <vector>
#include <ctime>
struct thing{
int X, Y;
};
thing player, goal;
std::vector<thing> trap;
std::vector<thing> enemy;
char grid[10][10];
int level;
void resetGame() {
bool failure;
do {
std::cout << "Choose level: ";
int temp = NULL;
std::cin >> temp;
if (std::cin.fail()) {
std::cout << "Invalid input.\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
failure = true;
}
else {
failure = false;
level = temp;
}
} while (failure);
player.X = 0, player.Y = 0, goal.X = 9, goal.Y = 9;
thing foo;
trap.erase(trap.begin(),trap.end());
for (int i = 0; i < level; i++) {
foo.X = rand() % 10;
foo.Y = rand() % 10;
if ((foo.X == 0 && foo.Y == 0) || (foo.X == 9 && foo.Y == 9)) {
i--;
}
else {
trap.push_back(foo);
}
}
enemy.erase(enemy.begin(), enemy.end());
for (int i = 0; i < level; i++) {
foo.X = rand() % 10;
foo.Y = rand() % 10;
if ((foo.X == 0 && foo.Y == 0) || (foo.X == 9 && foo.Y == 9)) {
i--;
}
else {
enemy.push_back(foo);
}
}
}
void displayInfo() {
std::cout << "\nUse arrow keys to move 'P' to the goal, 'G'. Avoid any traps and enemies along the way. Press 'R' while playing to restart. The level corresponds with the frequency of traps.\n\nPress -> to continue.";
for (;;) {
if (_getch() == 77) {
return;
}
}
}
void moveEnemy() {
for (int i = 0; i < level; i++) {
switch (rand() % 4) {
case 0:
if (enemy[i].Y > 0)
enemy[i].Y--;
break;
case 1:
if (enemy[i].Y < 9)
enemy[i].Y++;
break;
case 2:
if (enemy[i].X < 9)
enemy[i].X++;
break;
case 3:
if (enemy[i].X > 0)
enemy[i].X--;
break;
}
}
}
int main()
{
srand((int)time(NULL));
resetGame();
for (;;) {
beginning:
system("cls");
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
grid[i][j] = '.';
}
}
for (int i = 0; i < level; i++) {
for (int j = 0; j < level; j++) {
if (enemy[i].X == trap[j].X && enemy[i].Y == enemy[i].Y) {
enemy.erase(enemy.begin() + i);
trap.erase(trap.begin() + i);
}
}
}
grid[goal.X][goal.Y] = 'G';
grid[player.X][player.Y] = 'P';
for (int i = 0; i < level; i++) {
grid[trap[i].X][trap[i].Y] = 'X';
}
for (int i = 0; i < level; i++) {
grid[enemy[i].X][enemy[i].Y] = 'E';
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
std::cout << grid[j][i] << " ";
}
std::cout << "\n\n";
}
std::cout << "Press 'i' for info.\n";
for (int i = 0; i < level; i++) {
if (player.X == trap[i].X && player.Y == trap[i].Y) {
std::cout << "You lose. Restart? (Y or N)\n";
for (;;) {
switch (_getch()) {
case 121:
resetGame();
goto beginning;
case 110:
return 0;
}
}
}
}
for (int i = 0; i < level; i++) {
if (player.X == enemy[i].X && player.Y == enemy[i].Y) {
std::cout << "You lose. Restart? (Y or N)\n";
for (;;) {
switch (_getch()) {
case 121:
resetGame();
goto beginning;
case 110:
return 0;
}
}
}
}
if (player.X == goal.X && player.Y == goal.Y) {
std::cout << "You Win! Play again? (Y or N)\n";
for (;;) {
switch (_getch()) {
case 121:
resetGame();
goto beginning;
case 110:
return 0;
}
}
}
else {
switch (_getch()) {
case 72:
moveEnemy();
if (player.Y > 0)
player.Y--;
break;
case 80:
moveEnemy();
if (player.Y < 9)
player.Y++;
break;
case 75:
moveEnemy();
if (player.X > 0)
player.X--;
break;
case 77:
moveEnemy();
if (player.X < 9)
player.X++;
break;
case 32:
moveEnemy();
break;
case 105:
displayInfo();
break;
case 114:
std::cout << "Are you sure you want to restart? (Y or N)\n";
for (;;) {
switch (_getch()) {
case 121:
resetGame();
goto beginning;
case 110:
goto beginning;
}
}
}
}
}
}
This part of this code for some reason doesn't work and causes the subscript out of range error at seemingly random points (more likely if you input a higher level):
1 2 3 4 5 6 7 8
for (int i = 0; i < level; i++) {
for (int j = 0; j < level; j++) {
if (enemy[i].X == trap[j].X && enemy[i].Y == enemy[i].Y) {
enemy.erase(enemy.begin() + i);
trap.erase(trap.begin() + i);
}
}
}
I need this code in order to make it so that when an enemy occupies the same space as a trap, the enemy sets off the trap and both disappear. Other than this, the code works perfectly.
In case you need to know, this was compiled as Win32 using VisualStudios.
I Would suggest you use a debugger and go through your program step by step, I assume you know where it crashes. Your error means that you are accessing memory you dont have. So step through it one line at a time and figure out why that happens.
You need to use functions more, if you have 6 levels of nesting then you know you can improve things. Candidates for functions are compound statements - statements in braces. So this could be the bodies of a for loop, the body of an if statement etc. Also, anything that you have repeated, like restart. And Menus, can be in a function.
You should also have a default: case for each switch, to catch bad input.
Also, goto is tempting, but bad news. One can return early from void functions.
Consider a range based for loop if you want to process all the items in a container. Google to see how that works :+) The advantage is that you won't go out of bounds.
I would also avoid infinite conditions on loops where ever possible, the one on line 192 doesn't do anything: both cases cause the program to jump elsewhere. If neither case is satisfied you have what you don't want: an infinite loop. It shouldn't be hard to come up with an end condition.
You can have a char in a switch, those numbers are bit cryptic.