Tic tac toe program in visual basic 2010




















PaintEventArgs If Button7. PaintEventArgs If Button2. PaintEventArgs If Button3. Click Form1. Show Me. Hide End Sub End Class.

Public Class easy. Object , ByVal e As System. If Button3. ElseIf Button5. ElseIf Button4. ElseIf Button6. ElseIf Button7. ElseIf Button8. ElseIf Button9. End If. If Button1. If Button4. This program violates the rule partly to make this point and partly because this is a relatively simple piece of code where it's easier to see what is being done and avoid problems later. A player selection of a game square is processed by calling the GamePlay subroutine with Index as the argument.

First, you check to see if an unoccupied square was clicked. Once we're sure this is a legitimate move, the move counter iMove is incremented.

The next two lines are very interesting since they translate the coordinates from the one-dimensional If lblPlayGround component array to two-dimensional indexes that you can use in either iXPos or iOPos.

Mod and integer division the 'backslash' are mathematical operations that you don't use every day, but here's a great example showing how they can be very useful. The value in sPlaySign, a variable with module scope, keeps track of which player made the move.

Once the move arrays are updated, the label components in the playing grid can be updated with the appropriate sign. For example, when the X player clicks the top left corner of the grid, variables will have the following values:.

The user screen shows only an X in the upper left box, while the iXPos has a 1 in the upper left box and 0 in all of the others. The iOPos has 0 in every box. The values changes when the O player clicks the center square of the grid. Now th iOPos shows a 1 in the center box while the user screen shows an X in the upper left and an O in the center box. The iXPos shows only the 1 in the upper left corner, with 0 in all of the other boxes.

Now that you know where a player clicked, and which player did the clicking using the value in sPlaySign , all you have to do is find out if someone won a game and figure out how to show that in the display.

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. 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 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 you have a winner. Notice that you 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! The only code not yet discussed is 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, you have only to call the InitPlayGround subroutine. As a convenience for players since the button could be clicked in the middle of a game, you ask for confirmation before going ahead.

You also ask for confirmation before restarting the scoreboard. Actively scan device characteristics for identification. Use precise geolocation data. Select personalised content. Create a personalised content profile. Measure ad performance. Select basic ads. Create a personalised ads profile. Select personalised ads.

Apply market research to generate audience insights. Measure content performance. Develop and improve products. List of Partners vendors. Share Flipboard Email. Table of Contents Expand. How to Play Tic Tac Toe. Starting the Program. I'm having lots of problems getting this done, especially with the array and creating the console text view.

Load MessageBox. Show "Welcome to Tic Tac Toe! Would you like to play a game? WriteLine Button1. Text Console. WriteLine "" Console. WriteLine Button4. WriteLine Button7. KeyPress, Button1. KeyPress Console. WriteLine e. KeyChar Select Case e. KeyChar Case "1" Button1.

The display is a 3x3 grid. That does not mean that the best form of data organisation for the state of the game is 3x3 - maybe it is, maybe it isn't. There is no particular reason that the display format should dictate how you manage you data - that should be controlled by the processing that you intend to apply to that data. In this case, the game play that I mentioned is more important than the display. How do you intend to manage things like selecting the best next move, detecting a winning situation, and working out who the winner is?

These questions have nothing to do with VB - it's to do with understanding the problem and designing the solution. As Acamar has stated, using a multidimensional array to store the game state information may or may not be the best way to proceed While you can still setup an example of a Tic-Tac-Toe game that uses a multidimensional state array for demonstration purposes, the actual logic for a Tic-Tac-Toe game would be easier to implement using simple integers to hold bit-flag values.

You essentially have nine bits of data and all possible "winning combinations" can represented by the integer with that particular bit pattern. Organizing those nine bits into a grid pattern is simply a conceptual issue which is handled when the output is rendered to the screen again, as Acamar has stated.

So just for fun, and because it can't possibly be used as the actual solution to this requirement if the requirement is to demonstrate multidimensional arrays , here is a simple example of a console program to play Tic-Tac-Toe against an unintelligent AI the AI simply makes random choices and does not try to actually block the player from winning - you could certainly expand it to do so though.

Reed Kimble - "When you do things right, people won't be sure you've done anything at all". If you look at this thread , near the bottom is a Tic-Tac-Toe game that Iron Razerz posted - it is simple and efficient and should give you some good ideas. I'm old and retired. I like to program if you could call what I do programming. However I'd like to code for you!



0コメント

  • 1000 / 1000