vb.net - running a class method from a form

Associate
Joined
15 Sep 2007
Posts
48
I am making a poker game and i am trying to get the computer to make a random move based on the options fold check call and raise.

I have a PLAYER class which when the game runs has, for example, 10 instances, which i put into an arraylist called playerList() so player1 is playerList(0), player2 is playerList(1) etc. etc.

I can get and set all values for these players, eg:

playerList(0).pChipCount = 1000
playerList(1).pChipCount = 800 etc. etc.


BUT...when i try and access the method below it doesnt work.

This is the call to it from my form:

Private Sub preFlop()

playerList(1).pAvailableMoves = "Fold" & "Check" & "Call" & "Raise"
MsgBox(playerList(1).pName & "," & playerList(1).pActive & "," & playerList(1).pStyle & "," & playerList(1).pAvailableMoves)

'IT WORKS UP UNTIL HERE
playerList(1).mMove(1)

End Sub


This is the method in my class PLAYER:

Public Sub mMove(ByVal player As Integer)
Dim RandomClass As New Random()
Dim randomNumber As Integer

Dim availableMoves(5) As String
availableMoves(1) = "Fold"
availableMoves(2) = "Check"
availableMoves(3) = "Call"
availableMoves(4) = "Raise"

'RANDOM PLAYER
If playerList(player).pStyle = "Random" Then
'if the randomly selected move is in the available move list for the player then use it
Do Until InStr(availableMoves(randomNumber), playerList(player).pAvailableMoves)
randomNumber = RandomClass.Next(1, 4)
Loop
End If

'this sets the players chosen move property
playerList(player).pMove = availableMoves(randomNumber)

End Sub


Hopefully that makes sense...VS2005 just freezes and i have to stop debuggin and after 3hours cannot work out how to fix it!!!
 
im in a generous mood today, i would do it something like this

Code:
Public Class Player
    Public Enum ValidMoves
        Fold = 1
        Check = 2
        aCall = 3
        Raise = 4
    End Enum

    Dim _AvaliableMoves As New Collection
    Public Property moves() As ValidMoves
        Get
            Dim Rand As New Random()
            Dim n As Integer = Fix(Rand.NextDouble() * _AvaliableMoves.Count) + 1

            Return _AvaliableMoves(n)
        End Get
        Set(ByVal value As ValidMoves)
            _AvaliableMoves.Add(value)
        End Set
    End Property
End Class


add your valid moves
Code:
dim aplayer as new players

        aplayer.moves = Player.ValidMoves.aCall
        aplayer.moves = Player.ValidMoves.Check
        aplayer.moves = Player.ValidMoves.Fold
        aplayer.moves = Player.ValidMoves.Raise

get your random move from the ones provided, you can even increase the chance of a particular move by adding it more than once.
Code:
       msgbox ("your move = " & [Enum].GetName(GetType(Player.ValidMoves), aplayer.moves))
 
Last edited:
im in a generous mood today, i would do it something like this

Code:
Public Class Player
    Public Enum ValidMoves
        Fold = 1
        Check = 2
        aCall = 3
        Raise = 4
    End Enum

    Dim _AvaliableMoves As New Collection
    Public Property moves() As ValidMoves
        Get
            Dim Rand As New Random()
            Dim n As Integer = Fix(Rand.NextDouble() * _AvaliableMoves.Count) + 1

            Return _AvaliableMoves(n)
        End Get
        Set(ByVal value As ValidMoves)
            _AvaliableMoves.Add(value)
        End Set
    End Property
End Class

The moves property you've written there is semantically rather dodgy. Properties should have exactly the same semantics as normal variables, and should exhibit the same behaviour to client code. Yours behaves rather oddly in that setting it just adds to a collection (which in itself isn't actually setting anything), and getting from it returns a random item from that collection. Furthermore, the property name doesn't imply this behaviour at all.

What you should be doing instead is using an AddMove method to add moves to the list, and then a GetRandomMove method to get one of them.

Also, you should be using a generic .NET list (System.Collections.Generic.List(Of T))rather than a VB Collection.

However, an even better way of doing it would be to use a flags enumeration to hold all of the information regarding moves in a single variable.
 
thanks guys. i still don't understand why i cant call:

Code:
playerList(1).mMove(1)

this should (in my head) determine a random move for player1...???
 
While I agree that the property should really be to methods (I changed my mind halfway though writing it and couldn’t be bothered to change it), I think your comments about my naming are completely irrelevant what ive called something in a quick example has no relevance to what I name things in a real program.

The collection v .net list thing, well ive migrated from VB and still have a few habits I need to change.

Hmmm flags enumeration looks interesting, didn’t know you could do that (you learn something every day).
 
The moves property you've written there is semantically rather dodgy. Properties should have exactly the same semantics as normal variables, and should exhibit the same behaviour to client code.

Are you saying that properties should behave like a simple variable assignment? If so I disagree, else what is the point of a property. A property is there to provide a public interface to a variable and to abstract any logic required in setting or getting the variable. There are even such things as internal properties for such purposes.
 
Are you saying that properties should behave like a simple variable assignment? If so I disagree, else what is the point of a property. A property is there to provide a public interface to a variable and to abstract any logic required in setting or getting the variable. There are even such things as internal properties for such purposes.

Nope, I said they should exhibit the same behaviour to client code (not necessarily behave the same internally), and that they should have the same semantics :)
 
Back
Top Bottom