Skip to content

Method: static {...}

1: package de.fhdw.gaming.ipspiel23.ssp.strategy;
2:
3: import java.util.Optional;
4: import java.util.Random;
5:
6: import de.fhdw.gaming.core.domain.GameException;
7: import de.fhdw.gaming.ipspiel23.ssp.domain.SspPlayer;
8: import de.fhdw.gaming.ipspiel23.ssp.domain.SspState;
9: import de.fhdw.gaming.ipspiel23.ssp.domain.SspStrategy;
10: import de.fhdw.gaming.ipspiel23.ssp.moves.SspMove;
11: import de.fhdw.gaming.ipspiel23.ssp.moves.factory.SspMoveFactory;
12:
13: /**
14: * Implements {@link SspStrategy} by always choosing a random Answer.
15: *
16: * @author DW
17: *
18: */
19: public final class SspMixedStrategy implements SspStrategy {
20:
21: /**
22: * The random generator used to create random SSP moves.
23: *
24: */
25: private static final Random RND = new Random();
26:
27: /**
28: * The factory for creating SSP moves.
29: */
30: private final SspMoveFactory moveFactory;
31:
32: /**
33: * Creates an {@link SspMixedStrategy}.
34: *
35: * @param moveFactory The factory for creating SSP moves.
36: */
37: SspMixedStrategy(final SspMoveFactory moveFactory) {
38: this.moveFactory = moveFactory;
39:
40: }
41:
42: /**
43: * Computes a random next move.
44: */
45: @Override
46: public Optional<SspMove> computeNextMove(final int gameId, final SspPlayer player, final SspState state)
47: throws GameException, InterruptedException {
48:
49: switch (RND.nextInt(3)) {
50: case 0:
51: return Optional.of(moveFactory.createPaperMove());
52:
53: case 1:
54: return Optional.of(moveFactory.createScissorsMove());
55:
56: case 2:
57: return Optional.of(moveFactory.createStoneMove());
58: default:
59: throw new GameException("mixedStrategy: random number outside of expectation!");
60: }
61:
62: }
63:
64: @Override
65: public String toString() {
66: return SspMixedStrategy.class.getSimpleName();
67: }
68:
69: @Override
70: public boolean equals(final Object obj) {
71: return obj != null && this.toString().equals(obj.toString());
72: }
73:
74: @Override
75: public int hashCode() {
76:
77: return super.hashCode() * this.toString().hashCode();
78: }
79: }