Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views6 pages

Min Max Algorithm

The document provides a Python implementation of the Minimax algorithm for a Tic Tac Toe game. It includes functions to initialize the game, display the board, check for winners, and manage game flow between a human player and the computer. The computer plays first, and the game continues until there is a winner or a draw.

Uploaded by

mgiri63021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Min Max Algorithm

The document provides a Python implementation of the Minimax algorithm for a Tic Tac Toe game. It includes functions to initialize the game, display the board, check for winners, and manage game flow between a human player and the computer. The computer plays first, and the game continues until there is a winner or a draw.

Uploaded by

mgiri63021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

4.Implement the Minimax algorithm for a simple game (e.g., Tic Tac Toe).

import random

import time

# Constants for the game

COMPUTER = 1

HUMAN = 2

SIDE = 3

COMPUTERMOVE = 'O'

HUMANMOVE = 'X'

# Function to initialise the game / Tic-Tac-Toe board

def initialise():

board = [[' ' for _ in range(SIDE)] for _ in range(SIDE)]

moves = [i for i in range(SIDE*SIDE)]

random.shuffle(moves)

return board, moves

# Function to print the Tic-Tac-Toe board

def showBoard(board):

print("\n\n")

print("\t\t\t {} | {} | {} ".format(board[0][0], board[0][1], board[0][2]))

print("\t\t\t--------------")

print("\t\t\t {} | {} | {} ".format(board[1][0], board[1][1], board[1][2]))


print("\t\t\t--------------")

print("\t\t\t {} | {} | {} \n\n".format(board[2][0], board[2][1], board[2][2]))

# Function to show the instructions

def showInstructions():

print("\t\t\t Tic-Tac-Toe\n\n")

print("Choose a cell numbered from 1 to 9 as below and play\n\n")

print("\t\t\t 1 | 2 | 3 ")

print("\t\t\t--------------")

print("\t\t\t 4 | 5 | 6 ")

print("\t\t\t--------------")

print("\t\t\t 7 | 8 | 9 \n\n")

print("-\t-\t-\t-\t-\t-\t-\t-\t-\t-\n\n")

# Function to declare the winner of the game

def declareWinner(whoseTurn):

if whoseTurn == COMPUTER:

print("COMPUTER has won")

else:

print("HUMAN has won")

# Functions to check if any of the rows, columns, or diagonals have been crossed

def rowCrossed(board):

for i in range(SIDE):

if board[i][0] == board[i][1] and board[i][1] == board[i][2] and board[i][0] != ' ':

return True

return False
def columnCrossed(board):

for i in range(SIDE):

if board[0][i] == board[1][i] and board[1][i] == board[2][i] and board[0][i] != ' ':

return True

return False

def diagonalCrossed(board):

if board[0][0] == board[1][1] and board[1][1] == board[2][2] and board[0][0] != ' ':

return True

if board[0][2] == board[1][1] and board[1][1] == board[2][0] and board[0][2] != ' ':

return True

return False

# Function to check if the game is over

def gameOver(board):

return rowCrossed(board) or columnCrossed(board) or diagonalCrossed(board)

# Function to play Tic-Tac-Toe

def playTicTacToe(whoseTurn):

board, moves = initialise()

showInstructions()

moveIndex = 0

while not gameOver(board) and moveIndex != SIDE*SIDE:

if whoseTurn == COMPUTER:

x = moves[moveIndex] // SIDE

y = moves[moveIndex] % SIDE

board[x][y] = COMPUTERMOVE

print("COMPUTER has put a {} in cell {}".format(COMPUTERMOVE, moves[moveIndex]


+1))
showBoard(board)

moveIndex += 1

whoseTurn = HUMAN

elif whoseTurn == HUMAN:

x = moves[moveIndex] // SIDE

y = moves[moveIndex] % SIDE

board[x][y] = HUMANMOVE

print("HUMAN has put a {} in cell {}".format(HUMANMOVE, moves[moveIndex]+1))

showBoard(board)

moveIndex += 1

whoseTurn = COMPUTER

if not gameOver(board) and moveIndex == SIDE*SIDE:

print("It's a draw")

else:

if whoseTurn == COMPUTER:

whoseTurn = HUMAN

elif whoseTurn == HUMAN:

whoseTurn = COMPUTER

declareWinner(whoseTurn)

# Driver function

if __name__ == "__main__":

# Let us play the game with COMPUTER starting first

playTicTacToe(COMPUTER)
OUTPUT

Tic-Tac-Toe

Choose a cell numbered from 1 to 9 as below and play

1 | 2 | 3
--------------
4 | 5 | 6
--------------
7 | 8 | 9

- - - - - - - - - -

COMPUTER has put a O in cell 7

| |
--------------
| |
--------------
O | |

HUMAN has put a X in cell 8

| |
--------------
| |
--------------
O | X |

COMPUTER has put a O in cell 3


| | O
--------------
| |
--------------
O | X |

HUMAN has put a X in cell 9

| | O
--------------
| |
--------------
O | X | X

COMPUTER has put a O in cell 5

| | O
--------------
| O |
--------------
O | X | X

COMPUTER has won

You might also like