VB AI Help

Need help with this game

Page 1 of 1

13 Replies - 4181 Views - Last Post: 18 May 2008 - 11:30 AM Rate Topic: -----

#1 coolcat12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-April 08

VB AI Help

Post icon  Posted 27 April 2008 - 03:17 PM

Ok I am in the process of making my tic tac toe program. I finished a 2 player game. I need help on my 1player game with AI. I just need help on a simple AI. So when I click a picture box a random picture box is selected for the AI, no intelligence needed. Ok I am using picture boxes. I have 27. 8 blanks, 8X's and 8 O's. I created arrays for all of them:

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

	   
		picBlank(0) = picNone1
		picBlank(1) = picNone2
		picBlank(2) = picNone3
		picBlank(3) = picNone4
		picBlank(4) = picNone5
		picBlank(5) = picNone6
		picBlank(6) = picNone7
		picBlank(7) = picNone8
		picBlank(8) = picNone9
		
	
		picX(0) = picX1
		picX(1) = picX2
		picX(2) = picX3
		picX(3) = picX4
		picX(4) = picX5
		picX(5) = picX6
		picX(6) = picX7
		picX(7) = picX8
		picX(8) = picX9

		
		picO(0) = picO1
		picO(1) = picO2
		picO(2) = picO3
		picO(3) = picO4
		picO(4) = picO5
		picO(5) = picO6
		picO(6) = picO7
		picO(7) = picO8
		picO(8) = picO9



I then created a private sub called comp

Private Sub comp()
		Dim random As New Random
		Dim pictureboxselected As Integer = random.Next(0, 10)

	End Sub


And thats all I got for 1 player game. I don't know what to do next. Help ? Thanks.

This post has been edited by coolcat12: 27 April 2008 - 03:19 PM


Is This A Good Question/Topic? 0
  • +

Replies To: VB AI Help

#2 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Re: VB AI Help

Posted 27 April 2008 - 03:50 PM

Isn't this VB.NET code???

I only ask this as VB6 and below doesn't use the Handles MyBase.Load section of code.

This all depends on what exactly you want from your AI.
I am assuming that this code simply makes the computer decide on a random move at the start of the game but not actually do anything??

For all I see where your going with 27 picture boxes, it seems like a lot of work. My suggestion would include 3 resources ( a blank image, a x and an o) and have 9 picture boxes, then when a user clicks an image it sets the picture box clicked to the image selected e.g.

Then I would handle all 9 picture box clicks with one sub as shown below:
	Private Sub PictureBox_Click(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
		sender.image = My.Resources.o
		Check4Win
		DoCompMove
	End Sub



My suggestion from there would be using maybe the Tag property to store whether it is a cross or circle.
Call Check4Win which you will need to code to check to see if you have a row
Then call DoCompMove.

Because I don't want to do it all I won't show what to do here but I will give you an idea. Generate a random number has you have already done, then check it hasn't already been taken before setting it to have the cross as it's picture. Then at the end of the move call Check4Win again

I hope this helps give you an idea of where to go.

Danny_kay1710
Was This Post Helpful? 0
  • +
  • -

#3 coolcat12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-April 08

Re: VB AI Help

Posted 27 April 2008 - 04:14 PM

View Postdanny_kay1710, on 27 Apr, 2008 - 03:50 PM, said:

This all depends on what exactly you want from your AI.
I am assuming that this code simply makes the computer decide on a random move at the start of the game but not actually do anything??

For all I see where your going with 27 picture boxes, it seems like a lot of work. My suggestion would include 3 resources ( a blank image, a x and an o) and have 9 picture boxes, then when a user clicks an image it sets the picture box clicked to the image selected e.g.


Danny_kay1710


Yea, the computer just picks a random spot. I started my program with the 3 resources. I didn't know how to handle it all so I stuck with the old way. And now I am to far in to change back.
So is there a way to do this my way and not the resources way with only having 9 picture boxes?
Was This Post Helpful? 0
  • +
  • -

#4 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Re: VB AI Help

Posted 27 April 2008 - 04:33 PM

View Postcoolcat12, on 27 Apr, 2008 - 04:14 PM, said:

Yea, the computer just picks a random spot. I started my program with the 3 resources. I didn't know how to handle it all so I stuck with the old way. And now I am to far in to change back.
So is there a way to do this my way and not the resources way with only having 9 picture boxes?


To be honest with you, you haven't really started with any AI and have a lot of code to do something a simple task (prepare a tic tac toe board) and sticking with that method is going to make you have a lot more than is needed, however I am not going to try and stop you using a method that you understand and/or prefer for whatever reason. I am just sayin that you will end up with a lot of code that could be reduced.

NOTE: Please read the entire post before starting to modify code it will become obvious when you read through why

Basically what you would need to do is modify the last code I posted

	Private Sub PictureBox_Click(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
		sender.image = My.Resources.o
	End Sub



to reference the 9 blank picture boxes. So replace the Handles ... with the 9 blank picture boxes you have already created and then instead of using sender.image = my.resources.0 you would use sender.visible = false.

The problem then comes when making the circle or cross picture box visible as you don't know the position on the board only the actual picture box you clicked. This would probably mean the easiest way to progress is to adopt the method of doing a sub for each individual picturebox

	Private Sub PicBlank8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PicBlank8.Click
		Me.PicBlank8.Visible = False
		Me.picO8.Visible = True
		Check4Win
	   CompMove
	End Sub



You would need to have this 9 times and change every instance of 8 to the appropriate picturebox number.

Then you would use similar logic to before however instead of storing it in the tag property you would look for 3 visible boxes of the same type (either 0 or x) in a row to constitute a win.

Then for a comp move generate a random number check that the blank tile is visible if it is then set it to X for example if not generate another random number

This help you any better?

danny_kay1710

This post has been edited by danny_kay1710: 27 April 2008 - 04:42 PM

Was This Post Helpful? 0
  • +
  • -

#5 coolcat12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-April 08

Re: VB AI Help

Posted 27 April 2008 - 05:30 PM

Yea, I know I dont got alot of AI code. But I have alot of 2 player code. So I dont want to use the resources and all that. I'll review what you said some more and see what i can do.
Also, I need help on the random number thing. I dont understand that. I already created a private sub for each picturebox. This is for the two player game. Can I use the same thing for 1 player game. Example:

Private Sub picnone1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picNone1.Click
		'
		picNone1.Visible = False

		If blnTurnXorO = True Then
			picX1.Visible = True
			blnTurnXorO = False


		ElseIf blnTurnXorO = False Then
			picO1.Visible = True
			blnTurnXorO = True
		End If


		Call win()
		Call CurrentTurn()

This post has been edited by coolcat12: 27 April 2008 - 05:37 PM

Was This Post Helpful? 0
  • +
  • -

#6 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Re: VB AI Help

Posted 27 April 2008 - 05:39 PM

View Postcoolcat12, on 27 Apr, 2008 - 05:30 PM, said:

Yea, I know I dont got alot of AI code. But I have alot of 2 player code. So I dont want to use the resources ad all that. I'll review what you said some more and see what i can do.


Hope what I said helps to give you an idea on how to go about doing the AI code and I certainly didn't want it to seem as if I was trying to talk you out of doing it the way you are.

The Check4Win code or similar you should probably already have from your 2 player game and like I have said the AI without any specific intelligence is generate a random number check that the tile hasn't already been used and then effectively you could make it as simple as run the second player code.

Danny_kay1710
Was This Post Helpful? 0
  • +
  • -

#7 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 733
  • Joined: 16-January 08

Re: VB AI Help

Posted 29 April 2008 - 01:54 AM

This is not AI, AI is when it learns and this would not learn what you need to learn is algorithms then try again. Have alook at this website:
http://webster.cs.uc...cToe/ttt_1.html
this should help.
Was This Post Helpful? 0
  • +
  • -

#8 coolcat12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-April 08

Re: VB AI Help

Posted 29 April 2008 - 04:44 PM

View Postsam_benne, on 29 Apr, 2008 - 01:54 AM, said:

This is not AI, AI is when it learns and this would not learn what you need to learn is algorithms then try again. Have alook at this website:
http://webster.cs.uc...cToe/ttt_1.html
this should help.



When I said AI I meant random number. So when I click a picture box a random picture box is selected for the AI.
Was This Post Helpful? 0
  • +
  • -

#9 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 733
  • Joined: 16-January 08

Re: VB AI Help

Posted 30 April 2008 - 03:11 AM

Well just create an array with a random number generator.
Was This Post Helpful? 0
  • +
  • -

#10 coolcat12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-April 08

Re: VB AI Help

Posted 01 May 2008 - 04:00 PM

Hi people. I managed to get some random numbering to work. I got it so When I click a picture box (X) an O is randomly selected for the AI. My problem is sometimes when I have an X in a spot and O appears over my X for the AI or the AI picks the same box twice. I need help on how to make it when a picture box is already selected so that the random numbers doesn't go in a selected one.

This is what I got in my AI sub:
Private Sub AI()

		Dim random As Random = New Random(DateTime.Now.Millisecond)
		Dim picselect As Integer = random.Next(0, 8)

	   
		If blnTurnXorO = True Then
			picX(picselect).Visible = True
			picBlank(picselect).Visible = False
			blnTurnXorO = False
		Else

			picO(picselect).Visible = True
			picBlank(picselect).Visible = False

			blnTurnXorO = True

		End If
	End Sub


Was This Post Helpful? 0
  • +
  • -

#11 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 733
  • Joined: 16-January 08

Re: VB AI Help

Posted 01 May 2008 - 11:44 PM

I have not created this but this is a working Tic Tac Toe game with "AI".

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#12 coolcat12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-April 08

Re: VB AI Help

Posted 02 May 2008 - 07:30 PM

Is there any other way to help me out rather than me downlaoding someones eleses code?
Was This Post Helpful? 0
  • +
  • -

#13 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 733
  • Joined: 16-January 08

Re: VB AI Help

Posted 03 May 2008 - 12:19 AM

Yuo can learn how this person has done it and you can do it much easier then
Was This Post Helpful? 0
  • +
  • -

#14 coolcat12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-April 08

Re: VB AI Help

Posted 18 May 2008 - 11:30 AM

Hello. So I am almost done wtih this game. I need help on one small thing I cant figure out. I have message boxes for all possible ways X and O can win, but how fdo i make it so when it is a DRAw and no ne wins, so a message box pops up and says DRAW? Thanks.

Heres my code for my WIN sub, I do think I put it in here righT?

[code]Private Sub win()


'make messagebox appears when X or O wins
If picX1.Visible = True And picX2.Visible = True And picX3.Visible = True Then

MessageBox.Show("X wins")


etc...

This post has been edited by coolcat12: 18 May 2008 - 08:58 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1