Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

- Add a minimax AI to this Tic-Tac-Toe game: board = [0,0,0,0,0,0,0,0,0] full = False def addInput(input): if input < 1 or input > 9:

- Add a minimax AI to this Tic-Tac-Toe game:
board = [0,0,0,0,0,0,0,0,0]
full = False
def addInput(input):
if input < 1 or input > 9:
print("please enter a number between 1-9")
elif board[input-1] == 0:
board[input-1] = 1
else:
print(" this place have already been marked ")
printBoard()
def printBoard():
for idx, x in enumerate(board, start=1):
if idx == 4 or idx == 7:
print()
if x == 0:
print("e |", end = ' ')
elif x == 1:
print("O |", end = ' ')
else:
print("X |", end = ' ')
def checkWinner():
#horizontal
if (board[0] == board[1] == board[2]) and board[0] != 0:
if(board[0] == 1):
return -10
elif(board[0] == 2):
return 10
print("Hor")
print("Player",board[0])
elif (board[3] == board[4] == board[5]) and board[3] != 0:
if(board[3] == 1):
return -10
elif(board[3] == 2):
return 10
print("Hor2")
print("Player",board[0])
elif (board[6] == board[7] == board[8]) and board[6] != 0:
if(board[6] == 1):
return -10
elif(board[6] == 2):
return 10
print("Hor3")
print("Player",board[0])
#vertical
elif (board[0] == board[3] == board[6]) and board[0] != 0:
if(board[0] == 1):
return -10
elif(board[0] == 2):
return 10
print("Ver")
print("Player",board[0])
elif (board[1] == board[4] == board[7]) and board[1] != 0:
if(board[1] == 1):
return -10
elif(board[1] == 2):
return 10
print("Ver2")
print("Player",board[0])
elif (board[2] == board[5] == board[8]) and board[2] != 0:
if(board[2] == 1):
return -10
elif(board[2] == 2):
return 10
print("Ver3")
print("Player",board[0])
#diagnoal
elif (board[0] == board[4] == board[8]) and board[0] != 0:
if(board[0] == 1):
return -10
elif(board[0] == 2):
return 10
print("DIA")
print("Player",board[0])
#anti-diagnoal
elif (board[2] == board[4] == board[6]) and board[2] != 0:
if(board[0] == 1):
return -10
elif(board[0] == 2):
return 10
print("ADIA")
print("Player",board[0])
return 0
#Main
while not full:
humanturn = int(input(" Enter the where you wanna play: "))
addInput(humanturn)
if 0 not in board:
print(" board is full")
full = True
full = checkWinner()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions