Tic Tac Toe game

getting winning options

Page 1 of 1

8 Replies - 1834 Views - Last Post: 05 September 2009 - 10:54 AM Rate Topic: -----

#1 Garnaal  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 04-September 09

Tic Tac Toe game

Posted 04 September 2009 - 09:02 AM

Hello, I'm trying to make a small Tic Tac Toe game with visual basic 2008,
my previous version had 9 buttons, created in the interface, and the only code i had was super simple code for the buttons if you clicked it.

now I'm trying to make a Tic Tac Toe game that creates when you start the game(all by code):
the 9 buttons as play field
1 button for a new game
and a scoring label.

my question is, i don't really know how to get the winning options,
i so far created the code that if you start the game, it will create 9 buttons(the play field)
the buttons are from a special class which knows when you press any button, which button it is( i tested it out with labels, it will say 1,1 or 2,3 etc.)
i created an integer named turn, which declares the turn.
if it's 0 then when you click any button, the text will change to X,
and if it's 1 the text will change to O
the integer cant be greater then 1, it will automatically return to 0.

but for the scoring, i don't know, what kind of code to create for this problem:
if you have 3 X's in a row, then the score for X should gain 1, sounds logical to me,
but i don't know how to create the code for it, so it will automatically see there are 3 X's or O's in a row

maybe someone knows the problem and can help me?


Main code:
Public Class MainForm
	Public turn As Integer = 0
	Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		'creating buttons
		Call createbuttons()

		'other code:
		'like a New Game button
		'Score counting
		'Wich player begins
		'Stuff like that
	End Sub

	Private Sub createbuttons()

		Dim sizo As New Size(25, 25)
		Dim loca As New Point(25, 25)
		For i As Integer = 1 To 3
			For o As Integer = 1 To 3
				Dim butto As New Buttonno
				butto.Location = loca
				butto.Size = sizo
				butto.arow = i
				butto.acollum = o
				Me.Controls.Add(butto)
				loca.X += 28
			Next o
			loca.X -= 84
			loca.Y += 28
		Next i

	End Sub
End Class



Extra button class:
Public Class Buttonno
	Inherits Button

	Public arow As Integer
	Public acollum As Integer

	Private Sub Buttonno_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
		If MainForm.turn = 0 Then
			Me.Text = "X"
			MainForm.turn += 1
			Me.Enabled = False
			If MainForm.turn > 1 Then
				MainForm.turn = 0
			End If
		ElseIf MainForm.turn = 1 Then
			Me.Text = "O"
			MainForm.turn += 1
			Me.Enabled = False
			If MainForm.turn > 1 Then
				MainForm.turn = 0
			End If
		End If
	End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe game

#2 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Tic Tac Toe game

Posted 04 September 2009 - 09:59 AM

There are 8 possible winning combinations. The buttons need 3 states - undefined, X and O. Say they are represented by X = -1, undefined = 0 and O = 1 then just try summing each column(3), row(3) and diagonal(2) and if the sum of any is 3 or -3 then O or X has won respectively. If not, then the game is not over. You also need a turn counter to know that 9 turns have completed and the game is tied if you don't have a winner.
Was This Post Helpful? 0
  • +
  • -

#3 Garnaal  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 04-September 09

Re: Tic Tac Toe game

Posted 04 September 2009 - 10:35 AM

View Postmark.bottomley, on 4 Sep, 2009 - 08:59 AM, said:

There are 8 possible winning combinations. The buttons need 3 states - undefined, X and O. Say they are represented by X = -1, undefined = 0 and O = 1 then just try summing each column(3), row(3) and diagonal(2) and if the sum of any is 3 or -3 then O or X has won respectively. If not, then the game is not over. You also need a turn counter to know that 9 turns have completed and the game is tied if you don't have a winner.

I now integrated a so called totalturn integer that starts at 1,
and when a button is clicked adds 1 to it
if it gets to 9 then it pops up an msgbox that says: tied new game yes no bla bla.

i also integrated a buttonstatus integer that starts at -1
when a button is pressed, the buttonstatus will change to a 0 or to a 1
that depends on if it's changing to an X or to an O

i tried adding some code for the columns and the rows like this:
	Public Sub winX()

		If acollum = 3 Then
			MsgBox("X Wins!")
		End If
		If arow = 3 Then
			MsgBox("X Wins!")
		End If
		If acollum And arow = 3 Then
			MsgBox("X Wins!")
		End If
	End Sub
	Public Sub WinO()

		If acollum = 3 Then
			MsgBox("O Wins!")
		End If
		If arow = 3 Then
			MsgBox("O Wins!")
		End If
		If acollum And arow = 3 Then
			MsgBox("O Wins!")
		End If
	End Sub



and those functions are automatically called after a button is clicked
now the only problem is, it just always gives that message, if you click the buttons (1,3 - 2,3 - 3,3)
there don't need to be any X's or O's in front of them..
anyone knows how to fix it?
Was This Post Helpful? 0
  • +
  • -

#4 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Tic Tac Toe game

Posted 04 September 2009 - 10:44 AM

You only show the results checking, but how do you calculate arow and acollum (spelling => acolumn). You probably need an adiagonal variable as well. You are only checking 2 variables so I suspect the checking code is very bogus.
Was This Post Helpful? 0
  • +
  • -

#5 Garnaal  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 04-September 09

Re: Tic Tac Toe game

Posted 04 September 2009 - 10:52 AM

For i As Integer = 1 To 3
			For o As Integer = 1 To 3
				Dim butto As New Buttonno
				butto.Location = loca
				butto.Size = sizo
				butto.arow = i
				butto.acollum = o
				butto.buttonstatus = -1
				Me.Controls.Add(butto)
				loca.X += 28



as you can see in that piece of code (the piece above), when the buttons are created, the arow and acollum are automatically assigned a number(1 to 3, 1 to 3)

		If MainForm.turn = 0 Then
			Me.Text = "X"
			buttonstatus = 0
			MainForm.turn += 1
			MainForm.totalturn += 1
			Call winX()



and in this piece of code(the piece above(when you click a button))
it assigns the buttonstatus 0 to the button (because this is the IF for X)
in the IF piece of O it becomes 1

how can it check the adiagonal then?
i'm kinda beginner so i don't really get it ;D
Was This Post Helpful? 0
  • +
  • -

#6 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Tic Tac Toe game

Posted 04 September 2009 - 11:30 AM

There are 8 things to check for X and 8 for O. Because you just added random, unnamed controls to the form, you need to check all controls to do anything - that is you cannot directly walk rows or columns which could be done if they had been inserted in a 2 dimensional array. Calculating the winning conditions for random controls is difficult, so change to assigning them into an array then...

Something like
Public Board(3,3) As Button
Dim RowResults(3) As Integer
Dim ColResults(3) As Integer
Dim DiagResults(2) As Integer
For I = 0 to 2 
  For J = 0 to 2
	RowResults(I) += Board(I, J)
	ColResults(I) += Board(J, I)
  Next
Next
For I = 0 to 2
  DiagResults(0) += Board(I, I)
  DiagResults(1) += Board(I, 2-I)
Next


Now you need to check the contents of the arrays for appropriate values.
You should have undefined as 0 and use X and O as 1 and -1 then you can check for any 3's for an X win and any -3's for an O win.

Optional way is to take each button press and update the appropriate row and column and possibly diagonal sums on each press. and then check the sum for Math.Abs() equal 3.
Was This Post Helpful? 0
  • +
  • -

#7 Garnaal  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 04-September 09

Re: Tic Tac Toe game

Posted 05 September 2009 - 03:16 AM

Well I'm totally lost now..
I really don't get it anymore :')
I tried applying your code, but i get these 2 errors

'Public' is not valid on a local variable declaration.
Operator '+' is not defined for types 'Integer' and 'System.Windows.Forms.Button'.

don't really know what to do now..
Was This Post Helpful? 0
  • +
  • -

#8 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Re: Tic Tac Toe game

Posted 05 September 2009 - 04:05 AM

You could find some answers here:
http://www.dreaminco.../snippet874.htm

and here:

http://visualbasic.a...l/aa093002a.htm
Was This Post Helpful? 0
  • +
  • -

#9 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Tic Tac Toe game

Posted 05 September 2009 - 10:54 AM

Dim Board(3,3) As Buttonno
and
RowResults(I) += Board(I, J).buttonstatus
should be the type of changes to use it with your Buttonno Classs. I was not giving you a working solution, but ideas to adapt to your specific problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1