SIGN UP MEMBER LOGIN:    
ARTICLE

Kovan's Tic-Tac-Toe

Posted by Kovan Abdulla Articles | Games Programming C# January 26, 2004
This is a tic-tac-toe game written in VB.NET and GDI+.
Reader Level:
Download Files:
 

Screen shot of what it looks like:

(you need 9 label controls set font size to something big, and make them all the same size).



Form variables.

Private playerLastPlayed As Integer = 0 'initially no one went

Which will hold the value of the last player that went.

When each label is clicked, this function will get called.

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click, Label2.Click, Label3.Click, Label4.Click, Label5.Click, Label6.Click, Label7.Click, Label8.Click, Label9.Click
labelClicked(sender)
End Sub

Private
Sub labelClicked(ByVal lbl As Label)
If RadioButton1.Checked Then
If playerLastPlayed = 1 Then
MessageBox.Show("its not your turn")
Return
End If
lbl.Text = "X"
playerLastPlayed = 1
'player one played
Else
If playerLastPlayed = 2 Then
MessageBox.Show("its not your turn")
Return
End If
lbl.Text = "O"
playerLastPlayed = 2
'player two played
End If

'disable so its no longer clickable (make that square used)
lbl.Enabled = False
checkForWinner(1)
checkForWinner(2)
End Sub

Now we must check to see if there is a winner.

Private Sub checkForWinner(ByVal playerNumber As Integer)
Dim chr As String = ""
Select Case playerNumber
Case 1
chr = "X"
Case 2
chr = "O"
End Select

'check for column 1,2,3
'check for column 4,5,6
'check for column 7,8,9

'check for column 1,4,7
'check for column 2,5,8
'check for column 3,6,9

'check for column 1,5,9
'check for column 3,5,7

If (Label1.Text = chr And Label2.Text = chr And Label3.Text = chr) Then
Or
(Label4.Text = chr And Label5.Text = chr And Label6.Text = chr) _
Or (Label7.Text = chr And Label8.Text = chr And Label9.Text = chr) _
Or (Label1.Text = chr And Label4.Text = chr And Label7.Text = chr) _
Or (Label2.Text = chr And Label5.Text = chr And Label8.Text = chr) _
Or (Label3.Text = chr And Label6.Text = chr And Label9.Text = chr) _
Or (Label1.Text = chr And Label5.Text = chr And Label9.Text = chr) _
Or (Label3.Text = chr And Label5.Text = chr And Label7.Text = chr) Then

'we found a winner
If playerNumber = 1 Then
MessageBox.Show("player one WON")
reset()
ElseIf playerNumber = 2 Then
MessageBox.Show("player two WON")
reset()
End If
End If
End
Sub

Now lets see the reset function.

Private Sub reset()
'remove the x, o
For Each lbl As Control In Me.Controls
If lbl.GetType Is GetType(Label) Then
lbl.Text = ""
End If
Next
'reset last player played.
playerLastPlayed = 0
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
reset()
End Sub

Login to add your contents and source code to this article
share this article :
post comment
 

In What version of Visual C++ do you make it?

Posted by Yoh asakura Nov 27, 2011

good

Posted by austin bacchus Jan 22, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor