| XNA Game-Themed CS1 Examples (XGC1) | |
|
Release 2.0 (XNA V3.1) |
|
References:
Goals:
1. Obtain the example code
In this tutorial, you can control the location of the
soccer ball using the right thumbstick. At the top of the screen, the
contents of an array of numbers is displayed (in this case, the array holds the
values 2, 3, 7, and 9). If the soccer ball isn't
overlapping with any blocks, then the message "No Number To Search For!" is displayed at
the bottom of the screen, as pictured here:

If the soccer ball overlaps with a block, then
the program figures out what number is on the block, and looks through the array
of numbers to try and find the block's number. If the program finds the
block's number in the array, then the "Found the value X in the array!" is
displayed at the bottom of the screen (where X is the block's number), as
pictured here:

If the program finds the block's number in the array, then
the "X was not found in the array!" is displayed at the bottom of the screen
(where X is the block's number), as pictured here:

The major new concept for this tutorial has less to do with C# syntax, semantics, or techniques, and more to do with clearly identifying a pattern that programmers frequently use: a linear search.
Arrays are used to store a collection of things, which we may then need to locate again later. Whether those things are numbers, such as employee ID numbers or prices, or whether those things are objects, like on-screen rectangles, isn't really important for the purposes of doing linear search. What is important is that we will frequently need to determine if a particular object (or number) is in the array (collection), based on some criteria. As an example, let's say that we have an array of the employee ID numbers for employees who are on vacation. Let's say that we want to know if someone is on vacation (let's call him Joe), and we have their employee ID number. One way to see if Joe's employee ID number is in the array is to simply start with element 0, and ask "Is this Joe's ID number?" If it is, then we know Joe is on vacation (and we can stop our search). If not, then we go on to element #1, and again ask "Is this Joe's ID number?" If it is, then we know Joe is on vacation (and we can stop our search). If not, then we go on to element #2, and ask the same question again. If Joe's ID number is anywhere in the array, then we will eventually find it. If Joe's number is NOT in the array, then we will traverse the entire array, find that it's not there, and conclude that Joe's number is not in the array.
This technique is called a 'linear search' for at least a couple of reasons:
The time required to search the array is directly
proportional to the number of elements in the array. If it takes the
computer, say, 15 units of time (don't worry about the units - maybe it's
seconds, maybe it's milliseconds - it doesn't actually matter), then searching
10 elements will take (10*15=) 150 units of time. Searching 100 elements
will take (100*15=)1500 units of time. In a nutshell, (time required) = 15
* (number of elements). If we translate this into a mathematical formula,
and say that (time required) is y, and (number of elements) is x, then we get
the equation y = 15x, which is clearly a linear equation. Thus, we
call this search technique a linear search because it will require linear time,
as a function of the number of array elements.
For now, don't worry about the details of this -
you will see these
concepts again later, but don't really need to understand them in detail
right now.
You can think about the linear search as starting at one
end of the array, and then stepping through the array, as if it was following
the blue line in the following picture:

In this tutorial, we'll examine two ways in which you can use the 'linear
search' idea: first, to find an object in an array that satisfies some criteria
(in this case, "Does the soccer ball overlap the block?"), and second, to look
through an array of numbers, and attempt to find a particular number (in this
case, the block's number)
2. Examining The Program:
Let's examine the
C# source code that produces the behavior we see on-screen
|
//
constant size for the array private const int BLOCK_ARRAY_SIZE = 10; // an array of Circles to represent the SoccerBalls private XNACS1Rectangle[] m_Blocks; private XNACS1Circle m_SoccerBall; // this is under user control private int[] m_NumbersToSearchThrough; |
and then setting the value of each element by hand:
m_NumbersToSearchThrough[0] = 2;
m_NumbersToSearchThrough[1] = 3;
m_NumbersToSearchThrough[2] = 7;
m_NumbersToSearchThrough[3] = 9;
This is a reasonable approach, since we've picked the numbers to search for out of thin air, and there's no obvious pattern here (i.e., we can't easily replace this with a loop).
|
protected override
void
UpdateWorld() { if (GamePad.ButtonBackClicked()) this.Exit(); m_SoccerBall.CenterX += GamePad.ThumbSticks.Right.X; m_SoccerBall.CenterY += GamePad.ThumbSticks.Right.Y; DisplayNumbersToSearchAtTop(); String foundString = "No Number To Search For!"; // now try to find the colliding block bool found = false; int i = 0; while ((!found) && (i < m_Blocks.Length)) { if (m_Blocks[i].Collided(m_SoccerBall)) { // found! found = true;} else i = i + 1; } if (found) { int numberToFind = i; foundString = numberToFind + " was not found in the array"; // This is the classic linear search: for (int j = 0; j < m_NumbersToSearchThrough.Length; j++) { if (m_NumbersToSearchThrough[j] == numberToFind) { foundString = "Found the value " + numberToFind + " in the array!";} } } EchoToBottomStatus(foundString); } |
String foundString = "No Number To Search For!";
bool found = false;
int i = 0;
while ((!found) && (i < m_Blocks.Length))
{
if (m_Blocks[i].Collided(m_SoccerBall))
{
// found!
found =
true;}
else
i = i + 1;
}
foundString = numberToFind +
" was not found in the array";
- First, we create and initialize numberToFind. This variable isn't necessary, but it does provide a clearer, easier to understand name. Essentially, we know that since the blocks are numbered 0, 1, 2....9, and that since these numbers correspond with the blocks' indices in the array, therefore the index of the block is the same as it's number. Since we're looking for the block's number, then the number to find is the same as the block's index.
- Next, we set up foundString with a message telling the user that the block's number isn't in the array. If we do find the number in the array, then we'll replace this message with the "Found it!" message, but if we don't, then we're all set up to display this message.
// This is the classic linear search:
for (int j = 0; j < m_NumbersToSearchThrough.Length; j++)
if (m_NumbersToSearchThrough[j] == numberToFind)
for (int j = 0; j < m_NumbersToSearchThrough.Length; j++)
{
if (m_NumbersToSearchThrough[j] == numberToFind)
{
foundString =
"Found the value " + numberToFind + " in the array!";break;
}
}
This tells C# to break out of the nearest enclosing loop (in this case, the for loop)
FURTHER EXERCISES: