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

Task 3, 3 test not passing #10

Open
wants to merge 1 commit 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
25 changes: 20 additions & 5 deletions SnakeController/SnakeController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ Controller::Controller(IPort& p_displayPort, IPort& p_foodPort, IPort& p_scorePo
void Controller::handleTimePassed(const TimeoutInd&)
{
Segment newHead = getNewHead();

if(doesCollideWithSnake(newHead))
{
notifyAboutFailure();
Expand Down Expand Up @@ -99,10 +98,12 @@ void Controller::handleTimePassed(const TimeoutInd&)

void Controller::handleDirectionChange(const DirectionInd& directionInd)
{
auto direction = directionInd.direction;
if(!isPauseOn){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For smaller number of indentations just use if (isPauseOn) return; :)

auto direction = directionInd.direction;

if ((m_currentDirection & 0b01) != (direction & 0b01)) {
m_currentDirection = direction;
if ((m_currentDirection & 0b01) != (direction & 0b01)) {
m_currentDirection = direction;
}
}
}

Expand Down Expand Up @@ -152,17 +153,22 @@ void Controller::handleNewFood(const FoodResp& requestedFood)

bool Controller::doesCollideWithSnake(const Controller::Segment &newSegment) const
{
if(!isPauseOn){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as line 101

for (auto segment : m_segments) {
if (segment.x == newSegment.x and segment.y == newSegment.y) {
return true;
}
}
}
return false;
}

bool Controller::doesCollideWithWall(const Controller::Segment &newSegment) const
{
return newSegment.x < 0 or newSegment.y < 0 or
if (isPauseOn)
return false;
else
return newSegment.x < 0 or newSegment.y < 0 or
newSegment.x >= m_mapDimension.first or
newSegment.y >= m_mapDimension.second;
}
Expand Down Expand Up @@ -213,6 +219,14 @@ Controller::Segment Controller::getNewHead() const
return newHead;
}

void Controller::handlePause(const PauseInd& Pause)
{
if(!Pause.isPauseOn)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just negate current isPauseOn value here

isPauseOn = true;
else
isPauseOn = false;
}

void Controller::receive(std::unique_ptr<Event> e)
{
switch(e->getMessageId())
Expand All @@ -221,6 +235,7 @@ void Controller::receive(std::unique_ptr<Event> e)
case DirectionInd::MESSAGE_ID: return handleDirectionChange(*static_cast<EventT<DirectionInd> const&>(*e));
case FoodInd::MESSAGE_ID: return handleFoodPositionChange(*static_cast<EventT<FoodInd> const&>(*e));
case FoodResp::MESSAGE_ID: return handleNewFood(*static_cast<EventT<FoodResp> const&>(*e));
case PauseInd::MESSAGE_ID: return handlePause(*static_cast<EventT<PauseInd> const&>(*e));
default: throw UnexpectedEventException();
};
}
Expand Down
3 changes: 2 additions & 1 deletion SnakeController/SnakeController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Controller : public IEventHandler
void handleDirectionChange(const DirectionInd&);
void handleFoodPositionChange(const FoodInd& receivedFood);
void handleNewFood(const FoodResp& requestedFood);
void handlePause(const PauseInd& Pause);

struct Segment
{
Expand All @@ -56,7 +57,7 @@ class Controller : public IEventHandler

void cleanNotExistingSnakeSegments();


bool isPauseOn = false;
IPort& m_displayPort;
IPort& m_foodPort;
IPort& m_scorePort;
Expand Down
1 change: 1 addition & 0 deletions SnakeController/SnakeInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ struct LooseInd

struct PauseInd
{
bool isPauseOn = 0;
static constexpr std::uint32_t MESSAGE_ID = 0x91;
};

Expand Down