473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problem with Arrays (TicTacToe)

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


Jul 17 '05 #1
1 2300
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
"Randi" <RS******@stny.rr.com> wrote in message
news:eh********************@twister.nyroc.rr.com.. .
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


[SNIP]
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
4
by: sonjaa | last post by:
Hi last week I posted a problem with running out of memory when changing values in NumPy arrays. Since then I have tried many different approaches and work-arounds but to no avail. I was...
0
by: chandniashar | last post by:
This is my perl program for tictactoe. Can anyone help me run the program as a cgi script? #!/usr/bin/perl # Description: This program implements the game of Tic Tac Toe # The grid...
9
by: wizardRahl | last post by:
Hello, I'm attempting to write a TicTacToe program for class and need some help with arrays. We have to write a program that will allow two users to play tic-tac-toe. The program needs to have...
7
by: Warren Hoskins | last post by:
Old title: Homework Due 2-20-07 can"t understand why this will not compile. I've been working on tis all week end. Need Help desperately
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
1
by: racshah | last post by:
I have a tictactoe script with 2 users playing. I need a perl script in which one user plays with the computer. Can anyone help me with it???
0
by: Sean McIlroy | last post by:
""" AUTHOR: Sean McIlroy LANGUAGE: Python 2.5 OVERVIEW: instances of "tictactoeplayer" play optimal tictactoe SPECIALIZED TYPES: player={ex,oh}; empty={blank}; cell=player+empty; board= TYPE...
1
by: jpenguin | last post by:
This should be simple, but I'm stuck <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db('joshdye', $con);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.