|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Author : Luke McGuire <[email protected]> |
| 4 | +Date : 2024-07-06 |
| 5 | +Purpose: Interactive Tic-Tac-Toe |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +from typing import List, NamedTuple, Optional |
| 10 | + |
| 11 | + |
| 12 | +# -------------------------------------------------- |
| 13 | +class State(NamedTuple): |
| 14 | + """Holds the state information for the Tic-Tac-Toe game""" |
| 15 | + |
| 16 | + board: List[str] = list("." * 9) |
| 17 | + player: str = "X" |
| 18 | + quit: bool = False |
| 19 | + draw: bool = False |
| 20 | + error: Optional[str] = None |
| 21 | + winner: Optional[str] = None |
| 22 | + |
| 23 | + |
| 24 | +# -------------------------------------------------- |
| 25 | +def clear() -> None: |
| 26 | + """Clear the terminal screen""" |
| 27 | + if os.name == "nt": |
| 28 | + os.system("cls") |
| 29 | + else: |
| 30 | + os.system("clear") |
| 31 | + |
| 32 | + |
| 33 | +# -------------------------------------------------- |
| 34 | +def get_move(state: State) -> State: |
| 35 | + """Get the next move from user input""" |
| 36 | + |
| 37 | + player = state.player |
| 38 | + cell = input(f"Player {player} What is your move? [q to quit]: ") |
| 39 | + |
| 40 | + if cell == "q": |
| 41 | + return state._replace(quit=True) |
| 42 | + |
| 43 | + if not (cell.isdigit() and int(cell) in range(1, 10)): |
| 44 | + return state._replace(error=f'Invalid cell "{cell}", please use 1-9') |
| 45 | + |
| 46 | + board = state.board |
| 47 | + |
| 48 | + idx = int(cell) - 1 |
| 49 | + if board[idx] != ".": |
| 50 | + return state._replace(error=f'Cell "{cell}" already taken') |
| 51 | + |
| 52 | + board[idx] = player |
| 53 | + return state._replace( |
| 54 | + board=board, |
| 55 | + player="O" if player == "X" else "X", |
| 56 | + winner=find_winner(board), |
| 57 | + draw="." not in board, |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +# -------------------------------------------------- |
| 62 | +def format_board(board: List[str]) -> str: |
| 63 | + """Formats the board string as a tic-tac-toe grid""" |
| 64 | + |
| 65 | + row_divider = "-------------" |
| 66 | + cells = [str(i) if c == "." else c for i, c in enumerate(board, start=1)] |
| 67 | + |
| 68 | + cells_templ = "| {} | {} | {} |" |
| 69 | + |
| 70 | + return "\n".join( |
| 71 | + [ |
| 72 | + row_divider, |
| 73 | + cells_templ.format(*cells[:3]), |
| 74 | + row_divider, |
| 75 | + cells_templ.format(*cells[3:6]), |
| 76 | + row_divider, |
| 77 | + cells_templ.format(*cells[6:9]), |
| 78 | + row_divider, |
| 79 | + ] |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +# -------------------------------------------------- |
| 84 | +def find_winner(board: List[str]) -> Optional[str]: |
| 85 | + """Determine if there is a winner""" |
| 86 | + winning = [ |
| 87 | + [0, 1, 2], |
| 88 | + [3, 4, 5], |
| 89 | + [6, 7, 8], |
| 90 | + [0, 3, 6], |
| 91 | + [1, 4, 7], |
| 92 | + [2, 5, 8], |
| 93 | + [0, 4, 8], |
| 94 | + [2, 4, 6], |
| 95 | + ] |
| 96 | + |
| 97 | + for player in ["X", "O"]: |
| 98 | + for i, j, k in winning: |
| 99 | + combo = [board[i], board[j], board[k]] |
| 100 | + if combo == [player, player, player]: |
| 101 | + return player |
| 102 | + |
| 103 | + return None |
| 104 | + |
| 105 | + |
| 106 | +# -------------------------------------------------- |
| 107 | +def main() -> None: |
| 108 | + """Make a jazz noise here""" |
| 109 | + |
| 110 | + state = State() |
| 111 | + |
| 112 | + while True: |
| 113 | + clear() |
| 114 | + print(format_board(state.board)) |
| 115 | + |
| 116 | + if state.error: |
| 117 | + print(state.error) |
| 118 | + state = state._replace(error=None) |
| 119 | + elif state.winner: |
| 120 | + print(f"{state.winner} has won!") |
| 121 | + break |
| 122 | + elif state.draw: |
| 123 | + print("All right, we'll call it a draw.") |
| 124 | + break |
| 125 | + |
| 126 | + state = get_move(state) |
| 127 | + if state.quit: |
| 128 | + print("You lose, loser!") |
| 129 | + break |
| 130 | + |
| 131 | + |
| 132 | +# -------------------------------------------------- |
| 133 | +if __name__ == "__main__": |
| 134 | + main() |
0 commit comments