Using Barcode generator for ASP.NET Control to generate, create Matrix 2D Barcode image in ASP.NET applications.

www.OnBarcode.com

You now need to write some C# that will work through the array and find the best score. At this point, you have a problem. If I ask you, Which element in Figure 8-3 has the winning time in it you would glance at the page and say, The element with subscript 3. And you d be right. The problem is that you won t necessarily be able to tell me precisely how you worked it out. You simply looked along the row, and that number was the smallest number that wasn t negative. It was obvious. Unfortunately, you can t say to the C# compiler, Look along the row and find me the winner. You need to set things out in simpler and much more boring steps. The program must look at each element in turn and see if it wins. At any given point in the process, the program would have an idea of the best result it has seen so far. If it sees a value that is better, it now has a new winner and so on. If you think about it, this is what people really do, particularly if they are working through 1,000 numbers instead. In that case, they would take care to remember the best result that they had seen so far as you went through and probably write it down on a piece of paper. With all this in mind, consider the following code:

Using Barcode maker for iPhone Control to generate, create Data Matrix ECC200 image in iPhone applications.

www.OnBarcode.com

This code uses a variable called winningValue to hold the smallest value it has seen so far. It starts by setting it to a large value that is guaranteed not to be a winner. It then compares winningValue with each element in the array in turn. If the element is smaller than the current smallest, it sets winningValue to the new value. Before it tests winningValue, the code makes sure that the count is a valid one in that the button must have been pressed.

This code selects the winning string for the A and B buttons on gamepad 1. The string winnerName is set with the name of the winning button and gamepad and can be displayed on the screen at the end of the game. You could write more statements for each of the other buttons and gamepads. Note

You need to be careful to make sure that when you check the buttons, you set the correct elements in the array; otherwise, the wrong names will be displayed.

Using an Array as a Lookup Table

The previous code will produce a string that contains the name of the winning gamepad and button. But you still need to perform all those conditional statements to decide the string to display. However, you do have a way to make your life easier, and it starts by finding out the position in the array of the winning score.

This is the same loop as before, but you now have a variable called winnerSubscript that will hold the position in the array of the winning value. Note that the program copies the value of i into the winnerSubscript when it finds a new winning value. Remember that when you find a new winning value, the variable i will hold the subscript in the array where that value is stored.