9
D - Tic Tac Toe Let's use our 9 sparkles to build a tic tac toe game! © 2019 courses.techcamp.org.uk/ Page 1 of 9

D - Tic Tac Toe - Amazon Web Services · INTRODUCTION Let's use our 9 sparkles to build a tic tac toe game! Step 1 Assemble the Robot First step - assemble the robot! Don't forget

Embed Size (px)

Citation preview

Page 1: D - Tic Tac Toe - Amazon Web Services · INTRODUCTION Let's use our 9 sparkles to build a tic tac toe game! Step 1 Assemble the Robot First step - assemble the robot! Don't forget

D - Tic Tac ToeLet's use our 9 sparkles to build a tic tac toe game!

© 2019 courses.techcamp.org.uk/ Page 1 of 9

Page 2: D - Tic Tac Toe - Amazon Web Services · INTRODUCTION Let's use our 9 sparkles to build a tic tac toe game! Step 1 Assemble the Robot First step - assemble the robot! Don't forget

INTRODUCTION

Let's use our 9 sparkles to build a tic tac toe game!

Step 1 Assemble the Robot

First step - assemble the robot!

Don't forget to put the sparkles into d1.

Step 2 Arrays

This game is quite complicated to program, and weare going to need to learn some new programmingconcepts to write it!

First thing - arrays. An array is just a special type ofvariable, which can contain a group of othervariables.

They are really useful when you need to store lots ofseparate bits of information, which would normallyrequire lots of variables.

You can make an array in almost the same way as avariable, except you also need to specify how manythings it contains.

Check the picture for an example! This code makesan array called numbers, that contains 3 intvariables.

© 2019 courses.techcamp.org.uk/ Page 2 of 9

Page 3: D - Tic Tac Toe - Amazon Web Services · INTRODUCTION Let's use our 9 sparkles to build a tic tac toe game! Step 1 Assemble the Robot First step - assemble the robot! Don't forget

Step 3 Make an Array

Let's make our own array!

Starting with a new program, make a new arraycalled numbers outside the setup and loop - it shouldcontain 5 different numbers.

To access the variables (or elements of the array,we use the index of the element. This is just itsposition - for example, the first element has index 0,the second index 1 and so on.

To access an element in an array, we use:

arrayName[index]

For example, to access the 3rd element of thenumbers array, we would write numbers[2] (as thethird element has index 2).

In the setup, use Serial.println to print the 4thelement of numbers to the Serial Monitor.

Step 4 LED Array

Let's make an LED array, so we can control thesparkles without having a variable for each!

Outside the setup and loop, make an int arraycalled LED with 9 elements, all of which are 0.

© 2019 courses.techcamp.org.uk/ Page 3 of 9

Page 4: D - Tic Tac Toe - Amazon Web Services · INTRODUCTION Let's use our 9 sparkles to build a tic tac toe game! Step 1 Assemble the Robot First step - assemble the robot! Don't forget

Step 5 Arrays and For Loops

Arrays become very powerful when you combinethem with a for loop.

The key is to use the incrementor (i usually) toaccess the array elements, that way the code canlook at any sequence of elements you want,automatically.

Add the for loop in the picture into your program -what do you think it will do?

Run the code and open the serial monitor to find out!

Make sure you understand how this code worksbefore moving on - ask your teacher if you needhelp.

Step 6 Update the LEDs

We are going to use this array to store the 'states' ofthe LEDs for the tic tac toe game:

Each element represents an LED - the firstelement is LED 0, the second LED 1 and so on.

If the element is 0, the LED is off (not selected bya player)

1 means taken by the red player

2 means taken by the blue player

Using the layout in the picture, fill in the code in theloop so that the sparkle colours are updateddepending on the values of the elements in the array.

Change some of the values in the array to 1 or 2 totest it out!

© 2019 courses.techcamp.org.uk/ Page 4 of 9

Page 5: D - Tic Tac Toe - Amazon Web Services · INTRODUCTION Let's use our 9 sparkles to build a tic tac toe game! Step 1 Assemble the Robot First step - assemble the robot! Don't forget

Step 7 Functions

We have been using functions since the start, likepinMode, invent.motorRun and so on.

Now its time to learn how to make our own!

Writing functions allows you to reuse code in longerprograms, where you might need to do the samething more than once.

For example, we are going to want to update theLEDs from the array in more than one place!

We first need to declare the function before we canuse it- this sets the name, what inputs it takes, andif it returns any data.

Have a look at the diagram - we'll just use void typefunctions for now (this means they do something, butdon't return any data).

