This project is a web-based Tic-Tac-Toe game implemented using the actor model in Go. It leverages channels for communication between actors to ensure better encapsulation and scalability. The game supports creating new games, making moves, and subscribing to game updates via WebSockets.
tictactoe/
├── server/
│ └── main.go
│
├── internal/
│ ├── actor/
│ │ ├── actor.go
│ │ └── actor.go
│ ├── game/
│ │ ├── game.go
│ │ └── board.go
│ ├── handlers/
│ │ └── handlers.go
│ └── message/
│ └── message.go
├── templates/
│ ├── base.templ
│ ├── index.templ
│ └── game.templ
├── static/
│ └── styles.css
├── go.mod
└── go.sum
goos: linux
goarch: amd64
pkg: github.com/sarthak0714/ttt/internal/actor
cpu: 13th Gen Intel(R) Core(TM) i5-13450HX
=== RUN BenchmarkActorModel
BenchmarkActorModel
2024/09/06 17:44:06 Benchmark results:
2024/09/06 17:44:06 100 games and 300 tested in 524.361µs:
2024/09/06 17:44:06 Benchmark results:
2024/09/06 17:44:06 10000 games and 30000 tested in 71.397478ms:
2024/09/06 17:44:07 Benchmark results:
2024/09/06 17:44:07 168000 games and 504000 tested in 1.186196461s:
BenchmarkActorModel-16 1680 706076 ns/op 229612 B/op 4617 allocs/op
PASS
ok github.com/sarthak0714/ttt/internal/actor 1.263s
-
Clone the repository:
git clone https://github.com/sarthak0714/ttt.git cd ttt
-
Install the
templ
cli:go install github.com/a-h/templ/cmd/templ@latest
-
Install dependencies:
make deps
-
Generate Go files from templ:
make gen
-
Build Server:
make build
-
Run Server (Running will also build and gen):
make run
The server will start on port 8080. You can access the game at http://localhost:8080
.
- Go to the home page (
http://localhost:8080
). - Enter the IDs of two players and create a new game.
- You will be redirected to the game page where you can make moves.
- On the game page, players can make moves by clicking on the board.
- The game will indicate whose turn it is and display the game's status (ongoing, draw, or winner).
- The game page uses WebSocket to subscribe to game updates.
- When a move is made, all connected clients will be updated in real-time.
- GameActor: Manages all games and handles game-related messages.
- PlayerActor: Manages player-specific messages (not extensively used in this implementation but can be expanded).
Messages define the communication between actors:
CreateGame
GetGame
MakeMove
Subscribe
Unsubscribe
- handlers.go: Defines HTTP handlers for serving the web pages and handling user actions (creating games, making moves, WebSocket connections).
- game.go: Defines the
Game
andPlayer
structs and the main game logic. - board.go: Defines the
Board
struct and functions for checking the game status (winner, draw, etc.).
- base.templ: Base HTML template.
- index.templ: Template for the home page.
- game.templ: Template for the game page.
- styles.css: Basic CSS for styling the web pages.
Feel free to submit issues and pull requests. Contributions are welcome!