Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/tic_tac_toe/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from src.tic_tac_toe.utils import GameState, ask_for_name, turn
from src.tic_tac_toe.utils import GameState, State, ask_for_name, print_state, turn

def main():
print("=== Tic Tac Toe ===")
Expand All @@ -7,11 +7,14 @@ def main():
name2 = ask_for_name("player #2")
print(f"Starting game for {name1} and {name2}")

state = None #todo: initialize state
state = State.new(name1, name2)
p1_turn = True
while state.state() == GameState.UNFINISHED:
print_state(state)
turn(state, p1_turn)
p1_turn = not p1_turn

print(f"Game has finished with state: {state.state()}")

if __name__ == "__main__":
main()
39 changes: 36 additions & 3 deletions src/tic_tac_toe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
def ask_for_name(player_tag) -> str:
return input(f"Enter name of {player_tag}:")

class GameState(enum.EnumType):
class GameState(enum.Enum):
PLAYER_1 = 1
PLAYER_2 = 2
DRAW = 3
Expand All @@ -16,13 +16,46 @@ def __init__(self, board, p1_name, p2_name):
self.board = board
self.p1_name = p1_name
self.p2_name = p2_name
@staticmethod
def new(p1_name, p2_name):
return State([[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]], p1_name, p2_name)
def state(self) -> GameState:
# todo
return GameState.UNFINISHED
def are_coordinates_valid(self, coordinates: typing.Tuple[int, int]):
return 0 < coordinates[0] < 3 and 0 < coordinates[1] < 3
def at_coordinates(self, coordinates: typing.Tuple[int, int]):
return self.board[coordinates[0]][coordinates[1]]

def print_state(state: State):
# todo
pass
def ask_for_row():
alphabet = ["1", "2", "3"]
result = None
while len(result := input("Specify column")) != 1 or result not in alphabet:
print("Need one character (1, 2 or 3)")
return ord(result) - 65
def ask_for_column():
alphabet = ["A", "B", "C"]
result = None
while len(result := input("Specify column")) != 1 or result not in alphabet:
print("Need one character (A, B or C)")
return ord(result) - 65
def ask_for_coordinates():
row = ask_for_row()
col = ask_for_column()
return (row, col)
def turn(state: State, p1_turn: bool):
# todo
pass
coordinates = ask_for_coordinates()
while (invalid := not state.are_coordinates_valid(coordinates)) or\
(tile := state.at_coordinates(coordinates)) != ' ':
if invalid:
print("Invalid coordinates (out of board), asking again...")
else:
print(f"Tile has been taken ('{tile}' sign has been placed)")
coordinates = ask_for_coordinates()
tile = "X" if p1_turn else "O"
state.board[coordinates[0]][coordinates[1]] = tile