Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General 2 / October 2003



Tip: Looking for answers? Try searching our database.

Problem with Arrays (TicTacToe)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Randi - 31 Oct 2003 17:41 GMT
Hi,
I have this assignment to make a Tic Tac Toe game, I believe I got the whoe
thing done.  I set up 9 lbl through the use of an array, I can check to see
if there is a winner.  But I don't know what to do if there is a tie.  Below
is my code, if you can follow it.  I would really appreciate any help.

Thanks,
Kelsey

Option Explicit
Dim player As Integer
Dim status(1 To 3, 1 To 3) As Integer

Private Sub Form_Load()
Dim X As Integer, Y As Integer, i As Integer
Dim W As Single, H As Single
picBoard.Height = picBoard.Width
W = picBoard.ScaleWidth / 3
H = W
lblMark(1).Move 0, 0, W, H
For i = 2 To 9
   Load lblMark(i)
   Call ItoXY(i, X, Y)
   lblMark(i).Move (Y - 1) * W, (X - 1) * H
   lblMark(i).Visible = True
   Next i
player = 1
End Sub

Private Sub ItoXY(i As Integer, X As Integer, Y As Integer)
X = Int((i - 1) / 3) + 1
Y = (i - 1) Mod 3 + 1
End Sub

Private Function XYtoI(X As Integer, Y As Integer) As Integer
XYtoI = (X - 1) * 3 + Y
End Function

Private Sub FindWinner()

Dim ThePlayer As Integer
If status(1, 1) Then
   ThePlayer = status(1, 1)
   If status(2, 2) = ThePlayer And status(3, 3) = ThePlayer Then
       MsgBox lblMark(XYtoI(1, 1)).Caption _
           & " is the winner!", , "Winner"
       MsgBox "test", vbYesNo, "Test"
       Exit Sub
   End If
End If

Dim i As Integer
For i = 1 To 3
   If status(i, 1) Then
       ThePlayer = status(i, 1)
       If status(i, 2) = ThePlayer And status(i, 3) = ThePlayer Then
           MsgBox lblMark(XYtoI(i, 1)).Caption _
               & "is the winner!", , "Winner"
       Exit Sub
       End If
   End If
Next i

For i = 1 To 3
   If status(1, i) Then
       ThePlayer = status(1, i)
       If status(2, i) = ThePlayer And status(3, i) = ThePlayer Then
           MsgBox lblMark(XYtoI(1, i)).Caption _
               & "is the winner!", , "Winner"
       Exit Sub
       End If
   End If
Next i

If status(1, 3) Then
   ThePlayer = status(1, 3)
   If status(2, 2) = ThePlayer And status(3, 1) = ThePlayer Then
       MsgBox lblMark(XYtoI(3, 1)).Caption _
           & " is the winner!", , "Winner"
       If MsgBox("Do you want to end the current game?", _
           vbYesNo, "Start a New Game?") = 6 Then
       Call InitPlayGround
       Else: End
       End If
       Exit Sub
   End If
End If
End Sub

Private Sub lblMark_Click(Index As Integer)
Dim X As Integer, Y As Integer, imove As Integer
Call ItoXY(Index, X, Y)
If status(X, Y) Then
   Beep
   Exit Sub
End If
status(X, Y) = player
Select Case player
Case 1
   lblMark(Index).Caption = "X"
Case -1
   lblMark(Index).Caption = "O"
End Select
player = -player
Call FindWinner
End Sub

Private Sub InitPlayGround()
Dim i As Integer
Dim X As Integer, Y As Integer
' Erase any playing grid symbols
For i = 1 To 9
  lblMark(i).Caption = ""
  Call ItoXY(i, X, Y)
  status(X, Y) = 0
Next i
player = 1
End Sub
Karl S - 31 Oct 2003 21:52 GMT
Kelsey,

Declare an integer variable such as intMovesTaken in the Declarations
section.
Since the only way to get into the "FindWinner" Subroutine is a valid move,
then just increment the intMovesTaken variable at the beginning.

intMovesTaken = intMovesTaken + 1

and;
since the only way to hit the "End Sub" in that routine is if there is no
winner, simply check the intMovesTaken for a value of 9 (all blocks filled)
as in the following code.

If intMovesTaken = 9 Then ' we got here with all nine blocks filled in and
no winner
  MsgBox "It was a tie!"
  ' Reset counts and clear board...
End If

> Hi,
>  I have this assignment to make a Tic Tac Toe game, I believe I got the whoe
[quoted text clipped - 4 lines]
> Thanks,
> Kelsey

[SNIP]
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.