Slide 1

Slide 1 text

BUILDING A TIC TAC TOE BOT IN PYTHON Kiran Gangadharan

Slide 2

Slide 2 text

WHAT THIS TALK IS NOT ABOUT

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

BUILDING A SIMPLE, EFFECTIVE AI FOR TIC TAC TOE (that almost always beats you)

Slide 5

Slide 5 text

MINIMAX

Slide 6

Slide 6 text

MINIMAX ➤ A decision rule used in game theory to minimise the worst case scenario (loss). ➤ It’s about minimising one’s maximum loss or maximising one’s minimum gain (hence the term Minimax) ➤ Originally designed for two player games where each player would alternate turns.

Slide 7

Slide 7 text

WHY USE MINIMAX?

Slide 8

Slide 8 text

https://xkcd.com/832/

Slide 9

Slide 9 text

HOW DOES IT WORK?

Slide 10

Slide 10 text

Image from http://neverstopbuilding.com/minimax

Slide 11

Slide 11 text

ALGORITHM

Slide 12

Slide 12 text

➤ If leaf node, return score from AI’s perspective ➤ Otherwise, get all possible moves in board ➤ For each move ➤ Add minimax of that move to a scores list ➤ If it’s the AI’s turn, return max(scores) ➤ If it’s the player’s turn, return min(scores)

Slide 13

Slide 13 text

LET’S LOOK AT SOME CODE!

Slide 14

Slide 14 text

MINIMAX def minimax(self, board, player): empty_cells = self.empty_cells(board) score = self.get_score(board) if score == 0: # Check for draw if len(empty_cells) == 0: return score else: return score scores = [] for cell in empty_cells: b = list(board) b[cell] = player scores.append(self.minimax(b, self.opponent(player))) if player == self.player: return max(scores) else: return min(scores)

Slide 15

Slide 15 text

HEURISTIC FUNCTION def get_score(self, board): opp = self.opponent(self.player) win = self.has_won(board, self.player) has_two_in_line = self.has_two_in_line(board, self.player) opp_win = self.has_won(board, opp) opp_has_two_in_line = self.has_two_in_line(board, opp) score = 0 if win: score += 100 if has_two_in_line: score += 10 if opp_win: score -= 100 if opp_has_two_in_line: score -= 10 return score

Slide 16

Slide 16 text

DEMO

Slide 17

Slide 17 text

NEXT STEPS

Slide 18

Slide 18 text

ALPHA-BETA PRUNING

Slide 19

Slide 19 text

http://yuliana.lecturer.pens.ac.id/Kecerdasan%20Buatan/ppt/Game%20Playing/contoh%20prunning/

Slide 20

Slide 20 text

RESOURCES https://youtu.be/STjW3eH0Cik

Slide 21

Slide 21 text

https://github.com/kirang89/tictac

Slide 22

Slide 22 text

THANKS!

Slide 23

Slide 23 text

kirang89 @kirang89 http://kirang.in