Question: # Tic-Tac-Toe functions from random import * def print_board(board): for i in range (len(board)): print (board[i], end=' | ') if (i==2 or i == 5
# Tic-Tac-Toe functions
from random import *
def print_board(board):
for i in range (len(board)):
print (board[i], end=' | ')
if (i==2 or i == 5 or i==8):
print (" ")
print("--------------")
def play_turns(start):
current = start
for i in range(9):
if (current == 'player1'):
print ("Player1 turn ----")
move=int(input("Choose a place player1 : "))
board[move-1]=player1
current= 'player2'
elif(current == 'player2'):
print ("player2 turn ---")
move=int(input("Choose a place player2 : "))
#x = randint(0,8)
#while (board[x] == 'X' or board[x] == '0'):
#x = randint(0,8)
board[move-1] = player2
current = 'player1'
print ("Board State")
print_board(board)
def check_winner(board):
winner = False
#check rows
for i in range (0,len(board),3):
if (board[i]==board[i+1] and board[i+1]==board[i+2]):
print ("Winner is ", board[i])
winner = True
#check columns
for i in range (0,3):
if (board[i]==board[i+3] and board[i+3]==board[i+6]):
print ("Winner is ", board[i])
winner = True
#check diagonal
if (board[0] == board[4] and board[4] == board[8]):
print ("Winner is ", board[0])
winner = True
if winner == False:
print ("It's a tie")
-------------------------------
# To play Tic-Tac-Toe we will use the following code
x = randint(0,1)
if x == 0:
player1= 'X'
player2= '0'
else:
player2= 'X'
player1= '0'
x = randint(0,1)
if x == 0:
start='player1'
else:
start='player2'
print ("Welcome to tic tac toe ")
print ("Player1 plays with ", player1)
print ("Player2 plays with ",player2)
print (start, " Starts")
board = [ i+1 for i in range(9) ]
#print board
print ("Begin Board State")
print_board(board)
play_turns(start)
#print board
print ("Final Board State")
print_board(board)
#check winner
check_winner(board)
----------------------------
Welcome to tic tac toe
Player1 plays with 0
Player2 plays with X
player2 Starts
Begin Board State
1 | 2 | 3 |
--------------
4 | 5 | 6 |
--------------
7 | 8 | 9 |
--------------
player2 turn ---
Choose a place player2 : 1
Board State
X | 2 | 3 |
--------------
4 | 5 | 6 |
--------------
7 | 8 | 9 |
--------------
Player1 turn ----
Choose a place player1 : 5
Board State
X | 2 | 3 |
--------------
4 | 0 | 6 |
--------------
7 | 8 | 9 |
--------------
player2 turn ---
Choose a place player2 : 2
Board State
X | X | 3 |
--------------
4 | 0 | 6 |
--------------
7 | 8 | 9 |
--------------
Player1 turn ----
Choose a place player1 : 3
Board State
X | X | 0 |
--------------
4 | 0 | 6 |
--------------
7 | 8 | 9 |
--------------
player2 turn ---
Choose a place player2 : 4
Board State
X | X | 0 |
--------------
X | 0 | 6 |
--------------
7 | 8 | 9 |
--------------
Player1 turn ----
Choose a place player1 : 6
Board State
X | X | 0 |
--------------
X | 0 | 0 |
--------------
7 | 8 | 9 |
--------------
player2 turn ---
Choose a place player2 : 7
Board State
X | X | 0 |
--------------
X | 0 | 0 |
--------------
X | 8 | 9 |
--------------
Player1 turn ----
Choose a place player1 : 8
Board State
X | X | 0 |
--------------
X | 0 | 0 |
--------------
X | 0 | 9 |
--------------
player2 turn ---
Choose a place player2 : 9
Board State
X | X | 0 |
--------------
X | 0 | 0 |
--------------
X | 0 | X |
--------------
Final Board State
X | X | 0 |
--------------
X | 0 | 0 |
--------------
X | 0 | X |
--------------
Winner is X
You have to define a class called TicTacToe using the logic used in the cells above. You can use the functions defined above or you can design your own functions. The objective is to instantiate one object when you decide to play the game. One object corresponds to 1 full game. This object would be used to play the turns and decide the winner. At the end, the object should print who the winner is or if it is a tie.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
