Character moving when it shouldn't

Hey,
i decided to make a space invaders game after seeing someone else trying on here.
Everything works up to now pretty much.
There is just one problem with the movement.
When i go right and reach the boundary it stops as it should.
When i go left though when i reach the boundary the character moves back right one space instead of staying where it is.
The code for both is practically identical.
I was wondering if anyone knows the cause ?
Any other criticisms are welcome :)
Graphics.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #ifndef GRAPHICS_H_INCLUDED
#define GRAPHICS_H_INCLUDED
class Graphics
{
private:
    static const int Width = 40, Height = 20;
    char drawGame [Height][Width];
public:
    void makegame();
    void drawgame();
    void drawPlayer(int x , int y);
};


#endif // GRAPHICS_H_INCLUDED

Physics.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef PHYSICS_H_INCLUDED
#define PHYSICS_H_INCLUDED
#include "Graphics.h"

class Physics
{
private:
Graphics SpaceGraph;
int PlayerPos;
public:
Physics():PlayerPos(19){}
    void MovePlayer();
    void Play();

};

#endif // PHYSICS_H_INCLUDED 

Physics.cpp
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
#include <iostream>
#include "Physics.h"
#include <conio.h>

void Physics::MovePlayer()
{
    using namespace std;
    char Move(0);
    int y = 18;
    while(Move != 'l')
    {
        SpaceGraph.makegame();
        Move = getch();
        switch (Move)
        {
            case 's' :
            if(PlayerPos != 1)
            {
            PlayerPos--;
            SpaceGraph.drawPlayer(y, PlayerPos);
            break;
            }
            case 'd':
            if(PlayerPos != 38)
            {
            PlayerPos++;
            SpaceGraph.drawPlayer(y, PlayerPos);
            break;
            }
        }
    }

}
void Physics::Play()
{
    SpaceGraph.makegame();
    MovePlayer();
}

Graphics.cpp
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
#include <iostream>
#include "Graphics.h"
void Graphics::makegame()
{
        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] = ' ';
        }
    }
}
void Graphics::drawgame()
{
    using namespace std;
    cout << string( 100, '\n' );
    for(int i = 0; i < 20; i++)
    {
    for(int j = 0; j < 40; j++)
        {
            cout<<drawGame[i][j];
        }
        cout<<endl;
    }
}
void Graphics::drawPlayer(int x = 18 , int y = 19)
{
    char X = 'X';
    drawGame[x][y] = X;
    drawgame();
}

main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Physics.h"

using namespace std;

int main()
{
Physics SpaceInvader;
SpaceInvader.Play();
}
Didn't really read very closely, but I guess your left boudary is 0 not 1.
Don't think its that.
Because even if i had it set to 10 or whatever it's moving when it shouldn't be in the opposite direction to the way it should be.
I think you'd have to run the code to understand what i mean.
Thanks anyway
In physics.cpp the switch-case statement has the break in the wrong place.
Because it was inside the block controlled by the if statement, when the condition is false, control falls through and executes the following case -in this case it executes the code for case 'd' as well.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        switch (Move)
        {
            case 's' :
                if(PlayerPos != 1)
                {
                    PlayerPos--;
                    SpaceGraph.drawPlayer(y, PlayerPos);
                }
                break;

            case 'd':
                if(PlayerPos != 38)
                {
                    PlayerPos++;
                    SpaceGraph.drawPlayer(y, PlayerPos);
                }
                break;
        }
ahhh right.
That was pretty stupid.
Thanks :)
also what's the best way to clear the console screen ?
what's the best way to clear the console screen ?

This depends on the operating system (and possibly on the compiler).
There are some useful solutions here:
http://www.cplusplus.com/forum/articles/10515/
Topic archived. No new replies allowed.