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
12 changes: 10 additions & 2 deletions src/tic_tac_toe/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from src.tic_tac_toe.utils import ColoringPrinter, GameState, NormalPrinter, State, UnderlinedReverseVideoDecoratingPrinter, ask_for_name, get_printer, print_state, set_printer, turn
from src.tic_tac_toe.utils import ColoringPrinter, GameState, NormalPrinter, State,\
UnderlinedReverseVideoDecoratingPrinter, ask_for_name, get_printer, print_state, set_printer, turn, ask_if_pvp


def ask_boolean(question):
Expand All @@ -15,7 +16,13 @@ def ask_for_color():

def main():
print("=== Tic Tac Toe ===")

if ask_if_pvp()=="P":
name1 = ask_for_name("player #1")
name2 = ask_for_name("player #2")
else:
name1 = ask_for_name("player #1")
name2 = "Bot"
print(f"Starting game for {name1} and {name2}")
color = ask_for_color()
if color != None:
set_printer(ColoringPrinter(color))
Expand All @@ -36,5 +43,6 @@ def main():

print_state(state)


if __name__ == "__main__":
main()
29 changes: 28 additions & 1 deletion src/tic_tac_toe/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import enum
import typing
import random


def ask_for_name(player_tag) -> str:
while True:
name=input(f"Enter name of {player_tag}:")

if name!="Bot":
return name
import abc

class Printer(abc.ABC):
Expand Down Expand Up @@ -109,7 +118,16 @@ def ask_for_coordinates():
col = ask_for_column()
return (row, col)
def turn(state: State, p1_turn: bool):
coordinates = ask_for_coordinates()
if state.p2_name=="Bot" and p1_turn:
coordinates = ask_for_coordinates()
elif state.p2_name=="Bot" and not p1_turn:
while True:
coordinates=generate_random_position()
if state.board[coordinates[0]][coordinates[1]]==" ":
break
else:
coordinates = ask_for_coordinates()

while (invalid := not state.are_coordinates_valid(coordinates)) or\
(tile := state.at_coordinates(coordinates)) != ' ':
if invalid:
Expand All @@ -119,5 +137,14 @@ def turn(state: State, p1_turn: bool):
coordinates = ask_for_coordinates()
tile = "X" if p1_turn else "O"
state.board[coordinates[0]][coordinates[1]] = tile
def ask_if_pvp():
valid = ["p","b"]
while True:
result = input("Chcesz zagrać na bota(B), czy z innym graczem?(P) ")
if result.lower() in valid:
return result

def generate_random_position():
return (random.randint(0,2),random.randint(0,2))