Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

23020046 - Hoàng Hữu Đức - L7-Snake #328

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
45 changes: 25 additions & 20 deletions AdvProg_L7-Snake/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,19 @@ Game::~Game()
***/

void Game::snakeMoveTo(Position pos) {
// START CODE HERE
//
//
//
//
// END CODE HERE
if (getCellType(pos) == CELL_OFF_BOARD || getCellType(pos) == CELL_SNAKE) {
status = GAME_OVER;
return;
}

if (getCellType(pos) == CELL_CHERRY) {
score += 1;
snake.eatCherry();
addCherry();
return;
}

setCellType(pos, CELL_SNAKE);
}


Expand All @@ -72,12 +79,7 @@ void Game::snakeMoveTo(Position pos) {
***/
void Game::snakeLeave(Position position)
{
// Suggestion: use setCellType() method in Game class
// START CODE HERE
//
//
//
// END CODE HERE
setCellType(position, CELL_EMPTY);
}


Expand All @@ -104,8 +106,8 @@ void Game::processUserInput(Direction direction)
***/
bool Game::canChange(Direction current, Direction next) const {
if (current == UP || current == DOWN)
return 0; // YOUR CODE HERE
return 0;// YOUR CODE HERE
return (next != UP && next != DOWN);
return (next != LEFT && next != RIGHT);
}


Expand All @@ -128,14 +130,14 @@ void Game::nextStep()
{
while (!inputQueue.empty()) {
// get the input direction from input queue
Direction next ; // YOUR CODE HERE

Direction next = inputQueue.front();
inputQueue.pop();
// remove the front of input queue
// YOUR CODE HERE

// check if snake can move to the next direction, set current direction as next
if (canChange(currentDirection, next)) {
// YOUR CODE HERE
currentDirection = next;
break;
}
}
Expand All @@ -162,15 +164,15 @@ void Game::addCherry()
// init a random position inside the play screen (width, height)
// Suggestion: use rand() function

Position randomPos; // YOUR CODE HERE
Position randomPos = {rand() % width, rand() % height}; // YOUR CODE HERE

// check if the randomPos is EMPTY
if (getCellType(randomPos) == CELL_EMPTY) {

// assign the cherry position as randomPos, and set randomPos type as CELL_CHERRY

// YOUR CODE HERE
// YOUR CODE HERE
cherryPosition = randomPos;
setCellType(cherryPosition, CELL_CHERRY);

break;
}
Expand Down Expand Up @@ -199,6 +201,9 @@ void Game::setCellType(Position pos, CellType cellType)
// START CODE HERE
//
// END CODE HERE
if (pos.isInsideBox(0, 0, width, height)) {
squares[pos.y][pos.x] = cellType;
}
}


Expand Down
26 changes: 23 additions & 3 deletions AdvProg_L7-Snake/Snake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ Snake::~Snake()
p = nextNode;
}
*/
SnakeNode* p = tail;

while (p != NULL) {
SnakeNode* nxt = p->next;
delete p;
p = nxt;
}
}

// DO NOT CHANGE METHOD
Expand Down Expand Up @@ -54,6 +61,8 @@ void Snake::growAtFront(Position newPosition)
// head of snake grow at new position

/* YOUR CODE HERE */
head->next = new SnakeNode(newPosition);
head = head->next;
}


Expand Down Expand Up @@ -84,14 +93,18 @@ void Snake::slideTo(Position newPosition)
if (tail->next == nullptr) {
// position is assigned by new position.
/* YOUR CODE HERE */
tail->position = newPosition;
}
else {
SnakeNode *oldTailNode = tail;
//cut the old tail off the snake
/* YOUR CODE HERE */

tail = tail->next;
oldTailNode->next = NULL;
// move it to the head of the snake
/* YOUR CODE HERE */
oldTailNode->position = newPosition;
head->next = oldTailNode;
head = oldTailNode;
}
}
Expand All @@ -111,6 +124,7 @@ void Snake::slideTo(Position newPosition)
void Snake::eatCherry()
{
/* YOUR CODE HERE */
cherry++;
}

/***
Expand Down Expand Up @@ -142,18 +156,24 @@ void Snake::eatCherry()
void Snake::move(Direction direction)
{
Position newPosition = head->position.move(direction);

game.snakeMoveTo(newPosition);
/* YOUR CODE HERE */

// If gameOver, return ;
/* YOUR CODE HERE */
if (game.isGameOver()) {
return;
}

// If cherry > 0, cherry descrease one and growAtFront() with newPosition
if (cherry > 0) {
/* YOUR CODE HERE */
cherry--;
growAtFront(newPosition);
} else {
game.snakeLeave(tail->position);
/* YOUR CODE HERE */
/* YOUR CODE HERE */
slideTo(newPosition);
}
}

Expand Down
Loading