|
-
Jan 23rd, 2008, 06:59 PM
#1
Thread Starter
Member
Quick question. i Am a huge newb. please help, its easy!!
okkkkkkkkkkkkkkkk...
So i made a Tic Tac Toe game for vb 2005, in a windows application.
I need a play again button, i made a bar at the top with a play again button, but need the code for it. A classmate told me this...
Code:
me.(what ever you named the picbox)
thats all i remember lol
ending is = nothing
How could i do this?
this is my code for the play now button right now. (im not sure what the gamedone thing is there for)
Code:
Private Sub PlayAgainToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayAgainToolStripMenuItem.Click
If GameDone = 1 Then
GameDone = 0
End If
Thanks alot, please reply asap, im leaving soon and need this for tomrrow 
cya
-
Jan 23rd, 2008, 07:02 PM
#2
Re: Quick question. i Am a huge newb. please help, its easy!!
I'm afraid that isn't even close to enough information to expect an answer. How do you play it the first time?
My usual boring signature: Nothing
 
-
Jan 23rd, 2008, 07:55 PM
#3
Thread Starter
Member
Re: Quick question. i Am a huge newb. please help, its easy!!
What do you mean?
I have a Play now button.. and i need the proper code for my app to basically Loop.
What info do you need more? my code?
Code:
Public Class Form1
Public GameDone As Byte
'Name: 435354
'Date: January 20/ 08
'Purpose: For this game you need 2 players. One will be assigned "X" and the other "O". your objective is to get threes "X's" or "O's" in a 3 squared column, row or diaganle.
'Tic-Tac-Toe has been played until a winner or a draw is occured
Private Sub btnMoveMade_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btn00.Click, btn01.Click, _
btn02.Click, btn10.Click, btn11.Click, btn12.Click, btn20.Click, _
btn21.Click, btn22.Click
Dim btnSquareClicked As Button = sender
Static chrTTT(2, 2) As Char 'store player moves
Static player As Char = "X" 'X goes first
'Check for existing X or O
If btnSquareClicked.Text <> Nothing Then
MessageBox.Show("Invalid move.")
Else
'Show move
btnSquareClicked.Text = player
'Store move in chrTTT()
Dim index As String
index = btnSquareClicked.Tag
Dim x As Integer = Val(index.Chars(0))
Dim y As Integer = Val(index.Chars(2))
Call StoreMove(x, y, player, chrTTT)
'Check for winner
If IsWinner(chrTTT) Then
MessageBox.Show("Game over!")
Else 'next player’s turn
If player = "X" Then
player = "O"
Else
player = "X"
End If
End If
End If
End Sub
'Store Tic-Tac-Toe move in chrTTT().
'
'pre: x and y is a valid index. chrTTT is a 3 x 3 array.
'post: Tic-Tac-Toe move has been stored in chrTTT.
'
Sub StoreMove(ByVal x As Integer, ByVal y As Integer, _
ByVal player As Char, ByRef TTT(,) As Char)
TTT(x, y) = player
End Sub
'Determines if there is a winner.
'
'pre: chrTTT is a 3 x 3 array.
'post: True has been returned if a winner is found
'or if all squares are filled.
'
Function IsWinner(ByRef TTT(,) As Char) As Boolean
'Check all rows
For row As Integer = 0 To 2
If TTT(row, 0) = TTT(row, 1) And _
TTT(row, 1) = TTT(row, 2) And _
(TTT(row, 0) = "X" Or TTT(row, 0) = "O") Then
Return True 'winner
End If
Next row
'Check all columns
For col As Integer = 0 To 2
If TTT(0, col) = TTT(1, col) And _
TTT(1, col) = TTT(2, col) And _
(TTT(0, col) = "X" Or TTT(0, col) = "O") Then
Return True 'winner
End If
Next col
'Check one diagonal
If TTT(0, 0) = TTT(1, 1) And TTT(1, 1) = TTT(2, 2) _
And (TTT(0, 0) = "X" Or TTT(0, 0) = "O") Then
Return True 'winner
End If
'Check other diagonal
If TTT(0, 2) = TTT(1, 1) And TTT(1, 1) = TTT(2, 0) _
And (TTT(0, 2) = "X" Or TTT(0, 2) = "O") Then
Return True 'winner
End If
'Check for empty squares
Dim movesLeft As Boolean = False
For row As Integer = 0 To 2
For col As Integer = 0 To 2
If TTT(row, col) = Nothing Then
movesLeft = True
End If
Next col
Next row
If Not movesLeft Then
Return True 'all squares filled
End If
Return False 'no winner found
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub QuitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub RulesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RulesToolStripMenuItem.Click
Dim Form1 As New Rules
Form1.Visible = True
End Sub
Private Sub PlayAgainToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayAgainToolStripMenuItem.Click
me.(PlayAgainToolStripMenuItem)
If GameDone = 1 Then
GameDone = 0
End If
End Sub
End Class
-
Jan 23rd, 2008, 08:14 PM
#4
Re: Quick question. i Am a huge newb. please help, its easy!!
To us your game is a black box. If you say here's my black box with a Play Again button. Now how do I wire it up to play again? If we don't know anything about what's inside the black box then how can we tell you how to make it do anything?
Now, playing again just means starting a new game. If you've already played one game then you've already started a new game so you should just be doing the same thing again.
-
Jan 23rd, 2008, 08:31 PM
#5
Thread Starter
Member
Re: Quick question. i Am a huge newb. please help, its easy!!
^^What? im confused lol.
I would like it so that i can run it as a .exe and just press play again when the game is over.....
Do you want me to upload my program to somthing and you can have a look at it, if its a black box?
thanks
-
Jan 23rd, 2008, 08:38 PM
#6
New Member
Re: Quick question. i Am a huge newb. please help, its easy!!
How about if you take a print screen of your GUI and post a picture for us, that might help.
Do you need to keep the results of the previous game?
It sounds to me like you might just need to clear the array that you store your moves in, and reset the buttons to their original state, this can be very easy.
-
Jan 23rd, 2008, 08:43 PM
#7
Thread Starter
Member
Re: Quick question. i Am a huge newb. please help, its easy!!
Ok, here my array.
Thanks for the help everyone 
-
Jan 23rd, 2008, 08:55 PM
#8
Re: Quick question. i Am a huge newb. please help, its easy!!
Looks like what you need to do is clear all the buttons by setting the captions to "", then clear the TTT array.
My usual boring signature: Nothing
 
