Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Code for Tic tac toe game in python

Rating
-
Sold
-
Pages
1
Uploaded on
21-10-2023
Written in
2023/2024

A document that you can copy and paste in you coding app python

Institution
Module

Content preview

Code for Tic-tac-toe game in python
import random

def display_board(board):
print("\n".join(map(str, board)))

def check_win(board):
for row in board:
if row.count(row[0]) == len(row) and row[0] != 0:
return True

for col in range(len(board)):
check_col = []
for row in board:
check_col.append(row[col])
if check_col.count(check_col[0]) == len(check_col) and check_col[0] != 0:
return True

return False

def check_draw(board):
for row in board:
for item in row:
if item == 0:
return False
return True

def player_move(board, player):
valid_move = False
while not valid_move:
move = input(f"Player {player}, enter your move (1-9): ")
if move.isdigit() and 1 <= int(move) <= 9:
move = int(move)
row = (move - 1) // 3
col = (move - 1) % 3
if board[row][col] == 0:
valid_move = True
board[row][col] = player
else:
print("Invalid move, that spot is already occupied.")
else:
print("Invalid move, please enter a number between 1 and 9.")

def play_game():
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
current_player = 1
display_board(board)

while not check_win(board) and not check_draw(board):
player_move(board, current_player)
display_board(board)
current_player = 2 if current_player == 1 else 1

if check_win(board):
print(f"Player {current_player} wins!")
else:
print("It's a draw!")

play_game()

Written for

Module

Document information

Uploaded on
October 21, 2023
Number of pages
1
Written in
2023/2024
Type
SUMMARY

Subjects

£4.47
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
rodrigoraulsbrugemanis

Get to know the seller

Seller avatar
rodrigoraulsbrugemanis
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
3
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Trending documents

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these revision notes.

Didn't get what you expected? Choose another document

No problem! You can straightaway pick a different document that better suits what you're after.

Pay as you like, start learning straight away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and smashed it. It really can be that simple.”

Alisha Student

Frequently asked questions