Feat/multiplayer - #3
Open
mshaikh19 wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GameBoard::GameBoard(),GameBoard::play_game(),GameBoard::step(),GameBoard::handle_input(),GameBoard::prompt_name_and_save(),GameBoard::show_game_over_screen(),Food::spawn()include/Food.h,include/GameBoard.h,src/Food.cpp,src/GameBoard.cppAnalysis
We had to change Food::spawn(), play_game(), prompt_name_and_save(), and show_game_over_screen(). These functions are not directly responsible for the movement of the snakes, but they still depended on the game having only one player. For example, Food::spawn() originally checked only one snake, so it had to be changed to prevent the fruit from spawning inside either snakes. play_game() had to handle the second snake and display both scores simultaneously. The game-over functions also had to show which player lost and display both scores.
The assumption was spread across the code. There was no single place defining that the game supports only one snake or anything like that. Instead, different functions directly used snake_ and score_ which led to assumption that there was exactly one snake in the game. Because of this, adding a second snake affected several areas such as input, movement, food spawning, rendering, scoring and collision detection with walls and with other snake.
Our prediction included play_game(), step(), handle_input(), Food::spawn(), several Snake methods (occupies(), move(), reset(), collides_with_self()), and main.cpp.
In reality, the Snake methods and main.cpp did not need changes because we simply instantiated a second snake and called their existing behaviors. However, we missed the GameBoard constructor, prompt_name_and_save(), and show_game_over_screen(). We missed these because we focused purely on snake mechanics and movement, overlooking the UI adjustments required to announce who lost and reset both scores/snakes on a game restart.
A Player class or structure containing the snake, score, controls, and player state would have made this much easier for us to make changes in the code. The game could then store the players in a collection and use the same logic for each one of the snakes of the class or structure. Adding Player 2 would mostly involve adding another player instead of changing many separate functions such as snake2_, score2_, and the input logic.