Step 8 Make a Function

Let's make a function that updates the LEDs.

Move all your code from the loop that updates theLEDs, into a function that:

Is void type

Called updateLeds

Takes no inputs (empty brackets)

Make sure the function is outside the setup and loop,and above them as well.

Your code should look like the image - make sure itis indented properly!

© 2019 courses.techcamp.org.uk/ Page 5 of 9

Page 6: D - Tic Tac Toe - Amazon Web Services · INTRODUCTION Let's use our 9 sparkles to build a tic tac toe game! Step 1 Assemble the Robot First step - assemble the robot! Don't forget

Step 9 Use the Function

Now we have declared the function, we can use itwherever we like!

You use (or call) a function just like the functions wehave been using already - write its name!

Call the updateLeds() function inside the loop, andtest your code still works like before. Don't forget thesemicolon!

Step 10 Space Select

To play the game, the user has to select which spacethey want to take. We will use a switch to cyclethrough the LEDs, so they can choose where theywant to go.

Firstly, add some pinMode lines to the setup for thebuzzer and switches.

We need some more variables first - create theseunderneath the leds array:

int turn - this keeps track of whose turn it is.Initialise it to 1 (red player).

int space - this keeps track of which space iscurrently selected. Initialise it to 0.

Add an IF statement to the loop, that checks if theswitch in a1 is pressed. If it is, increase space by 1.

We need to make sure space doesn't go higher than8, as this is the last LED!

Add another if statement that checks if space ==9 - ifit does, reset space to 0.

© 2019 courses.techcamp.org.uk/ Page 6 of 9

Page 7: D - Tic Tac Toe - Amazon Web Services · INTRODUCTION Let's use our 9 sparkles to build a tic tac toe game! Step 1 Assemble the Robot First step - assemble the robot! Don't forget

Step 11 Select LED

Final step - we want to turn the currently selectedLED green, to show the user which space theyhave selected.

We need to move some things around and updatethe LEDs in the right places to make this work. Yourloop should:

Check the switch - if it is pressed, use a whileloop to wait for it to be released

Increase space by 1

Check if space has reached 9 - if it has, reset it to0

Update the LEDs from the array

Change the LED with the same number as spaceto green.

Check the picture for more hints if you need them!

Step 12 Place Counter

You code should now allow you to cycle the greenLED by pressing the switch!

Now we need the use the other switch to place theplayers counter, and turn an LED red or blue.

Add an if statement at the end of the loop, to checkthe second switch. If it is pressed, add a while loopto wait until it is released.

© 2019 courses.techcamp.org.uk/ Page 7 of 9

Page 8: D - Tic Tac Toe - Amazon Web Services · INTRODUCTION Let's use our 9 sparkles to build a tic tac toe game! Step 1 Assemble the Robot First step - assemble the robot! Don't forget

Step 13 Place Counter

We now need to store the space the player haschosen in the leds array.

Inside the if checking the second switch, add somecode to:

Check if turn ==1 - if it does, set leds[space] to 1(red space), and set turn =2

Otherwise it must be blue's turn, so setleds[space] to 2, and set turn=1

Test your code properly - you mind find a fewproblems that stop the game working exactly how itshould!

Step 14 Stop the Cheating!

You might have found that you can select a spacethat the other player has already taken!

For this challenge, add a while loop after space isincreased, that keeps adding 1 to space until theselected space has not already been taken!

Make sure you test your program properly and ironout any bugs!

Here's a hint - you will need to find a way of stoppingthis loop going forever when there are no spaces left!You can do this by counting the number of turnstaken.......

© 2019 courses.techcamp.org.uk/ Page 8 of 9

Page 9: D - Tic Tac Toe - Amazon Web Services · INTRODUCTION Let's use our 9 sparkles to build a tic tac toe game! Step 1 Assemble the Robot First step - assemble the robot! Don't forget

Step 15 Extension Tasks

Hopefully you should now have a basic workinggame!

Here are some ideas for improvements you canmake:

When all of the spaces are used up, flash all theLEDs white and sound the buzzer, then reset thegame

Hard challenge - write another function thatautomatically checks if someone has won thegame, i.e. got three in a row. This is very difficult!

Really really super mega hard challenge - extendthe winner checking function so that it checks if itis still possible to win the game at all!

© 2019 courses.techcamp.org.uk/ Page 9 of 9