#include <iostream>
#include <windows.h>
#include <ctime>
#include <string>
#include <cstdlib>
usingnamespace std;
string gameboard [40][20] // Game board where the game takes place
{
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""! @ !""! @ @ !""! @ @ @ !""! @ @ @ @ !""! @ @ @ @ @ !""! @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ !""! @ @ @ @ @ !""! @ @ @ @ !""! @ @ @ !""! @ @ !""! @ !""! !""! !""! !""! !""! !""! !""! X !""!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
};
bool endgame = false;
int speedtimer = 100; // How fast the player can move
int enemyrandnum = rand() %50 + 1; // The aliens' bombs landing in random places
int grand (time(0)); // How fast the bombs are falling
int Gmaxhealth = 400; // The maximum health a player has
int Shealth = Gmaxhealth; // Calculates how much health is left for the player
int main()
{
system ("color0a"); // Color of the board
while (endgame==false)
{
system("cls");
for (int y=0; y<40; y++) // to move the player
{
int gMap[y];
cout << gMap[y];
}
cout << "HEALTH:" << Gmaxhealth << "/" << Shealth; // Updates current health of player
for (int y=0; y<40; y++) // moves player in the array
{
for (int x=0; x<20; x++) // moves player in the array
{
int gMap[y][x];
switch (gMap[y][x])
{
case'X':
if (GetAsyncKeyState (VK_LEFT)!=0) // moves the player to the left
{
int deuxiemex = x-1; // new position of the player
switch (gMap [y][deuxiemex])
{
case' ':
gMap[y][x] = ' '; // Replaces old position of the player
x--;
gMap[y][deuxiemex]='X'; // New position of the player
break;
}
}
if (GetAsyncKeyState (VK_RIGHT)!=0) // Moves the player to the right
{
int deuxiemex = x+1;
switch (gMap [y][deuxiemex])
{
case' ':
gMap[y][x] = ' '; // Replaces the old position of the player
x++;
gMap[y][deuxiemex] = 'X'; // New position of the player
break;
}
}
if (GetAsyncKeyState (VK_SPACE)!=0)
{
y--;
gMap[y][x] = '#'; // Alien bombs falling downward
}
break;
case'#':
gMap [y][x] = ' ';
y--;
if (gMap [y][x] != '!' && gMap [y][x] != '@')// The shooter's bullet
{
gMap[y][x] = '!';
}
elseif (gMap [y][x] == '@') // delete's the alien if the bullet hits the alien
{
gMap [y][x] = ' ';
}
break;
enemyrandnum = rand()%50+1; // randomizes where the alien bomb's fall
if (enemyrandnum == 1)
{
y++;
gMap[y][x] = '*'; // What alien bombs will look like
}
break;
case'*':
gMap[y][x] = ' ';
if (gMap != 41 && gMap != 'X') // if the bomb hits the boarder, then nothing is deleted
{
y++;
gMap[y][x] = '*';
}
ifelse (gMap == 'X') // Takes away 20 points of health if the bomb hits the player
{
Shealth-=20;
if (Gmaxhealth <=0) // When the health is all used up, then the game ends and you lose.
{
endgame = true;
}
break;
}
}
}
Sleep (speedtimer);
}
system ("cls");
cout << "GAME OVER" << endl;
system ("PAUSE");
return EXIT_SUCCESS;
}
Not sure but i think its because gMap is an array and your trying to access it as an int. I think you have to dereference it like gMap[x][y] or whatever array element you want to check.
Like
Thank you @jidder & @toum. That fixed the problem, now the output is completely wrong. When it outputs, the code has a multitude of numbers and then the health score appears.
#include <iostream>
#include <windows.h>
#include <ctime>
#include <string>
#include <cstdlib>
usingnamespace std;
string gameboard [40][20] // Game board where the game takes place
{
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""! @ !""! @ @ !""! @ @ @ !""! @ @ @ @ !""! @ @ @ @ @ !""! @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ !""! @ @ @ @ @ !""! @ @ @ @ !""! @ @ @ !""! @ @ !""! @ !""! !""! !""! !""! !""! !""! !""! X !""!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
};
bool endgame = false;
int speedtimer = 100; // How fast the player can move
int enemyrandnum = rand() %50 + 1; // The aliens' bombs landing in random places
int grand (time(0)); // How fast the bombs are falling
int Gmaxhealth = 400; // The maximum health a player has
int Shealth = Gmaxhealth; // Calculates how much health is left for the player
int main()
{
constint Width = 40, Height = 20;
char gameboard [Height][Width];
for(int i = 0; i < 20; i++)
{
for(int j = 0; j < 40; j++)
{
if(i == 0 || i == 19 || j == 0 || j == 39) // Prints out the gameboard
gameboard[i][j] = '!';
else
gameboard[i][j] = ' ';
}
}
for(int i = 0; i < 20; i++)
{
for(int j = 0; j < 40; j++)
{
cout<<gameboard[i][j];
}
cout<<endl;
}
while (endgame==false)
{
for (int y=0; y<40; y++) // to move the player
{
int gameboard[y];
cout << gameboard[y];
}
cout << "HEALTH:" << Gmaxhealth << "/" << Shealth; // Updates current health of player
for (int y=0; y<40; y++) // moves player in the array
{
for (int x=0; x<20; x++) // moves player in the array
{
int gameboard[y][x];
switch (gameboard[y][x])
{
case'X':
if (GetAsyncKeyState (VK_LEFT)!=0) // moves the player to the left
{
int deuxiemex = x-1; // new position of the player
switch (gameboard [y][deuxiemex])
{
case' ':
gameboard[y][x] = ' '; // Replaces old position of the player
x--;
gameboard[y][deuxiemex]='X'; // New position of the player
break;
}
}
if (GetAsyncKeyState (VK_RIGHT)!=0) // Moves the player to the right
{
int deuxiemex = x+1;
switch (gameboard [y][deuxiemex])
{
case' ':
gameboard[y][x] = ' '; // Replaces the old position of the player
x++;
gameboard[y][deuxiemex] = 'X'; // New position of the player
break;
}
}
if (GetAsyncKeyState (VK_SPACE)!=0)
{
y--;
gameboard[y][x] = '#'; // Alien bombs falling downward
}
break;
case'#':
gameboard [y][x] = ' ';
y--;
if (gameboard [y][x] != '!' && gameboard [y][x] != '@')// The shooter's bullet
{
gameboard[y][x] = '!';
}
elseif (gameboard [y][x] == '@') // delete's the alien if the bullet hits the alien
{
gameboard [y][x] = ' ';
}
break;
enemyrandnum = rand()%50+1; // randomizes where the alien bomb's fall
if (enemyrandnum == 1)
{
y++;
gameboard[y][x] = '*'; // What alien bombs will look like
}
break;
case'*':
gameboard[y][x] = ' ';
if (gameboard [y][x]!= 41 && gameboard [y][x] != 'X') // if the bomb hits the boarder, then nothing is deleted
{
y++;
gameboard[y][x] = '*';
}
elseif (gameboard [y][x] == 'X') // Takes away 20 points of health if the bomb hits the player
{
Shealth-=20;
if (Gmaxhealth <=0) // When the health is all used up, then the game ends and you lose.
{
endgame = true;
}
break;
}
}
}
Sleep (speedtimer);
}
cout << "GAME OVER" << endl;
system ("PAUSE");
return EXIT_SUCCESS;
}
}
Now boarder prints out, but the contents within still need to look like the array above.
The updated code should not even compile according to C++ standard. You are declaring an int array of non-const size in lines 67, 75, and that's not allowed.
Also, what is line 9? you declare a 2D array of strings, but initialize one string only. So now gameboard[0][0] = the entire board picture, while boards[0][1] to [40][20] are left blank. I belive you ment to declare 2D array of chars?
Lines 81-82:
1 2
int deuxiemex = x-1; // new position of the player
switch (gameboard [y][deuxiemex])
You are allowing deuximex to be '-1'. That would crash the program.
@jidderWould that work for making the triangle on the screen ?
How would you make
gameboard[x][y] = '@'
make a triangle on the screen ??
@JockX
It does compile properly but the output is not coming out as wanted. My teacher suggested I use an array of strings. But I am trying anything for the program to work.
You are allowing deuximex to be '-1'. That would crash the program.
It is not crashing the program, but how would you fix it ?
just select the coordinates of where you want the triangle to be.
say the top of the triangle was at position down 1, 20 across then the code would be.
gameboard[20][1] = '@';
It would probably be easier to create a for loop to fill it in for you though similar to the one i posted before.
Also this
He meant redraw aja output after each time anything moves or is updated you can move curso r to new x y coord and put a whitespace at the old x y or just put a bunch of new lines then redraw the whole board