diff options
| author | Simon Green <mail@simon.green> | 2025-08-23 15:27:04 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2025-08-23 15:27:04 +1000 |
| commit | 5fded4c63588ee61d55e38c0698abc6bd8a20e4a (patch) | |
| tree | 3ef28d4fb8ebc04cdcd56f18c8ee41b54de6bc58 /challenge-335/sgreen/python/ch-2.py | |
| parent | 4f766edf1327ad3628c824c3c00f1c1f10c50b38 (diff) | |
| download | perlweeklychallenge-club-5fded4c63588ee61d55e38c0698abc6bd8a20e4a.tar.gz perlweeklychallenge-club-5fded4c63588ee61d55e38c0698abc6bd8a20e4a.tar.bz2 perlweeklychallenge-club-5fded4c63588ee61d55e38c0698abc6bd8a20e4a.zip | |
sgreen solutions to challenge 335
Diffstat (limited to 'challenge-335/sgreen/python/ch-2.py')
| -rwxr-xr-x | challenge-335/sgreen/python/ch-2.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/challenge-335/sgreen/python/ch-2.py b/challenge-335/sgreen/python/ch-2.py new file mode 100755 index 0000000000..bf9b286364 --- /dev/null +++ b/challenge-335/sgreen/python/ch-2.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 + +import sys + + +def check_winner(board: list[list[str]]) -> str | None: + """ + Check the current state of the Tic Tac Toe board for a winner. + + Args: + board (list[list[str]]): The current state of the Tic Tac Toe board + + Returns: + str | None: 'A' if player A wins, 'B' if player B wins, or None if no winner yet + """ + # Check rows and columns + for i in range(3): + if board[i][0] == board[i][1] == board[i][2] != '_': + return board[i][0] + if board[0][i] == board[1][i] == board[2][i] != '_': + return board[0][i] + + # Check diagonals + if board[0][0] == board[1][1] == board[2][2] != '_': + return board[0][0] + if board[0][2] == board[1][1] == board[2][0] != '_': + return board[0][2] + + return None + + +def find_winner(moves: list[list[int]]) -> str: + # Initialize the board + board = [['_' for _ in range(3)] for _ in range(3)] + + # Player A starts first + current_player = 'A' + + for move in moves: + # Check the move is on the board and not already taken + if not 0 <= move[0] <= 2 and not 0 <= move[1] <= 2: + return "Invalid move (out of bounds)" + if board[move[0]][move[1]] != '_': + return "Invalid move (already taken)" + + # Place the move on the board + board[move[0]][move[1]] = current_player + + # Check for a win + result = check_winner(board) + if result: + return result + + # Switch players + current_player = 'B' if current_player == 'A' else 'A' + + # We've made all moves, check for pending or draw + return "Pending" if any('_' in row for row in board) else "Draw" + + +def main(): + # Convert input into integers, and pair them up + array = [int(n) for n in sys.argv[1:]] + moves = [array[i:i + 2] for i in range(0, len(array), 2)] + + result = find_winner(moves) + print(result) + + +if __name__ == '__main__': + main() |
