Slide 1

Slide 1 text

Developing an Efficient Algorithm for Nimber Calculation in Dots and Boxes by Andrew Brinker

Slide 2

Slide 2 text

What is Dots and Boxes?

Slide 3

Slide 3 text

Picture courtesy of Tiger66 < wiki/File:Dots-and-boxes.svg

Slide 4

Slide 4 text

There are 4 levels of play

Slide 5

Slide 5 text

You need chain control

Slide 6

Slide 6 text

This is a chain

Slide 7

Slide 7 text

There are actually 2 games

Slide 8

Slide 8 text

Enter Nim

Slide 9

Slide 9 text

NIM http://hanno-rein.de/images/main/2009_01/dsc00962.jpg

Slide 10

Slide 10 text

http://hanno-rein.de/images/main/2009_01/dsc00962.jpg

Slide 11

Slide 11 text

It all relies on the nim-sum

Slide 12

Slide 12 text

And the Sprague–Grundy Theorem

Slide 13

Slide 13 text

Win the 1st game, win the 2nd

Slide 14

Slide 14 text

Presenting naive_nim()

Slide 15

Slide 15 text

def mex(values): counter = 0 for value in values: if counter != value: return counter counter += 1 return values[len(values) - 1] + 1 ! def naive_nim(position): if all following moves for position are loony: return 0 return mex(all following positions to position)

Slide 16

Slide 16 text

It’s O(n!) (really slow)

Slide 17

Slide 17 text

Time for memoization (it’s a real word)

Slide 18

Slide 18 text

It’s better in reverse

Slide 19

Slide 19 text

Presenting fast_nim()

Slide 20

Slide 20 text

function fast_nim(region): board = board with the same dimensions as region l = number of missing lines in region n = 1 saved = set of positions w/out 1 line and w/ value 0 while (n != l): G = set of antecedent positions w/out n lines for (each position in G): F = set of follower positions to position nim_values = [] for (each follower in F): {follower position will always be in saved} nim_values = saved[follower] saved[position] = mex(nim_values) return saved

Slide 21

Slide 21 text

It’s O(n3) (much faster)

Slide 22

Slide 22 text

It’s usable now!

Slide 23

Slide 23 text

There’s still more to do

Slide 24

Slide 24 text

Any questions?