11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

11 Reaction Timer Game Session 8.1. Session Overview  Find out how an XNA program can measure the passage of time and trigger events at certain points.
Chapter 10 Introduction to Arrays
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Chapter 9 Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
11 Games and Content Session 4.1. Session Overview  Show how games are made up of program code and content  Find out about the content management system.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
COMPUTER PROGRAMMING 2 Timers. Game Idea: Mob Reaction Timer Use a timer variable to keep track of time and a variable for each player to measure the.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
A First Book of ANSI C Fourth Edition
11 Adding Sounds Session 7.1. Session Overview  Find out how to capture and manipulate sound on a Windows PC  Show how sound is managed as an item of.
Mark Dunlop, Computer and Information Sciences, Strathclyde University 1 Algorithms & Complexity 5 Games Mark D Dunlop.
11 Using the Keyboard Session Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types.
19/10/20151 Data Structures Arrays. 219/10/2015 Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Records Engineering 1D04, Teaching Session 11. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng1 Records  So far we've seen a number of different.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
11 Getting Player Input Using a Gamepad Session 3.1.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
CHAPTER 3 Getting Player Input XNA Game Studio 4.0.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
CHAPTER 2 The Game Loop - Variables, Types, Classes and Objects in XNA XNA Game Studio 4.0.
11 Writing Text Session 5.1. Session Overview  Show how fonts are managed in computers  Discover the difference between bitmap fonts and vector fonts.
11 Debugging Programs Session Session Overview  Create and test a method to calculate percentages  Discover how to use Microsoft Visual Studio.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
11 Using the Keyboard in XNA Session 9.1. Session Overview  Discover more detail on how the XNA keyboard is implemented  Find out how to use arrays.
Lives and Scoring Games Programming in Scratch. Games Programming in Scratch L2 Lives and Scoring Learning Objectives Define a variable Understand the.
Arrays. Arrays are objects that help us organize large amounts of information.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 19 A Ray of Sunshine.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Microsoft Visual Basic 2005: Reloaded Second Edition
Arrays, For loop While loop Do while loop
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays.
Presentation transcript:

11 Finding Winners Using Arrays Session 8.2

Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values of a particular type  Learn how to work with individual values stored as part of an array  Use arrays to make a program that automatically displays the winning score from the Reaction Timer game  Use an array as a look-up table to identify the winning players of the Reaction Timer game  Find out how the C# language makes it easy to create an array that contains multiple values of a particular type  Learn how to work with individual values stored as part of an array  Use arrays to make a program that automatically displays the winning score from the Reaction Timer game  Use an array as a look-up table to identify the winning players of the Reaction Timer game Chapter 8.2: Finding Winners Using Arrays2

Reaction Timer Winner Display  At the moment the players have to decide who won a Reaction Timer game  They have to find the lowest button time and then work out which player had that time  It would be nice if the game program could do this  At the moment the players have to decide who won a Reaction Timer game  They have to find the lowest button time and then work out which player had that time  It would be nice if the game program could do this Chapter 8.2: Finding Winners Using Arrays3

Finding the Winning Score  A winning score is one which is greater than 0, and less than any other score  This means that the player pressed their button before anyone else  We can create a condition that will test if a particular score is the winning one  The program just has to compare the value with all the ones that it needs to beat  A winning score is one which is greater than 0, and less than any other score  This means that the player pressed their button before anyone else  We can create a condition that will test if a particular score is the winning one  The program just has to compare the value with all the ones that it needs to beat Chapter 8.2: Finding Winners Using Arrays4

