1 of 112

Workshop:

Snake AI

2018 | Confidential and Proprietary

2 of 112

Goals:

​

  1. Build a web-based game
  2. Build AI player algorithms
  3. Learn about full stack development with node.js and socke.io
  4. Have fun!

2018 | Confidential and Proprietary

3 of 112

The Game: Snake

2018 | Confidential and Proprietary

4 of 112

Fun before a time phones were smart

2018 | Confidential and Proprietary

5 of 112

2018 | Confidential and Proprietary

6 of 112

What you will be doing today?

Part 1 Snake game

Part 2 AI player

Part 3 Multiplayer

2018 | Confidential and Proprietary

7 of 112

But why?

2018 | Confidential and Proprietary

8 of 112

Relevant challenges

Robotics

  • Autonomous agents
  • Simulation
  • Visualization��

​

Full stack development

​

2018 | Confidential and Proprietary

9 of 112

Workshop Outline

  1. Intro to Snake: Single player
    1. Code Outline provided
    2. Fill in code for parts of the game (e.g. did the snake eat the food, change direction)
  2. AI: Algorithms for the snake
    • Avoid walls
    • Shortest path to food
  3. Multiplayer
    • Client/server overview
    • Play AIs against each other

2018 | Confidential and Proprietary

10 of 112

Please ask lots of questions!

🤔

2018 | Confidential and Proprietary

11 of 112

Building the game

Snake AI - Part 1

2018 | Confidential and Proprietary

2018 | Confidential and Proprietary

12 of 112

Tools & Technologies

​

  • Javascript
    • Plus a little HTML & CSS
  • Glitch: tool to create and publish web apps

​

nst each other

2018 | Confidential and Proprietary

13 of 112

Glitch

Screenshare tour of new project

​

  1. File Browser
    1. Script, css, html
  2. Project view
  3. Split View

​

nst each other

2018 | Confidential and Proprietary

14 of 112

Glitch Signup

Make an account at

glitch.com/signin

  1. Go to: glitch.com/signin
  2. Click “create an account”
    1. If you have an account already, feel free to use it!

2018 | Confidential and Proprietary

2018 | Confidential and Proprietary

15 of 112

Make your personal copy of the Snake project

​

  1. https://glitch.com/edit/#!/snake-ai-part-1
  2. Click drop down in upper left and choose “Remix project”

​

nst each other

2018 | Confidential and Proprietary

16 of 112

Game Tour

Screenshare tour of new snake project. How the game works, how it runs now, what code is missing, etc.

​

  1. What does this version of it do?
  2. What is missing?
    1. Snake doesn’t eat food
    2. Snake doesn’t change directioner

2018 | Confidential and Proprietary

17 of 112

Parts of the game

Breaking the game into smaller pieces

​

  1. Let’s make a list of the different components of the game
    1. E.g. drawing the board

2018 | Confidential and Proprietary

18 of 112

Drawing the Board

Mini intro to Canvas element

2018 | Confidential and Proprietary

19 of 112

Things you can make with canvas

Google Maps

Scratch

Jamboard

p5.js

Games

Animations

Physics simulations

Visualizations

MS Paint

2018 | Confidential and Proprietary

20 of 112

What do we need to draw?

🐍 🍔

2018 | Confidential and Proprietary

21 of 112

Snake drawing code

What do you think these do?

2018 | Confidential and Proprietary

22 of 112

Snake drawing code

Putting it all together.

2018 | Confidential and Proprietary

23 of 112

Let’s see the drawing in action

​

​

​

2018 | Confidential and Proprietary

24 of 112

Let’s see what the game does now

2018 | Confidential and Proprietary

25 of 112

Where’s the board?

2018 | Confidential and Proprietary

2018 | Confidential and Proprietary

26 of 112

Let’s call drawGame(gameState)

2018 | Confidential and Proprietary

27 of 112

Now we should have a board!

🐍🍔

2018 | Confidential and Proprietary

28 of 112

Board background

- Load an image for the background

- Where do we put the code to draw it?

2018 | Confidential and Proprietary

29 of 112

Bonus:

If you get ahead of us, try spending some time customizing the look of the game board.

2018 | Confidential and Proprietary

30 of 112

Game State

Keeping track of what is happening

2018 | Confidential and Proprietary

31 of 112

What information about the game state do we need to keep track of?

2018 | Confidential and Proprietary

32 of 112

Game state

  • Where is the snake?
  • Which way is it going?
  • Where is the food?
  • What is the score?

2018 | Confidential and Proprietary

33 of 112

What needs to be updated at each iteration of the game?

2018 | Confidential and Proprietary

34 of 112

Check for players input

Move the snake

Was the food eaten?

Update score, generate new food

Did the snake collide?

Game Over

2018 | Confidential and Proprietary

35 of 112

🏁 Where should we start? 🏁

Check for players input

Move the snake

Was the food eaten?

Update score, generate new food

Did the snake collide?

Game Over

2018 | Confidential and Proprietary

36 of 112

Check for players input

Move the snake

Was the food eaten?

Update score, generate new food

Did the snake collide?

Game Over

2018 | Confidential and Proprietary

37 of 112

moveSnake

-What does this function do now?

​

​

Side question: What’s unshift do? What would be some good ways to figure it out?

2018 | Confidential and Proprietary

38 of 112

When should we call moveSnake?

2018 | Confidential and Proprietary

39 of 112

2018 | Confidential and Proprietary

40 of 112

Tip: Developer console

You can open the Developer Console (Ctrl+Shift+I, or right click anywhere in the page)

​

Try calling:

updateGame(gameState);

drawGame(gameState);

​

2018 | Confidential and Proprietary

41 of 112

Game Loop

2018 | Confidential and Proprietary

42 of 112

Game Loop

What does setTimeout do?

​

What is this “100” value?

2018 | Confidential and Proprietary

43 of 112

🎉 It moves! 🎉

🐍 💨

2018 | Confidential and Proprietary

44 of 112

What next?

Check for players input

Move the snake

Was the food eaten?

Update score, generate new food

Did the snake collide?

Game Over

✅

2018 | Confidential and Proprietary

45 of 112

Check for players input

Move the snake

Was the food eaten?

Update score, generate new food

Did the snake collide?

Game Over

2018 | Confidential and Proprietary

46 of 112

Keyboard controls

2018 | Confidential and Proprietary

47 of 112

Listening

How to set up keyboard listeners

​

2018 | Confidential and Proprietary

48 of 112

Open the javascript console while the game is running and hit some keys. What do you see?

2018 | Confidential and Proprietary

49 of 112

Which key got pressed?

​

The browser gives us an event with a bunch of information in it.

​

2018 | Confidential and Proprietary

50 of 112

The event has a keyCode property

Key codes for arrows

​

➡️ left 37

⬆️ up 38

➡️ right 39

⬇️ down 40

​

​

2018 | Confidential and Proprietary

51 of 112

Mapping a key to a direction

We wrote a little utility to map from keycode to the direction

2018 | Confidential and Proprietary

52 of 112

See it in action

Your console should now print the direction of the arrow key you pressed

2018 | Confidential and Proprietary

53 of 112

Capture the player’s

input

2018 | Confidential and Proprietary

54 of 112

Remember how we didn’t finish moveSnake?

Time to connect what direction we want to go with the rest of the game

2018 | Confidential and Proprietary

55 of 112

Incorporating

It in each game

update

If you haven’t yet, time to query player.nextMoveFunction(gameState) during each game iteration

2018 | Confidential and Proprietary

56 of 112

Bonus:

No key press left behind!

What happens if you press multiple keys during a single game iteration?

​

What is the right behavior in that case?

​

Can you change nextCommand to keep track of all of them

2018 | Confidential and Proprietary

57 of 112

Bonus: Reversing direction is not allowed! 🚫

In the original version of snake, you couldn’t reverse direction.

​

If you were heading right and hit the left arrow, nothing would happen. Instead, you’d have to move up or down a row before you turned back to the left.

​

Don’t allow your snake to reverse direction!

​

Hint: Look at the direction you are going and compare it to the direction the key press told you to go.

2018 | Confidential and Proprietary

58 of 112

🎉 The keys control the snake! 🎉

⌨️ 🐍 💨

2018 | Confidential and Proprietary

59 of 112

Progress!

Check for players input

Move the snake

Was the food eaten?

Update score, generate new food

Did the snake collide?

Game Over

✅

✅

2018 | Confidential and Proprietary

60 of 112

What next?

Check for players input

Move the snake

Was the food eaten?

Update score, generate new food

Did the snake collide?

Game Over

✅

✅

2018 | Confidential and Proprietary

61 of 112

How do we know if the snake ate the food?

2018 | Confidential and Proprietary

62 of 112

Here’s the function we need to fill in

Hints:

Look at the parameters. What information do they contain?

Look at the name of the function

2018 | Confidential and Proprietary

63 of 112

When should we check if we ate the food?

2018 | Confidential and Proprietary

64 of 112

Remember our friendly moveSnake function and the TODO notes in it?

2018 | Confidential and Proprietary

65 of 112

Back in moveSnake

// TODO

​

We need to check if the snake ate the food, in which case we need to update the score, let the snake grow and generate a new item of food on the board.

2018 | Confidential and Proprietary

66 of 112

Updating the score

- Where do we keep track of the score?

2018 | Confidential and Proprietary

67 of 112

Make more food!

🍔 🍕 🍟 🌯 🍦 🧀 🥞 🍛 🥗 🧆 🥓

​

​

2018 | Confidential and Proprietary

68 of 112

Growing the snake

​

