-
Notifications
You must be signed in to change notification settings - Fork 0
tic tac toe draw and check #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hankazajicova
wants to merge
2
commits into
master
Choose a base branch
from
tic-tac-toe
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: ") | ||
| 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) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.