By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 916 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Tic Tac Toe on a 4x4

P: 2
I have the task of creating a tic tac toe game on a 4x4 board. It is a one player game with the computer as the opponent. The player (blue) clicks on a button and the computer (red) should automatically select a random positon on the board until there is a win lose or draw. I can't quite figure out how to setup the code for the colors or creating random number selection using this CInt(Int(Rnd() * 16 + 1)). Here is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. 'Project: Tic Tac Toe
  2. 'Programmer: Paige Mims
  3. 'Date: 04/20/2007
  4. 'Description: This project plays Tic Tac Toe.
  5.  
  6. Public Class Tictactoe
  7.  
  8.     Private Sub Random()
  9.  
  10.         Randomize()
  11.         Dim RandomClass As New Random()  'Declare RandomClass.Next(0, 17) 'Generates the random number between a range: 0 to 17 
  12.         Dim RandomNumber As Integer
  13.         'RandomNumber = RandomClass.Next'
  14.         Randomize()
  15.         RandomNumber = CInt(Int(Rnd() * 16 + 1))
  16.         'Seed Random number generator
  17.         'Initialize the random # generator
  18.         Dim Board(4, 4) As String
  19.  
  20.     End Sub
  21.     Private Sub SetPiece(ByVal Row As Integer, ByVal Column As Integer, ByVal pic As PictureBox)
  22.         If Board(Row, Column) = Red Or Board(Row, Column) = Blue Then
  23.             ' Do nothing if Red or Blue already in square
  24.  
  25.             If IsBlue Then
  26.                 Board(Row, Column) = color.blue
  27.                 Me.Text = "Player:" & red
  28.             Else
  29.                 Board(Row, Column) = color.red
  30.                 Me.Text = "Player:" & blue
  31.             End If
  32.         End If
  33.     End Sub
  34.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, _
  35.     Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click, Button10.Click, _
  36.     Button11.Click, Button12.Click, Button13.Click, Button14.Click, Button15.Click, Button16.Click
  37.  
  38.  
  39.         sender.Backcolor = Color.Blue
  40.  
  41.         'Turns a square blue
  42.  
  43.         If sender.BackColor = Color.Blue Then
  44.  
  45.             Do
  46.  
  47.                 Select Case sender.backcolor
  48.  
  49.                     Case Is = 1 And Button1.BackColor = Color.Red
  50.                         Button1.BackColor = Color.Blue
  51.  
  52.                     Case Is = 2 And Button2.BackColor = Color.Red
  53.                         Button2.BackColor = Color.Blue
  54.  
  55.                     Case Is = 3 And Button3.BackColor = Color.Red
  56.                         Button3.BackColor = Color.Blue
  57.  
  58.                     Case Is = 4 And Button4.BackColor = Color.Red
  59.                         Button4.BackColor = Color.Blue
  60.  
  61.                     Case Is = 5 And Button5.BackColor = Color.Red
  62.                         Button5.BackColor = Color.Blue
  63.  
  64.                     Case Is = 6 And Button6.BackColor = Color.Red
  65.                         Button6.BackColor = Color.Blue
  66.  
  67.                     Case Is = 7 And Button7.BackColor = Color.Red
  68.                         Button7.BackColor = Color.Blue
  69.  
  70.                     Case Is = 8 And Button8.BackColor = Color.Red
  71.                         Button8.BackColor = Color.Blue
  72.  
  73.                     Case Is = 9 And Button9.BackColor = Color.Red
  74.                         Button9.BackColor = Color.Blue
  75.  
  76.                     Case Is = 10 And Button10.BackColor = Color.Red
  77.                         Button10.BackColor = Color.Blue
  78.  
  79.                     Case Is = 11 And Button11.BackColor = Color.Red
  80.                         Button11.BackColor = Color.Blue
  81.  
  82.                     Case Is = 12 And Button12.BackColor = Color.Red
  83.                         Button12.BackColor = Color.Blue
  84.  
  85.                     Case Is = 13 And Button13.BackColor = Color.Red
  86.                         Button13.BackColor = Color.Blue
  87.  
  88.                     Case Is = 14 And Button14.BackColor = Color.Red
  89.                         Button14.BackColor = Color.Blue
  90.  
  91.                     Case Is = 15 And Button15.BackColor = Color.Red
  92.                         Button15.BackColor = Color.Blue
  93.  
  94.                     Case Is = 16 And Button16.BackColor = Color.Red
  95.                         Button16.BackColor = Color.Blue
  96.  
  97.                 End Select
  98.  
  99.             Loop Until True
  100.  
  101.         End If
  102.  
  103.     End Sub
  104.  
  105.  
  106.     Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, _
  107.     Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click, Button10.Click, _
  108.     Button11.Click, Button12.Click, Button13.Click, Button14.Click, Button15.Click, Button16.Click
  109.  
  110.         Button1.BackColor = Color.Gray
  111.         Button2.BackColor = Color.Gray
  112.         Button3.BackColor = Color.Gray
  113.         Button4.BackColor = Color.Gray
  114.         Button5.BackColor = Color.Gray
  115.         Button6.BackColor = Color.Gray
  116.         Button7.BackColor = Color.Gray
  117.         Button8.BackColor = Color.Gray
  118.         Button9.BackColor = Color.Gray
  119.         Button10.BackColor = Color.Gray
  120.         Button11.BackColor = Color.Gray
  121.         Button12.BackColor = Color.Gray
  122.         Button13.BackColor = Color.Gray
  123.         Button14.BackColor = Color.Gray
  124.         Button15.BackColor = Color.Gray
  125.         Button16.BackColor = Color.Gray
  126.  
  127.  
  128.  
  129.     End Sub
  130. End Class
Where have I gone wrong????
Apr 20 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 5K+
P: 8,434
I think you need to be more specific about what problems you have in this code. As you've probably gathered by now, not too many people are going to spend their time scouring such a long chunk of code trying to work out what's wrong without a good idea of what they're looking for.

Have you made any more progress yet? Can you give us a better idea of what is wrong? What should be happening, but isn't? Or shouldn't be happening, but is?

For that matter, what version of VB are you using?
Apr 22 '07 #2

P: 2
To start I am using VB 2005. This game will use colors (red, blue) instead of x and o. When I click a button it should turn blue and then the computer should randomly select a place on the board, which should turn red. I am able to click on a button to get the blue color but I not sure of how to elicit an automatic response from the computer.
Apr 23 '07 #3

Expert 5K+
P: 8,434
To start I am using VB 2005. This game will use colors (red, blue) instead of x and o. When I click a button it should turn blue and then the computer should randomly select a place on the board, which should turn red. I am able to click on a button to get the blue color but I not sure of how to elicit an automatic response from the computer.
At least for a "first draft", can't you just call it at the end of the "handle the user click" routine? This will just mean that the computer responds very quickly.
Apr 24 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.