XNA Game-Themed CS1 Examples (XGC1) Release 2.0 (XNA V3.1) 2/8/2010
Topic: Topic.6.Arrays
Example: Ex_6.SearchBlocksWithWhile

# Arrays: Linear Search Through An Array (While Loop)

References:

• Pre-requisite: it is assumed that you have read through the prior tutorials, and are familiar with the concepts covered in those tutorials.

Goals:

• In this tutorial, we will:
• Examine a variation of a 'linear search' works, using a while loop instead of a for loop..

1. Obtain the example code

Download and unzip the zip file and you will see an ExampleProgram folder. Open the ExampleProgram folder, the EXE folder contains the compiled program and you can double click on the .sln file to work with the source code.

When the game starts, you'll see a screen that looks similar to this:

The program is nearly identical to the earlier tutorial, 260.PongSoccerWithArray.  The only change for this tutorial is that we're doing our linear search using a while loop, rather than a for loop.

2. Examining The Program:

Let's examine the C# source code that produces the behavior we see on-screen

• BounceOffBlocks() (called from UpdateWorld() ): /// /// Test for collision with the blocks, if collision occurs, /// flip the sign of the x-component of the velocity. /// private void BounceOffBlocks() {     #region this was what we used to do with no array ...     //     // if (m_ABlock.Collided(m_TheSoccer))     // {     //     m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX;     //     EchoToTopStatus("Last block collision: A-Block");     //     PlayACue("Break");     // }     // else if (m_BBlock.Collided(m_TheSoccer)) ...     #endregion     #region This was the "for-loop" solution:     // notice, we only really need to find the first colliding block     // there is no reason for further test after one collision has been     // detected: physically impossible to collide with two blocks!     //     // for (int i=0; i
• As you can see, the while loop can also be used to do a linear search.
• This particular while loop uses the 'set found to true' trick that will end the loop early, once we find a block that overlaps the soccer ball.

FURTHER EXERCISES:

1. Start from a blank starter project (1000.201, if you need it), and re-do the code from memory as much as possible.  On your first try, do what you can, and keep the above code open so that when you get stuck, you can quickly look up what you forgot (and that after you finish a line, so that you can compare your line to the 'correct' line).  On the next try, do the same thing, but try to use the finished code less.  Repeat this until you can type everything, without refering the tutorial's code.
• Repeat this exercise daily for several days, so that you really get the hang of this.  As you go on, periodically review this by re-doing this exercise.
2. Listing All Matches
For this exercise, you should use the same project that was explained in the above tutorial.
Modify the program so that instead of displaying the contents of the m_NumbersToSearchThrough array along the top, you instead display all the rectangles that the soccer ball overlaps.  If the soccer ball is overlapping no rectangles, then display a message like "No overlapping rectangles".  If the soccer ball only overlaps rectangle #5, then display something like "Overlap with #5", and if the soccer ball overlaps #5 and #6, display something like "Overlap with #5, #6".
• While it is impossible for the soccer ball to overlap more than 2 squares, you should still write your code so that the program displays a list of all overlapping rectangles - if the soccer ball was large enough to overlap all of the rectangles, then your code should display a list of all the rectangles.