|
| 1 | +import random |
| 2 | + |
| 3 | +# ───────────────────────────────────────── |
| 4 | +# 🎮 Tic Tac Toe — SahiDawa Python Projects |
| 5 | +# ───────────────────────────────────────── |
| 6 | + |
| 7 | +EMPTY = "⬜" |
| 8 | +X = "❌" |
| 9 | +O = "⭕" |
| 10 | + |
| 11 | + |
| 12 | +def create_board(): |
| 13 | + """Return a fresh 3×3 board (list of 9 cells).""" |
| 14 | + return [EMPTY] * 9 |
| 15 | + |
| 16 | + |
| 17 | +def display_board(board): |
| 18 | + """Print the board in a 3×3 emoji grid.""" |
| 19 | + print() |
| 20 | + for i in range(0, 9, 3): |
| 21 | + print(f" {board[i]} {board[i+1]} {board[i+2]}") |
| 22 | + print() |
| 23 | + |
| 24 | + |
| 25 | +def display_position_guide(): |
| 26 | + """Show the position reference grid (1-9).""" |
| 27 | + print("\n 📌 Position guide:") |
| 28 | + for i in range(1, 10, 3): |
| 29 | + print(f" {i} {i+1} {i+2}") |
| 30 | + print() |
| 31 | + |
| 32 | + |
| 33 | +def check_winner(board, symbol): |
| 34 | + """Return True if the given symbol has won.""" |
| 35 | + wins = [ |
| 36 | + [0, 1, 2], [3, 4, 5], [6, 7, 8], # rows |
| 37 | + [0, 3, 6], [1, 4, 7], [2, 5, 8], # columns |
| 38 | + [0, 4, 8], [2, 4, 6], # diagonals |
| 39 | + ] |
| 40 | + return any(board[a] == board[b] == board[c] == symbol for a, b, c in wins) |
| 41 | + |
| 42 | + |
| 43 | +def check_draw(board): |
| 44 | + """Return True if the board is full and no winner.""" |
| 45 | + return all(cell != EMPTY for cell in board) |
| 46 | + |
| 47 | + |
| 48 | +def get_player_move(board, symbol, name): |
| 49 | + """Prompt the player for a valid position (1-9).""" |
| 50 | + while True: |
| 51 | + try: |
| 52 | + pos = int(input(f" {symbol} {name}'s turn!\n ➡️ Enter position (1-9): ")) |
| 53 | + if pos < 1 or pos > 9: |
| 54 | + print(" ⚠️ Please enter a number between 1 and 9.\n") |
| 55 | + elif board[pos - 1] != EMPTY: |
| 56 | + print(" ⚠️ That position is already taken! Try another.\n") |
| 57 | + else: |
| 58 | + return pos - 1 # convert to 0-indexed |
| 59 | + except ValueError: |
| 60 | + print(" ⚠️ Invalid input. Please enter a number between 1 and 9.\n") |
| 61 | + |
| 62 | + |
| 63 | +def get_computer_move(board): |
| 64 | + """Return a random available position for the computer.""" |
| 65 | + available = [i for i, cell in enumerate(board) if cell == EMPTY] |
| 66 | + return random.choice(available) |
| 67 | + |
| 68 | + |
| 69 | +def play_two_players(): |
| 70 | + """Run a full 2-player game loop.""" |
| 71 | + board = create_board() |
| 72 | + players = [ |
| 73 | + {"name": "Player 1", "symbol": X}, |
| 74 | + {"name": "Player 2", "symbol": O}, |
| 75 | + ] |
| 76 | + print(f"\n 👤 Player 1 = {X} | Player 2 = {O}") |
| 77 | + display_position_guide() |
| 78 | + |
| 79 | + turn = 0 |
| 80 | + while True: |
| 81 | + display_board(board) |
| 82 | + player = players[turn % 2] |
| 83 | + move = get_player_move(board, player["symbol"], player["name"]) |
| 84 | + board[move] = player["symbol"] |
| 85 | + |
| 86 | + if check_winner(board, player["symbol"]): |
| 87 | + display_board(board) |
| 88 | + print(f" 🎉 {player['name']} wins! Congratulations! 🏆\n") |
| 89 | + return |
| 90 | + |
| 91 | + if check_draw(board): |
| 92 | + display_board(board) |
| 93 | + print(" 🤝 It's a draw! Well played both!\n") |
| 94 | + return |
| 95 | + |
| 96 | + turn += 1 |
| 97 | + |
| 98 | + |
| 99 | +def play_vs_computer(): |
| 100 | + """Run a player vs computer game loop.""" |
| 101 | + board = create_board() |
| 102 | + print(f"\n 👤 You = {X} | 🤖 Computer = {O}") |
| 103 | + display_position_guide() |
| 104 | + |
| 105 | + while True: |
| 106 | + # Player turn |
| 107 | + display_board(board) |
| 108 | + move = get_player_move(board, X, "You") |
| 109 | + board[move] = X |
| 110 | + |
| 111 | + if check_winner(board, X): |
| 112 | + display_board(board) |
| 113 | + print(" 🎉 You win! Congratulations! 🏆\n") |
| 114 | + return |
| 115 | + |
| 116 | + if check_draw(board): |
| 117 | + display_board(board) |
| 118 | + print(" 🤝 It's a draw!\n") |
| 119 | + return |
| 120 | + |
| 121 | + # Computer turn |
| 122 | + print(" 🤖 Computer is thinking...") |
| 123 | + comp_move = get_computer_move(board) |
| 124 | + board[comp_move] = O |
| 125 | + print(f" 🤖 Computer chose position {comp_move + 1}") |
| 126 | + |
| 127 | + if check_winner(board, O): |
| 128 | + display_board(board) |
| 129 | + print(" 😔 Computer wins! Better luck next time!\n") |
| 130 | + return |
| 131 | + |
| 132 | + if check_draw(board): |
| 133 | + display_board(board) |
| 134 | + print(" 🤝 It's a draw!\n") |
| 135 | + return |
| 136 | + |
| 137 | + |
| 138 | +def choose_mode(): |
| 139 | + """Prompt the user to choose game mode.""" |
| 140 | + while True: |
| 141 | + print(" Choose mode:") |
| 142 | + print(" 1️⃣ 2 Players") |
| 143 | + print(" 2️⃣ vs Computer") |
| 144 | + choice = input(" ➡️ Enter choice (1/2): ").strip() |
| 145 | + if choice == "1": |
| 146 | + return "two" |
| 147 | + elif choice == "2": |
| 148 | + return "computer" |
| 149 | + else: |
| 150 | + print(" ⚠️ Invalid choice. Please enter 1 or 2.\n") |
| 151 | + |
| 152 | + |
| 153 | +def play_again(): |
| 154 | + """Ask if the player wants to play again.""" |
| 155 | + while True: |
| 156 | + answer = input(" 🔄 Play again? (y/n): ").strip().lower() |
| 157 | + if answer in ("y", "yes"): |
| 158 | + return True |
| 159 | + elif answer in ("n", "no"): |
| 160 | + return False |
| 161 | + else: |
| 162 | + print(" ⚠️ Please enter y or n.\n") |
| 163 | + |
| 164 | + |
| 165 | +def main(): |
| 166 | + print("\n" + "=" * 40) |
| 167 | + print(" 🎮 Tic Tac Toe 🎮") |
| 168 | + print(" A classic game of X's and O's!") |
| 169 | + print("=" * 40) |
| 170 | + |
| 171 | + while True: |
| 172 | + print() |
| 173 | + mode = choose_mode() |
| 174 | + |
| 175 | + if mode == "two": |
| 176 | + play_two_players() |
| 177 | + else: |
| 178 | + play_vs_computer() |
| 179 | + |
| 180 | + if not play_again(): |
| 181 | + print("\n 👋 Thanks for playing! See you next time!\n") |
| 182 | + break |
| 183 | + |
| 184 | + |
| 185 | +if __name__ == "__main__": |
| 186 | + main() |
0 commit comments