Professional Documents
Culture Documents
S5AI - IAI - Team 8 - Course Project
S5AI - IAI - Team 8 - Course Project
( 5) ADITHYA BIJU
( 6) ADITHYAN A J
(30) E R ARAVIND NAMPOOTHIRI
(34) HARIKRISHNAN P
(35) HENRY B PHILIP
S5 CSE(AI)
To
DEPARTMENT OF CSE(AI)
ABSTRACT
The minimax algorithm evaluates all possible plays from the current
game state down to the end of each game and then backs up the values
to the current state. The algorithm assumes that both players’ plays
optimal, the MAX player will chose the play with the highest score
and the MIN player will chose the play with the lowest score.
CONTENTS
INTRODUCTION
In knoughts & crosses, this means that either one of the players gets a
line of three and wins, or the board is full and the game ends in a tie.
PSEUDO CODE
if depth == 0 or game_over(state):
score = evaluate(state)
return [-1, -1, score]
if player == MAX:
if score[2] > best[2]:
best = score
else:
if score[2] < best[2]:
best = score
return best
WORKING
Both players start with your worst score. If player is MAX, its score is
-inf. Else if player is MIN, its score is +inf.
The best move on the board is [-1, -1] (row and column) for all.
If the depth is equal zero, then the board hasn't new empty cells to
play. Or, if a player wins, then the game ended for MAX or MIN. So
the score for that state will be returned.
The move (+1 or -1) on the board is undo and the row, column are
collected.
For MAX player, a bigger score will be received. For a MIN player, a
lower score will be received. And in the end, the best move is
returned.
GAME TREE
SOURCE CODE
def evaluate(state):
if wins(state, ai):
score = +1
elif wins(state, user):
score = -1
else:
score = 0
return score
def game_over(state):
return wins(state, user) or wins(state, ai)
def empty_cells(state):
cells = []
for x, row in enumerate(state):
for y, cell in enumerate(row):
if cell == 0: cells.append([x, y])
return cells
def clean():
os_name = platform.system().lower()
if 'windows' in os_name:
system('cls')
else:
system('clear')
else:
move = minimax(board, depth, ai)
x, y = move[0], move[1]
set_move(x, y, ai)
time.sleep(1)
def main():
clean()
user_choice = ''
ai_choice = ''
first = ''
else:
clean()
render(board, ai_choice, user_choice)
print('DRAW!')
exit()
main()
OUTPUT
Choose X or O
Chosen: X
Do you wish to start first? [y/n] : Y
User turn [X]
----------------
----------------
| || || |
----------------
| || || |
----------------
| || || |
----------------
Use numpad (1..9): 1
Computer turn [O]
----------------
----------------
| X || || |
----------------
| || || |
----------------
| || || |
----------------
User turn [X]
----------------
----------------
| X || || |
----------------
| || O || |
----------------
| || || |
----------------
Use numpad (1..9): 4
----------------
| X || || |
----------------
| X || O || |
----------------
| || || |
----------------
User turn [X]
----------------
----------------
| X || || |
----------------
| X || O || |
----------------
| O || || |
----------------
Use numpad (1..9): 3
Computer turn [O]
----------------
----------------
| X || || X |
----------------
| X || O || |
----------------
| O || || |
----------------
User turn [X]
----------------
----------------
| X || O || X |
----------------
| X || O || |
----------------
| O || || |
----------------
Use numpad (1..9): 8
Department of Computer Science and Engineering (AI), ASIET Page 16
Implementation of Minimax algorithm January 2023
----------------
| X || O || X |
----------------
| X || O || |
----------------
| O || X || |
----------------
User turn [X]
----------------
----------------
| X || O || X |
----------------
| X || O || O |
----------------
| O || X || |
----------------
Use numpad (1..9): 9
----------------
----------------
| X || O || X |
----------------
| X || O || O |
----------------
| O || X || X |
----------------
DRAW!
CONCLUSION
REFERENCES
Clederson Cruz
Clederson Cruz (cledersonbc.github.io)