Closed suchja closed 3 weeks ago
Adding it to milestone 0.1 as it is core gaming logic
What is required for the whole story?
Snake
(head & body) advances one step into the set movement direction.
Snake
keeps moving into the previously set direction andSnake
currently moves up and user presses arrow up key -> nothing changes!Snake
currently moves up and user presses arrow down key
Snake
(head & 1 body element) currently moves to the left and user presses arrow up key.
Snake
moves down and user presses (between 2 moves) arrow left key and arrow right key immediately after another. (left is overridden by right)Snake
(head & 1 body element) currently moves to the left and user presses arrow up key, Snake
moves and user presses arrow right key.
Starting with the first test:
- User presses "return" or "W" key,
Snake
keeps moving into the previously set direction and- Hint "Key is not supported!" is shown in the statusbar
GameViewModel::processKeyboardAction
knows whether it is a supported key. Thus it could raise the GameViewModel::userMessageUpdated
signal and directly provide the updated user message with it. Thus we could separate the user message from the gameStarted
signal.userMessageUpdated
signal on the GameViewModel
Game::setMoveDirection
. This would either require a more complex test or adding a mocking library to the mix. Currently I consider both as too complex. therefore I go ahead without such a test. However, I added at least on the todo list to check this later on.UserMessages::StartGame
stays until an arrow key is pressed even if an unsupported key is pressed.userMessageChanged
is emitted with UserMessages::None
when the game is properly starteduserMesaageChanged
is emitted with UserMessages::KeyNotSupported
when a unsupported key is pressed after the game is starteduserMessageChanged
is emitted with UserMessages::None
with next supported key, after GameViewModel::m_current_message
was set to UserMessages::KeyNotSupported
.UserMessages
handling from gameStarted
signal. (update #9 accordingly) -> Improves Separation of ConcernsUserMessages
to only emit a signal when it really changes. At least in MVVM for .NET this is best practice and should improve performance here as well.GameViewModel
) several times until they made sense.Proceeding with next test:
Snake currently moves up and user presses arrow down key:
- Snake does not change direction (move is ignored)
- A hint is shown to the user "Not possible to immediately go to opposite direction!"
- hint disappears with next "correct" arrow key
setMoveDirection
on Snake
and Game
will return a bool
indicating whether the direction was accepted.GameViewModel::userMessageChanged
will transfer the added UserMessages::WrongDirection
Snake
should get the business logic ensuring that opposite direction is not possible.test_GameViewModel::processKeyboardInput_shouldSignalUserMessageUpdated_whenOppositeDirectionIsPressed()
isn't that good. On that specific test I wondered why it stills fails and it was of the preconditions that I forgot to set in the test. Here something like mock object would have helped.Here we go with the next test:
Snake (head & 1 body element) currently moves to the left and user presses arrow up key.
- at next move the head moves one tile up while the remaining body still moves one tile to the left
- one additional move and the head moves still up, the element of the body now follows up.
Snake::m_body
is important (last element is assumed to be first element in QList
). This probably needs to be checked with a test!Now let's move to the final test:
Important: Behavior / Test changed as it was inconsistent in its original version!
Snake moves down and user presses (between 2 moves)
- arrow left key and arrow right key (immediately one after another)
- This would result in the the opposite direction handling as covered by previous test, but
- To have a consistent behavior only the first key press will be accepted. Once an arrow key is pressed, the snake needs to move first, before another arrow key press is accepted.
- arrow left key and arrow up key (immediately one after another)
- Only the first key press will be accepted. Once an arrow key is pressed, the snake needs to move first, before another arrow key press is accepted.
- This case would otherwise imply an implicit move to the opposite direction!
- If one of these cases is detected, the message "Only one direction change per move is allowed" should appear.
Snake
(as well as GameViewModel
) need to transfer 2 different errors. Therefore the Snake::setMoveDirection
needs to be changed. I decided to go for an enum
instead of exception
for returning the different errors.exception
for returning the errors from Snake::setMoveDirection
, I decided against it, becauseGame
. As it only forwards the information the change should be really small.noexcept
I would go (for the moment) with the probably minor performance improvement.
Notes
Acceptance Criteria
Quick key presses between 2 movements will override each other (the last one wins!)Last Acceptance criteria replaced (initial version kept for traceability), because it contradicted with the "opposite direction" criteria