Skip to contentMethod: SspRandomStrategy(SspMoveFactory)
      1: /*
2:  * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3:  *
4:  * This file is part of ipspiel24-Ssp.
5:  *
6:  * Ipspiel24-Ssp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
7:  * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
8:  * version.
9:  *
10:  * Ipspiel24-Ssp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
11:  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12:  *
13:  * You should have received a copy of the GNU General Public License along with ipspiel24-Ssp. If not, see
14:  * <http://www.gnu.org/licenses/>.
15:  */
16: package de.fhdw.gaming.ipspiel24.ssp.strategy;
17: 
18: import java.util.Optional;
19: 
20: import de.fhdw.gaming.ipspiel24.ssp.domain.SspPlayer;
21: import de.fhdw.gaming.ipspiel24.ssp.domain.SspState;
22: import de.fhdw.gaming.ipspiel24.ssp.domain.SspStrategy;
23: import de.fhdw.gaming.ipspiel24.ssp.moves.SspMove;
24: import de.fhdw.gaming.ipspiel24.ssp.moves.factory.SspMoveFactory;
25: import java.util.Random;
26: 
27: /**
28:  * Implements {@link SspStrategy} by always choosing random.
29:  */
30: public final class SspRandomStrategy implements SspStrategy {
31: 
32:     /**
33:      * Generates random number.
34:      */
35:     private static Random random = new Random();
36:     /**
37:      * The factory for creating Ssp moves.
38:      */
39:     private final SspMoveFactory moveFactory;
40: 
41:     /**
42:      * Creates an {@link SspRandomStrategy}.
43:      *
44:      * @param moveFactory The factory for creating Ssp moves.
45:      */
46:     SspRandomStrategy(final SspMoveFactory moveFactory) {
47:         this.moveFactory = moveFactory;
48:     }
49: 
50:     @Override
51:     public Optional<SspMove> computeNextMove(
52:             final int gameId,
53:             final SspPlayer player,
54:             final SspState state,
55:             final long maxComputationTimePerMove) {
56: 
57:         switch (random.nextInt(3)) {
58:         case 0:
59:             return Optional.of(this.moveFactory.createPaperMove());
60:         case 1:
61:             return Optional.of(this.moveFactory.createRockMove());
62:         case 2:
63:             return Optional.of(this.moveFactory.createScissorsMove());
64:         default:
65:             throw new IllegalStateException();
66:         }
67:     }
68: 
69:     @Override
70:     public String toString() {
71:         return SspRandomStrategy.class.getSimpleName();
72:     }
73: }