https://github.com/albertllousas/tic-tac-toe-in-haskell
https://github.com/albertllousas/tic-tac-toe-in-haskell
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/albertllousas/tic-tac-toe-in-haskell
- Owner: albertllousas
- License: bsd-3-clause
- Created: 2022-07-03T06:16:54.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-07-03T06:19:19.000Z (almost 3 years ago)
- Last Synced: 2025-01-30T22:48:26.255Z (3 months ago)
- Language: Haskell
- Size: 7.81 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
- License: LICENSE
Awesome Lists containing this project
README
# tic-tac-toe-in-haskell
The rules of the tic-tac-toe game are the following:
- A game has nine fields in a 3x3 grid
- There are two player in the game ("X" and "O")
- The "X" player goes always first
- A player can take a field if not already taken
- Players take turns taking fields until the game is over
- A game is over when all fields in a row are taken by a player, this player wins the game
- A game is over when all fields in a column are taken by a player, this player wins the game
- A game is over when all fields in a diagonal are taken by a player, this player wins the game
- A game is over when all fields are taken, there is no winner in this case## Solution
The solution is just one function:
```haskell
play :: Player -> GridPosition -> Game -> Either TicTacToeError Game
```
Where grid:
```shell
| (0,0) (0,1) (0,2) |
| (1,0) (1,1) (1,2) |
| (2,0) (2,1) (2,2) |
```Take a look on the [tests](/test/TicTacToeSpec.hs) to see it in action!
## Running tests
```shell
stack testTicTacToe
acceptance
Play a full game
game rules
A game starts with all the fields empty in the grid
The 'X' player goes always first
A player can take a field if not already taken
Players take turns taking fields until the game is over
A game is over when all fields in a row are taken by a player, this player wins the game
A game is over when all fields in a column are taken by a player, this player wins the game
A game is over when all fields in a column are taken by a player, this player wins the game
A game is over when all fields are taken, there is no winner in this case
game errors
should fail when trying to play a wrong field position
should fail when a player plays in the wrong turn
should fail when trying to play an already marked field in the grid
should fail when trying to play a finished game with a winner
should fail when trying to play a finished game with a draw
should not be valid if it has not nine fields in a 3x3 gridFinished in 0.0018 seconds
15 examples, 0 failures
```