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

Similar documents
C - Colour Mixing. Let's use the Sparkle module and some switches to make a colour mixer! 2018 courses.techcamp.org.

Tic Tac Toe Game! Day 8

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class

CS12020 for CGVG. Practical 2. Jim Finnis

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

University of Hull Department of Computer Science C4DI Interfacing with Arduinos

Robotics and Electronics Unit 5

BEGINNER PHP Table of Contents

Session 8.2. Finding Winners Using Arrays

Two Dimensional Arrays

CSC 108. Prof. Yue-Ling Wong. Lab 6: Tic-tac-toe Game

Tic-Tac-Toe. By the time you are done with this activity, you and your team should be able to:

CTEC 1802 Embedded Programming Labs

Today in CS161. Lecture #12 Arrays. Learning about arrays. Examples. Graphical. Being able to store more than one item using a variable

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Now find the button component in the tool box. [if toolbox isn't present click VIEW on the top and click toolbox]

Question 1 [20 points]

CS12020 for CGVG. Practical 1. Jim Finnis

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

CS12020 (Computer Graphics, Vision and Games) Worksheet 1

Static Methods. Why use methods?

PYTHON YEAR 10 RESOURCE. Practical 01: Printing to the Shell KS3. Integrated Development Environment

CS 115 Lecture. Boolean logic Taken from notes by Dr. Neil Moore

[ the academy_of_code] Senior Beginners

AN INTRODUCTION PROGRAMMING. Simon Long

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

ISY00245 Principles of Programming. Module 7

(9-1) Arrays IV Parallel and H&K Chapter 7. Instructor - Andrew S. O Fallon CptS 121 (March 5, 2018) Washington State University

Add in a new balloon sprite, and a suitable stage backdrop.

SHAW ACADEMY NOTES. Diploma in Lightroom

Lab 9: Pointers and arrays

Provided by - Microsoft Placement Paper Technical 2012

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Noughts and Crosses. Step 1: Drawing the grid. Introduction

B - Broken Track Page 1 of 8

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

Introduction to the Turing Programming Language program commands run Exercise #1 not var put get put Programming Names File/Save As..

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Lecture 10: for, do, and switch

2D Array Practice. Lecture 26

IT115: Introduction to Java Programming 1. IT115: Introduction to Java Programming. Tic Tac Toe Application. Trina VanderLouw

ASCII Art. Introduction: Python

[key, Left subtree, Right subtree]

Exercise 1 Using Boolean variables, incorporating JavaScript code into your HTML webpage and using the document object

EEN118 LAB FOUR. h = v t ½ g t 2

AP Computer Science Lists The Array type

printf( Please enter another number: ); scanf( %d, &num2);

Microsoft Expression Web is usually obtained as a program within Microsoft Expression Studio. This tutorial deals specifically with Versions 3 and 4,

University of Hull Department of Computer Science

Excel Basics: Working with Spreadsheets

Chapter 14 Testing Tactics

Initial Coding Guidelines

EEN118 LAB FOUR. h = v t ½ g t 2

Lesson 10: Quiz #1 and Getting User Input (W03D2)

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

EEN118 LAB FOUR. h = v t ½ g t 2

ios Tic Tac Toe Game John Robinson at Rowan University

The first program: Little Crab

The word pixel means a picture element, or if you must a dot on the screen.

StenBOT Robot Kit. Stensat Group LLC, Copyright 2018

CISC 1600 Lecture 3.1 Introduction to Processing

Inheritance (IS A Relationship)

The for Loop. Lesson 11

Here is a sample IDLE window illustrating the use of these two functions:

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

1. Introduction EE108A. Lab 1: Combinational Logic: Extension of the Tic Tac Toe Game

Lab 4 Due April 18 th

COMP : Practical 6 Buttons and First Script Instructions

Below you'll find some browser and device specific instructions, as well as links to more expansive tutorials if you need them.

Account Settings. Private Messages. Reporting Abuse. Create a Ladder. Ladder Admin Guide. Ladd Admin Guide - Quick Challenge / Result

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Arduinos without (much) programming

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Conditionals: More Fun with If-Then and Logic Operators

EEN118 LAB FOUR. h = v t - ½ g t 2

Introduction to Computer Science Unit 3. Programs

Getting Started With Linux and Fortran Part 2

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

5. Applicative Programming. 1. Juli 2011

COMPUTER SCIENCE. Time Allowed 2 hours

Basic Reliable Transport Protocols

Linked Lists. What is a Linked List?

1007 Imperative Programming Part II

Troubleshooting ROBOTC with Cortex

USER MANUAL ARDUINO I/O EXPANSION SHIELD

Java Coding 3. Over & over again!

Making a maze with Scratch

5.6.1 The Special Variable this

Com S 227 Spring 2018 Assignment points Due Date: Thursday, September 27, 11:59 pm (midnight) "Late" deadline: Friday, September 28, 11:59 pm

ORB Education Quality Teaching Resources

Haskell Refresher Informatics 2D

Mehran Sahami Handout #7 CS 106A September 24, 2014

Jade Support TM Introduction & User Guide

CSE 331 Final Exam 12/9/13

Karlen Communications Add Accessible PowerPoint Placeholders. Karen McCall, M.Ed.

Using Windows Explorer and Libraries in Windows 7

Art 486: Introduction to Interactive Media.

Transcription:

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

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 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. Step 3 2017 courses.techcamp.org.uk/ Page 2 of 9

Step 3 Make an Array 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. Step 4 LED Array 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. Step 5 2017 courses.techcamp.org.uk/ Page 3 of 9

Step 5 Loops Arrays and For 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. Step 6 Update the LEDs 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! Step 7 2017 courses.techcamp.org.uk/ Page 4 of 9

Step 7 Functions 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). Step 8 Make a Function 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! Step 9 2017 courses.techcamp.org.uk/ Page 5 of 9

Step 9 Use the Function 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! Step 10 Space Select 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. Step 11 2017 courses.techcamp.org.uk/ Page 6 of 9

Step 11 Select LED 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! Step 12 Place Counter 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. Step 13 2017 courses.techcamp.org.uk/ Page 7 of 9

Step 13 Place Counter 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! Step 14 Stop the Cheating! 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... Step 15 2017 courses.techcamp.org.uk/ Page 8 of 9

Step 15 Extension Tasks 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! 2017 courses.techcamp.org.uk/ Page 9 of 9