Skip to contentMethod: hashCode()
      1: package de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.probability;
2: 
3: import java.util.Optional;
4: import java.util.Random;
5: 
6: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaPlayer;
7: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaState;
8: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaStrategy;
9: import de.fhdw.gaming.ipspiel23.dilemma.moves.IDilemmaMove;
10: import de.fhdw.gaming.ipspiel23.dilemma.moves.IDilemmaMoveFactory;
11: import de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.DilemmaStrategy;
12: 
13: /**
14:  * Implements {@link IDilemmaStrategy} by cooperating with a fixed probability.
15:  */
16: public class DilemmaProbabilityStrategy extends DilemmaStrategy {
17: 
18:     /**
19:      * Fixed probability with which this strategy cooperates.
20:      */
21:     static final double PROBABILITY_FOR_COOPERATION = 0.75d;
22:     
23:     /**
24:      * A portal to the realm of entropy and unexpectedness.
25:      */
26:     private static final Random RANDOM = new Random();
27: 
28:     /**
29:      * Creates an {@link DilemmaCooperateStrategy}.
30:      *
31:      * @param moveFactory The factory for creating Dilemma moves.
32:      */
33:     DilemmaProbabilityStrategy(final IDilemmaMoveFactory moveFactory) {
34:         super(moveFactory);
35:     }
36:     
37:     @Override
38:     public Optional<IDilemmaMove> computeNextMove(final int gameId,
39:             final IDilemmaPlayer player,
40:             final IDilemmaState state) {
41:         return Optional.of(RANDOM.nextDouble() <= PROBABILITY_FOR_COOPERATION
42:             ? getMoveFactory().createCooperateMove()
43:             : getMoveFactory().createDefectMove());
44:     }
45: 
46:     @Override
47:     public String toString() {
48:         return DilemmaProbabilityStrategy.class.getSimpleName();
49:     }
50: 
51:     @Override
52:     public boolean equals(final Object object) {
53:         if (object instanceof DilemmaProbabilityStrategy) {
54:             final DilemmaProbabilityStrategy probabilityStrategy = (DilemmaProbabilityStrategy) object;
55:             return this.toString().equals(probabilityStrategy.toString());
56:         } else {
57:             return false;
58:         }
59:     }
60:     
61:     @Override
62:     public int hashCode() {
63:         return super.hashCode() * this.toString().hashCode();
64:     }
65: }