building a wall console game

Hi guys I'm creating a simple snake console game and I have run into trouble while trying to build the wall for the right side and top and bottom

the left side seems to have worked great when I hit the wall it tells me invalid

but when I hit the right side wall my character overwrites the wall on the side,
I want an invalid to print if they hit the wall on the right side and also to and left

this is what I have so far,if you run it you can see why this isn't working

any help would be great


thanks

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
  #include <iostream>
#include <stdlib.h>

using namespace std;

char board[20][20];
int row = 1;
int col = 1;

void initBoard(){

   for(int i = 0; i < 20; i++){

        for(int j = 0; j < 20;j++){


            board[i][j] = ' ';
        }
    }

     for(int i = 0; i < 20; i++){  // make left side wall

        for(int j = 0; j < 1;j++){


            board[i][j] = '|';
        }
    }

    for(int i = 0; i < 20; i++){  // make right side wall

        for(int j = 19; j <= 20;j++){
            board[i][j] = '|';
        }
    }
}

bool inBounds(int row,int col){

    if((row <= 0 || row >= 20) || (col <= 0 || col >= 20)){

        return false;
    }else{
       return true;
    }
}

void printBoard(){

   for(int i = 0; i < 20; i++){
        cout << endl;
        for(int j = 0; j < 20;j++){

            cout << board[i][j];
        }
    }
   cout << endl;
}

int main()
{
    char o = 'O';
    initBoard();

    board[row][col] = 'O';
    int choice;

    cout << "1 up,2 down,3 right,4 left" << endl;

    while(true){

        printBoard();
        cin >> choice;
        switch(choice){

    case 1:
        if(inBounds(row+1,col)){

            board[row][col] = ' ';
            row++;
            board[row][col] = 'O';
            system("cls");
        }else{
         cout << "invalid " << endl;
         system("cls");

        }
        break;
        case 2:
        if(inBounds(row-1,col)){

            board[row][col] = ' ';
            row--;
            board[row][col] = 'O';
            system("cls");
        }else{

          cout << "invalid" << endl;
          system("cls");
        }
        break;
        case 3:
        if(inBounds(row,col+1)){

            board[row][col] = ' ';
            col++;
            board[row][col] = 'O';
            system("cls");
        }
        else{

          cout << "invalid" << endl;
          system("cls");
        }
        break;
        case 4:
        if(inBounds(row,col-1)){

            board[row][col] = ' ';
            col--;
            board[row][col] = 'O';
            system("cls");
        }
        else{
          cout << "invalid" << endl;
          system("cls");
        }
        break;
        default:
            cout << "invalid" << endl;
        }
    }
}
should in-bounds account for the wall characters and check column > 1 && column < 19 instead for true (or < 2 && > 18 for false)
Last edited on
Hi Jonnin

so I should change the inbounds function ?

*edit

I changed the logic in the inBounds function a little but still getting the same problem but this time when I go out of bounds the character goes to the next line instead

1
2
3
4
5
6
7
8
9
10
11

bool inBounds(int row,int col){

    if((row > 1  && row < 19) && (col > 1 || col < 19)){

        return true;
    }else{
       return false;
    }
}


*edit

sorry I should have used an && instead of ||

seems to be working for now =))

thanks
Last edited on
heres the updated game

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

#include <iostream>
#include <stdlib.h>

using namespace std;

char board[20][20];
int row = 2;
int col = 2;

void initBoard(){

   for(int i = 0; i < 20; i++){

        for(int j = 0; j < 20;j++){

            board[i][j] = ' '; // initialise board
        }
    }
     for(int i = 0; i < 20; i++){  // make left side wall

        for(int j = 0; j < 1;j++){

            board[i][j] = '|';
        }
    }
    for(int i = 0; i < 20; i++){  // make right side wall

        for(int j = 19; j <= 20;j++){
            board[i][j] = '|';
        }
    }

     for(int i = 0; i < 1; i++){  // make top of wall

        for(int j = 0; j < 20;j++){
            board[i][j] = '_';
        }
    }
    for(int i = 18; i < 19; i++){  // make bottom of wall

        for(int j = 0; j < 20;j++){
                board[i][j] = '-';
        }
    }
}

bool inBounds(int row,int col){

    if((row >= 1  && row <= 17) && (col > 1 && col < 19)){

        return true;
    }else{
       return false;
    }
}

void printBoard(){

   for(int i = 0; i < 20; i++){
        cout << endl;
        for(int j = 0; j < 20;j++){

            cout << board[i][j];
        }
    }
   cout << endl;
}

int main()
{
    char o = 'O';
    initBoard();

    board[row][col] = 'O';
    int choice;

    cout << "1 up,2 down,3 right,4 left" << endl;

    while(true){

        printBoard();
        cin >> choice;
        switch(choice){

    case 1:
        if(inBounds(row+1,col)){

            board[row][col] = ' ';
            row++;
            board[row][col] = 'O';
            system("cls");
        }else{
         cout << "invalid " << endl;
         system("cls");

        }
        break;
        case 2:
        if(inBounds(row-1,col)){

            board[row][col] = ' ';
            row--;
            board[row][col] = 'O';
            system("cls");
        }else{

          cout << "invalid" << endl;
          system("cls");
        }
        break;
        case 3:
        if(inBounds(row,col+1)){

            board[row][col] = ' ';
            col++;
            board[row][col] = 'O';
            system("cls");
        }
        else{

          cout << "invalid" << endl;
          system("cls");
        }
        break;
        case 4:
        if(inBounds(row,col-1)){

            board[row][col] = ' ';
            col--;
            board[row][col] = 'O';
            system("cls");
        }
        else{

          cout << "invalid" << endl;
          system("cls");
        }
        break;
        default:
            cout << "invalid" << endl;
        }
    }
}


the functionality works so far but the board looks a little wonky to be honest
Last edited on
Another update

here is the game now to be honest I'm not impressed with it but it does work in my opinion the code is pretty sloppy,

anyway here is the code now I need to implement a way to keep moving up and down and set the speed in whitch it moves up and down(not sure how this will be done)

any suggestions are more than welcome thanks

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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <cstdlib>

using namespace std;

char board[20][20];
int row = 2;
int col = 2;

void initBoard(){

   for(int i = 0; i < 20; i++){

        for(int j = 0; j < 20;j++){

            board[i][j] = ' '; // initialise board
        }
    }
     for(int i = 0; i < 20; i++){  // make left side wall

        for(int j = 0; j < 1;j++){

            board[i][j] = '|';
        }
    }
    for(int i = 0; i < 20; i++){  // make right side wall

        for(int j = 19; j <= 20;j++){
            board[i][j] = '|';
        }
    }

     for(int i = 0; i < 1; i++){  // make top of wall

        for(int j = 0; j < 20;j++){
            board[i][j] = '_';
        }
    }
    for(int i = 18; i < 19; i++){  // make top side wall

        for(int j = 0; j < 20;j++){
                board[i][j] = '-';
        }
    }
}

bool inBounds(int row,int col){

    if((row >= 1  && row <= 17) && (col >= 1 && col < 19)){

        return true;
    }else{
       return false;
    }
}

void printBoard(){

   for(int i = 0; i < 20; i++){
        cout << endl;
        for(int j = 0; j < 20;j++){

            cout << board[i][j];
        }
    }
   cout << endl;
}

int main()
{
    srand(time(0));
    char o = 'O';
    bool gotIt = false;
    bool correctPos = false;
    bool up = false;
    bool down = false; // implement a way to keep on moving it may be better to use if blocks instead of switch statement VV
    bool right = false;
    bool left = false;
    int foodRow = 1+(rand()%16);
    int foodCol = 1+(rand()%17);
    initBoard();

    cout << foodRow << endl;
    cout << foodCol << endl;

    while(!correctPos){

        if(board[foodRow][foodCol] = ' ' ){

            correctPos = true;
            board[foodRow][foodCol] = 'F';
        }else{

           foodRow = 1+(rand()%16);
           foodCol = 1+(rand()%17);
        }
    }

    board[row][col] = 'O';
    int choice;

    cout << "1 up,2 down,3 right,4 left" << endl;

    while(true){

        printBoard();
        cin >> choice;

        if(gotIt){

            foodRow = 1+(rand()%16);
            foodCol = 1+(rand()%17);
            board[foodRow][foodCol] = 'F';
            gotIt = false;
        }
        switch(choice){

    case 1:
        if(inBounds(row+1,col)){

            board[row][col] = ' ';
            row++;
            board[row][col] = 'O';
            system("cls");
        }else{
         cout << "invalid " << endl;
         system("cls");

        }
        break;
        case 2:
        if(inBounds(row-1,col)){

            board[row][col] = ' ';
            row--;
            board[row][col] = 'O';
            system("cls");
        }else{

          cout << "invalid" << endl;
          system("cls");
        }
        break;
        case 3:
        if(inBounds(row,col+1)){

            board[row][col] = ' ';
            col++;
            board[row][col] = 'O';
            system("cls");
        }
        else{

          cout << "invalid" << endl;
          system("cls");
        }
        break;
        case 4:
        if(inBounds(row,col-1)){

            board[row][col] = ' ';
            col--;
            board[row][col] = 'O';
            system("cls");
        }
        else{
          cout << "invalid" << endl;
          system("cls");
        }
        break;
        default:
            cout << "invalid" << endl;
        }
        if(board[row][col] == board[foodRow][foodCol]){

            gotIt = true;
        }
    }
}



working code is good, clean broken code is less good. Better is clean working code, of course, but one thing at a time.

consider...
for(int i = 0; i < 20; i++){ // make left side wall

for(int j = 0; j < 1;j++){

board[i][j] = '|';
}
}

if you don't plan on making the boards differently, this is too much..
for(int i = 0; i < 20; i++){ // make left and right side wall
{
board[i][0] = '|';
board[i][19] = '|';
}
and even more amusing, you can do top and bottom with one liners:
memset(&(board[0][0]), '_', 20); //top wall
memset(&(board[19][0]), '_', 20); //bottom wall
//if I got that right, memset should be used very, very carefully, some say not at all...

5 lines replaces almost 50, and it is more efficient to boot.

depending on what the game is supposed to do, sleep(some time) or a timer routine can govern input / rate of execution.



Line 2: Why are you including <stdlib.h> when you also include <cstdlib> ?

Line 8-10: Globals should be avoided.

Line 8 + many more places: You should define the size of your board using consts. If you ever want to change the size of your board, you have many places to change. Using consts properly, you would have only a single place to change.

thanks guys

and very true I've used the memset function before it's a c function I think that would be a neat idea to be honest would save quite a bit of code

I never knew that so stdlib.h includes cstdlib or vice versa?

and yes I hear global variables should be avoided at all costs I have not read into it yet but will do

also sleep function would be a very good idea I decided to check another persons snake c++ console game and the idea he came up with,he has a video on youtube anyway I followed his video and actually followed his idea a little bit(just for movement of the snake) is this good or bad practice?

I never knew that so stdlib.h includes cstdlib or vice versa?

The difference between <stdlib.h> and <cstdlib> is that <cstdlib> is in the std namespace.
C and <stdlib.h> have no concept of namespaces.

Having using namespace std; makes the difference moot. However, using namespace std; is generally considered a poor idea.
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

I personally detest sleep functions in games, but to each his own.
sleep will be fine for a text based game. It probably is not the best for a graphical game with lots of complex code, but here, its ok. Its really meant to sleep worker threads, not the main thread, but I have a thing for simple solutions for simple problems :)



Topic archived. No new replies allowed.