+ - 0:00:00
Notes for current slide
Notes for next slide

Welcome to Intermediate Ruby Group!

Tock and Negamax

1 / 46

About AIR

  • Restarting after the founder moved to DC.
2 / 46

About AIR

  • Restarting after the founder moved to DC.

  • Project focused, mentoring focused.

3 / 46

About AIR

  • Restarting after the founder moved to DC.

  • Project focused, mentoring focused.

  • We're interested in the nitty gritty.

  • Used a new language? Great.
  • Used a cool library? Fine.
4 / 46

About AIR

  • Restarting after the founder moved to DC.

  • Project focused, mentoring focused.

  • We're interested in the nitty gritty.

  • Used a new language? Great.
  • Used a cool library? Fine.

  • Tell us what you built with it.

5 / 46

Tic-Tac-Toe

  • A game of Tic-Tac-Toe.
6 / 46

Tic-Tac-Toe

  • A game of Tic-Tac-Toe.

  • With AI! (wow)

7 / 46

Tic-Tac-Toe

  • A game of Tic-Tac-Toe.

  • With AI! (wow)

  • We remember Tic Tac Toe, right?

8 / 46

Tic-Tac-Toe

  • A game of Tic-Tac-Toe.

  • With AI! (wow)

  • We remember Tic Tac Toe, right?

  • It's a special case of m,n,k games.

  • But specifics are pedagogically helpful.

  • Disclaimer: I have not tried my algorithm on arbitrary m,n,k games.

9 / 46

Enter Tock

10 / 46

Enter Tock

  • Tock on github.

  • Loose feature overview:

    1. Any mixture of 2 Human/Computer/SuperComputer players.
11 / 46

Enter Tock

  • Tock on github.

  • Loose feature overview:

    1. Any mixture of 2 Human/Computer/SuperComputer players.

    2. Any NxN board size. (Diagonal wins only supported on odd boards.)

12 / 46

Enter Tock

  • Tock on github.

  • Loose feature overview:

    1. Any mixture of 2 Human/Computer/SuperComputer players.

    2. Any NxN board size. (Diagonal wins only supported on odd boards.)

    3. Some tests I guess. (Yeah, not really a feature.)

13 / 46

Enter Tock

  • Tock on github.

  • Loose feature overview:

    1. Any mixture of 2 Human/Computer/SuperComputer players.

    2. Any NxN board size. (Diagonal wins only supported on odd boards.)

    3. Some tests I guess. (Yeah, not really a feature.)

  • The following objects:

14 / 46

Enter Tock

  • Tock on github.

  • Loose feature overview:

    1. Any mixture of 2 Human/Computer/SuperComputer players.

    2. Any NxN board size. (Diagonal wins only supported on odd boards.)

    3. Some tests I guess. (Yeah, not really a feature.)

  • The following objects:

    • Board
15 / 46

Enter Tock

  • Tock on github.

  • Loose feature overview:

    1. Any mixture of 2 Human/Computer/SuperComputer players.

    2. Any NxN board size. (Diagonal wins only supported on odd boards.)

    3. Some tests I guess. (Yeah, not really a feature.)

  • The following objects:

    • Board

    • Human, Computer, SuperComputer

16 / 46

Enter Tock

  • Tock on github.

  • Loose feature overview:

    1. Any mixture of 2 Human/Computer/SuperComputer players.

    2. Any NxN board size. (Diagonal wins only supported on odd boards.)

    3. Some tests I guess. (Yeah, not really a feature.)

  • The following objects:

    • Board

    • Human, Computer, SuperComputer

    • TicTacToe

17 / 46

Enter Tock

  • Tock on github.

  • Loose feature overview:

    1. Any mixture of 2 Human/Computer/SuperComputer players.

    2. Any NxN board size. (Diagonal wins only supported on odd boards.)

    3. Some tests I guess. (Yeah, not really a feature.)

  • The following objects:

    • Board

    • Human, Computer, SuperComputer

    • TicTacToe

    • Menu

18 / 46

Enter Minimax (jk Negamax)

  • Disclaimer: I threw these slides together this afternoon. Expect whiteboarding.
19 / 46

Enter Minimax (jk Negamax)

  • Disclaimer: I threw these slides together this afternoon. Expect whiteboarding.

  • Final Disclaimer: I wrote all this code in about 5 hours Sunday and last night.

20 / 46

Enter Minimax (jk Negamax)

  • Disclaimer: I threw these slides together this afternoon. Expect whiteboarding.

  • Final Disclaimer: I wrote all this code in about 5 hours Sunday and last night.

The core idea is pretty simple actually ...

  1. Write a scoring function for your game state.
21 / 46

Enter Minimax (jk Negamax)

  • Disclaimer: I threw these slides together this afternoon. Expect whiteboarding.

  • Final Disclaimer: I wrote all this code in about 5 hours Sunday and last night.

