Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions cpp-programming/Project_Snake_Game/snake-game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

void Logic() {
const int MAX_TAIL = 100;
// PRECOGS_FIX: clamp nt to valid bounds to prevent out-of-bounds access
if (nt < 0) nt = 0;
if (nt > MAX_TAIL) nt = MAX_TAIL;

int prevx = x;
int prevy = y;
int prev2x, prev2y;
tx[0] = x;
ty[0] = y;
for (int i = 1; i < nt; i++) {
prev2x = tx[i];
prev2y = ty[i];
tx[i] = prevx;
ty[i] = prevy;
prevx = prev2x;
prevy = prev2y;
}


switch (dir)
{
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;

default:
break;
}
if (x >= width) x = 0; else if (x < 0) x = width - 1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;

// Use bounded tailCount for collision checks
int tailCount = nt;
if (tailCount < 0) tailCount = 0;
if (tailCount > MAX_TAIL) tailCount = MAX_TAIL;
for (int i = 0; i < tailCount; i++) {
if (tx[i] == x && ty[i] == y) {
lostgame();
}

}

if (x == fx && y == fy) {
score += 1;
if (nt < MAX_TAIL) nt++; // PRECOGS_FIX: only increase nt if capacity allows
fx = rand() % width;
fy = rand() % height;
}
}