-
Jan 23rd, 2008, 08:56 PM
#9
Thread Starter
Member
Re: Quick question. i Am a huge newb. please help, its easy!!
eekkkkkk... im unfamiliar with that.
do you mind giving me a tiny step by step process in doing this?
again, thanks a lot this is great
-
Jan 23rd, 2008, 09:03 PM
#10
New Member
Re: Quick question. i Am a huge newb. please help, its easy!!
Try this
vb Code:
Private Sub PlayAgainToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayAgainToolStripMenuItem.Click
Dim i As Integer
Dim ii As Integer
For i = 0 To 2
For ii = 0 To 2
chrTTT(i, ii) = ""
Next
Next
btn00.text = ""
btn01.text = ""
btn02.text = ""
btn10.text = ""
btn11.text = ""
btn12.text = ""
btn20.text = ""
btn21.text = ""
btn22.text = ""
player = "X"
End Sub
-
Jan 23rd, 2008, 09:50 PM
#11
Thread Starter
Member
Re: Quick question. i Am a huge newb. please help, its easy!!
Great. thanks for the code!!!
Im still recieving an error...
heres a pic. as you can see im getting the blue lines .

thanks alot, this is great !
-
Jan 23rd, 2008, 10:04 PM
#12
PowerPoster
Re: Quick question. i Am a huge newb. please help, its easy!!
put your mouse over the wiggly line and itll tell you whats wrong ( as it says in the description at the bottom of the capture "its not declared"
when it comes to
vb Code:
btn00.text = "" btn01.text = "" btn02.text = "" btn10.text = "" btn11.text = "" btn12.text = "" btn20.text = "" btn21.text = "" btn22.text = ""
i always put that in a for each loop as its less code
vb Code:
for each ctrl as control in me.controls if typeof ctrl is button then ctrl.text = "" end if next ctrl
Last edited by BrailleSchool; Jan 23rd, 2008 at 10:08 PM.
-
Jan 23rd, 2008, 10:09 PM
#13
Thread Starter
Member
Re: Quick question. i Am a huge newb. please help, its easy!!
Thanks, it says both of them is not declared?
What do i do for the Player one?
And how do you use the loop your talking about?
THANKS ALOT EVERYONE!!
-
Jan 23rd, 2008, 10:25 PM
#14
PowerPoster
Re: Quick question. i Am a huge newb. please help, its easy!!
chrtt is declared in btnMoveMade_Click as a static variable, it should be public at the top of the code form so then you can use it in all subs. if you declare it in a sub, its not available anywhere else and itll say its not declared. the for each loop is added to my post above.
-
Jan 23rd, 2008, 10:28 PM
#15
PowerPoster
Re: Quick question. i Am a huge newb. please help, its easy!!
 Originally Posted by fix23
Thanks, it says both of them is not declared?
you see it right in the screen shot you posted
What do i do for the Player one?
put both of these (remove from the sub too otherwise youll get an error saying its already declared) at the top of your code window and declare it public (will only be public for the form and not the whole app
vb Code:
Static chrTTT(2, 2) As Char 'store player moves Static player As Char = "X" 'X goes first
Last edited by BrailleSchool; Jan 23rd, 2008 at 10:33 PM.
-
Jan 23rd, 2008, 10:51 PM
#16
Thread Starter
Member
Re: Quick question. i Am a huge newb. please help, its easy!!
Thanks alot man, thats helping greatly 
but i have a problem... i think i may be doing somthing wrong from the advie you guys are giving me... im still a newb to this and some of the stuff im still trying to figure out :P
anyways... the first game works... and then when i click "Play Again" is goes to a new game.... which is good, excpet the first move i make (X) it right away says Game over!... then i cant play, i only have the option to use X and after every square i use it says Game over.
Is there somthing i did wrong? i must have.
do you want a pic of my code? which part?
Thanks alot
cya
-
Jan 24th, 2008, 01:00 AM
#17
Thread Starter
Member
Re: Quick question. i Am a huge newb. please help, its easy!!
arrrrrrrrg.. well i got a buddy to help me out and fixed a couple of things, but neither of us can fix the game over bug when the play again button it pressed.
when you press play again and chose your first square it just says GAME OVER. which is not supposed to happen.
Heres my code now. thanks
Code:
Public Class Form1
Public chrTTT(2, 2) As Char 'store player moves
Public player As Char = "X" 'X goes first
'Date: January 20/ 08
'Purpose: For this game you need 2 players. One will be assigned "X" and the other "O". your objective is to get threes "X's" or "O's" in a 3 squared column, row or diaganle.
Private Sub btnMoveMade_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btn00.Click, btn01.Click, _
btn02.Click, btn10.Click, btn11.Click, btn12.Click, btn20.Click, _
btn21.Click, btn22.Click
Dim btnSquareClicked As Button = sender
Static chrTTT(2, 2) As Char 'store player moves
Static player As Char = "X" 'X goes first
'Check for existing X or O
If btnSquareClicked.Text <> Nothing Then
MessageBox.Show("Invalid move.")
Else
'Show move
btnSquareClicked.Text = player
'Store move in chrTTT()
Dim index As String
index = btnSquareClicked.Tag
Dim x As Integer = Val(index.Chars(0))
Dim y As Integer = Val(index.Chars(2))
Call StoreMove(x, y, player, chrTTT)
'Check for winner
If IsWinner(chrTTT) Then
MessageBox.Show("Game over! Click Menu > Play Again if you wish to play again!")
Else 'next player’s turn
If player = "X" Then
player = "O"
ToolStripStatusLabel1.Text = "It's player O's turn"
Else
player = "X"
ToolStripStatusLabel1.Text = "It's player X's turn"
End If
End If
End If
End Sub
'Store Tic-Tac-Toe move in chrTTT().
Sub StoreMove(ByVal x As Integer, ByVal y As Integer, _
ByVal player As Char, ByRef TTT(,) As Char)
TTT(x, y) = player
End Sub
'Determines if there is a winner.
'returned if a winner is found or if all squares are occupied.
Function IsWinner(ByRef TTT(,) As Char) As Boolean
'Check all rows
For row As Integer = 0 To 2
If TTT(row, 0) = TTT(row, 1) And _
TTT(row, 1) = TTT(row, 2) And _
(TTT(row, 0) = "X" Or TTT(row, 0) = "O") Then
Return True 'winner.
End If
Next row
'Check all columns
For col As Integer = 0 To 2
If TTT(0, col) = TTT(1, col) And _
TTT(1, col) = TTT(2, col) And _
(TTT(0, col) = "X" Or TTT(0, col) = "O") Then
Return True 'winner
End If
Next col
'Check one diagonal
If TTT(0, 0) = TTT(1, 1) And TTT(1, 1) = TTT(2, 2) _
And (TTT(0, 0) = "X" Or TTT(0, 0) = "O") Then
Return True 'winner
End If
'Check other diagonal
If TTT(0, 2) = TTT(1, 1) And TTT(1, 1) = TTT(2, 0) _
And (TTT(0, 2) = "X" Or TTT(0, 2) = "O") Then
Return True 'winner
End If
'Check for empty squares
Dim movesLeft As Boolean = False
For row As Integer = 0 To 2
For col As Integer = 0 To 2
If TTT(row, col) = Nothing Then
movesLeft = True
End If
Next col
Next row
If movesLeft = False Then
Return True 'all squares filled
End If
Return False 'no winner found
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub QuitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub RulesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RulesToolStripMenuItem.Click
Dim NewRules As New Rules
NewRules.ShowDialog()
End Sub
Private Sub PlayAgainToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayAgainToolStripMenuItem.Click
ReDim chrTTT(2, 2)
btn00.Text = ""
btn01.Text = ""
btn02.Text = ""
btn10.Text = ""
btn11.Text = ""
btn12.Text = ""
btn20.Text = ""
btn21.Text = ""
btn22.Text = ""
player = "X"
End Sub
End Class
-
Jan 24th, 2008, 01:50 AM
#18
Addicted Member
Re: Quick question. i Am a huge newb. please help, its easy!!
Try setting the Text property of each button to Nothing rather than "" in the PlayAgainToolStripMenuItem_Click eventhandler.
vbcode Code:
Private Sub PlayAgainToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayAgainToolStripMenuItem.Click
ReDim chrTTT(2, 2)
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Button Then
ctrl.Text = Nothing
End If
Next ctrl
player = "X"
End Sub
Just a suggestion. Make a sub like "StartNewGame" and move the code you have in the PlayAgainToolStripMenuItem_Click eventhandler to that sub. Now you can call the sub from the Form_Load event and also the PlayAgainToolStripMenuItem_Click eventhandler to start a fresh game. This now becomes your generic "Cleanup" routine.
Hope it helps,
Peace!
Anyone who has never made a mistake has never tried anything new. - Einstein
 Peace! 
-
Jan 24th, 2008, 01:59 AM
#19
Thread Starter
Member
Re: Quick question. i Am a huge newb. please help, its easy!!
hey thanks alot man!!
can you explain a little more bout that coding you said?
this is my new code, is it right? how do i call the form? thanks
also please add my msn. [email protected] i think yahoo people can still talk to me.
thanks
Code:
Public Class Form1
Public chrTTT(2, 2) As Char 'store player moves
Public player As Char = "X" 'X goes first
'Date: January 20/ 08
'Purpose: For this game you need 2 players. One will be assigned "X" and the other "O". your objective is to get threes "X's" or "O's" in a 3 squared column, row or diaganle.
Private Sub btnMoveMade_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btn00.Click, btn01.Click, _
btn02.Click, btn10.Click, btn11.Click, btn12.Click, btn20.Click, _
btn21.Click, btn22.Click
Dim btnSquareClicked As Button = sender
Static chrTTT(2, 2) As Char 'store player moves
Static player As Char = "X" 'X goes first
'Check for existing X or O
If btnSquareClicked.Text <> Nothing Then
MessageBox.Show("Invalid move.")
Else
'Show move
btnSquareClicked.Text = player
'Store move in chrTTT()
Dim index As String
index = btnSquareClicked.Tag
Dim x As Integer = Val(index.Chars(0))
Dim y As Integer = Val(index.Chars(2))
Call StoreMove(x, y, player, chrTTT)
'Check for winner
If IsWinner(chrTTT) Then
MessageBox.Show("Game over! Click Menu > Play Again if you wish to play again!")
Else 'next player’s turn
If player = "X" Then
player = "O"
ToolStripStatusLabel1.Text = "It's player O's turn"
Else
player = "X"
ToolStripStatusLabel1.Text = "It's player X's turn"
End If
End If
End If
End Sub
'Store Tic-Tac-Toe move in chrTTT().
Sub StoreMove(ByVal x As Integer, ByVal y As Integer, _
ByVal player As Char, ByRef TTT(,) As Char)
TTT(x, y) = player
End Sub
'Determines if there is a winner.
'returned if a winner is found or if all squares are occupied.
Function IsWinner(ByRef TTT(,) As Char) As Boolean
'Check all rows
For row As Integer = 0 To 2
If TTT(row, 0) = TTT(row, 1) And _
TTT(row, 1) = TTT(row, 2) And _
(TTT(row, 0) = "X" Or TTT(row, 0) = "O") Then
Return True 'winner.
End If
Next row
'Check all columns
For col As Integer = 0 To 2
If TTT(0, col) = TTT(1, col) And _
TTT(1, col) = TTT(2, col) And _
(TTT(0, col) = "X" Or TTT(0, col) = "O") Then
Return True 'winner
End If
Next col
'Check one diagonal
If TTT(0, 0) = TTT(1, 1) And TTT(1, 1) = TTT(2, 2) _
And (TTT(0, 0) = "X" Or TTT(0, 0) = "O") Then
Return True 'winner
End If
'Check other diagonal
If TTT(0, 2) = TTT(1, 1) And TTT(1, 1) = TTT(2, 0) _
And (TTT(0, 2) = "X" Or TTT(0, 2) = "O") Then
Return True 'winner
End If
'Check for empty squares
Dim movesLeft As Boolean = False
For row As Integer = 0 To 2
For col As Integer = 0 To 2
If TTT(row, col) = Nothing Then
movesLeft = True
End If
Next col
Next row
If movesLeft = False Then
Return True 'all squares filled
End If
Return False 'no winner found
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub QuitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub RulesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RulesToolStripMenuItem.Click
Dim NewRules As New Rules
NewRules.ShowDialog()
End Sub
Private Sub PlayAgainToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayAgainToolStripMenuItem.Click
ReDim chrTTT(2, 2)
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Button Then
ctrl.Text = Nothing
End If
Next ctrl
player = "X"
End Sub
Sub StartNewGame()
ReDim chrTTT(2, 2)
btn00.Text = ""
btn01.Text = ""
btn02.Text = ""
btn10.Text = ""
btn11.Text = ""
btn12.Text = ""
btn20.Text = ""
btn21.Text = ""
btn22.Text = ""
player = "X"
End Sub
End Class
Last edited by fix23; Nov 8th, 2008 at 06:13 PM.
-
Jan 24th, 2008, 02:46 AM
#20
Addicted Member
Re: Quick question. i Am a huge newb. please help, its easy!!
Declare StartNewGame() like :
vbcode Code:
Private Sub StartNewGame()
ReDim chrTTT(2, 2)
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Button Then
ctrl.Text = Nothing
End If
Next ctrl
player = "X"
End Sub
Now you can use this routine to start a new game from any event.
vbcode Code:
Private Sub Form_Load (ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
StartNewGame()
End Sub
And also,
vbcode Code:
Private Sub PlayAgainToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayAgainToolStripMenuItem.Click
StartNewGame()
End Sub
Simplifies things, doesnt it?
Peace!
Anyone who has never made a mistake has never tried anything new. - Einstein
 Peace! 
-
Jan 24th, 2008, 02:51 AM
#21
Thread Starter
Member
Re: Quick question. i Am a huge newb. please help, its easy!!
thanks! it does make things easyer.... except im still having the same problem 
when you press play again and chose your first square it just says GAME OVER. which is not supposed to happen.
-
Jan 24th, 2008, 03:11 AM
#22
Addicted Member
Re: Quick question. i Am a huge newb. please help, its easy!!
I see that you have duplicate declarations.
1. Public chrTTT(2, 2) As Char 'In Class Form1
2. Static chrTTT(2, 2) As Char 'In Sub btnMoveMade_Click
Remove the Static declaration in the sub btnMoveMade_Click and see if that helps.
Last edited by garyjohn_2000; Jan 24th, 2008 at 03:20 AM.
Anyone who has never made a mistake has never tried anything new. - Einstein
 Peace! 
-
Jan 24th, 2008, 03:15 AM
#23
Thread Starter
Member
Re: Quick question. i Am a huge newb. please help, its easy!!
hmmm i get an error message wen i chose my first box then it highlights this dim in yello
Code:
Dim x As Integer = Val(index.Chars(0))
aahhhhhhhhhhhhhhhhhhhhhh.. i gotta wake up in 3 hours for school.
any other options hear?
thnx alot
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|