The core idea is pretty simple actually ...

  1. Write a scoring function for your game state.

  2. Recursively enumerate all possible future game states.

22 / 46

Enter Minimax (jk Negamax)

  • Disclaimer: I threw these slides together this afternoon. Expect whiteboarding.

  • Final Disclaimer: I wrote all this code in about 5 hours Sunday and last night.

The core idea is pretty simple actually ...

  1. Write a scoring function for your game state.

  2. Recursively enumerate all possible future game states.

  3. ???

23 / 46

Enter Minimax (jk Negamax)

  • Disclaimer: I threw these slides together this afternoon. Expect whiteboarding.

  • Final Disclaimer: I wrote all this code in about 5 hours Sunday and last night.

The core idea is pretty simple actually ...

  1. Write a scoring function for your game state.

  2. Recursively enumerate all possible future game states.

  3. ???

  4. PROFIT!

24 / 46

Uhhh....

mind_blown

25 / 46

Okay

  • Every "Game State" can lead to a number of possible future states based on how a player chooses to move.
26 / 46

Okay

  • Every "Game State" can lead to a number of possible future states based on how a player chooses to move.

  • Those states have their own future states based on the opponent's move and so on.

27 / 46

Okay

  • Every "Game State" can lead to a number of possible future states based on how a player chooses to move.

  • Those states have their own future states based on the opponent's move and so on.

  • We build the "Game Tree" then we just have to walk up and down it and figure out the move with the "best score".

28 / 46

Okay

  • Every "Game State" can lead to a number of possible future states based on how a player chooses to move.

  • Those states have their own future states based on the opponent's move and so on.

  • We build the "Game Tree" then we just have to walk up and down it and figure out the move with the "best score".

  • Recursion is a natural fit for tree traversal.

29 / 46

Okay

  • Every "Game State" can lead to a number of possible future states based on how a player chooses to move.

  • Those states have their own future states based on the opponent's move and so on.

  • We build the "Game Tree" then we just have to walk up and down it and figure out the move with the "best score".

  • Recursion is a natural fit for tree traversal.

  • Not a super common technique in Ruby. You can blow the stack if you're not careful.

30 / 46

Okay

  • Every "Game State" can lead to a number of possible future states based on how a player chooses to move.

  • Those states have their own future states based on the opponent's move and so on.

  • We build the "Game Tree" then we just have to walk up and down it and figure out the move with the "best score".

  • Recursion is a natural fit for tree traversal.

  • Not a super common technique in Ruby. You can blow the stack if you're not careful.

  • (Recent Rubies do support Tail Call Optimization, not sure how involved this is.)

31 / 46

Game Theory

  • Y'all saw A Beautiful Mind, right?
32 / 46

Game Theory

  • Y'all saw A Beautiful Mind, right?

  • Minimax works because Tic Tac Toe is a Zero-Sum game with perfect information.

33 / 46

Game Theory

  • Y'all saw A Beautiful Mind, right?

  • Minimax works because Tic Tac Toe is a Zero-Sum game with perfect information.

  • God we are so smart.

beautiful

34 / 46

So I've heard of Minimax, Why Negamax?

  • Really just a coding simplification.

  • Has to do with how we track the scores for alternating players.

35 / 46

How to Test Negamax Mo Betta

  1. Test prevention of forks. If the opponent forks, we've lost already.

    (Sidebar: How many ways can you set up a fork?)

36 / 46

How to Test Negamax Mo Betta

  1. Test prevention of forks. If the opponent forks, we've lost already.

    (Sidebar: How many ways can you set up a fork?)

  2. Test that we block opponent wins.

37 / 46

How to Test Negamax Mo Betta

  1. Test prevention of forks. If the opponent forks, we've lost already.

    (Sidebar: How many ways can you set up a fork?)

  2. Test that we block opponent wins.

  3. Test that we take available wins.

38 / 46

Optimization

  • Well, run Rubinius (currently not installable on Yosemite T_T).
39 / 46

Optimization

  • Well, run Rubinius (currently not installable on Yosemite T_T).

  • Or rewrite it in: C, Lisp, OCaml, etc.

40 / 46

Optimization

  • Well, run Rubinius (currently not installable on Yosemite T_T).

  • Or rewrite it in: C, Lisp, OCaml, etc.

  • "Here's a nickel kid, get yerself a real programming language."

41 / 46

Optimization

  • Well, run Rubinius (currently not installable on Yosemite T_T).

  • Or rewrite it in: C, Lisp, OCaml, etc.

  • "Here's a nickel kid, get yerself a real programming language."

  • ^^ #jerk

42 / 46

Optimization, pt. 2

Use a better algorithm!

43 / 46

Optimization, pt. 2

Use a better algorithm!

44 / 46

Optimization, pt. 2

Use a better algorithm!

45 / 46

Questions?

46 / 46

About AIR

  • Restarting after the founder moved to DC.
2 / 46
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow