0% found this document useful (0 votes)
6 views3 pages

Tik-Tak-Toe - Python

This document contains a Python implementation of a simple Tic-Tac-Toe game where a player competes against the computer. It includes functions for printing the game board, checking for wins or draws, handling player and computer moves, and managing the game flow. The player is represented by 'X' and the computer by 'O'.

Uploaded by

Szemenyei Csaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Tik-Tak-Toe - Python

This document contains a Python implementation of a simple Tic-Tac-Toe game where a player competes against the computer. It includes functions for printing the game board, checking for wins or draws, handling player and computer moves, and managing the game flow. The player is represented by 'X' and the computer by 'O'.

Uploaded by

Szemenyei Csaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import random

def print_board(board):

for row in board:

print(" | ".join(row))

print("-" * 5)

def check_win(board, player):

for row in board:

if all(cell == player for cell in row):

return True

for col in range(3):

if all(board[row][col] == player for row in range(3)):

return True

if all(board[i][i] == player for i in range(3)):

return True

if all(board[i][2 - i] == player for i in range(3)):

return True

return False

def check_draw(board):

return all(cell != " " for row in board for cell in row)

def player_move(board):

while True:

try:

row = int(input("Enter row (0, 1, or 2): "))

col = int(input("Enter column (0, 1, or 2): "))

except ValueError:

print("Please enter a number!")

continue
if row not in range(3) or col not in range(3):

print("Invalid coordinates. Try again.")

continue

if board[row][col] != " ":

print("Cell already taken. Try again.")

continue

board[row][col] = "X"

break

def computer_move(board):

empty_cells = [(row, col) for row in range(3) for col in range(3) if board[row][col] == " "]

if empty_cells:

row, col = random.choice(empty_cells)

board[row][col] = "O"

def play_game():

board = [[" " for _ in range(3)] for _ in range(3)]

print("You are X, computer is O.")

while True:

print_board(board)

player_move(board)

if check_win(board, "X"):

print_board(board)

print("You win! Congratulations!")

break

if check_draw(board):
print_board(board)

print("It's a draw!")

break

computer_move(board)

if check_win(board, "O"):

print_board(board)

print("Computer wins! Try again!")

break

if check_draw(board):

print_board(board)

print("It's a draw!")

break

if __name__ == "__main__":

play_game()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy