CODES For Long Weekend
CODES For Long Weekend
CODES For Long Weekend
The following are the codes for TIC TAC TOE game
def print_board(board):
for row in board:
print(" | ".join(row))
print("-" * 5)
def check_winner(board):
for row in board:
if row.count(row[0]) == len(row) and row[0] != ' ':
return True
for col in range(len(board[0])):
if board[0][col] == board[1][col] == board[2][col] and board[0][col] != ' ':
return True
if board[0][0] == board[1][1] == board[2][2] and board[0][0] != ' ':
return True
if board[0][2] == board[1][1] == board[2][0] and board[0][2] != ' ':
return True
return False
def is_board_full(board):
for row in board:
if ' ' in row:
return False
return True
def tic_tac_toe():
board = [[' ' for _ in range(3)] for _ in range(3)]
player = 'X'
while True:
print_board(board)
try:
row = int(input(f"Player {player}, enter row number (0, 1, 2): "))
col = int(input(f"Player {player}, enter column number (0, 1, 2): "))
except ValueError:
print("Invalid input! Please enter a number.")
continue
if row < 0 or row > 2 or col < 0 or col > 2:
print("Invalid input! Please enter a number between 0 and 2.")
continue
if board[row][col] == ' ':
board[row][col] = player
if check_winner(board):
print_board(board)
print(f"Player {player} wins!")
return
elif is_board_full(board):
print_board(board)
print("It's a tie!")
return
else:
player = 'O' if player == 'X' else 'X'
else:
print("That spot is already taken! Try again.")
tic_tac_toe()