3 Replies - 2593 Views - Last Post: 24 June 2008 - 09:08 PM Rate Topic: -----

#1 sparkygate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-January 08

TicTacToe AI Help

Post icon  Posted 24 June 2008 - 05:03 AM

Ok, i know there are a lot of forums out there on AI, but none of them seem to be able to suit my programing for an AI.

Basically i know where the code goes BUT i don't know how to start it or know how to get it working (This is where you pros come in).

As you can see from my program (included in the attachment) that i've already made the two player of Tic Tac Toe but have had no luck in programing the AI for one player.

(Two player code came from cespage.com i've been tweaking there code to suit my needs)

Here's the code for the One Player Game, with my tiny attempts to code an AI
Public Class frmOneplayer_game

	Dim counterX As Integer = 0

	Dim counterO As Integer = 0

	Dim counterD As Integer = 0


	Public Sub SetRecieve(ByVal Human As String)
		lblHuman.Text = Human
	End Sub


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


	End Sub






	' BEGINNING OF TIC-TAC-TOE

	'Declaring the picture box
	Dim DrawPicture As New PictureBox
	Dim DrawBitmap As Bitmap
	Dim DrawGraphics As Graphics

	'Declaring the colour of the nought and cross
	Dim CirclePen As New Pen(Color.Blue, 4)
	Dim CrossPen As New Pen(Color.Red, 4)

	' whether its a nought or a cross
	Dim IsCross As Boolean

	' Declaring what type of vauable is the nought and cross
	Dim Nought As String = "O"
	Dim Cross As String = "X"

	Dim Won As Boolean
	Dim Board(3, 3) As String




	'Drawing of the the X line- how its drawn- size, diameter, length, width
	Public Sub DrawX() 'sub-routine
		DrawGraphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
		'two possible ways of drawing the "x" lines
		DrawGraphics.DrawLine(CrossPen, 10, 10, 50, 50)
		DrawGraphics.DrawLine(CrossPen, 50, 10, 10, 50)
	End Sub

	'Drawing of the O line
	Public Sub DrawO() 'sub-routine
		DrawGraphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
		DrawGraphics.DrawEllipse(CirclePen, 8, 8, 40, 40)
	End Sub



	Private Sub SetPiece(ByVal Row As Integer, _
	ByVal Column As Integer, ByVal pic As PictureBox)



		If Board(Row, Column) = Nought Or Board(Row, Column) = Cross Then
			' Do nothing --> if Nought or Cross already in square
		Else
			DrawPicture = CType(pic, PictureBox)  ' delcaring the picturebox as an object to put "X"s and "O"s in
			DrawBitmap = New Bitmap(DrawPicture.Width, DrawPicture.Height) 'drawing the "X" and "O" in picturebox
			DrawGraphics = Graphics.FromImage(DrawBitmap) 'Where to draw it
			DrawPicture.Image = DrawBitmap


			If IsCross Then
				lblTurn.Text = lblHuman.Text ' & " O"
				DrawX()
				Board(Row, Column) = Cross

			Else
				lblTurn.Text = lblComputer.Text  ' & " X"


				AI() ' the start of the artifical intelligences

				Board(Row, Column) = Nought



			End If

		End If
	End Sub

	Private Sub AI() 'THE BRAIN of the AI
		Dim AImove As Integer

		Select Case AImove
			Case Is
				If Board(1, 3) = Cross Then
					DrawO()
					Board(3, 1) = Nought
				End If

		End Select


	End Sub


	Private Sub Check(ByVal Player As String, ByVal x1 As Integer, ByVal y1 As Integer, _
	ByVal x2 As Integer, ByVal y2 As Integer, ByVal x3 As Integer, ByVal y3 As Integer)

		If Board(x1, y1) = Player And Board(x2, y2) = Player And Board(x3, y3) = Player Then

			Won = True

			If Player = Cross Then
				lblTurn.Text = lblHuman.Text & " Won!"
				MsgBox(lblHuman.Text & " Won!", MsgBoxStyle.Information, "Noughts and Crossses")
				counterX = counterX + 1
				' lblPlayerXScore.Text = counterX

			Else
				lblTurn.Text = lblComputer.Text & " Won!"
				MsgBox(lblComputer.Text & " Won!", MsgBoxStyle.Information, "Noughts and Crossses")
				counterO = counterO + 1
				' lblPlayerOScore.Text = counterO
			End If

		End If

	End Sub


	Private Sub CheckWon(ByVal Player As String)
		' program checking possible places where the player won
		Check(Player, 1, 1, 1, 2, 1, 3)
		Check(Player, 2, 1, 2, 2, 2, 3)
		Check(Player, 3, 1, 3, 2, 3, 3)
		Check(Player, 1, 1, 2, 1, 3, 1)
		Check(Player, 1, 2, 2, 2, 3, 2)
		Check(Player, 1, 3, 2, 3, 3, 3)
		Check(Player, 1, 1, 2, 2, 3, 3)
		Check(Player, 3, 1, 2, 2, 1, 3)
	End Sub

	Private Sub CheckDraw() ' another sub-routine telling the program to check for any draws
		If Not Board(1, 1) = "" And Not Board(1, 2) = "" And Not Board(1, 3) = "" Then
			If Not Board(2, 1) = "" And Not Board(2, 2) = "" And Not Board(2, 3) = "" Then
				If Not Board(3, 1) = "" And Not Board(3, 2) = "" And Not Board(3, 3) = "" Then
					If Not Won Then
						lblTurn.Text = "Draw!"
						MsgBox("Draw!", MsgBoxStyle.Information, "Noughts and Crossses")
						counterD = counterD + 1
						' lblDrawScore.Text = counterD
					End If
				End If
			End If
		End If
	End Sub

	'Main programing of the game--> LOGICAL THINKING!!
	Private Sub PictureClick(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles PictureBox1.Click, _
	PictureBox2.Click, PictureBox3.Click, PictureBox4.Click, _
	PictureBox5.Click, PictureBox6.Click, PictureBox7.Click, _
	PictureBox8.Click, PictureBox9.Click
		'assigning values to each picturebox piece
		If Not Won Then
			Select Case sender.name
				Case "PictureBox1"
					SetPiece(1, 1, sender)
				Case "PictureBox2"
					SetPiece(1, 2, sender)
				Case "PictureBox3"
					SetPiece(1, 3, sender)
				Case "PictureBox4"
					SetPiece(2, 1, sender)
				Case "PictureBox5"
					SetPiece(2, 2, sender)
				Case "PictureBox6"
					SetPiece(2, 3, sender)
				Case "PictureBox7"
					SetPiece(3, 1, sender)
				Case "PictureBox8"
					SetPiece(3, 2, sender)
				Case "PictureBox9"
					SetPiece(3, 3, sender)
			End Select
			CheckWon(Cross)
			CheckWon(Nought)
			CheckDraw()
			IsCross = Not IsCross
			' btnFinsh.Enabled = True
		Else
			MsgBox("Game is over, please click either NEW GAME OR NEXT")
			'		  btnFinsh.Enabled = True

		End If
	End Sub





	Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
		Me.Close()
		frmHighscore.ShowDialog()


	End Sub
End Class





Here is my Two Player Code that you guys might want to refer too
Public Class frmTwoplayer_game

	Dim counterX As Integer = 0

	Dim counterO As Integer = 0
  
	Dim counterD As Integer = 0
 


	' subroutine starts --> "Public" indicates that it is from frmTwoplayer_name
	'These names goes under the X and O
	Public Sub SetRecieve3(ByVal PlayerOne As String)
		lblPlayerX.Text = PlayerOne
	End Sub
	Public Sub SetRecieve4(ByVal PlayerTwo As String)
		lblPlayerO.Text = PlayerTwo
	End Sub
	' end of subroutine



	'These names goes under the scoreboard
	Public Sub SetRecieve5(ByVal PlayerOne As String)
		lblPlayerX2.Text = PlayerOne
	End Sub
	Public Sub SetRecieve6(ByVal PlayerTwo As String)
		lblPlayerO2.Text = PlayerTwo
	End Sub
	' end of subroutine



	Private Sub frmTwoplayer_game_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		' can't click on the END GAME button when the form shows up on the screen
		btnFinsh.Enabled = False

		' btnReset.PerformClick()

	End Sub



	'declared the Radio Button in frmTwoplayer_flipthegame as a public subroutine
	Public Sub RadioButton1(ByVal Radiobutton1 As RadioButton)
		' using the binary language so the program can check who goes first, so if the first
		' radiobutton is checked then that person automatically becomes X 
		If Radiobutton1.Enabled = True Then
			IsCross = True
			lblTurn.Text = lblPlayerX2.Text
		Else
			IsCross = False
			lblTurn.Text = lblPlayerO2.Text
		End If
	End Sub

	Public Sub RadioButton2(ByVal Radiobutton2 As RadioButton)
		' using the binary language so the program can check who goes first, so if the first
		' radiobutton is checked then that person automatically becomes X 
		If Radiobutton2.Enabled = True Then
			IsCross = True
			lblTurn.Text = lblPlayerO2.Text
		Else
			IsCross = False
			lblTurn.Text = lblPlayerX2.Text
		End If
	End Sub




	' BEGINNING OF TIC-TAC-TOE

	'Declaring the picture box
	Dim DrawPicture As New PictureBox
	Dim DrawBitmap As Bitmap
	Dim DrawGraphics As Graphics

	'Declaring the colour of the nought and cross
	Dim CirclePen As New Pen(Color.Blue, 4)
	Dim CrossPen As New Pen(Color.Red, 4)

	' whether its a nought or a cross
	Dim IsCross As Boolean

	' Declaring what type of vauable is the nought and cross
	Dim Nought As String = "O"
	Dim Cross As String = "X"

	Dim Won As Boolean
	Dim Board(3, 3) As String




	'Drawing of the the X line- how its drawn- size, diameter, length, width
	Public Sub DrawX() 'sub-routine
		DrawGraphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
		'two possible ways of drawing the "x" lines
		DrawGraphics.DrawLine(CrossPen, 10, 10, 50, 50)
		DrawGraphics.DrawLine(CrossPen, 50, 10, 10, 50)
	End Sub

	'Drawing of the O line
	Public Sub DrawO() 'sub-routine
		DrawGraphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
		DrawGraphics.DrawEllipse(CirclePen, 8, 8, 40, 40)
	End Sub



	Private Sub SetPiece(ByVal Row As Integer, _
	ByVal Column As Integer, ByVal pic As PictureBox)



		If Board(Row, Column) = Nought Or Board(Row, Column) = Cross Then
			' Do nothing --> if Nought or Cross already in square
		Else
			DrawPicture = CType(pic, PictureBox)  ' delcaring the picturebox as an object to put "X"s and "O"s in
			DrawBitmap = New Bitmap(DrawPicture.Width, DrawPicture.Height) 'drawing the "X" and "O" in picturebox
			DrawGraphics = Graphics.FromImage(DrawBitmap) 'Where to draw it
			DrawPicture.Image = DrawBitmap


			If IsCross Then
				lblTurn.Text = lblPlayerX.Text ' & " O"
				DrawX()
				Board(Row, Column) = Cross

			Else
				lblTurn.Text = lblPlayerO.Text  ' & " X"
				DrawO()
				Board(Row, Column) = Nought

			End If

		End If
	End Sub


	Private Sub Check(ByVal Player As String, ByVal x1 As Integer, ByVal y1 As Integer, _
	ByVal x2 As Integer, ByVal y2 As Integer, ByVal x3 As Integer, ByVal y3 As Integer)

		If Board(x1, y1) = Player And Board(x2, y2) = Player And Board(x3, y3) = Player Then

			Won = True

			If Player = Cross Then
				lblTurn.Text = lblPlayerX.Text & " Won!"
				MsgBox(lblPlayerX.Text & " Won!", MsgBoxStyle.Information, "Noughts and Crossses")
				counterX = counterX + 1
				lblPlayerXScore.Text = counterX

			Else
				lblTurn.Text = lblPlayerO.Text & " Won!"
				MsgBox(lblPlayerO.Text & " Won!", MsgBoxStyle.Information, "Noughts and Crossses")
				counterO = counterO + 1
				lblPlayerOScore.Text = counterO
			End If

			' Orignal Code
			' lblTurn.Text = Player & " Won!"
			' MsgBox(Player & " Won!", MsgBoxStyle.Information, "Noughts and Crossses")
		End If

	End Sub


	Private Sub CheckWon(ByVal Player As String)
		' program checking possible places where the player won
		Check(Player, 1, 1, 1, 2, 1, 3)
		Check(Player, 2, 1, 2, 2, 2, 3)
		Check(Player, 3, 1, 3, 2, 3, 3)
		Check(Player, 1, 1, 2, 1, 3, 1)
		Check(Player, 1, 2, 2, 2, 3, 2)
		Check(Player, 1, 3, 2, 3, 3, 3)
		Check(Player, 1, 1, 2, 2, 3, 3)
		Check(Player, 3, 1, 2, 2, 1, 3)
	End Sub

	Private Sub CheckDraw() ' another sub-routine telling the program to check for any draws
		If Not Board(1, 1) = "" And Not Board(1, 2) = "" And Not Board(1, 3) = "" Then
			If Not Board(2, 1) = "" And Not Board(2, 2) = "" And Not Board(2, 3) = "" Then
				If Not Board(3, 1) = "" And Not Board(3, 2) = "" And Not Board(3, 3) = "" Then
					If Not Won Then
						lblTurn.Text = "Draw!"
						MsgBox("Draw!", MsgBoxStyle.Information, "Noughts and Crossses")
						counterD = counterD + 1
						lblDrawScore.Text = counterD
					End If
				End If
			End If
		End If
	End Sub

	'Main programing of the game--> LOGICAL THINKING!!
	Private Sub PictureClick(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles PictureBox1.Click, _
	PictureBox2.Click, PictureBox3.Click, PictureBox4.Click, _
	PictureBox5.Click, PictureBox6.Click, PictureBox7.Click, _
	PictureBox8.Click, PictureBox9.Click
		'assigning values to each picturebox piece
		If Not Won Then
			Select Case sender.name
				Case "PictureBox1"
					SetPiece(1, 1, sender)
				Case "PictureBox2"
					SetPiece(1, 2, sender)
				Case "PictureBox3"
					SetPiece(1, 3, sender)
				Case "PictureBox4"
					SetPiece(2, 1, sender)
				Case "PictureBox5"
					SetPiece(2, 2, sender)
				Case "PictureBox6"
					SetPiece(2, 3, sender)
				Case "PictureBox7"
					SetPiece(3, 1, sender)
				Case "PictureBox8"
					SetPiece(3, 2, sender)
				Case "PictureBox9"
					SetPiece(3, 3, sender)
			End Select
			CheckWon(Cross)
			CheckWon(Nought)
			CheckDraw()
			IsCross = Not IsCross
			btnFinsh.Enabled = True
			'		Else
			'		   MsgBox("Game is over, please click either NEW GAME OR NEXT")
			'		  btnFinsh.Enabled = True

		End If
	End Sub





	Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResetBoard.Click

		Reset()

	End Sub


	Private Sub Reset()
		Dim msg As MsgBoxResult

		Board(1, 1) = ""
		Board(1, 2) = ""
		Board(1, 3) = ""
		Board(2, 1) = ""
		Board(2, 2) = ""
		Board(2, 3) = ""
		Board(3, 1) = ""
		Board(3, 2) = ""
		Board(3, 3) = ""
		PictureBox1.Image = Nothing
		PictureBox2.Image = Nothing
		PictureBox3.Image = Nothing
		PictureBox4.Image = Nothing
		PictureBox5.Image = Nothing
		PictureBox6.Image = Nothing
		PictureBox7.Image = Nothing
		PictureBox8.Image = Nothing
		PictureBox9.Image = Nothing

		Won = False

		msg = MsgBox(lblPlayerX.Text & " to go first?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Noughts and Crossses")
		If msg = MsgBoxResult.Yes Then
			IsCross = True
			Me.Text = lblPlayerX.Text & " X"
		Else
			IsCross = False
			Me.Text = lblPlayerO.Text & " O"
		End If

	End Sub

	Private Sub btnResetScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResetScore.Click
		counterX = 0
		lblPlayerXScore.Text = counterX

		counterO = 0
		lblPlayerOScore.Text = counterO

		counterD = 0
		lblDrawScore.Text = counterD
	End Sub

	Private Sub btnFinsh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinsh.Click
		Me.Close()
		frmTwoplayer_name.Close()
		frmTwoplayer_flipthecoin.Close()

		frmMenu.Show()

	End Sub
End Class




Please help me with constructing an AI!

Thanks,
Sparkygate

Is This A Good Question/Topic? 0
  • +

Replies To: TicTacToe AI Help

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9585
  • View blog
  • Posts: 36,323
  • Joined: 12-June 08

Re: TicTacToe AI Help

Posted 24 June 2008 - 07:40 AM

Sorry, I didn't have the attention span to read through all that code.. but where you want to start is like this:

START:
- player moves
- program analyses each possible move, recursively, with each possible player's move until a the 'end of game' scenerio hits. Of those reviewed the one where the computer is the winner are culled from the rest... now of that subset you can have it pick one random path and make the next move of that path, or have another selection option.
- check if end of game happens
if not go to START
if yes, end game.
Was This Post Helpful? 0
  • +
  • -

#3 sparkygate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-January 08

Re: TicTacToe AI Help

Posted 24 June 2008 - 07:03 PM

View Postmodi123_1, on 24 Jun, 2008 - 07:40 AM, said:

Sorry, I didn't have the attention span to read through all that code.. but where you want to start is like this:

START:
- player moves
- program analyses each possible move, recursively, with each possible player's move until a the 'end of game' scenerio hits. Of those reviewed the one where the computer is the winner are culled from the rest... now of that subset you can have it pick one random path and make the next move of that path, or have another selection option.
- check if end of game happens
if not go to START
if yes, end game.


With that in mind, should i be using Case Select for the AIs Brain, so like if human clicks on Board (1,3) then AI will select Board(3,3)?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9585
  • View blog
  • Posts: 36,323
  • Joined: 12-June 08

Re: TicTacToe AI Help

Posted 24 June 2008 - 09:08 PM

View Postsparkygate, on 24 Jun, 2008 - 09:03 PM, said:

View Postmodi123_1, on 24 Jun, 2008 - 07:40 AM, said:

Sorry, I didn't have the attention span to read through all that code.. but where you want to start is like this:

START:
- player moves
- program analyses each possible move, recursively, with each possible player's move until a the 'end of game' scenerio hits. Of those reviewed the one where the computer is the winner are culled from the rest... now of that subset you can have it pick one random path and make the next move of that path, or have another selection option.
- check if end of game happens
if not go to START
if yes, end game.


With that in mind, should i be using Case Select for the AIs Brain, so like if human clicks on Board (1,3) then AI will select Board(3,3)?


Naw.. see that's the beauty of "relatively" low depth in a recursive tree... it checks ALL possible moves to the end of the game based off what the board looks like.. so essentially you enumerate ALL of them, get the only winning ones, and based off what is a good next move (that's something you can program to apply after you enumerated all possible winning scenarios.. example: if the middle square is open it is preferable choice than a random move.. etc) No real case logic involved.. just brute force!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1