Space Invaders code help

Pages: 12
May 5, 2013 at 9:46pm
I am having compiler issues with this code. Here are the two issues.

1. The compiler expects a ( before the else statement.
2. At the end of the program it expects a }.

I have tried fixing these errors but for some reason it is not working. HELP !!

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
#include <iostream>
#include <windows.h>
#include <ctime>
#include <string>
#include <cstdlib>

using namespace 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] = '!';
                          }
                         else if (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] = '*';
                        }

                        if else (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;
}
May 5, 2013 at 9:59pm
should be
"else if"
not
"if else"
also you are missing a bracket at the end of your code.
the bracket currently at the end is for the while statement.

hope this helps :)
May 5, 2013 at 10:04pm
Thank you !
But what about line 116, it says:

ISSO C++ forbids comparison between pointer and integer.

I have no clue how to fix the problem.
May 5, 2013 at 10:09pm
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
 
if (gMap[y][x] != 41 && gMap[y][x]  != 'X') 

May 5, 2013 at 10:11pm
It should be somethink like "gMap[y][x]" and not simply "gMap"
May 5, 2013 at 11:47pm
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.

I do not understand why the output is incorrect.
May 9, 2013 at 9:38pm
For some reason, the program is not working as planned. My gameboard array will not print out. Instead the array prints out as numbers. Please HELP !!
May 10, 2013 at 10:44am
Maybe try it this way ?
If you dont understand anything just ask.
hope it helps :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    const int Width = 40, Height = 20;
    char drawGame [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)
                drawGame[i][j] = '!';
            else
                drawGame[i][j] = ' ';
        }
    }
    for(int i = 0; i < 20; i++)
    {
    for(int j = 0; j < 40; j++)
        {
            cout<<drawGame[i][j];
        }
        cout<<endl;
    }
May 10, 2013 at 3:19pm
can you post your updated code ?
May 10, 2013 at 11:08pm
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
#include <iostream>
#include <windows.h>
#include <ctime>
#include <string>
#include <cstdlib>

using namespace 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()
{
    const int 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] = '!';
                          }
                         else if (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] = '*';
                        }

                        else if (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.
Last edited on May 10, 2013 at 11:09pm
May 11, 2013 at 12:02am
Just use
 
gameboard[x][y] = '@'

x and y are the coordinates of where you want the character .
@ is the character you want there.
May 11, 2013 at 12:30am
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.
Last edited on May 11, 2013 at 12:48am
May 11, 2013 at 2:28am
@jidder Would 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 ?
Last edited on May 12, 2013 at 1:45am
May 12, 2013 at 4:07am
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
string gameboard [40][20] // Game board where the game takes place
{
    "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    "!                @                  !"
    "!               @ @                 !"
    "!              @ @ @                !"
    "!             @ @ @ @               !"
    "!            @ @ @ @ @              !"
    "! @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ !"
    "!            @ @ @ @ @              !"
    "!             @ @ @ @               !"
    "!              @ @ @                !"
    "!               @ @                 !"
    "!                @                  !"
    "!                                   !"
    "!                                   !"
    "!                                   !"
    "!                                   !"
    "!                                   !"
    "!                                   !"
    "!                X                  !"
    "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
};

isn't needed at all.

Finally your going to want to draw the board everytime any character moves so you might want to think about incorporating a draw function.
May 12, 2013 at 4:39am
I have never heard of the draw function, how does it work for this problem ??
May 12, 2013 at 4:56am
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
May 12, 2013 at 4:58am
@giblit

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


Is there a quick example you can write ??
May 12, 2013 at 5:17am
Well you probably don't want the cursor showing also during your game but here are what I do
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
#include <windows.h>
#include <iostream>
void hideCursor( void )
{
    HANDLE outHandle = GetStdHandle( STD_OUTPUT_HANDLE );
    SetConsoleCursorInfo( outHandle , false );
}

void gotoXY( const unsigned int x , const unsigned int y )
{
    HANDLE outHandle = GetStdHandle( STD_OUTPUT_HANDLE );
    COORD position = { x , y };
    SetConsoleCursorPosition( outHandle , position );
}

int main( void )
{
    hideCursor();

    gotoXY( 5 , 10);
    std::cout << "Hello world!" << std::flush;
    Sleep( 1000 );

    gotoXY( 11 , 10 );
    std::cout << "W" << std::flush;
    Sleep( 1000 );

    gotoXY( 5 , 10 );
    std::cout << "             " << std::flush;
    Sleep( 1000 );

    gotoXY( 0 ,  0 );
    std::cout << "Good bye!" << std::endl;

    return( 0 );
}


Try that and if you have any questions I can answer tomorrow probably going to bed shortly
May 12, 2013 at 11:15pm
I finally got the output and the shooter correctly but the @ symbol is still not working. I will send you the code.
May 13, 2013 at 11:07pm
Why are is your gameboard a two dimensional array of 800 strings? You've only delcared ten. You probably wanted to use char instead.
Pages: 12