#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>
#include <limits>
usingnamespace std;
int main ()
{
char grid [10][10]; // basic game grid size
int grid_check [10][10]; //to flag positions once something is there o=nothing 1=trap 2=player 3=goal 4=bad guy
int x = 0, y = 0; //x-axis and y-axis position
int randx = 0, randy =0; //random values to be passed to x and y position
int ran1; //temp store for random number
int rn; //variable for number of random numbers required
int level; //place to store level selection
int n;
int move; //direction of move integer
bool game_end=0; // end of game flag
int a=0, b=0;
// ############################## Setting up and printing initial game grid #####################################
srand((unsigned)time(0)); //set off random counter
// Take appropriate level input
while ((cout << string(50, '\n') <<"Please select difficulty level 1-5\n") && (!(cin >> level) || level<1 || level>5))
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// basing number of random nmbers needed on level
rn = (10*level);
// Fill check_array with 0
for (x=0; x<10; x++)
for (y=0; y<10; y++)
grid_check[x][y]=0;
// generating the numbers and putting markers into the check array memory
for (n=0; n<=rn; n++)
{
randx =rand()% 11;
randy =rand()% 11;
grid_check[randx][randy]=1;
}
//fill rest of array up with dots
for (x=0; x<10; x++)
for (y=0; y<10; y++)
if (grid_check[x][y] !=1)
grid[x][y]='.';
else grid [x][y] = 'T';
// Placing the treasure
grid [9][9]='X';
grid_check [9][9] =3;
// Placing the player
grid[0][0] = 'C';
grid_check [0][0] = 2;
//create space
cout << string(50, '\n');
//print out array
for (x=0; x<10; x++)
{
cout << "\n\t\t\t\t";
for (y=0; y<10; y++)
cout << grid[x][y] << " ";
}
// ******************************* Main game loop ********************************************************
while (game_end == 0)
{
randx =rand()% 11;
randy =rand()% 11;
// Generate a random bad guy each turn
while (grid_check[randx][randy]!=0) // makes sure that spot is vacant
{
randx =rand()% 11;
randy =rand()% 11;
}
grid_check[randx][randy]=4; // flagging the spot
grid [randx][randy]='@'; // placing bad guy
// getting appropriate input
while ( cout << "\nPlease input direction choice:" && (!(cin >> move) || move<1 || move>9))
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// Move down
if (move == 2)
{
grid_check[a][b]=0; // unflag spot about to be left
grid[a][b]='.'; // Place a point on it
a++; // Change value of x-axis
if (grid_check[a][b]!=0)
game_end=1;
else
{
grid[a][b]='C'; // Put character in new spot
grid_check[a][b]=2;} // Flag new spot
}
// Move right
elseif (move == 6)
{
grid_check[a][b]=0; // unflag spot about to be left
grid[a][b]='.'; // Place a point on it
b++; // Change value of y-axis
if (grid_check[a][b]!=0)
game_end=1;
else
{
grid[a][b]='C'; // Put character in new spot
grid_check[a][b]=2;} // Flag new spot
}
// Move up
elseif (move == 8)
{
grid_check[a][b]=0; // unflag spot about to be left
grid[a][b]='.'; // Place a point on it
a--; // Change value of x-axis
if (grid_check[a][b]!=0)
game_end=1;
else
{
grid[a][b]='C'; // Put character in new spot
grid_check[a][b]=2;} // Flag new spot
}
// Move left
elseif (move == 4)
{
grid_check[a][b]=0; // unflag spot about to be left
grid[a][b]='.'; // Place a point on it
b--; // Change value of y-axis
if (grid_check[a][b]!=0)
game_end=1;
else
{
grid[a][b]='C'; // Put character in new spot
grid_check[a][b]=2;} // Flag new spot
}
//create space
cout << string(50, '\n');
// A little help to clear a space
randx =rand()% 11;
randy =rand()% 11;
if (grid_check[randx][randy] != 0 && grid_check[randx][randy]!=2 && grid_check[randx][randy] !=3)
{
grid_check[randx][randy]=0;
grid [randx][randy]='.';
}
//print out array
for (x=0; x<10; x++)
{
cout << "\n\t\t\t\t";
for (y=0; y<10; y++)
cout << grid[x][y] << " ";
}
// Check for game end situations
if (grid_check [9][9]==2 || b>9 || a>9 || a<0 || b<0)
{
game_end=1;
}
} // ######################################### End of main game loop ##########################################
// Final messages
if (grid_check[a][b]==3)
cout << "\nCongratulations, you did it!\n";
elseif (grid_check[a][b]==4)
cout << "\nSorry, Killed by an ALIEN!!!\n";
else
cout << "\nSorry, not this time - you're DEAD!!\n";
return 0;
}
One thing I noticed that you need to take care of is this couple of lines:
1 2
randx =rand()% 11;
randy =rand()% 11;
It should be:
1 2
randx =rand()% 10;
randy =rand()% 10;
rand()%11 gives you a number between 0 and 10. You don't want that...
What you want is a number between 0 and 9, which is exactly what rand()%10 gives you.
Once again, many thanks - all my buggy errors seem to have vanished. I remembered that the ran call gave numbers up to but not including the limit given, however forgot momentarily that my array numbers were 0-9.
All's well now - not sure if I need to make the 'aliens' move in random directions as per exercise as have already gone above and beyond its scope. I think I'm going to move on to 'fun with functions' now - have been itching to get here as I know it will cut down massively on my code length - should also go some-way to satisfy my aesthetic sense of how code should look (wouldn't have thought that this would bother me at the start of all this!)
Okay, couldn't help but tweak a little bit more - here is my final attempt at the game, allowed the player to 'eat' up to three 'aliens' in their quest for the treaasure. really moving on now ... honest.
#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>
#include <limits>
usingnamespace std;
int main ()
{
char grid [10][10]; // basic game grid size
int grid_check [10][10]; //to flag positions once something is there o=nothing 1=trap 2=player 3=goal 4=bad guy
int x = 0, y = 0; //x-axis and y-axis position
int randx = 0, randy =0; //random values to be passed to x and y position
int ran1; //temp store for random number
int rn; //variable for number of random numbers required
int level; //place to store level selection
int n;
int move; //direction of move integer
int eat=0;
bool game_end=0; // end of game flag
int a=0, b=0;
// ################################ Setting up and printing initial game grid ######################################
srand((unsigned)time(0)); //set off random counter
// Instructions
cout << string(20, '\n') << "########################## Welcome to DUNGEON CRAWL #########################\n\n\n\n\n";
cout << "C = YOU \nX = TREASURE\n";
cout << "T = TRAP \n@ = BAD GUY\n\n\n";
cout << "Use the numberpad direction arrows to move around\nYou can 'eat' three bad guys but no more!!\n\n\n";
cout << "\n\nGOOD LUCK!!!\n\n\n";
// Take appropriate level input
while ((cout <<"Please select difficulty level 1-5\n\n") && (!(cin >> level) || level<1 || level>5))
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// basing number of random nmbers needed on level
rn = (10*level);
// Fill check_array with 0
for (x=0; x<10; x++)
for (y=0; y<10; y++)
grid_check[x][y]=0;
// generating the numbers and putting markers into the check array memory
for (n=0; n<=rn; n++)
{
randx =rand()% 10;
randy =rand()% 10;
grid_check[randx][randy]=1;
}
//fill rest of array up with dots
for (x=0; x<10; x++)
for (y=0; y<10; y++)
if (grid_check[x][y] !=1)
grid[x][y]='.';
else grid [x][y] = 'T';
// Placing the treasure
grid [9][9]='X';
grid_check [9][9] =3;
// Placing the player
grid[0][0] = 'C';
grid_check [0][0] = 2;
//create space
cout << string(50, '\n');
//print out array
for (x=0; x<10; x++)
{
cout << "\n\t\t\t\t";
for (y=0; y<10; y++)
cout << grid[x][y] << " ";
}
// ******************************* Main game loop ********************************************************
while (game_end == 0)
{
randx =rand()% 10;
randy =rand()% 10;
// Generate a random bad guy each turn
while (grid_check[randx][randy]!=0) // makes sure that spot is vacant
{
randx =rand()% 10;
randy =rand()% 10;
}
grid_check[randx][randy]=4; // flagging the spot
grid [randx][randy]='@'; // placing bad guy
// getting appropriate input
while ( cout << "\nPlease input direction choice:" && (!(cin >> move) || move<1 || move>9))
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// Move down
if (move == 2)
{
grid_check[a][b]=0; // unflag spot about to be left
grid[a][b]='.'; // Place a point on it
a++; // Change value of x-axis
if (eat > 2 && grid_check[a][b]!=0)
game_end=1;
elseif (grid_check[a][b]==4 && eat <= 2)
{
grid[a][b]='C';
grid_check[a][b]=2;
eat++;
}
elseif (grid_check[a][b]==1)
game_end=1;
elseif (grid_check[a][b]==3)
game_end=1;
else
{
grid[a][b]='C'; // Put character in new spot
grid_check[a][b]=2;} // Flag new spot
}
// Move right
elseif (move == 6)
{
grid_check[a][b]=0; // unflag spot about to be left
grid[a][b]='.'; // Place a point on it
b++; // Change value of y-axis
if (eat > 2 && grid_check[a][b]!=0)
game_end=1;
elseif (grid_check[a][b]==4 && eat <= 2)
{
grid[a][b]='C';
grid_check[a][b]=2;
eat++;
}
elseif (grid_check[a][b]==1)
game_end=1;
elseif (grid_check[a][b]==3)
game_end=1;
else
{
grid[a][b]='C'; // Put character in new spot
grid_check[a][b]=2;} // Flag new spot
}
// Move up
elseif (move == 8)
{
grid_check[a][b]=0; // unflag spot about to be left
grid[a][b]='.'; // Place a point on it
a--; // Change value of x-axis
if (eat > 2 && grid_check[a][b]!=0)
game_end=1;
elseif (grid_check[a][b]==4 && eat <= 2)
{
grid[a][b]='C';
grid_check[a][b]=2;
eat++;
}
elseif (grid_check[a][b]==1)
game_end=1;
elseif (grid_check[a][b]==3)
game_end=1;
else
{
grid[a][b]='C'; // Put character in new spot
grid_check[a][b]=2;} // Flag new spot
}
// Move left
elseif (move == 4)
{
grid_check[a][b]=0; // unflag spot about to be left
grid[a][b]='.'; // Place a point on it
b--; // Change value of y-axis
if (eat > 2 && grid_check[a][b]!=0)
game_end=1;
elseif (grid_check[a][b]==4 && eat <= 2)
{
grid[a][b]='C';
grid_check[a][b]=2;
eat++;
}
elseif (grid_check[a][b]==1)
game_end=1;
elseif (grid_check[a][b]==3)
game_end=1;
else
{
grid[a][b]='C'; // Put character in new spot
grid_check[a][b]=2;} // Flag new spot
}
//create space
cout << string(50, '\n');
// A little help to clear a space
randx =rand()% 10;
randy =rand()% 10;
if (grid_check[randx][randy] != 0 && grid_check[randx][randy]!=2 && grid_check[randx][randy] !=3)
{
grid_check[randx][randy]=0;
grid [randx][randy]='.';
}
//print out array
for (x=0; x<10; x++)
{
cout << "\n\t\t\t\t";
for (y=0; y<10; y++)
cout << grid[x][y] << " ";
}
// Check for game end situations
if (grid_check [9][9]==2 || b>9 || a>9 || a<0 || b<0)
{
game_end=1;
}
} // ######################################### End of main game loop ##########################################
// Final messages
if (grid_check[a][b]==3)
cout << "\nCongratulations, you did it!\n";
elseif (eat >2 && grid_check[a][b]==4)
cout << "\a\a\a\nSorry, Killed by an ALIEN!!!\n";
else
cout << "\a\a\a\nSorry, not this time - you're DEAD!!\n";
return 0;
}