A multiplayer Tic-Tac-Toe game where two players compete against each other over the network in real time โ from different browser tabs or even different computers. Built with Node.js and WebSockets as part of the course Javascript fรถr webbutveckling (ISGB15) at Karlstad University.
This is a two-player online version of the classic Tic-Tac-Toe (Three in a Row) game. Instead of passing a device back and forth, each player connects to the game through their own browser. The server keeps both players in sync โ when one player makes a move, the other sees it instantly.
If a player takes too long (more than 5 seconds), their turn is automatically skipped and passed to the opponent.
(Someone needs to do this once โ usually whoever set up the project)
Open a terminal in the project folder and run:
node app.js
The game is now running at http://localhost:3000
Each player opens their browser and goes to:
http://localhost:3000
If playing across different computers on the same network, replace
localhostwith the host computer's local IP address (e.g.http://192.168.1.x:3000).
Each player fills in:
Rules for login:
Click Logga in to enter the game.
When the game ends (win or draw), each player can choose to:
To log out, visit:
http://localhost:3000/reset
This clears your session and returns you to the login screen.
Make sure you have Node.js installed. You can download it at nodejs.org.
Then install the project dependencies by running this once in the project folder:
npm install
/
โโโ app.js # Server โ all game logic lives here
โโโ servermodules/
โ โโโ game-modul.js # Shared game state object (globalObject)
โโโ static/ # Files served directly to the browser
โ โโโ (client-side scripts, CSS, etc.)
โโโ loggain.html # Login page
โโโ index.html # Game page
(For developers and the curious)
| Technology | Purpose |
|---|---|
| Node.js + Express | Web server, routing, serving HTML files |
| socket.io | Real-time two-way communication (WebSockets) |
| jsdom | Manipulating HTML on the server side |
| cookie-parser | Managing player session cookies |
Login โ A player submits their nickname and color via a POST request. The server validates the data, stores it in globalObject, sets secure session cookies (2-hour lifetime, HTTP-only), and redirects the player to the game.
Connection โ When a player opens the game page, a WebSocket connection is established. The server checks their cookies to identify them. Once both players are connected, the game begins and Player 1 gets the first move.
Moves โ Each move is sent to the server via a newMove event. The server updates the board, checks for a win or draw, and tells the next player it's their turn via a yourMove event.
Timer โ After each move, a 5-second countdown starts on the server. If no move arrives in time, the timeout function fires, skipping the current player's turn.
Game over โ When someone wins or the board is full, a gameover event is broadcast to both players with the result.
Javascript fรถr webbutveckling โ ISGB15
Karlstad University
Graded using the automatic feedback system: JSAutoFeedback