Simple game in c++

Hi, I have a problem - when I want to move, after compilation and start-up
programe tells that it can't be done (wrong move). I have no idea
where I made a mistake. Can anybody help and tell me what I should improve?
I have just started doing such programs (which are not the best as you can see)
so I'll be gratefull for any kind of 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include<iostream>
#include<fstream>
#define x 13
#define y 5

using namespace std;
char background[ x ][ y ];
int a = 1, b = 1, i, j;
char buf;
char move;

int main() {
    fstream file;
    file.open( "map1.txt", ios::in | ios::out );
    if( file.good() == false ) cout << "The file can't be opened!";
    
    noskipws( file );
    for( i = 0; i < 13; i++ ) {
        for( j = 0; j < 5; j++ ) {
            file >> background[ i ][ j ];
        }
    }
    for( i = 0; i < 13; i++ )
    {
        for( j = 0; j < 5; j++ )
        {
            cout << background[ i ][ j ];
        }
    }
    cout << endl;
    
    while( !( a == 10 && b == 1 ) ){
        cout << "Where do you want to move?" << endl;
        cin >> move;
        switch( move ) {
        case 'w':
            if( background[ a + 1 ][ b ] == ' ' ) {
                buf = background[ a ][ b ];
                background[ a ][ b ] = background[ a + 1 ][ b ];
                background[ a + 1 ][ b ] = buf;
                a++;
            }
            else if( background[ a + 1 ][ b ] == '#' && background[ a + 2 ][ b ] == ' ' ) {
                buf = background[ a + 1 ][ b ];
                background[ a + 1 ][ b ] = background[ a + 2 ][ b ];
                background[ a + 1 ][ b ] = buf;
                buf = background[ a ][ b ];
                background[ a ][ b ] = background[ a + 1 ][ b ];
                background[ a + 1 ][ b ] = buf;
                a++;
            }
            else if( background[ a + 1 ][ b ] == 'X' ) {
                cout << "wrong move";
                cout << endl;
            }
            else if( background[ a ][ b + 1 ] == '#' && background[ a ][ b + 2 ] == 'X' ) {
                cout << "wrong move";
                cout << endl;
            };
            break;
            
        case 's':
            if( background[ a - 1 ][ b ] == ' ' ) {
                buf = background[ a ][ b ];
                background[ a ][ b ] = background[ a - 1 ][ b ];
                background[ a - 1 ][ b ] = buf;
                a--;
            }
            else if( background[ a - 1 ][ b ] == '#' && background[ a - 2 ][ b ] == ' ' ) {
                buf = background[ a - 1 ][ b ];
                background[ a - 1 ][ b ] = background[ a - 1 ][ b ];
                background[ a - 1 ][ b ] = buf;
                buf = background[ a ][ b ];
                background[ a ][ b ] = background[ a - 1 ][ b ];
                background[ a - 1 ][ b ] = buf;
                a--;
            }
            else if( background[ a - 1 ][ b ] == 'X' ) {
                cout << "wrong move";
                cout << endl;
            }
            else if( background[ a - 1 ][ b ] == '#' && background[ a - 2 ][ b ] == 'X' ) {
                cout << "wrong move";
                cout << endl;;
                break;
                
            case 'a':
                if( background[ a ][ b - 1 ] == ' ' ) {
                    buf = background[ a ][ b ];
                    background[ a ][ b ] = background[ a ][ b - 1 ];
                    background[ a ][ b - 1 ] = buf;
                    b--;
                }
                else if( background[ a ][ b - 1 ] == '#' && background[ a ][ b - 2 ] == ' ' ) {
                    buf = background[ a ][ b - 1 ];
                    background[ a ][ b - 1 ] = background[ a ][ b - 2 ];
                    background[ a ][ b - 1 ] = buf;
                    buf = background[ a ][ b ];
                    background[ a ][ b ] = background[ a ][ b - 1 ];
                    background[ a ][ b - 1 ] = buf;
                    b--;
                }
                else if( background[ a ][ b - 1 ] == 'X' ) {
                    cout << "wrong move";
                    cout << endl;
                }
                else if( background[ a ][ b - 1 ] == '#' && background[ a ][ b - 2 ] == 'X' ) {
                    cout << "wrong move";
                    cout << endl;
                };
                break;
                
            case 'd':
                if( background[ a ][ b + 1 ] == ' ' ) {
                    buf = background[ a ][ b ];
                    background[ a ][ b ] = background[ a ][ b + 1 ];
                    background[ a ][ b + 1 ] = buf;
                    b++;
                }
                else if( background[ a ][ b + 1 ] == '#' && background[ a ][ b + 2 ] == ' ' ) {
                    buf = mapa[ a ][ b + 1 ];
                    background[ a ][ b + 1 ] = background[ a ][ b + 2 ];
                    background[ a ][ b + 1 ] = buf;
                    buf = background[ a ][ b ];
                    background[ a ][ b ] = background[ a ][ b + 1 ];
                    background[ a ][ b + 1 ] = buf;
                    b++;
                }
                else if( background[ a ][ b + 1 ] == 'X' ) {
                    cout << "wrong move";
                    cout << endl;
                }
                else if( background[ a ][ b + 1 ] == '#' && background[ a ][ b + 2 ] == 'X' ) {
                    cout << "wrong move";
                    cout << endl;
                };
                break;
                
                default:
                cout << "wrong move";
                break;
            }
            system( "cls" );
            for( i = 0; i < 13; i++ ) {
                for( j = 0; j < 5; j++ ) {
                    cout << background[ i ][ j ];
                }
            }
        }
    }
    system( "pause" );
    return 0;
}
Last edited on
Topic archived. No new replies allowed.