Tic Tac Toe codes for 1 player

tic tac toe codes for 1 player

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

48 Replies - 16657 Views - Last Post: 19 January 2007 - 02:10 PM Rate Topic: -----

#1 orangeslide8  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 203
  • Joined: 29-December 06

Tic Tac Toe codes for 1 player

Posted 29 December 2006 - 05:21 PM

Ok so im am trying to create tic tac toe in visual basic 2005 express version (Downloaded for free). what I did is take labels and arrange them like a tic tac toe board with 9 of them. Then entered the following code for when it is clicked.
Public Class Form1
	Dim Turn = 1
	Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
		If Turn = 1 Or 3 Or 5 Or 7 Or 9 Then
			MsgBox("Its Player 1s Turn")
			If Turn = 1 Or 3 Or 5 Or 7 Then
				Me.Label1.Text = "X"
				If Turn = 2 Or 4 Or 6 Or 8 Or 10 Then
					MsgBox("Its Player 2s Turn")
					If Turn = 2 Or 4 Or 6 Or 8 Or 10 Then
						Me.Label1.Text = "O"
					End If
				End If
			End If
		End If
		Turn = Turn + 1
	End Sub

i entered that code for each of the labels when clicked.
Then ran the program just to see if that part of the program worked (this is for running in 2 player mode) but when i ran it it opened and when i clicked any label first msgbox it is player1s turn came up then the box turned to "x" then immeideiteley after (without clicking anything) it came up with Msgbox player2s turn then the same label turned to O.
Can someone please help me to fix that part of the code and help me with creating codes for finding out when the game is over, making a 1 player mode for it, and anyother codes i need to make it
Thnx
John

Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe codes for 1 player

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Tic Tac Toe codes for 1 player

Posted 29 December 2006 - 06:12 PM

Your use of the OR operator is not quite correct. You need to repeat the variable being compared to each condition in the statement.

Like this:
If Turn = 1 Or Turn = 3 Or Turn = 5 Or Turn = 7 Or Turn = 9 Then



Also you can eliminate half of your IF statement by nesting both lines of code from each matching IF statement that is comparing the same thing into an IF/ElseIF statement.
If Turn = 1 Or Turn = 3 Or Turn = 5 Or Turn = 7 Or Turn = 9 Then
			MsgBox("Its Player 1s Turn")
			Me.Label1.Text = "X"
ElseIf Turn = 2 Or Turn = 4 Or Turn = 6 Or Turn = 8 Or Turn = 10 Then
			MsgBox("Its Player 2s Turn")
			Me.Label1.Text = "O"
End If


Was This Post Helpful? 0
  • +
  • -

#3 KeyWiz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 438
  • Joined: 26-October 06

Re: Tic Tac Toe codes for 1 player

Posted 29 December 2006 - 06:15 PM

There are a lot of better ways to do this, but I tried to maintain at your level of programming so you caould follow the Code Logic.

Public Class Form1
	Dim Turn As Integer  ' Declare VARIABLE

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'Initialize Turn
		Turn = 1
		'instead of Messagebox which makes user click OK use Label to simply display current Player
		lblTurn.Text = "Player 1"
		'Initiate Boxes
		Label1.Text = "[ ]"
		Label2.Text = "[ ]"
		Label3.Text = "[ ]"
		Label4.Text = "[ ]"
		Label5.Text = "[ ]"
		Label6.Text = "[ ]"
		Label7.Text = "[ ]"
		Label8.Text = "[ ]"
		Label9.Text = "[ ]"



	End Sub

	Private Sub WhichPlayer()
		'Divide Turn by 2, if the result is a whole number then the value of Turn is EVEN
		'Otherwise it is Odd
		If Int(Turn / 2) = Turn / 2 Then
			lblTurn.Text = "Player 2"
		Else
			lblTurn.Text = "Player 1"
		End If
	End Sub

	Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
		' Check to see if Box is Used
		If Label1.Text = "[ ]" Then
			' If not Assign Graphic according to Player Number
			If lblTurn.Text = "Player 1" Then
				Label1.Text = "X"
			Else
				Label1.Text = "O"
			End If
			'Advance to next play
			Turn = Turn + 1
			'Check to see whoose turn it is
			WhichPlayer()
		End If
	End Sub
	Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
		If Label2.Text = "[ ]" Then
			If lblTurn.Text = "Player 1" Then
				Label2.Text = "X"
			Else
				Label2.Text = "O"
			End If
			Turn = Turn + 1
			WhichPlayer()
		End If
	End Sub

	Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
		If Label3.Text = "[ ]" Then
			If lblTurn.Text = "Player 1" Then
				Label3.Text = "X"
			Else
				Label3.Text = "O"
			End If
			Turn = Turn + 1
			WhichPlayer()
		End If
	End Sub
End Class




FYI when checking for MULTIPLE CONDITIONS the condition must be set at each comparrison.

ie.

If Turn = 1 Or Turn = 3 Or Turn = 5 Or Turn = 7 Or Turn = 9 Then

but since you are looking for Odd or Even simply check to see if the value divides evenly by two, if it does, it is even.

Hope this helps.

This post has been edited by KeyWiz: 29 December 2006 - 06:22 PM

Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Tic Tac Toe codes for 1 player

Posted 29 December 2006 - 06:20 PM

KeyWiz, thank you for helping out orangeslide8...we do try to help members understand and guide them through the process instead of providing cleaned up working code right off, however...something to keep in mind. Many members here are able to quickly make all corrections to a program such as this, but it can sometimes be more valuable to the user themselves to create it as their own work with a little guidance.



Thanks for the perfect quote Amadeus. I couldn't have said it better myself, so I borrowed yours. :)
Was This Post Helpful? 0
  • +
  • -

#5 orangeslide8  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 203
  • Joined: 29-December 06

Re: Tic Tac Toe codes for 1 player

Posted 29 December 2006 - 08:08 PM

If there are eaisier ways to do this can you just tell me one of those?
Thnx
john
Was This Post Helpful? 0
  • +
  • -

#6 orangeslide8  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 203
  • Joined: 29-December 06

Re: Tic Tac Toe codes for 1 player

Posted 29 December 2006 - 08:32 PM

Hey keywiz one other question. When i run the code you gave me only the top row of tic tac toe works (Label1,Label2,and Label3) work while the others when clicked to not change. Any Hints
Thnx,
John
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Tic Tac Toe codes for 1 player

Posted 29 December 2006 - 08:43 PM

That is becasue KeyWiz has given you an example for the first three labels using the click event. A similar logic would have to be applied to the other labels.
Was This Post Helpful? 0
  • +
  • -

#8 orangeslide8  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 203
  • Joined: 29-December 06

Re: Tic Tac Toe codes for 1 player

Posted 30 December 2006 - 07:25 AM

ok some im tryin to make a tic tac toe game and it works for 2 player but i can't figure out how to end the game heres my code
Public Class Form1
	Dim Turn As Integer  ' Declare VARIABLE
	Dim Gameisover As Integer
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'Initialize Turn
		Turn = 1
		'instead of Messagebox which makes user click OK use Label to simply display current Player
		lblTurn.Text = "Player 1"
		'Initiate Boxes
		Label1.Text = "  "
		Label2.Text = "  "
		Label3.Text = "  "
		Label4.Text = "  "
		Label5.Text = "  "
		Label6.Text = "  "
		Label7.Text = "  "
		Label8.Text = "  "
		Label9.Text = "  "



	End Sub

	Private Sub WhichPlayer()
		'Divide Turn by 2, if the result is a whole number then the value of Turn is EVEN
		'Otherwise it is Odd
		If Int(Turn / 2) = Turn / 2 Then
			lblTurn.Text = "Player 2"
		Else
			lblTurn.Text = "Player 1"
		End If
	End Sub
	Public Sub Endgame()
		If Turn / 2 = 5 Then
			Gameisover = lblTurn.Text = ("The Game Is over Please start a new one by clicking new game on the control panel")
		End If
	End Sub
   
	Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
		' Check to see if Box is Used
		If Label1.Text = "  " Then
			' If not Assign Graphic according to Player Number
			If lblTurn.Text = "Player 1" Then
				Label1.Text = "X"
			Else
				Label1.Text = "O"
			End If
			'Advance to next play
			Turn = Turn + 1
			'Check to see whoose turn it is
			WhichPlayer()
			Endgame()
		End If
	End Sub
	Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
		If Label2.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label2.Text = "X"
			Else
				Label2.Text = "O"
			End If
			Turn = Turn + 1
	  WhichPlayer()
			Endgame()
		End If
	End Sub

	Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
		If Label3.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label3.Text = "X"
			Else
				Label3.Text = "O"
			End If
			Turn = Turn + 1
			WhichPlayer()
			Endgame()
	End If
	End Sub

	Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click
		If Label4.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label4.Text = "X"
			Else
				Label4.Text = "O"
			End If
			Turn = Turn + 1
		   WhichPlayer()
			Endgame()
		End If
	End Sub

	Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click
		If Label5.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label5.Text = "X"
			Else
				Label5.Text = "O"
			End If
			Turn = Turn + 1
		   WhichPlayer()
			Endgame()
		End If
	End Sub

	Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click
		If Label6.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label6.Text = "X"
			Else
				Label6.Text = "O"
			End If
			Turn = Turn + 1
		   WhichPlayer()
			Endgame()
		End If
	End Sub

	Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click
		If Label7.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label7.Text = "X"
			Else
				Label7.Text = "O"
			End If
			Turn = Turn + 1
		   WhichPlayer()
			Endgame()
		End If
	End Sub

	Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label8.Click
		If Label8.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label8.Text = "X"
			Else
				Label8.Text = "O"
			End If
			Turn = Turn + 1
		   WhichPlayer()
			Endgame()
		End If
	End Sub

	Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click
		If Label9.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label9.Text = "X"
			Else
				Label9.Text = "O"
			End If
			Turn = Turn + 1
			WhichPlayer()
			Endgame()
		End If
	End Sub
   
	Private Sub HowManyPlayers()

		' this is where i will create 1 player or 2 player on controls menu Don't worry about this part
		' also i have to create another form with radio buttons to tell weather it is 1 player or 2 player don't worry about anything below this unless you see something rong with it

	End Sub
	Private Sub Oneplayer1()
		'this is where i need to create the smartness of the computer
	End Sub
	Private Sub QuitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitToolStripMenuItem.Click
		Close()
	End Sub

	Private Sub NewGameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameToolStripMenuItem.Click
		'Initialize Turn
		Turn = 1
		'instead of Messagebox which makes user click OK use Label to simply display current Player
		lblTurn.Text = "Player 1"
		'Initiate Boxes
		Label1.Text = "  "
		Label2.Text = "  "
		Label3.Text = "  "
		Label4.Text = "  "
		Label5.Text = "  "
		Label6.Text = "  "
		Label7.Text = "  "
		Label8.Text = "  "
		Label9.Text = "  "
	End Sub


wut happens is it just doen's end
wut i tried to do is make it so when all the turns are over thats one end of game but that doesn't even work
Can someone help me and give me some tips on how to end games when a player got three in a row
thnx
John
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Tic Tac Toe codes for 1 player

Posted 30 December 2006 - 07:30 AM

Topics merged for continuity.
Was This Post Helpful? 0
  • +
  • -

#10 orangeslide8  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 203
  • Joined: 29-December 06

Re: Tic Tac Toe codes for 1 player

Posted 30 December 2006 - 07:36 AM

What do you mean?
and how could i fix it

This post has been edited by orangeslide8: 30 December 2006 - 07:38 AM

Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Tic Tac Toe codes for 1 player

Posted 30 December 2006 - 07:41 AM

My apologies...I simply meant to indicate that I had taken your original topic on tic tac toe in VB, and merged it with the second topic you began on the same subject. In this way, thread readers will have the whole story, and it cuts down on duplicate threads. I was not providing guidance on fixing the problem itself.
Was This Post Helpful? 0
  • +
  • -

#12 orangeslide8  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 203
  • Joined: 29-December 06

Re: Tic Tac Toe codes for 1 player

Posted 30 December 2006 - 07:54 AM

OK But do you know how to fix the problem?
thnx
john
Was This Post Helpful? 0
  • +
  • -

#13 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Tic Tac Toe codes for 1 player

Posted 30 December 2006 - 08:14 AM

I don't have VB 9 installed in this cpu, so ill play a typwriter :P

dim player_1_turn as boolean

private sub gamestart(something) handles click
   label1.text=" ":   label2.text=" ":   label3.text=" "
   label4.text=" ":   label5.text=" ":   label6.text=" "
   label7.text=" ":   label8.text=" ":   label9.text=" "
   player_1_turn=true
end sub

private sub label1(something) handles click
   call jagaja(i)
end sub
 
private sub label1(something) handles click:call jagaja(1):end sub
private sub label2(something) handles click:call jagaja(2):end sub
private sub label3(something) handles click:call jagaja(3):end sub
private sub label4(something) handles click:call jagaja(4):end sub
private sub label5(something) handles click:call jagaja(5):end sub
private sub label6(something) handles click:call jagaja(6):end sub
private sub label7(something) handles click:call jagaja(7):end sub
private sub label8(something) handles click:call jagaja(8):end sub
private sub label9(something) handles click:call jagaja(9):end sub

sub jagaja(nr)
	 select case nr
	   case 1:label1.text=iif(player_1_turn=true,"X","O"):player_1_turn=not player_1_turn
	   case 2:label2.text=iif(player_1_turn=true,"X","O"):player_1_turn=not player_1_turn
	   case 3:label3.text=iif(player_1_turn=true,"X","O"):player_1_turn=not player_1_turn
	   case 4:label4.text=iif(player_1_turn=true,"X","O"):player_1_turn=not player_1_turn
	   case 5:label5.text=iif(player_1_turn=true,"X","O"):player_1_turn=not player_1_turn
	   case 6:label6.text=iif(player_1_turn=true,"X","O"):player_1_turn=not player_1_turn
	   case 7:label7.text=iif(player_1_turn=true,"X","O"):player_1_turn=not player_1_turn
	   case 8:label8.text=iif(player_1_turn=true,"X","O"):player_1_turn=not player_1_turn
	   case 9:label9.text=iif(player_1_turn=true,"X","O"):player_1_turn=not player_1_turn
	 end select

	 if label1.text=label2.text=label3.text or label4.text=label5.text=label6.text or label7.text=label8.text=label9.text or label1.text=label4.text=label7.text or label2.text=label5.text=label8.text or label3.text=label6.text=label9.text or
label1.text=label5.text=label9.text or label7.text=label5.text=label3.text then 
msgbox("Game over." & chr(13) & "Player " & iif(player_1_turn=true,2,1) & " is the winner!")
end sub

Note: That if it would be VB 6 it could be done with lesser code. Because it enables array labels. Like so:
dim player_1_turn as boolean

Private Sub Command1_Click()
	For i = i To 9
		Label(i).Caption = " "
	Next i
End Sub
Private Sub Label_Click(Index As Integer)
	Label(Index).caption = IIf(player_1_turn = True, "X", "O"): player_1_turn = Not player_1_turn
	
If Label(1).caption = Label(2).caption = Label(3).caption Or Label(4).caption = Label(5).caption = Label(6).caption Or _
	Label(7).caption = Label(8).caption = Label(9).caption Or Label(1).caption = Label(4).caption = Label(7).caption Or _
	Label(2).caption = Label(5).caption = Label(8).caption Or Label(3).caption = Label(6).caption = Label(9).caption Or _
	Label(1).caption = Label(5).caption = Label(9).caption Or Label(7).caption = Label(5).caption = Label(3).caption Then
	MsgBox ("Game over." & Chr(13) & "Player " & IIf(player_1_turn = True, 2, 1) & " is the winner!")
End If
End Sub


Hope it helps :D

This post has been edited by m2s87: 30 December 2006 - 08:23 AM

Was This Post Helpful? 0
  • +
  • -

#14 orangeslide8  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 203
  • Joined: 29-December 06

Re: Tic Tac Toe codes for 1 player

Posted 30 December 2006 - 08:26 AM

Anyway so i made the whole tic tac toe game exept for 1 bug i can't figure out heres the code
 Public Class Form1
	Dim Turn As Integer  ' Declare VARIABLE
	Dim Gameisover As Integer
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'Initialize Turn
		Turn = 1
		'instead of Messagebox which makes user click OK use Label to simply display current Player
		lblTurn.Text = "Player 1"
		'Initiate Boxes
		Label1.Text = "  "
		Label2.Text = "  "
		Label3.Text = "  "
		Label4.Text = "  "
		Label5.Text = "  "
		Label6.Text = "  "
		Label7.Text = "  "
		Label8.Text = "  "
		Label9.Text = "  "



	End Sub

	Private Sub WhichPlayer()
		'Divide Turn by 2, if the result is a whole number then the value of Turn is EVEN
		'Otherwise it is Odd
		If Int(Turn / 2) = Turn / 2 Then
			lblTurn.Text = "Player 2"
		Else
			lblTurn.Text = "Player 1"
		End If
	End Sub
	Public Sub Endgame()
If Label1.Text = "X" Or "O" And Label2.Text = "X" Or "O" And Label3.Text = "X" Or "O" And Label4.Text = "X" Or "O" And Label5.Text = "X" Or "O" And Label6.Text = "X" Or "O" And Label7.Text = "X" Or "O" And Label8.Text = "X" Or "O" And Label9.Text = "X" Or "O" Then				   
MsgBox("Game Over Player 1 And Player 2 Tied")

		End If
		If Label1.Text = "X" And Label2.Text = "X" And Label3.Text = "X" Or Label4.Text = "X" And Label5.Text = "X" And Label6.Text = "X" Or Label7.Text = "X" And Label8.Text = "X" And Label9.Text = "X" Or Label1.Text = "X" And Label4.Text = "X" And Label7.Text = "X" Or Label2.Text = "X" And Label5.Text = "X" And Label8.Text = "X" Or Label3.Text = "X" And Label6.Text = "X" And Label9.Text = "X" Or Label1.Text = "X" And Label5.Text = "X" And Label9.Text = "X" Or Label7.Text = "X" And Label5.Text = "X" And Label3.Text = "X" Then
			MsgBox("Game over player 1 won")
		End If
		If Label1.Text = "O" And Label2.Text = "O" And Label3.Text = "O" Or Label4.Text = "O" And Label5.Text = "O" And Label6.Text = "O" Or Label7.Text = "O" And Label8.Text = "O" And Label9.Text = "O" Or Label1.Text = "O" And Label4.Text = "O" And Label7.Text = "O" Or Label2.Text = "O" And Label5.Text = "O" And Label8.Text = "O" Or Label3.Text = "O" And Label6.Text = "O" And Label9.Text = "O" Or Label1.Text = "O" And Label5.Text = "O" And Label9.Text = "O" Or Label7.Text = "O" And Label5.Text = "O" And Label3.Text = "O" Then
			MsgBox("Game over player 2 won")
		End If
	End Sub

   Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
		' Check to see if Box is Used
		If Label1.Text = "  " Then
			' If not Assign Graphic according to Player Number
			If lblTurn.Text = "Player 1" Then
				Label1.Text = "X"
			Else
				Label1.Text = "O"
			End If
			'Advance to next play
			Turn = Turn + 1
			'Check to see whoose turn it is
			WhichPlayer()
			Endgame()
		End If
	End Sub
	Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
		If Label2.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label2.Text = "X"
			Else
				Label2.Text = "O"
			End If
			Turn = Turn + 1
	  WhichPlayer()
			Endgame()
		End If
	End Sub

	Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
		If Label3.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label3.Text = "X"
			Else
				Label3.Text = "O"
			End If
			Turn = Turn + 1
			WhichPlayer()
			Endgame()
	End If
	End Sub

	Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click
		If Label4.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label4.Text = "X"
			Else
				Label4.Text = "O"
			End If
			Turn = Turn + 1
		   WhichPlayer()
			Endgame()
		End If
	End Sub

	Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click
		If Label5.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label5.Text = "X"
			Else
				Label5.Text = "O"
			End If
			Turn = Turn + 1
		   WhichPlayer()
			Endgame()
		End If
	End Sub

	Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click
		If Label6.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label6.Text = "X"
			Else
				Label6.Text = "O"
			End If
			Turn = Turn + 1
		   WhichPlayer()
			Endgame()
		End If
	End Sub

	Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click
		If Label7.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label7.Text = "X"
			Else
				Label7.Text = "O"
			End If
			Turn = Turn + 1
		   WhichPlayer()
			Endgame()
		End If
	End Sub

	Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label8.Click
		If Label8.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label8.Text = "X"
			Else
				Label8.Text = "O"
			End If
			Turn = Turn + 1
		   WhichPlayer()
			Endgame()
		End If
	End Sub

	Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click
		If Label9.Text = "  " Then
			If lblTurn.Text = "Player 1" Then
				Label9.Text = "X"
			Else
				Label9.Text = "O"
			End If
			Turn = Turn + 1
			WhichPlayer()
			Endgame()
		End If
	End Sub
   
	Private Sub HowManyPlayers()

		' this is where i will create 1 player or 2 player on controls menu Don't worry about this part
		' also i have to create another form with radio buttons to tell weather it is 1 player or 2 player don't worry about anything below this unless you see something rong with it

	End Sub
	Private Sub Oneplayer1()
		'this is where i need to create the smartness of the computer
	End Sub
	Private Sub QuitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitToolStripMenuItem.Click
		Close()
	End Sub

	Private Sub NewGameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameToolStripMenuItem.Click
		'Initialize Turn
		Turn = 1
		'instead of Messagebox which makes user click OK use Label to simply display current Player
		lblTurn.Text = "Player 1"
		'Initiate Boxes
		Label1.Text = "  "
		Label2.Text = "  "
		Label3.Text = "  "
		Label4.Text = "  "
		Label5.Text = "  "
		Label6.Text = "  "
		Label7.Text = "  "
		Label8.Text = "  "
		Label9.Text = "  "
	End Sub
End Class


the problem is when i run it an error comes up that says you can not convert "O" to a boolean and hilights this code
 If Label1.Text = "X" Or "O" And Label2.Text = "X" Or "O" And Label3.Text = "X" Or "O" And Label4.Text = "X" Or "O" And Label5.Text = "X" Or "O" And Label6.Text = "X" Or "O" And Label7.Text = "X" Or "O" And Label8.Text = "X" Or "O" And Label9.Text = "X" Or "O" Then 

can someone tell me why it says that thnx

This post has been edited by orangeslide8: 30 December 2006 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#15 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Tic Tac Toe codes for 1 player

Posted 30 December 2006 - 08:51 AM

View Postorangeslide8, on 30 Dec, 2006 - 08:26 AM, said:

a few problems with this 1 wut if the game is a draw and none wins? Second is a question will this work on visual basic 2005?


Because it is a player vs player game, it does note really matter if it is notified if it is a draw or not. But if you want it, you can add 1 global integer that is increased each time that legal move has been made. Add some if at the end of code to verifie that the game is over and nobody won.

Actually there is 1 ironical thing you might encounter. 1 move can be redone! So you need to verifie first that it is a legal move ! You can add the data to a global array or string.

And the first example was for the vb 8 a la vb 2005. Tho i did not remember if click event was captured like
private sub label1(e as expression_handler) handles label1.click

or something else, but it is irrelevant.

Hope it helps :D
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »