Snake Game boundaries checking

Hi guys so after debugging I found the x axis seems to be going up and down and the y axis seems to be going from right to left instead of the other way around,

with that being said when my snake 'O' when x equals 0 the program ends at first I thought something was wrong but I noticed that it had to be -1 due to having a for loop above so I minus one,anyway when the 'O' reaches 0 it still ends the game one before the '#' top wall

I want the snake to be able to move around the space beneath the wall not end the game when its one under or beside the wall

anyone notice whats wrong?

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

using namespace std;

enum edir{

   STOP = 0,
   UP,
   DOWN,
   RIGHT,
   LEFT
};

bool gameOver;
const int WIDTH = 20;
const int HEIGHT = 20;
int fruitX,fruitY,x,y;
edir dir;


void setUp(){

  gameOver = false;
  dir = STOP;
  x = WIDTH/2;
  y = HEIGHT/2;
  fruitX = rand() % WIDTH;
  fruitY = rand() % HEIGHT;

}

void draw(){

   system("cls");

   for(int i = 0; i < WIDTH+1; i++){

        cout << "#";
    }
     cout << endl;

     for(int i = -1; i <= HEIGHT+2; i++){

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

            if(j == 0){

                cout << "#";
            }
            if(j == WIDTH-1){

                cout << "#";
            }
            if(x == i && y == j){

                    if(x == -1){

                        gameOver = true;
                    }
                    cout << "O";
            }
            else if(fruitX == i && fruitY == j){
                cout << "F";
            }
            else
                cout << " ";

        }
           cout << endl;
     }

     for(int i = 0; i < WIDTH+1; i++){

        cout << "#";
     }

   }


void input(){

   while(_kbhit()){

    switch(_getch()){

case 'w':
    dir = UP;
    break;
case 'z':
    dir = DOWN;
    break;
case 'a':
    dir = LEFT;
    break;
case 's':
    dir = RIGHT;
    break;

    }
   }
}

void logic(){

 switch(dir){

 case UP:
     x--;
     break;
 case DOWN:
     x++;
     break;
 case LEFT:
    y--;
    break;
 case RIGHT:
    y++;
    break;
 }

}


int main()
{
   setUp();
   while(!gameOver){

   draw();
   input();
   logic();
   }
}
Ok I figured out why that won't work by saying -1 in the for loop I am just making the loop bigger anyway I came up with a solution that does not work :@

when i equals zero I tried to print the top wall but there seems to be a space between each # and on some parts there is two '#' in a row then a space

can't figure out how I can create the top and bottom parts of the wall
Ok guys solution two seemed to work for x BUT not for y

I did some debugging by printing the y co-ordinate and I find that the # must be printed at -1 because 0 is located at a blank space just before the '#' side wall

anyone have any idea why this is happening

any help would be more than appreciated,

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
void draw(){

   system("cls");

     bool notFirst = false;
     bool notLast = false;

     for(int i = 0; i <= HEIGHT+2; i++){

        for(int j = 0; j <= WIDTH+2; j++){

            if(i > 0){
                notFirst = false;
            }

            if(i < HEIGHT+2){

                notLast = false;
            }

            if(i == 0 && j <= WIDTH-1){

                cout << "#"; // top wall
                notFirst = true;
            }

            if(i == HEIGHT+2 && j <= WIDTH-2){

                cout << "#"; // bottom wall
                notLast = true;
            }

            if(j == 0 && i != 0){

                cout << "#";
            }
            if(j == WIDTH-1){

                cout << "#";
            }
            if(x == i && y == j){

                    if(x == 0 || x == HEIGHT+2){

                        gameOver = true;
                    }
                    if(y == 0 || y == WIDTH){

                        gameOver = true;
                    }
                    cout << y;
                    cout << "O";
            }
            else if(fruitX == i && fruitY == j){
                cout << "F";
            }
            else{
                if(!notFirst && !notLast){
                cout << " ";
                }
            }
        }
           cout << endl;
     }

   }
Why don't you create a board for the game?
Then you just need to set the position of the fruit and the snake inside the board.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
vector<string> Board = 
{
  "####################",
  "#                  #",
  "#                  #",
  "#                  #",
  "#                  #",
  "#                  #",
  "#                  #",
  "#                  #",
  "#                  #",
  "#   x              #",
  "#                  #",
  "#                  #",
  "#                  #",
  "#                  #",
  "#                  #",
  "#    0             #",
  "#                  #",
  "#                  #",
  "#                  #",
  "#                  #",
  "####################"
};
hey Thomas great idea

the only problems I could think of is how would you check if you are inside the board?

thanks
The x pos of the snake must be between 0 and the width of the board.
The y pos of the snake must be between 0 and the height of the board.

You could set the position like this:
Board[5][6] = 'O' for the snake
Topic archived. No new replies allowed.