Hello,
I am interested in creating a function, that I can call, that performs the same events over and over again. Eg: for a tic tac toe game, check to see if the user has one, after each click. I know you can do it in PHP, but am unfamiliar with how to do it in VB....
Any help is appreciated.
Scott

Function
AllUltima1
what are the controls making up the grid and how are you naming them
Jody Crozier
Thank you for the information.
It works well.
As a result of your post, I now have learned how to use arrays, and for loops.
Scott
nomadicspurs
In my game, I am using 9 buttons. When the buttons are clicked, they show an X label or a Y label, depending on which was clicked last. The buttons are labelled A1, A2, A3 etc.
The grid is:
A1, B1, C1
A2, B2, C2
A3, B3, C3
I want it to check if there are three of the same characters in a row... a win.
But I want to check after every click, by calling a function.
JosW
Scott;
I hesitate to post this code, because any "real" developer could probably tell you a thousand more elegant ways of doing this. I have been bailed out several times on this site though, so I am willing to risk the riducule to gain back some karma. In my defense, I tried to use a straight forward approach to clearly show how you might use a function to do what your asking.
You will notice, that the first thing that I do in the forms Load event, is to put all of your buttons into an array. This allows me to pass only an array to the function, instead of passing each button as a function parameter. Also, I cheated and used one button.click event to handle the button click for all of my TicTacToe buttons. You can choose to keep them seperate if you find it easier.
Each time that I have a button click, I call the function "isWinner", passing to it the array of buttons and getting from it a true or false based on whether a winner exists. You may have picked up on the fact that I didn't really have to pass the mybuttons array, because it is scoped to the entire class (it can be seen anywhere within the forms code). I passed it anyway to show an example of passing a parameter to a function.
The code works, except that I didn't check for a winner in the diagonal nor did I include a way to restart the game if nobody wins.
I hope that this answers your question. If not, at least I have myself a neat little TicTacToe gane now :).
SteveJ
Public
Class Form1 Dim mybuttons(8) As Button Dim whoseTurn As String = "X"
' Event occurrs when form loads - upon program startup Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' Put all of the buttons into an arraymybuttons(0) = Button1
mybuttons(1) = Button2
mybuttons(2) = Button3
mybuttons(3) = Button4
mybuttons(4) = Button5
mybuttons(5) = Button6
mybuttons(6) = Button7
mybuttons(7) = Button8
mybuttons(8) = Button9
' !!! CALLS A SUBROUTINE, DEFINED BELOW, TO CLEAR ALL BUTTONS !!! Me.clearButtons(mybuttons) End Sub
' THIS EVENT TRIGGERS WHENEVER ONE OF YOUR BUTTONS IS PRESSED Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, _
Button7.Click, Button8.Click, Button9.Click
Dim _button As Button_button =
CType(sender, Button) ' Don't allow player to write-over an already used button If _button.Text = "" Then_button.Text = whoseTurn
' !!!! CALL THE FUNCTION HERE !!!! ' It is getting passed the array, my buttons, ' It returns a true or false, based on whether ' a winner exists. If isWinner(mybuttons) = True ThenMsgBox(
"We have a winner: " & whoseTurn) ' !!! CALL SUBROUTINE TO CLEAR THE BUTTONS FOR THE NEXT GAME Me.clearButtons(mybuttons) End If If whoseTurn = "X" ThenwhoseTurn =
"O" ElsewhoseTurn =
"X" End If End If End Sub
' !!! FUNCTION DECLARATION - CHECK FOR WINNER !!! ' Function takes the array of buttons as an argument, and returns ' a true/false Private Function isWinner(ByVal buttons As Button()) As Boolean Dim i As Integer Dim _winner As Boolean = False For i = 0 To 2 ' Check for vertical winner If buttons(i).Text = buttons(i + 3).Text And buttons(i).Text = buttons(i + 6).Text And _ Not buttons(i).Text = "" Then_winner =
True End If ' Check for horizontal winner If buttons(3 * i).Text = buttons(3 * i + 1).Text And buttons(3 * i).Text = buttons(3 * i + 2).Text And _ Not buttons(3 * i).Text = "" Then_winner =
True End If Next ' Add code to check for diagonal winner Return _winner End Function
' SUBROUTINE - CLEARS BUTTONS Private Sub clearButtons(ByVal buttons As Button()) Dim _button As Button ' Make certain that the text for each button is empty For Each _button In mybuttons_button.Text =
"" Next End SubEnd
Class