From 89112a9dc0b548d5756e65ea3f0663be6afdd712 Mon Sep 17 00:00:00 2001 From: dm1sh Date: Tue, 10 Aug 2021 15:17:21 +0300 Subject: [PATCH] Fixed badges --- README.md | 121 +++++++++++++++++++++++++++++++++++++++++-- setup.py | 2 +- src/ttt_game/game.py | 8 ++- 3 files changed, 125 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9f885b0..f9efe02 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,127 @@ A simple tic tac toe game implementation ## Usage -tic_tac_toe module exports main game class `Game` and `Pl` and `Tr` enums to simplify typing. +### Installation + +Testing builds: + +```bash +python -m pip install -i https://test.pypi.org/simple/ ttt-game +``` + +Production builds: + +```bash +python -m pip install ttt-game +``` + +ttt-game module exports main game class `Game` and `Pl` and `Tr` enums to simplify typing. + +```python +from ttt_game import Game, Tr, Pl +``` ### `Game` class -TODO: Add Game class description +```python +class Game: + """ + Board indexes preview: + + [[0, 1, 2], + + [3, 4, 5], + + [6, 7, 8]] + + Board array itself: + + [Tr.E, Tr.E, Tr.E, Tr.E, Tr.E, Tr.E, Tr.E, Tr.E, Tr.E] + """ + _board: List[Tr] + + def get_board(self) -> List[Tr]: + """ + Returns copy of game board. Use it to display board in UI. + """ + + def check_move(self, pos: int) -> bool: + """ + Checks if board cell empty + """ + + def check_filled(self) -> bool: + """ + Checks if game board is filled + """ + + def check_win(self, pos: int) -> bool: + """ + Checks if this move will make player a winner. + """ + + def insert(self, pos: int, who: Tr) -> None: + """ + Sets game board's cell to specified value. Better use `move` method when possible + """ + + def move(self, pos: int, who: Pl) -> bool: + """ + Sets game board cell to specified value when possible. It also returns true if player has won. + """ + + def get_free(self) -> Tuple[int]: + """ + Returns indexes of free game board cells + """ +``` + +To use it you should initialize it like below: + +```python +game = Game() +``` + +To make move, below code is listed. `result` variable will contain `True` if `Pl.X` player won the game. + +```python +result = game.move(1, Tr.X) +if result: + print("Congrats, X player won!") +``` + +You also can get game board array with `get_board` method. To visualize it you can customize code listed below. + +```python +board = game.get_board() + +for i in range(9): + print(board[i], end=" ") + if i % 3 == 2: + print("\n", end="") + +""" +output: Tr.E Tr.X Tr.E + Tr.E Tr.E Tr.E + Tr.E Tr.E Tr.E +""" +``` ### `Pl` and `Tr` Enums -`Pl` has two members: `X` and `O` meaning X and O players. `Tr` enum has all members of `Pl` plus `E` entry meaning empty cell. +`Pl` has two members: `X` and `O` meaning X and O players. + +```python +class Pl(Enum): + X = 1 + O = 2 +``` + +`Tr` enum has all members of `Pl` plus `E` entry meaning empty cell. + +```python +class Tr(Enum): + E = 0 + X = Pl.X + O = Pl.O +``` diff --git a/setup.py b/setup.py index 6da9c91..cbf4322 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh: setuptools.setup( name="ttt_game", - version="0.0.1", + version="0.1.0", author="dm1sh", author_email="me@dmitriy.icu", description="A simple tic tac toe game implementation", diff --git a/src/ttt_game/game.py b/src/ttt_game/game.py index 3a4b4e2..74a59e5 100644 --- a/src/ttt_game/game.py +++ b/src/ttt_game/game.py @@ -3,7 +3,7 @@ from enum import Enum class Pl(Enum): - X = 3 + X = 1 O = 2 @@ -22,6 +22,10 @@ class Game: [3, 4, 5], [6, 7, 8]] + + Board array itself: + + [Tr.E, Tr.E, Tr.E, Tr.E, Tr.E, Tr.E, Tr.E, Tr.E, Tr.E] """ _board: List[Tr] @@ -86,7 +90,7 @@ class Game: def get_free(self) -> Tuple[int]: """ - Returns + Returns indexes of free game board cells """ return tuple(filter(lambda i: self._board[i] == Tr.E, range(9)))