Upload 371_159_252.py
Browse files- 371_159_252.py +71 -0
371_159_252.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""371.159.252
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1oXKGaEE20mV4mo0M2oDOSiIZ1ihX0tYc
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
def print_board(board):
|
| 11 |
+
print("\n".join((" | ".join(row) for row in board)))
|
| 12 |
+
print("\n")
|
| 13 |
+
|
| 14 |
+
def check_winner(board, player):
|
| 15 |
+
for row in board:
|
| 16 |
+
if all(cell == player for cell in row):
|
| 17 |
+
return True
|
| 18 |
+
|
| 19 |
+
for col in range(3):
|
| 20 |
+
if all(row[col] == player for row in board):
|
| 21 |
+
return True
|
| 22 |
+
|
| 23 |
+
if all(board[i][2 - i] == player for i in range(3)):
|
| 24 |
+
return True
|
| 25 |
+
return False
|
| 26 |
+
|
| 27 |
+
def is_full(board):
|
| 28 |
+
return all(cell !=" " for row in board for cell in row)
|
| 29 |
+
|
| 30 |
+
def get_move():
|
| 31 |
+
while True:
|
| 32 |
+
try:
|
| 33 |
+
move = int(input("Enter your move (1-9): "))
|
| 34 |
+
if move < 1 or move > 9:
|
| 35 |
+
print("Invalid move. Please enter a number between 1 and 9.")
|
| 36 |
+
else:
|
| 37 |
+
return move - 1
|
| 38 |
+
except ValueError:
|
| 39 |
+
print("Invalid input. Pleas enter a number.")
|
| 40 |
+
|
| 41 |
+
def play_game():
|
| 42 |
+
board = [[" " for _ in range(3)] for _ in range(3)]
|
| 43 |
+
player = ["X", "O"]
|
| 44 |
+
current_player = 0
|
| 45 |
+
|
| 46 |
+
print("welcome to Tic Tac Toe!")
|
| 47 |
+
print_board(board)
|
| 48 |
+
|
| 49 |
+
while True:
|
| 50 |
+
move = get_move()
|
| 51 |
+
row, col = divmod(move, 3)
|
| 52 |
+
|
| 53 |
+
if board[row][col] != " ":
|
| 54 |
+
print("Cell already taken. Try again.")
|
| 55 |
+
continue
|
| 56 |
+
|
| 57 |
+
board[row][col] = player[current_player]
|
| 58 |
+
print_board(board)
|
| 59 |
+
|
| 60 |
+
if check_winner(board, player[current_player]):
|
| 61 |
+
print(f"Player {player[current_player]} wins!")
|
| 62 |
+
break
|
| 63 |
+
|
| 64 |
+
if is_full(board):
|
| 65 |
+
print("It's a draw!")
|
| 66 |
+
break
|
| 67 |
+
|
| 68 |
+
current_player = 1 - current_player
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
play_game()
|