Practical 1
Practical 1
PRACTICAL - 1
Aim :
Write a program to implement Tic-Tac-Toe game problem.
Random Method :-
Input :
import random
else:
return False
return
print("It's a tie!")
Output:-
Name – Raj Bhuva Enrollment no – 201310132060
board = [1,2,3,4,5,6,7,8,9]
magic_square = [[2, 7, 6],[9, 5, 1], [4, 3, 8]]
magic_square_l = [2, 7, 6,9, 5, 1, 4, 3, 8]
occupied = []
computer = "O"
Human = "X"
n=3
def print_board(board):
print(f" {board[0][0]} | {board[0][1]} | {board[0][2]} ")
print("------------")
print(f" {board[1][0]} | {board[1][1]} | {board[1][2]} ")
print("------------")
print(f" {board[2][0]} | {board[2][1]} | {board[2][2]} ")
def rowcheck(board):
for i in range(n):
if (board[i][0] == board[i][1]) and (board[i][1] == board[i][2]) and (board[i][0] != "*"):
print("here")
return True
return False
def colcheck(board):
for i in range(n):
if (board[0][i] == board[1][i]) and (board[2][i] == board[0][i]) and (board[0][i] != "*"):
print("here")
return True
Name – Raj Bhuva Enrollment no – 201310132060
return False
def diagonalcheck(board):
if (board[0][0] == board[1][1]) and (board[1][1] == board[2][2]) and (board[0][0] != "*"):
print("here")
return True
if (board[0][2] == board[1][1]) and (board[1][1] == board[2][0]) and (board[0][2] != "*"):
print("here")
return True
return False
def gameover(board):
return(rowcheck(board) or colcheck(board) or diagonalcheck(board))
def gamedraw(board):
if gameover(board) == True:
return False
if gameover(board) == False:
for i in range(n):
for j in range(n):
if board[i][j] == "*":
return False
if diff in magic_square_l:
if diff not in occupied:
occupied.append(diff)
return diff
else:
return max(find_best_location(scores, s + 1, e), find_best_location(scores, s, e - 1))
elif diff >= 9:
s += 1
elif diff <= 0:
e -= 1
return -1
if temp_res == -1:
result[0] = -1
result[1] = -1
return result
else:
result[0] = -1
result[1] = -1
return result
else:
print("res is not -1")
z = magic_square_l.index(res)
result[0] = z / n
result[1] = z % n
return result
if loc[0] == -1:
loc = win_location(board, Human, movidx)
if loc[0] == -1:
x = random.randint(0 ,2)
y = random.randint(0, 2)
while board[x][y] != "*":
x = random.randint(0, 2)
y = random.randint(0, 2)
print(x,y)
board[x][y] = computer
movidx += 1
occupied.append(magic_square[x][y])
else:
print("com play move as human win")
board[int(loc[0])][int(loc[1])] = computer
movidx += 1
occupied.append(magic_square[int(loc[0])][int(loc[1])])
else:
print("com play move as computer win")
board[int(loc[0])][int(loc[1])] = computer
movidx += 1
occupied.append(magic_square[int(loc[0])][int(loc[1])])
print_board(board)
player = Human
elif player == Human:
x = int(input("enter loc x:"))
y = int(input("enter loc y:"))
if (board[x][y] != "*"):
print("already occupied")
else:
board[x][y] = Human
occupied.append(magic_square[x][y])
Name – Raj Bhuva Enrollment no – 201310132060
print_board(board)
player = computer
if gamedraw(board) == True:
print("match draw")
elif player == computer:
print("human won!")
else:
print("computer won")
board = [["*","*","*"],["*","*","*"],["*","*","*"]]
choice = int(input("enter 0 to play first or press 1 to let computer first : "))
while choice != 0 and choice != 1:
choice = int(input("enter 0 to play first or press 1 to let computer first : "))
movidx = 0
if choice == 1:
play_game(board, computer, movidx)
else:
play_game(board, Human, movidx)
Output :
Name – Raj Bhuva Enrollment no – 201310132060
Name – Raj Bhuva Enrollment no – 201310132060
Minmax method :-
Input :
# Function for Disply the tic-tac-toe game
def printBoard(board):
print(' '+ board[1] + ' | ' + board[2] + ' | ' + board[3])
print('---+---+---')
print(' '+ board[4] + ' | ' + board[5] + ' | ' + board[6])
print('---+---+---')
print(' '+ board[7] + ' | ' + board[8] + ' | ' + board[9])
print("\n")
else:
print("Player wins!")
exit()
return
else:
print("Can't insert there!")
position = int(input("Please enter new position: "))
insertLetter(letter, position)
return
def checkWhichMarkWon(mark):
if board[1] == board[2] and board[1] == board[3] and board[1] == mark:
return True
elif (board[4] == board[5] and board[4] == board[6] and board[4] == mark):
return True
elif (board[7] == board[8] and board[7] == board[9] and board[7] == mark):
return True
elif (board[1] == board[4] and board[1] == board[7] and board[1] == mark):
return True
elif (board[2] == board[5] and board[2] == board[8] and board[2] == mark):
return True
elif (board[3] == board[6] and board[3] == board[9] and board[3] == mark):
return True
elif (board[1] == board[5] and board[1] == board[9] and board[1] == mark):
return True
elif (board[7] == board[5] and board[7] == board[3] and board[7] == mark):
return True
else:
return False
def playerMove():
position = int(input("Enter the position for 'O': "))
insertLetter(player, position)
return
Name – Raj Bhuva Enrollment no – 201310132060
insertLetter(bot, bestMove)
return
if (isMaximizing):
bestScore = -800
for key in board.keys():
if (board[key] == ' '):
board[key] = bot
score = minimax(board, depth + 1, False)
Name – Raj Bhuva Enrollment no – 201310132060
else:
bestScore = 800
for key in board.keys():
if (board[key] == ' '):
board[key] = player
score = minimax(board, depth + 1, True)
board[key] = ' '
if (score < bestScore):
bestScore = score
return bestScore
printBoard(board)
print("Computer goes first! Good luck.")
print("Positions are as follow:")
print("1, 2, 3 ")
print("4, 5, 6 ")
print("7, 8, 9 ")
print("\n")
player = 'O'
bot = 'X'
global firstComputerMove
Name – Raj Bhuva Enrollment no – 201310132060
firstComputerMove = True
Output :