11 finding winners using arrays session 8.2. session overview find out how the c# language makes it...

36
1 Finding Winners Using Arrays Session 8.2

Upload: theodora-watts

Post on 29-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 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

11

Finding Winners Using Arrays

Session 8.2

Page 2: 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

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

Chapter 8.2: Finding Winners Using Arrays 2

Page 3: 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

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

Chapter 8.2: Finding Winners Using Arrays 3

Page 4: 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

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

Chapter 8.2: Finding Winners Using Arrays 4

Page 5: 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

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

Chapter 8.2: Finding Winners Using Arrays 5

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 }}

Page 6: 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

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

Chapter 8.2: Finding Winners Using Arrays 6

Page 7: 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

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

Chapter 8.2: Finding Winners Using Arrays 7

int[] scores = new int[4]; int[] scores = new int[4];

Page 8: 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

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

Chapter 8.2: Finding Winners Using Arrays 8

Page 9: 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

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

Chapter 8.2: Finding Winners Using Arrays 9

Page 10: 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

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

Chapter 8.2: Finding Winners Using Arrays 10

scores[1] = 99; scores[1] = 99;

Page 11: 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

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 Arrays 11

scores[4] = 99; scores[4] = 99;

Page 12: 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

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

Chapter 8.2: Finding Winners Using Arrays 12

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;}

Page 13: 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

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

Chapter 8.2: Finding Winners Using Arrays 13

Page 14: 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

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

Chapter 8.2: Finding Winners Using Arrays 14

Page 15: 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

Using a For Loop to Find the winner

This loop will find the winning score value

It will work for any sized array

Chapter 8.2: Finding Winners Using Arrays 15

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

Page 16: 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

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

Chapter 8.2: Finding Winners Using Arrays 16

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

Page 17: 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

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 Arrays 17

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

Page 18: 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

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 Arrays 18

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

Page 19: 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

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 Arrays 19

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

Page 20: 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

Test for a Valid Score

Only score values greater than 0 are valid

Less than 0 means the button was pressed early

Chapter 8.2: Finding Winners Using Arrays 20

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

Page 21: 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

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 Arrays 21

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

Page 22: 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

Updating the Winning Score

If we have a new winner, store it in the winningValue variable

Chapter 8.2: Finding Winners Using Arrays 22

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

int winningValue = 120;for (int i = 0; i < 4; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; } }}

Page 23: 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

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

Chapter 8.2: Finding Winners Using Arrays 23

Page 24: 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

Storing the Winner Subscript

The value of i for a high score is stored in winnerSubscript

Chapter 8.2: Finding Winners Using Arrays 24

int winningValue = 120;int winnerSubscript = 0;for (int i = 0; i < 16; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; winnerSubscript = i; } }}

int winningValue = 120;int winnerSubscript = 0;for (int i = 0; i < 16; i++){ if (scores[i] > 0) { if (scores[i] < winningValue) { winningValue = scores[i]; winnerSubscript = i; } }}

Page 25: 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

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

Chapter 8.2: Finding Winners Using Arrays 25

Page 26: 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

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

Chapter 8.2: Finding Winners Using Arrays 26

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"};

Page 27: 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

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

Chapter 8.2: Finding Winners Using Arrays 27

if (winningValue != 120){ winnerName = names[winnerSubscript];}else{ winnerName = "**NO WINNER**";}

if (winningValue != 120){ winnerName = names[winnerSubscript];}else{ winnerName = "**NO WINNER**";}

Page 28: 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

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

Chapter 8.2: Finding Winners Using Arrays 28

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}

Page 29: 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

1. Reaction Timer with Winner

Chapter 8.2: Finding Winners Using Arrays 29

This version of the reaction timer game uses the algorithm described above

The name of the winner is displayed

Page 30: 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

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

Chapter 8.2: Finding Winners Using Arrays 30

Page 31: 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

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.

Chapter 8.2: Finding Winners Using Arrays 31

Page 32: 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

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.

Chapter 8.2: Finding Winners Using Arrays 32

Page 33: 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

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.

Chapter 8.2: Finding Winners Using Arrays 33

Page 34: 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

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.

Chapter 8.2: Finding Winners Using Arrays 34

Page 35: 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

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.

Chapter 8.2: Finding Winners Using Arrays 35

Page 36: 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

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.

Chapter 8.2: Finding Winners Using Arrays 36