Finding the Winning Score  This code tests to see if button A has beaten all the other buttons on gamepad 1  To make the test for all the gamepads would require another 12 tests  This code tests to see if button A has beaten all the other buttons on gamepad 1  To make the test for all the gamepads would require another 12 tests Chapter 8.2: Finding Winners Using Arrays5 if ( ascore1 > 0 ) { if ( ascore1 < bscore1 && ascore1 < xscore1 && ascore1 < yscore1 ) { // if we get here button A of Gamepad 1 has won } } if ( ascore1 > 0 ) { if ( ascore1 < bscore1 && ascore1 < xscore1 && ascore1 < yscore1 ) { // if we get here button A of Gamepad 1 has won } }

The C# Array  To solve this problem we need another way of accessing data  We need a way that a program can work through a list of score values and find the smallest one that is the winner  In computer language terms we need to use an array  We know that computers can work with very large amounts of data, now we are going to find out how  To solve this problem we need another way of accessing data  We need a way that a program can work through a list of score values and find the smallest one that is the winner  In computer language terms we need to use an array  We know that computers can work with very large amounts of data, now we are going to find out how Chapter 8.2: Finding Winners Using Arrays6

Creating a “One Dimensional” Array  An array is declared like any other variable  The [] characters (square brackets) are very important  The above array is called scores and it has been created with a capacity of 4 values  The array is of type int, i.e. it can hold 4 integers  We can create arrays of any type we like  An array is declared like any other variable  The [] characters (square brackets) are very important  The above array is called scores and it has been created with a capacity of 4 values  The array is of type int, i.e. it can hold 4 integers  We can create arrays of any type we like Chapter 8.2: Finding Winners Using Arrays7 int[] scores = new int[4];

Visualizing the Array  You can visualize an array as a row of numbered boxes  In the case of the scores array there are four such boxes, each of which can hold a single integer  The first box in the row has the number 0, the last box has the number 3  You can visualize an array as a row of numbered boxes  In the case of the scores array there are four such boxes, each of which can hold a single integer  The first box in the row has the number 0, the last box has the number 3 Chapter 8.2: Finding Winners Using Arrays8

Arrays and Elements  Each box in the array is called an element  When an integer array is created all the elements are initialized to the value 0  A C# program can read and write the values in the elements in an array  Each box in the array is called an element  When an integer array is created all the elements are initialized to the value 0  A C# program can read and write the values in the elements in an array Chapter 8.2: Finding Winners Using Arrays9

Using a Subscript to Access an Element  A program can access a particular element by using a subscript value  The subscript is given in square brackets, as shown above  A program can access a particular element by using a subscript value  The subscript is given in square brackets, as shown above Chapter 8.2: Finding Winners Using Arrays10 scores[1] = 99;

Falling Off the End of the Array  An attempt to access an array element that does not exist will cause the program to fail with an exception Chapter 8.2: Finding Winners Using Arrays11 scores[4] = 99;

Storing Reactions Scores in an Array  This code replaces the variable ascore1 with the element at the start of the scores array  We can do this for all the other score values in the game, so that the time values are all held in the scores array  This code replaces the variable ascore1 with the element at the start of the scores array  We can do this for all the other score values in the game, so that the time values are all held in the scores array Chapter 8.2: Finding Winners Using Arrays12 if (oldpad1.Buttons.A == ButtonState.Released && pad1.Buttons.A == ButtonState.Pressed && scores[0] == 0) { scores[0] = timer; } if (oldpad1.Buttons.A == ButtonState.Released && pad1.Buttons.A == ButtonState.Pressed && scores[0] == 0) { scores[0] = timer; }

Storing the Scores in an Array  After a game has been played we might have a set of results as shown above  We now need to create some C# code that will find the winning score  We need to find the smallest score value which is greater than zero  After a game has been played we might have a set of results as shown above  We now need to create some C# code that will find the winning score  We need to find the smallest score value which is greater than zero Chapter 8.2: Finding Winners Using Arrays13

An Algorithm to Find the Winning Score “Look at each element in the array in turn. If the element contains a “better” value than the one you presently have, that is now the new best value”  This is what you actually did when you worked out the answer  If you had to do this for 10,000 score values you would write down the best value you had seen so far, so that you didn’t forget it and have to start again “Look at each element in the array in turn. If the element contains a “better” value than the one you presently have, that is now the new best value”  This is what you actually did when you worked out the answer  If you had to do this for 10,000 score values you would write down the best value you had seen so far, so that you didn’t forget it and have to start again Chapter 8.2: Finding Winners Using Arrays14

Using a For Loop to Find the winner  This loop will find the winning score value  It will work for any sized array  This loop will find the winning score value  It will work for any sized array Chapter 8.2: Finding Winners Using Arrays15 int winningValue = 120; for (int i = 0; i 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } } }

Creating Our “Highest So Far” Winning Value  Create a variable to hold the winning score  Set it to a very large value which is not a winner  Create a variable to hold the winning score  Set it to a very large value which is not a winner Chapter 8.2: Finding Winners Using Arrays16 int winningValue = 120; for (int i = 0; i 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } } }

Declaring and Setting Up Our Loop Counter  C# lets you declare and initialize a variable to be used as a loop counter in a single statement Chapter 8.2: Finding Winners Using Arrays17 int winningValue = 120; for (int i = 0; i 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } } }

Checking the End Condition of the Loop  If we try to use the subscript 4 the program will throw an exception, so the loop must stop when i reaches 4 Chapter 8.2: Finding Winners Using Arrays18 int winningValue = 120; for (int i = 0; i 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } } }

Moving on to the Next Element  Once we have tested one element we need to move on to the next one in the array Chapter 8.2: Finding Winners Using Arrays19 int winningValue = 120; for (int i = 0; i 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } } }

Test for a Valid Score  Only score values greater than 0 are valid  Less than 0 means the button was pressed early  Only score values greater than 0 are valid  Less than 0 means the button was pressed early Chapter 8.2: Finding Winners Using Arrays20 int winningValue = 120; for (int i = 0; i 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } } }

Test for a Winning Score  If this score value is less than the present winner then we have a new winning value Chapter 8.2: Finding Winners Using Arrays21 int winningValue = 120; for (int i = 0; i 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } } }

Updating the Winning Score  If we have a new winner, store it in the winningValue variable Chapter 8.2: Finding Winners Using Arrays22 int winningValue = 120; for (int i = 0; i 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } } }

Identifying the Winning Player  At the moment the program just works out the winning score  It does not say which button achieved that score  The program must display the winner as well  This means that it must “remember” the position in the array of the winning score value  We can then use this position to identify the winning player  At the moment the program just works out the winning score  It does not say which button achieved that score  The program must display the winner as well  This means that it must “remember” the position in the array of the winning score value  We can then use this position to identify the winning player Chapter 8.2: Finding Winners Using Arrays23

Storing the Winner Subscript  The value of i for a high score is stored in winnerSubscript Chapter 8.2: Finding Winners Using Arrays24 int winningValue = 120; int winnerSubscript = 0; for (int i = 0; i 0) { if (scores[i] < winningValue) { winningValue = scores[i]; winnerSubscript = i; } } }

Identifying the Winner Using a Look-Up Table  We now have code that will find out the position in the scores array of the winning score  We can create a second array which lets the program look up the name of button that generated this score  The names array is an array of strings  Each element holds the name of a button  We now have code that will find out the position in the scores array of the winning score  We can create a second array which lets the program look up the name of button that generated this score  The names array is an array of strings  Each element holds the name of a button Chapter 8.2: Finding Winners Using Arrays25

Declaring a Look-Up Table  The names array is an array of strings which are pre-set with the button names  The C# compiler can work out how many elements are being created, so there is no need to set the size of the names array  The text lines up with the elements in scores  The names array is an array of strings which are pre-set with the button names  The C# compiler can work out how many elements are being created, so there is no need to set the size of the names array  The text lines up with the elements in scores Chapter 8.2: Finding Winners Using Arrays26 string[] names = new string[] { "Gamepad 1 A", "Gamepad 1 B", "Gamepad 1 X", "Gamepad 1 Y" }; string[] names = new string[] { "Gamepad 1 A", "Gamepad 1 B", "Gamepad 1 X", "Gamepad 1 Y" };

Displaying the Winner  This code sets a string variable in the game world called winnerName to the name of the winner  If there are no winners (nobody pressed their button) the string is set to “No Winner”  The string is displayed by the Draw method  This code sets a string variable in the game world called winnerName to the name of the winner  If there are no winners (nobody pressed their button) the string is set to “No Winner”  The string is displayed by the Draw method Chapter 8.2: Finding Winners Using Arrays27 if (winningValue != 120) { winnerName = names[winnerSubscript]; } else { winnerName = "**NO WINNER**"; }

