Skip to content
Open
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
82 changes: 82 additions & 0 deletions src/pp_26_check_tic_tac_toe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
board_1 = [[1, 1, 0],
[2, 1, 0],
[2, 2, 2]]


def row_check(board: list) -> int:
"""check for same values in row

Args:
board (list): input board list

Returns:
int: won player
"""
for row in board:
if len(set(row)) == 1 and row[0] != 0:
print("won in row")
return row[0]

print("not in row")
return None


def column_check(board: list) -> int:
"""check for same values in column

Args:
board (list): input board list

Returns:
int: won player
"""
new_board = [[board[j][i] for j in range(len(board))] for i in range(len(board[0])-1,-1,-1)]
for column in new_board:
if len(set(column)) == 1 and column[0] != 0:
print("won in column")
return column[0]

print("not in column")
return None


def diagonale_check(board: list) -> int:
"""check for same values in diagonale

Args:
board (list): input board list

Returns:
int: won player
"""
if (board[0][0] == board[1][1] == board[2][2]) or (board[2][0] == board[1][1] == board[0][2]):
if board[1][1] != 0:
print("won in diagonale")
return board[1][1]

print("not in diagonale")
return None


def get_winner(board: list) -> int:
"""winner evaluation

Args:
board (list): input board list

Returns:
int: winner
"""
win_type = [row_check, column_check, diagonale_check]
for won in win_type:
winner = won(board)
if winner is not None:
print(f"The winner is player: {winner}")
return winner

print("There is no winner")
return 0

if __name__ == '__main__':
tic_tac_toe = get_winner(board_1)
print(tic_tac_toe)
67 changes: 67 additions & 0 deletions src/pp_27_tic_tac_toe_draw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from pp_26_check_tic_tac_toe import get_winner

game = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]

print(game[0])
print(game[1])
print(game[2])

PLAYER_1 = 'x'
PLAYER_2 = 'o'

def player_move(player_input: str) -> dict:
"""input tranformation into indices

Args:
player_input (str): user input

Returns:
dict: row index, column index
"""
player_split = player_input.split(",", 1)
player_row = int(player_split[0]) - 1
player_col = int(player_split[1]) - 1
return {
"row" : player_row,
"column" : player_col
}


def play_the_game(board: list):
"""game board filling

Args:
board (list): input board
"""
number_of_moves = 0
current_player = PLAYER_1
while True:
if number_of_moves >= 9:
break

print("Make your move as row number,column number")
player_input = input(f"Player {current_player}, you are on - row/column: ")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it looks good. 👍

What do you think about adding a validation for user input? It should prevent user to enter place, which is already taken or choose place out of the board.

players_move = player_move(player_input)

if board[players_move['row']][players_move['column']] == 0:
board[players_move['row']][players_move['column']] = current_player
number_of_moves += 1

print(game[0])
print(game[1])
print(game[2])

if get_winner(game) != 0:
break

if current_player == PLAYER_1:
current_player = PLAYER_2
else:
current_player = PLAYER_1

print("Game over")


play_the_game(game)