package com.zyy.pattern.service;import com.zyy.pattern.beans.Hand;publicinterfaceStrategy{
Hand nextHand();voidstudy(boolean win);}
WinningStrategy 類:
package com.zyy.pattern.beans;import java.util.Random;import com.zyy.pattern.service.Strategy;publicclassWinningStrategyimplementsStrategy{private Random random;privateboolean won =false;private Hand prevHand;publicWinningStrategy(long seed){
random =newRandom(seed);}@Overridepublic Hand nextHand(){if(!won){
prevHand = Hand.getHand(random.nextInt(3));}return prevHand;}@Overridepublicvoidstudy(boolean win){
won = win;}}
ProbStrategy 類:
package com.zyy.pattern.beans;import java.util.Random;import com.zyy.pattern.service.Strategy;publicclassProbStrategyimplementsStrategy{private Random random;privateint prevHandValue =0;privateint currentHandValue =0;privateint[][] history={{1,1,1},{1,1,1},{1,1,1}};publicProbStrategy(long seed){
random =newRandom(seed);}@Overridepublic Hand nextHand(){int bet = random.nextInt(getSum(currentHandValue));int handValue =0;if(bet < history[currentHandValue][0]){
handValue =0;}elseif(bet <(history[currentHandValue][0]+ history[currentHandValue][1])){
handValue =1;}else{
handValue =2;}return Hand.getHand(handValue);}privateintgetSum(int currentHandValue){int sum =0;for(int i =0;i <3;i++){
sum += history[currentHandValue][i];}return sum;}@Overridepublicvoidstudy(boolean win){if(win){
history[prevHandValue][currentHandValue]++;}else{
history[prevHandValue][(currentHandValue +1)%3]++;
history[prevHandValue][(currentHandValue +2)%3]++;}}}