100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 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
Course








Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Course

Document information

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

Subjects

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()
$5.43
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

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
0
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

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 tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right 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 aced it. It really can be that simple.”

Alisha Student

Frequently asked questions