A console 3x3 Tic-tac-toe for 2 friends on Go, in terminal, using functional programming.
Get someone and play locally in your terminal using keyboard only. Cannot play w/ computer yet so can play with yourself at worst :) See how to run below.
- Install Go (tested on go1.15.7 linux/amd64)
- Zero dependencies
Not needed, runs as is, just clone:
$ cd my_projects/
$ git clone https://github.com/dibikhin/tic-tac-toe-cli-fp-go.git
$ cd tic-tac-toe-cli-fp-go/
$ make test
...
>PASS
>ok tictactoe/game 0.010s
NOTE: The tests play the game itself too, scroll to see.
$ cd tic-tac-toe-cli-fp-go/
$ make run
> Hey! This is 3x3 Tic-tac-toe for 2 friends :)
>
> X X
> O X O
> X O
>
> Press 'x' or 'o' to choose mark for Player 1:
NOTE: Hit ctrl+c
to exit.
$ cd tic-tac-toe-cli-fp-go/
$ make
>Testing...
>...
>Building...
>...
$ make start
/game
— The game packagego.mod
main.go
— Entry pointMakefile
- The UI is CLI
- The 3x3 size is hardcoded
- No timeouts for turns
- Dirty input tolerant
- The code has excessive amount of comments for educational purposes
- The game is one active app (no client/server)
- Simple but structured
- Zero deps
- No patterns overkill
- A basic DI is under the hood for auto-tests (naive, no too smart DI)
- a simple IoC in
setup.go
, can stub/mock the user input strategy - an inner DI in the game loop (game ctx in
loop.go
)
- a simple IoC in
- Well-tested
- no mocks (behavior), just stubs (state)
- 90% code coverage
- pure and atomic fns mainly (no IO tests)
- NOTE: The tests play the game itself too. See in the end after expanding the
Test
section of the Github Actions job 'build'
- Paradigm: OOP + FP principles, SRP enforced
- Functional programming: a lot of pure fns; IO extracted but not isolated
This project is licensed under the MIT License — see the LICENSE file for details.
Thanks to:
- A Tour of Go — For the idea
- Tic-tac-toe — A lot of insights about the game