tic tac toe game

  • Thread starter Thread starter Valerie
  • Start date Start date
V

Valerie

Ok. Help. the quarter is ending soon and I have no interest in creating
games. I think that's why i have a mental block on this particular lab
assignment.

I'm using the same book as Steven Smith is using when One Handed Man helped
him.

And, I don't even know where to continue.

The application is a tic tac toe game.
I figure that i'll need a user-defined collection.
I coded it for all of the labels used in the game.

every time the game first comes up, "O" is the first mark on the board.

I think i need something that switches between "X" and "O" every time a
label is clicked.

I got the new game button coded so that the game squares are cleared when
the new game button is clicked.

Now what do I do?
 
First off, tic tac toe always starts with the "X", not the "O". To switch
between X and O, you could try using a static variable.
Private Sub lblBox_Click(...)
Static IsX as boolean

IsX = Not IsX
If LblBox(i).Text="" then
If IsX then
lblBox(i).Text="X"
Else
lblBox(i).Text="O"
End if
End If

' Code to analyze whether you have three in a row.

End Sub

It has been a few years since I used a control collection, so you will need
to adjust it to accomodate which label in the collection is clicked. If the
labels are not in the collection then the Static declaration needs to be
moved outside the sub to the form declarations. You will need to write code
to analyze if you have 3 in a row accross each row, down each column and
along each diagonal for Xs and Os. You can use If statements for this. HTH

Kim
 
Actually, I would be more likely to use a two dimensional array for this
kind of thing, I think it helps your thinking during design/coding because
you can represent it as you would see it.



--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
 
Hi One handed man,
Sorry I did write this in this threath after yours.
I did read it and thought has this really to be (3)(3).
Than I was making some arguments why that was not necessary.
Than I did think why argue about that, you can think about the educative
purpose of it.
I thought why not give a link to Google.
I found one and it was with a arraylist with one dimension.
And then I did post it on the wrong place.
Sorry
"Cor"
 
I guess it comes down to personal choice. It is just as valid to use a
single dimension as two dimensions. In my head though, I find it easier to
use two dimensions when representing a two dimensional object, after all
tic tac toe is two dimensional ( actually two on screen ).

I wrote a star trek game some time ago mapping out the iniverse and
galaxies, i did this in that format so I suppose I got used to mapping it
that way.

Anyhow, i hope all this helps the poster of the original question.

--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
 
Hi One Handed Man,

Two dimensions for Star Trek? How did you code all those Temporal
Anomalies and Sub-space Phase Shifts? Lol.

Regards,
Fergus
 
-----Original Message-----
Hi One Handed Man,

Two dimensions for Star Trek? How did you code all those Temporal
Anomalies and Sub-space Phase Shifts? Lol.


LOL nuff said... :)
 
Hi One Handed Man,

|| > Two dimensions for Star Trek?
|| > How did you code all those Temporal
|| > Anomalies and Sub-space Phase Shifts? Lol.
||
|| Easy, I just attached my cerebral cortex to the USB
|| port. Once I had bound my multi-phasing torpedo
|| to Seven-of-Nine's worm hole, I guess time stood
|| still, and thats the anomaly.

ROFL

What an energy configuration!!
Time stood still, but the Universe moved.

Yours, jealously,
Fergus
 
Hi Steven, Valerie

Except that instead of a RadioButton you'll be using a whose-turn-is-it
variable?

Regards,
Fergus
 
Eww, the thought of all those nanoprobes crawling around yer jefferies tubes
makes me shudder.

LOL

--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
 
Hi One Handed Man,

Happens every time I play the One-Handed game. :-D

Regards,
Fergus

ps. That <is> why you're the One Handed Man, isn't it?
 
I'm impressed.

Kim

One Handed Man said:
Easy, I just attached my cebral cortex to the USB port. Once I had bound my
multi phasing torpedo to seven of nine's worm hole , I guess time stood
still, and thats the anomaly.



--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
 
It's moozic, I tell ya, <mooozic>. [Pulls hair out] OW!! [Moves hand up to
head and pulls there instead!]

Fergus
 
Sadly no. I fell off my Fireblade and broke my collar bone and left wrist
two months ago, Ive just started to type again with me left hand but it
hurts still.



