Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Find a mistake in this C++ code: It is a code of snake game. #include #include bool gameOver; const int width = 20, height =

Find a mistake in this C++ code:

It is a code of snake game.

#include #include

bool gameOver; const int width = 20, height = 20; int x,y, FruitX, FruitY, score; enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN}; eDirection dir;

int TailX[100], TailY[100]; int nTail = 0;

void Setup() { initscr(); clear(); noecho(); cbreak(); curs_set(0);

gameOver = false; dir = STOP; x = width/2; y = height/2; FruitX = (rand()% width)+1; FruitY = (rand()% height)+1; score = 0;

}

void Draw() {

clear();

for (int i = 0; i < width+2; i++) mvprintw(0,i,"+");

for (int i = 0; i < height+2; i++) { for (int j = 0; j < width+2; j++) { if (i == 0 | i == 21) mvprintw(i,j,"+"); else if (j == 0 | j == 21) mvprintw(i,j,"+"); else if (i == y && j == x) mvprintw(i,j,"O"); else if (i == FruitY && j == FruitX) mvprintw(i,j,"0"); else for (int k = 0; k < nTail; k++) { if (TailX[k] == j && TailY[k] == i) mvprintw(i,j,"o");

}

} }

mvprintw(23,0,"Score %d",score); refresh();

}

void Input() { keypad(stdscr, TRUE); halfdelay(1);

int C = getch(); switch (C) { case KEY_LEFT: dir = LEFT; break; case KEY_RIGHT: dir = RIGHT; break; case KEY_UP: dir = UP; break; case KEY_DOWN: dir = DOWN; break; case 113: gameOver = true; break; } } void Logic() { int prevX = TailX[0]; int prevY = TailY[0]; int prev2X,prev2Y; TailX[0] = x; TailY[0] = y;

for (int i = 1; i < nTail; i++) { prev2X = TailX[i]; prev2Y = TailX[i]; TailX[i] = prevX; TailY[i] = prevY; prevX = prev2X; prevY = prev2Y; }

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

if (x > width || x < 1 || y < 1 || y > height) gameOver = true;

if (x == FruitX && y == FruitY) { score++; FruitX = (rand() % width)+1; FruitY = (rand() % height)+1; nTail++; } for (int i = 0; i < nTail; i++) if (TailX[i] == x && TailY[i] == y) { gameOver = true; } }

int main() { Setup();

while (!gameOver) { Draw(); Input(); Logic(); } getch(); endwin(); return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

More Books

Students also viewed these Databases questions

Question

Define organisational structure

Answered: 1 week ago

Question

Define line and staff authority

Answered: 1 week ago

Question

Define the process of communication

Answered: 1 week ago

Question

Explain the importance of effective communication

Answered: 1 week ago

Question

* What is the importance of soil testing in civil engineering?

Answered: 1 week ago

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago