Skip to contentMethod: SSPSayEquilibriumStrategy(SSPMoveFactory)
      1: package de.schereSteinPapier.strategy;
2: 
3: import java.util.Optional;
4: import java.util.Random;
5: 
6: import de.schereSteinPapier.domain.SSPPlayer;
7: import de.schereSteinPapier.domain.SSPState;
8: import de.schereSteinPapier.domain.SSPStrategy;
9: import de.schereSteinPapier.moves.SSPMove;
10: import de.schereSteinPapier.moves.factory.SSPMoveFactory;
11: 
12: /**
13:  * Implements {@link SSPStrategy} by saying "Papier, Stein, Schere" with a 1/3 chance per entry.
14:  */
15: public class SSPSayEquilibriumStrategy implements SSPStrategy {
16: 
17:     /**
18:      * The random number generator.
19:      */
20:     private static final Random RANDOM = new Random();
21: 
22:     /**
23:      * The factory for creating SSP moves.
24:      */
25:     private final SSPMoveFactory moveFactory;
26: 
27:     /**
28:      * Creates an {@link SSPSayEquilibriumStrategy}.
29:      *
30:      * @param moveFactory The factory for creating SSP moves.
31:      */
32:     SSPSayEquilibriumStrategy(final SSPMoveFactory moveFactory) {
33:         this.moveFactory = moveFactory;
34:     }
35: 
36:     /**
37:      * returns "SCHERE", "STEIN", "PAPIER" with a chance of 1/3 each.
38:      */
39:     public Optional<SSPMove> weightedRandomAnswer() {
40:         // generates a random number between 0 and 2 inclusively
41:         final int weight = RANDOM.nextInt(3);
42:         if (weight < 1) {
43:             return Optional.of(this.moveFactory.createSchereMove());
44:         }
45:         if (weight < 2) {
46:             return Optional.of(this.moveFactory.createSteinMove());
47:         }
48:         return Optional.of(this.moveFactory.createPapierMove());
49:     }
50: 
51:     @Override
52:     public Optional<SSPMove> computeNextMove(final int gameId, final SSPPlayer player, final SSPState state) {
53:         return this.weightedRandomAnswer();
54:     }
55: 
56:     @Override
57:     public String toString() {
58:         return SSPSayEquilibriumStrategy.class.getSimpleName();
59:     }
60: }