https://github.com/jfornoff/tictactoe
Elixir implementation of Tictactoe usable via Phoenix Channels
https://github.com/jfornoff/tictactoe
Last synced: 3 months ago
JSON representation
Elixir implementation of Tictactoe usable via Phoenix Channels
- Host: GitHub
- URL: https://github.com/jfornoff/tictactoe
- Owner: jfornoff
- Created: 2018-01-26T12:59:26.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-27T20:21:32.000Z (over 6 years ago)
- Last Synced: 2025-01-10T18:37:25.922Z (4 months ago)
- Language: Elixir
- Size: 55.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TicTacToe using Phoenix Channels
Produced for a session at the excellent [Rhein-Main Elixir Meetup](https://www.meetup.com/Elixir-Meetup-Rhein-Main) ([Session link](https://www.meetup.com/Elixir-Meetup-Rhein-Main/events/247183995/)).## Usage
### Joining a game
- Connect a Socket to `ws://localhost:4000/socket/websocket`
- Topics recognized are `game:` (e.g., `game:foo`)
- Joining will succeed or fail based on whether there is space in the game
- On success,
```json
{"playing_as": "X"}
```
will be returned.
- Game will be started if it does not exist and stopped when the last player leaves### Playing the game
#### `play`
When you have joined a game topic, you can now send messages to play.**Expected payload**:
```json
{"x": 0, "y": 1}
```
Coordinates of the field you would like to play, `(0,0)` is the bottom left, `(2,2)` is the top right of the board
The server will validate whether it's your turn and respond with success or error.The outcome of your turn is then broadcasted in another message.
### Game events
These are broadcasted whenever something happens in the game.#### `game_start`
Indicates that the game is full and players can now make moves -- they are not allowed beforehand.**Payload**:
- `current_player`: Who's turn it is ("X" or "O").
- `board`: Serialized version of the game board, e.g.:
```json
{"top": ["", "X", "O"], "middle": ["", "", ""], "bottom": ["O", "X", ""]}
```#### `game_update`
Indicates that the game is full and players can now make moves -- they are not allowed beforehand.**Payload**: Same as `game_start`
#### `game_end`
Indicates that the game is full and players can now make moves -- they are not allowed beforehand.**Payload**:
- `outcome`: Why the game ended ("Draw", "X wins", "O wins")
- `board`: Serialized version of the game board, e.g.:
```json
{"top": ["", "X", "O"], "middle": ["", "", ""], "bottom": ["O", "X", ""]}
```
#### `player_left`
Indicates that a player left the game (e.g., by closing the browser tab).**Payload**: none