console snake game hit not being detected

Hi guys I recently asked a similar question sorry about posting another thread I just have decided to start over because the thread was poorly explained and the title did not explain what the problem really was,

originally I thought the problem was random numbers not being generated but that does not seem to be the case,the problem seems to be that when my snake 'O' hits the food piece 'F' the hit does not get detected "HEY" never gets printed out

I tried using a debugger and setting break points but get a

Program received signal SIGTRAP, Trace/breakpoint trap. In ?? () () Cannot find bounds of current function Cannot find bounds of current function

error

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
 #include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <time.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;
  srand(time(NULL));
  fruitX = rand() % WIDTH-1;
  fruitY = rand() % HEIGHT-1;
  if(x == fruitX && y == fruitY){
    fruitX = (rand() % WIDTH-1)+1;
    fruitY = (rand() % HEIGHT-1)+1;
  }
}

void draw(){

     system("cls");

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

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

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

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

            if(j == 0){

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

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

                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;
 }

 if(x > WIDTH || x < 0 || y > HEIGHT-2 || y < 0) // check for in bounds
    gameOver = true;
  if(x == fruitX && y == fruitY){
        fruitX = (rand() % WIDTH-1)+1;
        fruitY = (rand() % HEIGHT-1)+1;
        cout << "HEY";
    }

}

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

   draw();
   input();
   logic();
   }
   if(gameOver){
    cout << endl;
    cout << endl;
    cout << "GAME OVER!!!!!!!!!!!!" << endl;
   }
}
In the draw() function i is the y-coordinate and j is the x-coordinate but when you compare them with the coordinates of the snake you have swapped them.

To avoid mistakes like this I would recommend naming the variables something more descriptive. i could be named y and j could be named x, and x and y could be named snakeX and snakeY which is more consistent with how you have named the coordinates for the fruit. If you don't want to rename the snake coordinates you could at least rename i and j as row and col (column).
Last edited on
wow nice spot Peter :o

this bug has was "bugging" me for hours,it worked

honestly I thought it wouldn't have mattered if I mixed i and j up with x and y but it did

also very good point better naming could have solved this situation will rename the variables right away

thanks so much :)
Last edited on
Topic archived. No new replies allowed.