Hi, use g++ at home on Linux and visual Studio c++ at work. Am working on 'dungeon crawl' exercise trying to get the 'player' to move using the number keypad. Have instigated '2' to move down, seemed to work with no problems on g++ at home, added the '6' or move right and no joy together with some 'random' characters in the grid.
I come to work with the code to work on, compile it in visual studio, run it to remind myself of the problem - but hey presto move down and right work fine.
Could someone try to explain why this is the case and what can be done to fix it please,
#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>
#include <limits>
usingnamespace std;
int main ()
{
char grid [10][10]; // basic game grid size
bool grid_check [10][10]; //to flag positions once something is there
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
srand((unsigned)time(0));
bool game_end=0;
int a=0, b=0;
// 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);
// 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]=1;
// Placing the player
grid[0][0] = 'C';
grid_check [0][0] = 1;
//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)
{
// getting appropriate input
while ( (!(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
grid[a][b]='C'; // Put character in new spot
grid_check[a][b]=1; // Flag new spot
}
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 x-axis
grid[a][b]='C'; // Put character in new spot
grid_check[a][b]=1; // Flag new spot
}
//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] << " ";
}
}
}
cout << endl;
return 0;
}