Introduction
Let's use our 9 sparkles to build a tic tac toe game!
Tools
No tools specified.
-
-
First step - assemble the robot!
-
Don't forget to put the sparkles into d1.
-
-
-
This game is quite complicated to program, and we are going to need to learn some new programming concepts to write it!
-
First thing - arrays. An array is just a special type of variable, which can contain a group of other variables.
-
They are really useful when you need to store lots of separate bits of information, which would normally require lots of variables.
-
You can make an array in almost the same way as a variable, except you also need to specify how many things it contains.
-
Check the picture for an example! This code makes an array called numbers, that contains 3 int variables.
-
-
-
Let's make our own array!
-
Starting with a new program, make a new array called numbers outside the setup and loop - it should contain 5 different numbers.
-
To access the variables (or elements of the array, we use the index of the element. This is just its position - 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 the numbers array, we would write numbers[2] (as the third element has index 2).
-
In the setup, use Serial.println to print the 4th element of numbers to the Serial Monitor.
-
-
-
Let's make an LED array, so we can control the sparkles without having a variable for each!
-
Outside the setup and loop, make an int array called LED with 9 elements, all of which are 0.
-
-
-
Arrays become very powerful when you combine them with a for loop.
-
The key is to use the incrementor (i usually) to access the array elements, that way the code can look 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 works before moving on - ask your teacher if you need help.
-
-
-
We are going to use this array to store the 'states' of the LEDs for the tic tac toe game:
-
Each element represents an LED - the first element is LED 0, the second LED 1 and so on.
-
If the element is 0, the LED is off (not selected by a 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 the loop so that the sparkle colours are updated depending on the values of the elements in the array.
-
Change some of the values in the array to 1 or 2 to test it out!
-
-
-
We have been using functions since the start, like pinMode, invent.motorRun and so on.
-
Now its time to learn how to make our own!
-
Writing functions allows you to reuse code in longer programs, where you might need to do the same thing more than once.
-
For example, we are going to want to update the LEDs from the array in more than one place!
-
We first need to declare the function before we can use it- this sets the name, what inputs it takes, and if it returns any data.
-
Have a look at the diagram - we'll just use void type functions for now (this means they do something, but don't return any data).
-
-
-
Let's make a function that updates the LEDs.
-
Move all your code from the loop that updates the LEDs, 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 it is indented properly!
-
-
-
Now we have declared the function, we can use it wherever we like!
-
You use (or call) a function just like the functions we have been using already - write its name!
-
Call the updateLeds() function inside the loop, and test your code still works like before. Don't forget the semicolon!
-
-
-
To play the game, the user has to select which space they want to take. We will use a switch to cycle through the LEDs, so they can choose where they want to go.
-
Firstly, add some pinMode lines to the setup for the buzzer and switches.
-
We need some more variables first - create these underneath 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 is currently selected. Initialise it to 0.
-
Add an IF statement to the loop, that checks if the switch in a1 is pressed. If it is, increase space by 1.
-
We need to make sure space doesn't go higher than 8, as this is the last LED!
-
Add another if statement that checks if space ==9 - if it does, reset space to 0.
-
-
-
Final step - we want to turn the currently selected LED green, to show the user which space they have selected.
-
We need to move some things around and update the LEDs in the right places to make this work. Your loop should:
-
Check the switch - if it is pressed, use a while loop to wait for it to be released
-
Increase space by 1
-
Check if space has reached 9 - if it has, reset it to 0
-
Update the LEDs from the array
-
Change the LED with the same number as space to green.
-
Check the picture for more hints if you need them!
-
-
-
You code should now allow you to cycle the green LED by pressing the switch!
-
Now we need the use the other switch to place the players counter, and turn an LED red or blue.
-
Add an if statement at the end of the loop, to check the second switch. If it is pressed, add a while loop to wait until it is released.
-
-
-
We now need to store the space the player has chosen in the leds array.
-
Inside the if checking the second switch, add some code 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 set leds[space] to 2, and set turn=1
-
Test your code properly - you mind find a few problems that stop the game working exactly how it should!
-
-
-
You might have found that you can select a space that the other player has already taken!
-
For this challenge, add a while loop after space is increased, that keeps adding 1 to space until the selected space has not already been taken!
-
Make sure you test your program properly and iron out any bugs!
-
Here's a hint - you will need to find a way of stopping this loop going forever when there are no spaces left! You can do this by counting the number of turns taken.......
-
-
-
Hopefully you should now have a basic working game!
-
Here are some ideas for improvements you can make:
-
When all of the spaces are used up, flash all the LEDs white and sound the buzzer, then reset the game
-
Hard challenge - write another function that automatically checks if someone has won the game, i.e. got three in a row. This is very difficult!
-
Really really super mega hard challenge - extend the winner checking function so that it checks if it is still possible to win the game at all!
-