--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
 
-----Original Message-----
Hi Steven, Valerie

Except that instead of a RadioButton you'll be using a whose-turn-is-it
variable?

Regards,
Fergus


Here's my first attempt at Tic-Tac-Toe it works as the
example describes but It first displays an "X" then
multiple "O's" but I know why it is doing this but I cant
figure out what kind of selection & repetition is
required to switch it back to an "X" doh :) also basix
newbie error checking is included to stop the user
writing over the others marker

//////

Dim mLabelCollection As New Collection()


Private Sub ExitButton_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ExitButton.Click
Me.Close()
End Sub



Private Sub NewGameButton_Click(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
NewGameButton.Click
'declare object & integer variables
Dim objLabel As Label
Dim intx As Integer

'looping to find Label controls on form &
clearing the contents
Do While intx < Controls.Count - 1
If TypeOf Controls.Item(intx) Is Label Then
objLabel = Controls.Item(intx)
objLabel.Text = ""
End If
intx = intx + 1
Loop

End Sub

Private Sub TicTacToeForm_Load(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles MyBase.Load

'add controls to the form level collection
mLabelCollection.Add(Me.TTT1Label)
mLabelCollection.Add(Me.TTT2Label)
mLabelCollection.Add(Me.TTT3Label)
mLabelCollection.Add(Me.TTT4Label)
mLabelCollection.Add(Me.TTT5Label)
mLabelCollection.Add(Me.TTT6Label)
mLabelCollection.Add(Me.TTT7Label)
mLabelCollection.Add(Me.TTT8Label)
mLabelCollection.Add(Me.TTT9Label)


End Sub

'determine player turn & process the label clicks to
display the correct marker
Private Sub PlacePlayersMarker(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles TTT1Label.Click, TTT2Label.Click,
TTT3Label.Click, _
TTT4Label.Click, TTT5Label.Click,
TTT6Label.Click, _
TTT7Label.Click, TTT8Label.Click,
TTT9Label.Click


'determine which label has been clicked and
display players marker

Static strPlayer As String = "X"

'declare object variables
Dim ObjLabel As Label
'assign sender parameter to object variable
ObjLabel = sender
'assign values to variables
ObjLabel.Text = strPlayer
strPlayer = "O"

'error checking validation to disable overwriting
the other players mark
If ObjLabel.Text <> "" Then
ObjLabel.Enabled = False
End If
End Sub
End Class
/////////
 
This is it Steven!!!!! I'm turning it in. Thank you, thank you, thank you to
all of you who answered my plea. Now on to chapter 7. I wonder how that will
go. Steven, I'll be in touch; please do the same.
Peace and blessings,
Valerie

Here's my code:


'form level variables

Private mLabelCollection As New Collection()

Private mstrXorO As String

'mYesIWon is initialized as False

Private mYesIWon As Boolean



Private Sub ExitButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ExitButton.Click

Me.Close()

End Sub



Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'display message of my name, course, registration number, and lab
number in the AboutLabel

Const conPgmmed As String = "Valerie Orr"

Const conCourse As String = "CPT155"

Const conRegNo As String = "35948"

Const conLabNo As String = "Lab #4"

Me.AboutLabel.Text = conPgmmed & vbNewLine & conCourse & vbNewLine &
conRegNo & vbNewLine & conLabNo



'add labels to label collection

mLabelCollection.Add(Me.TTT1Label)

mLabelCollection.Add(Me.TTT2Label)

mLabelCollection.Add(Me.TTT3Label)

mLabelCollection.Add(Me.TTT4Label)

mLabelCollection.Add(Me.TTT5Label)

mLabelCollection.Add(Me.TTT6Label)

mLabelCollection.Add(Me.TTT7Label)

mLabelCollection.Add(Me.TTT8Label)

mLabelCollection.Add(Me.TTT9Label)

End Sub



'enable each label

'make each label's background color control color

Private Sub NewGameButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) _

Handles NewGameButton.Click

Dim objLabelAddr As Label, objThatsAControl As Control

For Each objThatsAControl In mLabelCollection

If TypeOf objThatsAControl Is Label Then

objLabelAddr = objThatsAControl

objLabelAddr.Text = ""

objLabelAddr.Enabled = True

objLabelAddr.BackColor = Color.FromName("control")

End If

Next objThatsAControl



End Sub



Private Sub Label_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _

Handles TTT1Label.Click, TTT2Label.Click, TTT3Label.Click,
TTT4Label.Click, TTT5Label.Click, _

TTT6Label.Click, TTT7Label.Click, TTT8Label.Click,
TTT9Label.Click



'declare an object variable to store the address of the clicked
label in the collection

Dim objLabelAddr As Label, objThatsAControl As Control



'for each label in label collection look to see if it was the label
that was clicked

'for the label clicked, assign the address of the senderto
objlabeladdr, then

'Call DisplayXorO()

'the text for the clicked label receives the "X" or "O" assigned to
mstrXorO

'Call DidIWin()

'If mYesIWon = True Then Exit Sub End If

'For Each objthatsacontrol In mLabelCollection

'If TypeOf objThatsAControl Is Label Then



objLabelAddr = sender

Call DisplayXorO()

objLabelAddr.Text = mstrXorO

objLabelAddr.Enabled = False

Call DidIWin()

End Sub



'display the 'x' or 'o' of the player

Private Sub DisplayXorO()

Select Case mstrXorO

Case "O"

mstrXorO = "X"

Case "X"

mstrXorO = "O"

Case Else

mstrXorO = "O"

End Select

End Sub



'check to see if anyone won,

' if so, the background color of each label is changed to
red and all label disabled

' mYesIWon is changed to True

Private Sub DidIWin()

Dim objLabelAddr As Label

Select Case True

Case (Me.TTT1Label.Text = "X" AndAlso Me.TTT2Label.Text = "X"
AndAlso Me.TTT3Label.Text = "X") Or _

(Me.TTT1Label.Text = "O" AndAlso Me.TTT2Label.Text = "O"
AndAlso Me.TTT3Label.Text = "O")

Me.TTT1Label.BackColor = Color.FromName("Red")

Me.TTT2Label.BackColor = Color.FromName("Red")

Me.TTT3Label.BackColor = Color.FromName("Red")

mYesIWon = True

Case (Me.TTT4Label.Text = "X" AndAlso Me.TTT5Label.Text = "X"
AndAlso Me.TTT6Label.Text = "X") Or _

(Me.TTT4Label.Text = "O" AndAlso Me.TTT5Label.Text = "O"
AndAlso Me.TTT6Label.Text = "O")

Me.TTT4Label.BackColor = Color.FromName("Red")

Me.TTT5Label.BackColor = Color.FromName("Red")

Me.TTT6Label.BackColor = Color.FromName("Red")

mYesIWon = True

Case (Me.TTT7Label.Text = "X" AndAlso Me.TTT8Label.Text = "X"
AndAlso Me.TTT9Label.Text = "X") Or _

(Me.TTT7Label.Text = "O" AndAlso Me.TTT8Label.Text = "O"
AndAlso Me.TTT9Label.Text = "O")

Me.TTT7Label.BackColor = Color.FromName("Red")

Me.TTT8Label.BackColor = Color.FromName("Red")

Me.TTT9Label.BackColor = Color.FromName("Red")

mYesIWon = True

Case (Me.TTT1Label.Text = "X" AndAlso Me.TTT4Label.Text = "X"
AndAlso Me.TTT7Label.Text = "X") Or _

(Me.TTT1Label.Text = "O" AndAlso Me.TTT4Label.Text = "O"
AndAlso Me.TTT7Label.Text = "O")

Me.TTT1Label.BackColor = Color.FromName("Red")

Me.TTT4Label.BackColor = Color.FromName("Red")

Me.TTT7Label.BackColor = Color.FromName("Red")

mYesIWon = True

Case (Me.TTT2Label.Text = "X" AndAlso Me.TTT5Label.Text = "X"
AndAlso Me.TTT8Label.Text = "X") Or _

(Me.TTT2Label.Text = "O" AndAlso Me.TTT5Label.Text = "O"
AndAlso Me.TTT8Label.Text = "O")

Me.TTT2Label.BackColor = Color.FromName("Red")

Me.TTT5Label.BackColor = Color.FromName("Red")

Me.TTT8Label.BackColor = Color.FromName("Red")

mYesIWon = True

Case (Me.TTT3Label.Text = "X" AndAlso Me.TTT6Label.Text = "X"
AndAlso Me.TTT9Label.Text = "X") Or _

(Me.TTT3Label.Text = "O" AndAlso Me.TTT6Label.Text = "O"
AndAlso Me.TTT9Label.Text = "O")

Me.TTT3Label.BackColor = Color.FromName("Red")

Me.TTT6Label.BackColor = Color.FromName("Red")

Me.TTT9Label.BackColor = Color.FromName("Red")

mYesIWon = True

Case (Me.TTT1Label.Text = "X" AndAlso Me.TTT5Label.Text = "X"
AndAlso Me.TTT9Label.Text = "X") Or _

(Me.TTT1Label.Text = "O" AndAlso Me.TTT5Label.Text = "O"
AndAlso Me.TTT9Label.Text = "O")

Me.TTT1Label.BackColor = Color.FromName("Red")

Me.TTT5Label.BackColor = Color.FromName("Red")

Me.TTT9Label.BackColor = Color.FromName("Red")

mYesIWon = True

Case (Me.TTT3Label.Text = "X" AndAlso Me.TTT5Label.Text = "X"
AndAlso Me.TTT7Label.Text = "X") Or _

(Me.TTT3Label.Text = "O" AndAlso Me.TTT5Label.Text = "O"
AndAlso Me.TTT7Label.Text = "O")

Me.TTT3Label.BackColor = Color.FromName("Red")

Me.TTT5Label.BackColor = Color.FromName("Red")

Me.TTT7Label.BackColor = Color.FromName("Red")

mYesIWon = True

End Select



If mYesIWon = True Then

For Each objLabelAddr In mLabelCollection

objLabelAddr.Enabled = False

Next objLabelAddr

mYesIWon = False

End If

End Sub
 
Hi Valerie,

Code Review time:

=========================================
For Each objThatsAControl In mLabelCollection
If TypeOf objThatsAControl Is Label Then
objLabelAddr = objThatsAControl
objLabelAddr.Text = ""
objLabelAddr.Enabled = True
objLabelAddr.BackColor = Color.FromName("control")
End If
Next objThatsAControl

When will the If ever be false?

======================================
What's the difference between :

Me.TTT7Label.BackColor = Color.FromName("Red")
Me.TTT8Label.BackColor = Color.FromName("Red")
Me.TTT9Label.BackColor = Color.FromName("Red")

and
Private Color colorRed = Color.FromName("Red")

mLabelCollection(7).BackColor = colorRed
mLabelCollection(8).BackColor = colorRed
mLabelCollection(9).BackColor = colorRed
=========================================
What if you had a function:
ColourTheRow (7, 8, 9, colorRed)

How would it work ? Hint: look at the second block.
======================================
What's the difference between :

(Me.TTT1Label.Text = "X" _
AndAlso Me.TTT2Label.Text = "X" _
AndAlso Me.TTT3Label.Text = "X")
Or (Me.TTT1Label.Text = "O" _
AndAlso Me.TTT2Label.Text = "O" _
AndAlso Me.TTT3Label.Text = "O")

and
(mLabelCollection(1).Text = "X" _
AndAlso mLabelCollection(2).Text = "X" _
AndAlso mLabelCollection(3).Text = "X")
Or (mLabelCollection(1).Text = "O" _
AndAlso mLabelCollection(2).Text = "O" _
AndAlso mLabelCollection(3).Text = "O")

??
=========================================
What if you had a function:
tTheresAWinner (1, 2, 3)
which would return True or False exactly like the expression above.

How would it work ? Hint: look at the second lot of tests.
=========================================
What does your program say when the game ends in a draw?
=========================================
What does your game say when Player 1 has won?
What does your game say when Player 2 has won?
Could you adapt (and rename) tTheresAWinner (1, 2, 3) to tell you both
whether someone has won and who they are?

=========================================

These may go beyond the scope of your assignment. If so I apologise, but I
like to challenge you, so I don't.

All the best,
Fergus
 
Back
Top