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.
The SonarLint plugin was used to identify issues related to clean code standards, and the code was subsequently refactored.
Refactoring Description
1. Renamed Variables
box,iandrandwere ambiguously named.gameBoard,currentPlayer, andmove.2. Extracted Methods
mainmethod.makeMove,makeUserMove,makeComputerMove,checkWinner,printBoard, etc.) for better organisation.3. Extracted Classes
In the original code, the
Appclass was responsible for all tasks, including user interaction, game logic, and board management.Refactored Code: Responsibilities were distributed among multiple classes:
TicTacToeGamemanages the game flow.Boardhandles board management.Playermanages player-related logic.Resultdeals with game outcomes.Messagehandles user interactions and console output.Specifics:
Appclass serves as the entry point, creating an instance ofTicTacToeGameand starting the game by calling theplaymethod.TicTacToeGameclass manages the game loop, controls the flow, and handles player and board interactions, including checking win conditions and displaying results.Boardclass encapsulates the game board's state and includes methods for resetting the board, printing it, checking for a winner or draw, and updating cell values.Playerenum represents the two players, USER and COMPUTER, and contains their respective marks ('X' and 'O'). It also includes anext()method for switching between players.Resultenum defines possible game outcomes: USER_WON, COMPUTER_WON, and DRAW.Messageutility class centralises all user interaction logic, handling console output and formatted text.4. Moved Methods
initializeNumberedBoard,resetBoard,printBoard,checkRows,checkColumns,checkDiagonals,checkDraw, etc.) were moved to theBoardclass, where they logically belong. This class now encapsulates all board-related operations.TicTacToeGameclass now focuses on the game flow, including taking turns, making moves, and determining the game outcome. It uses theBoardclass to interact with the board.5. Removed Duplication
checkWinner,checkRows,checkColumns, andcheckDiagonalsin theBoardclass.6. Replaced Conditionals with Polymorphism
PlayerandResultenums to represent players and game outcomes, reducing the need for conditionals. This change simplifies the code structure, making it more extensible and easier to modify.7. Consolidated Methods
makeMove, which delegates tasks tomakeUserMoveormakeComputerMovebased on the player. This consolidation reduces code duplication and centralises similar operations, leading to a more maintainable codebase.8. Scanner Resource Management
Scannerinstance used for user input was properly closed by utilizing a try-with-resources block in theAppclass. This ensures that theScannerresource is automatically closed after its use, preventing potential resource leaks.9. Exception Handling for Invalid Input
getUserMovemethod in theTicTacToeGameclass was refactored to handle exceptions related to invalid user input. Specifically, atry-catchblock was added to catchNumberFormatExceptionwhen the user enters non-numeric input. The user is prompted to enter a valid move until a correct input is provided.