| Programming The Tic Tac Toe Game | ||
| Part 4: Finding a Winner | ||
|
After each move the CheckWin function checks for the winning combination. CheckWin works by adding down each row, across each column and through each diagonal. Tracing the steps through CheckWin using Visual Basic's Debug feature can be very educational. Finding a win is a matter of first, checking whether three 1's were found in each of the individual checks in the variable iScore, and then returning a unique "signature" value in Checkwin that is used as the array index to change the Visible property of one element in the linWin component array. If there is no winner, CheckWin will contain the value -1. If there is a winner, the display is updated, the scoreboard is changed, a congratulation message is displayed, and the game is restarted. Let's go through one of the checks in detail to see how it works. The others are similar. The first thing to notice is that the first index counter i counts down the rows while the second j counts across the columns. The outer loop, then simply moves from one row to the next. The inner loop counts the 1's in the current row. If there are three, then we have a winner. Notice that we also keep track of the total number of squares tested in the variable CheckWin, which is the value passed back when this function terminates. Each winning combination will end up with a unique value in CheckWin from 0 to 7 which is used to select one of the elements in the linWin() component array. This makes the order of the code in function CheckWin important too! If you moved one of the blocks of loop code (like the one above), the wrong line would be drawn on the playing grid when someone wins. Try it and see! Finishing DetailsThe only code we haven't discussed are the subroutine for a new game and the subroutine that will reset the score. The rest of the logic in the system makes creating these quite easy. To start a new game, we have only to call the InitPlayGround subroutine. As a convenience for players since the button could be clicked in the middle of a game, we ask for confirmation before going ahead. We also ask for confirmation before restarting the scoreboard. In the next article, we try out the VB.NET Upgrade Wizard that Microsoft says will help you convert VB 6 to VB.NET. You'll discover that it doesn't do the whole job. To read the article, Click Here. Start page > Building the GUI > Page 1, 2, 3, 4 |
||
