This document details the refactoring process applied to the Tic-Tac-Toe project.
The project has been restructured to group files by feature, improving modularity and scalability.
src/
├── app/
│ ├── App.tsx
│ └── main.tsx
├── features/
│ └── tic-tac-toe/
│ ├── components/
│ │ ├── Board.tsx
│ │ ├── Square.tsx
│ │ ├── GameInfo.tsx
│ │ └── ModeSelector.tsx
│ ├── hooks/
│ │ ├── useGame.ts
│ │ └── useBot.ts
│ ├── lib/
│ │ └── gameLogic.ts
│ └── types/
│ └── index.ts
├── shared/
│ ├── components/
│ │ ├── Label.tsx
│ │ └── RadioGroup.tsx
│ └── lib/
│ └── utils.ts
├── styles/
│ └── globals.css
└── vite-env.d.ts
- Grouped by Feature: Files are now organized by feature (
tic-tac-toe) instead of file type. This makes it easier to locate and work on related files. sharedandappDirectories: Ashareddirectory has been introduced for reusable components and utilities. The main application setup is now in theappdirectory.
- All
.jsxfiles have been converted to.tsxto ensure type safety and consistency. - Type definitions for the game state and props have been added in
src/features/tic-tac-toe/types/index.ts.
- The monolithic
useTicTacToe.jsxhook has been split into two smaller, more focused hooks:useGame.ts: Manages the core game state and logic.useBot.ts: Encapsulates the bot's logic and behavior.
- CSS files have been consolidated into a single
globals.cssfile in thestylesdirectory.
- The
vite.config.tsfile has been updated with a path alias (@) to simplify imports.
-
Install Dependencies:
npm install
-
Run the Development Server:
npm run dev
This will start the Vite development server and you can view the application in your browser.
This document outlines the necessary changes to refactor the existing Tic-Tac-Toe application into a real-time, online multiplayer game based on the "Odd/Even" rules.
The new implementation will be guided by two main principles from the assignment:
- Server Authority: The server will be the single source of truth for the game state. The client will only send player actions (increments) and render the state provided by the server.
- Operational Transforms: The client will send operations (e.g.,
INCREMENT) rather than final states. This allows the server to correctly handle simultaneous actions from multiple players without conflicts.
- What: Change the title of the application.
- How: Replace
<title>Vite + React + TS</title>with<title>Odd/Even Tic-Tac-Toe</title>. - Why: To accurately reflect the new game.
- Expected: The browser tab will show the new title.
- Requirement: General usability.
- What: Remove the
baseproperty. - How: Delete the line
base: "/tic-tac-toe/". - Why: The previous
basesetting was for GitHub Pages deployment. For local development with a WebSocket server, this is not needed and can cause issues. - Expected: The application will be served from the root of the domain.
- Requirement: Proper local development setup.
- What: Update the type definitions to match the new game rules.
- How:
- Change
SquareValuefrom"X" | "O" | nulltonumber. - Change
GameModetoPlayer, representing the player's role:export type Player = "ODD" | "EVEN";. - Remove
BotDifficulty.
- Change
- Why: To align the client-side types with the new game logic (numeric board, Odd/Even players).
- Expected: The TypeScript compiler will enforce the new data structures.
- Requirement: Core game rules.
- What: This hook will be completely rewritten to manage the WebSocket connection and the online game state.
- How:
- Remove the existing logic for turn-based play, local win detection, and bot interaction.
- Introduce state for the WebSocket connection, player role, connection status, and game over state:
const [ws, setWs] = useState<WebSocket | null>(null); const [player, setPlayer] = useState<Player | null>(null); const [board, setBoard] = useState<number[]>(Array(25).fill(0)); const [status, setStatus] = useState('Connecting...'); const [winner, setWinner] = useState<Player | null>(null);
- Use a
useEffecthook to establish the WebSocket connection on component mount. - Implement the
onmessagehandler to process messages from the server (PLAYER_ASSIGNED,UPDATE,GAME_OVER). - The
handleClickfunction will now only send anINCREMENTmessage through the WebSocket.
- Why: This is the core of the client-side logic for the online game, implementing the "Server Authority" model.
- Expected: The game will be driven by messages from the server, not local state changes.
- Requirement: WebSocket Connection, Server Authority, Player Assignment, Game Over.
- What: Delete these files.
- How:
rm src/features/tic-tac-toe/hooks/useBot.ts src/features/tic-tac-toe/lib/gameLogic.ts - Why: The bot logic and client-side win detection are no longer needed in the new multiplayer architecture.
- Expected: A cleaner codebase with no dead code.
- Requirement: Adherence to the server-authoritative model.
-
App.tsx:- What: Simplify the main component and add a "Game Over" overlay.
- How: Remove the
ModeSelector. The main component will now just render the board and game info. A conditional overlay will be shown whenwinneris not null. - Why: To adapt the UI to the new single-mode (online) game and to display the final game result.
- Expected: A cleaner UI that shows the game board and a "Game Over" message when the game ends.
- Requirement: Game Over display.
-
Board.tsx:- What: Change the board to a 5x5 grid.
- How: Update the inline style or CSS class for the board grid to have 5 columns. The component will now receive a
number[]for thesquaresprop. - Why: To match the new game rules.
- Expected: A 5x5 game board will be rendered.
- Requirement: 5x5 Game Board.
-
Square.tsx:- What: Display the numeric value of the square.
- How: The component will now render the
valueprop, which is anumber. - Why: To show the current number on each square as per the game rules.
- Expected: Each square will display a number (0, 1, 2, ...).
- Requirement: Display current board state.
-
GameInfo.tsx:- What: Display the player's role and the connection status.
- How: The component will now receive
playerandstatusprops and display messages like "You are the ODD player" and "Status: Connected". - Why: To provide necessary information to the player.
- Expected: The player will know their role and the status of the game connection.
- Requirement: Clear visual indication of player and connection status.
-
ModeSelector.tsx:- What: Delete this component.
- How:
rm src/features/tic-tac-toe/components/ModeSelector.tsx - Why: The game is now only multiplayer online, so there is no need to select a mode.
- Expected: A simpler UI.
- Requirement: The assignment is for a multiplayer game.
- What: Add styles for the "Game Over" overlay.
- How: Add CSS for a semi-transparent overlay and a message box to announce the winner.
- Why: To provide a clear and visually appealing "Game Over" screen.
- Expected: A modal or overlay will appear when the game ends.
- Requirement: Game Over display.
This plan covers all the requirements of the assignment and will result in a fully functional online multiplayer Odd/Even Tic-Tac-Toe game.