Timing the Game play  The game will display the winner two seconds after the sound effect was played  Code in the Update method can test for the timer value reaching 120 and trigger the code that finds the winning score and sets it for display  The game will display the winner two seconds after the sound effect was played  Code in the Update method can test for the timer value reaching 120 and trigger the code that finds the winning score and sets it for display Chapter 8.2: Finding Winners Using Arrays28 if (timer == 120) { // find the winning score // set the variable winnerName to the winner } if (timer == 120) { // find the winning score // set the variable winnerName to the winner }

1. Reaction Timer with Winner Chapter 8.2: Finding Winners Using Arrays29  This version of the reaction timer game uses the algorithm described above  The name of the winner is displayed  This version of the reaction timer game uses the algorithm described above  The name of the winner is displayed

Summary  A C# array allows a programmer to create a single variable that holds a large number of items of a particular type  Each item in an array is called an element, and particular elements are identified by means of a subscript value  In an array of size n, the subscript values range from 0 to n-1  Attempts to use an invalid subscript value will cause a program to throw an exception  A C# array allows a programmer to create a single variable that holds a large number of items of a particular type  Each item in an array is called an element, and particular elements are identified by means of a subscript value  In an array of size n, the subscript values range from 0 to n-1  Attempts to use an invalid subscript value will cause a program to throw an exception Chapter 8.2: Finding Winners Using Arrays30

True/False Revision Quiz  An array holds a collection of values of a particular type.  The first element in an array has the subscript value 1.  A given array can hold any number of values.  Arrays can be pre-set with values when they are created.  A program will throw an exception if a program uses an invalid subscript value.  An array holds a collection of values of a particular type.  The first element in an array has the subscript value 1.  A given array can hold any number of values.  Arrays can be pre-set with values when they are created.  A program will throw an exception if a program uses an invalid subscript value. Chapter 8.2: Finding Winners Using Arrays31

True/False Revision Quiz  An array holds a collection of values of a particular type.  The first element in an array has the subscript value 1.  A given array can hold any number of values.  Arrays can be pre-set with values when they are created.  A program will throw an exception if a program uses an invalid subscript value.  An array holds a collection of values of a particular type.  The first element in an array has the subscript value 1.  A given array can hold any number of values.  Arrays can be pre-set with values when they are created.  A program will throw an exception if a program uses an invalid subscript value. Chapter 8.2: Finding Winners Using Arrays32

True/False Revision Quiz  An array holds a collection of values of a particular type.  The first element in an array has the subscript value 1.  A given array can hold any number of values.  Arrays can be pre-set with values when they are created.  A program will throw an exception if a program uses an invalid subscript value.  An array holds a collection of values of a particular type.  The first element in an array has the subscript value 1.  A given array can hold any number of values.  Arrays can be pre-set with values when they are created.  A program will throw an exception if a program uses an invalid subscript value. Chapter 8.2: Finding Winners Using Arrays33

True/False Revision Quiz  An array holds a collection of values of a particular type.  The first element in an array has the subscript value 1.  A given array can hold any number of values.  Arrays can be pre-set with values when they are created.  A program will throw an exception if a program uses an invalid subscript value.  An array holds a collection of values of a particular type.  The first element in an array has the subscript value 1.  A given array can hold any number of values.  Arrays can be pre-set with values when they are created.  A program will throw an exception if a program uses an invalid subscript value. Chapter 8.2: Finding Winners Using Arrays34

True/False Revision Quiz  An array holds a collection of values of a particular type.  The first element in an array has the subscript value 1.  A given array can hold any number of values.  Arrays can be pre-set with values when they are created.  A program will throw an exception if a program uses an invalid subscript value.  An array holds a collection of values of a particular type.  The first element in an array has the subscript value 1.  A given array can hold any number of values.  Arrays can be pre-set with values when they are created.  A program will throw an exception if a program uses an invalid subscript value. Chapter 8.2: Finding Winners Using Arrays35

True/False Revision Quiz  An array holds a collection of values of a particular type.  The first element in an array has the subscript value 1.  A given array can hold any number of values.  Arrays can be pre-set with values when they are created.  A program will throw an exception if a program uses an invalid subscript value.  An array holds a collection of values of a particular type.  The first element in an array has the subscript value 1.  A given array can hold any number of values.  Arrays can be pre-set with values when they are created.  A program will throw an exception if a program uses an invalid subscript value. Chapter 8.2: Finding Winners Using Arrays36