We now need to make sure the snake grows by one unit when it eats the food.

​

How would you do that?

2018 | Confidential and Proprietary

69 of 112

🎉 The snake eats food! 🎉

⌨️ 🐍 💨 🍔

2018 | Confidential and Proprietary

70 of 112

Almost done!

Check for players input

Move the snake

Was the food eaten?

Update score, generate new food

Did the snake collide?

Game Over

✅

✅

✅

2018 | Confidential and Proprietary

71 of 112

What next?

Check for players input

Move the snake

Was the food eaten?

Update score, generate new food

Did the snake collide?

Game Over

✅

✅

✅

2018 | Confidential and Proprietary

72 of 112

Ending the Game

2018 | Confidential and Proprietary

73 of 112

How does the game end?

​

How do we tell if we hit a wall?

2018 | Confidential and Proprietary

74 of 112

Where should we check for the end of the game?

Which function gets called on every iteration of the game?

2018 | Confidential and Proprietary

75 of 112

Where do we keep track of whether the game is over?

2018 | Confidential and Proprietary

76 of 112

🎉 We have a full game! 🎉

⌨️ 🐍 💨 🍔 💯

2018 | Confidential and Proprietary

77 of 112

Break Time!

⏸

2018 | Confidential and Proprietary

78 of 112

The Snake AI

Snake AI - Part 2

2018 | Confidential and Proprietary

2018 | Confidential and Proprietary

79 of 112

Demo of AI

2018 | Confidential and Proprietary

80 of 112

New glitch project to remix

2018 | Confidential and Proprietary

81 of 112

Let’s get started with our first AI player

2018 | Confidential and Proprietary

82 of 112

What does this code do?

2018 | Confidential and Proprietary

83 of 112

Let’s try it

2018 | Confidential and Proprietary

84 of 112

Next: Avoiding the wall

2018 | Confidential and Proprietary

85 of 112

Remember the code to find out when the game is over?

​

This is very similar.

2018 | Confidential and Proprietary

86 of 112

Dealing with a lot of cases can be messy.

​

We can make it cleaner by using helper functions.

​

In Robotics and Physics, we often call this changing the reference frame.

2018 | Confidential and Proprietary

87 of 112

Your turn, put willCollideWithWall and rightTurnDirection to use.

2018 | Confidential and Proprietary

88 of 112

Next: Avoiding collisions with ourselves

Hint: We have done something very similar in part1

2018 | Confidential and Proprietary

89 of 112

Algorithm concepts: Composition

2018 | Confidential and Proprietary

90 of 112

Combining Random with avoiding collisions

2018 | Confidential and Proprietary

91 of 112

AI Concepts: Heuristics VS ML

​

Examples: Chess AI, Autonomous Vehicles

2018 | Confidential and Proprietary

92 of 112

Algorithm Concepts: Greedy

2018 | Confidential and Proprietary

93 of 112

Ideas share: Greedy strategies

2018 | Confidential and Proprietary

94 of 112

Demo of a greedy example

2018 | Confidential and Proprietary

95 of 112

Ideas share: non-greedy strategies

2018 | Confidential and Proprietary

96 of 112

Demo of “Slow and Steady”

Tip: Is there a way we could fast-forward?

2018 | Confidential and Proprietary

97 of 112

Time to unleash your creativity

2018 | Confidential and Proprietary

98 of 112

Which strategies worked well? Which didn’t?

2018 | Confidential and Proprietary

99 of 112

Two players

Demo time

2018 | Confidential and Proprietary

100 of 112

Do our AI strategies change for 2-players?

2018 | Confidential and Proprietary

101 of 112

Multiplayer

Battle time!

Snake AI - Part 3

2018 | Confidential and Proprietary

2018 | Confidential and Proprietary

102 of 112

Tools & Technologies

​

​

​

​

  • WebSockets using socket.io
    • For client-server communication

​

nst each other

2018 | Confidential and Proprietary

103 of 112

Going multiplayer!

2018 | Confidential and Proprietary

104 of 112

Let’s chat about Client and Server

2018 | Confidential and Proprietary

105 of 112

Player 1

Player 2

gameState

gameLoop

Client aka your computer

Client-only game

2018 | Confidential and Proprietary

106 of 112

Player 1

gameState

gameLoop

Client 1

Client+Server game

Server

Player 2

Client 2

Game commands

Game commands

2018 | Confidential and Proprietary

107 of 112

But how does it work?

2018 | Confidential and Proprietary

108 of 112

Client sends messages to server

​

2018 | Confidential and Proprietary

109 of 112

And the server handles each message the client sends

​

2018 | Confidential and Proprietary

110 of 112

Same game loop, but on the server side

​

2018 | Confidential and Proprietary

111 of 112

Cool. Now let’s play!

2018 | Confidential and Proprietary

112 of 112

Thank you.

Questions?

2018 | Confidential and Proprietary