GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes How to use the strategy pattern ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to use the strategy pattern ?" Watch "How to use the strategy pattern ?" New topic
Author

How to use the strategy pattern ?

dirk dj jaeckel
Greenhorn

Joined: May 31, 2012
Posts: 22
Hello,
i want to modify an application for a card game wich uses a shuffle method like this:



I now want to have the possebility to define (config) various methods of random
1 . normal random shuffling
2. start always with the same random number ( randomseed)
3. read defined cardstacks from files to have a predefined cardstack
4. whatever comes in my mind later

This behaivor i might also need for other random things like which player starts, which height is a bet and so on (open list)
so it might happen that i will give defined cards but the betting should be pure randomly
or that cards are given pure randomly but depending on the players strategie randomnumbers for bidding are always "high" "middle" or "low" (whatever that means in detail)



My questions:
should i build a Strategie for RandomStrategie which gives me the number i want ( low level) or should i interface the shuffle method and other methods extra for playerChoice and heightOfBid (highlevel)

if i do it on the lowlevel, how can i design it that its recognized if its a card shuffling or a randomnumber for a bidding ?

how would it look like when i do it on the higher level for shuffle() , getBid() and so on ?


Thanks for help

dj


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

dirk dj jaeckel wrote:My questions:
should i build a Strategie for RandomStrategie which gives me the number i want ( low level) or should i interface the shuffle method...

Doesn't sound to me like an either/or choice; but what I'd definitely advise is to use the java.util.Random class rather than Math.random(). I think you'll find that you can also set up the generator with a predefined seed.

Second: There's no need to use a Vector inside a method; it's unecessarily slow, because Vector is synchronized. Use an ArrayList.
You also don't need to add and remove elements; have a think how you might do it by swapping them instead.

As for the other methods, I really can't advise except to say that if you think there's a natural interface there, create it.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
dirk dj jaeckel
Greenhorn

Joined: May 31, 2012
Posts: 22

second is a fact i wont change it because i cant refactor the whole project of code andi want to change as little as possible

the existance of java.util.Random i know and i implemented it like that already, but then someone told me i should better use stragegies pattern for that, but how ? also kept in mind that there are more than just randomseed neeeds

what do you mean with natural interface?

regards
dj


dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
It doesn't look to me like you need the Strategy pattern. That pattern is for when you have variations in behavior, and you want to be able to invoke that behavior without having to know which variation you're invoking. Is that actually the case here? What would cause any particular version of shuffling to take place?
dirk dj jaeckel
Greenhorn

Joined: May 31, 2012
Posts: 22
Dennis Deems wrote:It doesn't look to me like you need the Strategy pattern. That pattern is for when you have variations in behavior, and you want to be able to invoke that behavior without having to know which variation you're invoking. Is that actually the case here? What would cause any particular version of shuffling to take place?

One usecase is replaying a special game or series of games after changing/training the AI Player to see if he plays the same series of games better or worse. I need this kind of evaluation to proof the quality of the AI.
For the final version this is also a nice to have that a player can replay a specific game (like some Solitär programs allow to do)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

dirk dj jaeckel wrote:One usecase is replaying a special game or series of games after changing/training the AI Player to see if he plays the same series of games better or worse. I need this kind of evaluation to proof the quality of the AI.
For the final version this is also a nice to have that a player can replay a specific game (like some Solitär programs allow to do)

Yes, but that doesn't necessarily mean re-generating the actions, such as shuffling; it simply means saving the state (although it's true that a seed is a lot smaller than storing an entire deck).

If this is for something like Bridge, I suspect that it's also going to be outweighed by the sequence of bids and tricks, dealer, vulnerability, score etc. that have to be stored in order to recreate a hand from scratch. I'm also not sure if Random provides any guarantees about the same seed providing the same results on different machines/JVMs (I suspect it does, but the documentation is silent on the subject), so you might be better off just saving the state of the deck after shuffling, or indeed, of the hands as they were dealt.

That, at least, is unambiguous, and deck state would only require 52 bytes (or 39 if you really fancy packing it).

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

dirk dj jaeckel wrote:what do you mean with natural interface?

I mean simply an interface that defines the behaviour you want. Interfaces are rarely a bad idea.

Winston
 
GeeCON Prague 2014
 
subject: How to use the strategy pattern ?