Package: MuenzwurfChooseRandomStrategy
MuenzwurfChooseRandomStrategy
| name | instruction | branch | complexity | line | method | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| MuenzwurfChooseRandomStrategy(MuenzwurfMoveFactory) | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
| computeNextMove(int, MuenzwurfPlayer, MuenzwurfState, long) | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
| toString() | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
Coverage
1: /*
2:  * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3:  *
4:  * This file is part of ipspiel24-demo.
5:  *
6:  * Ipspiel24-demo 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-demo 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-demo. If not, see
14:  * <http://www.gnu.org/licenses/>.
15:  */
16: package de.fhdw.gaming.ipspiel24.muenzwurf.core.strategy;
17: 
18: import java.util.Optional;
19: import java.util.Random;
20: 
21: import de.fhdw.gaming.ipspiel24.muenzwurf.core.domain.MuenzwurfPlayer;
22: import de.fhdw.gaming.ipspiel24.muenzwurf.core.domain.MuenzwurfState;
23: import de.fhdw.gaming.ipspiel24.muenzwurf.core.domain.MuenzwurfStrategy;
24: import de.fhdw.gaming.ipspiel24.muenzwurf.core.moves.MuenzwurfMove;
25: import de.fhdw.gaming.ipspiel24.muenzwurf.core.moves.factory.MuenzwurfMoveFactory;
26: 
27: /**
28:  * Implements {@link DemoStrategy} by always saying "no".
29:  */
30: public final class MuenzwurfChooseRandomStrategy implements MuenzwurfStrategy {
31: 
32:     /**
33:      * The factory for creating Demo moves.
34:      */
35:     private final MuenzwurfMoveFactory moveFactory;
36:     
37:     /**
38:      * Random-object to generate a random choice.
39:      */
40:     private final Random randomGenerator = new Random();
41: 
42:     /**
43:      * Creates an {@link MuenzwurfChooseRandomStrategy}.
44:      *
45:      * @param moveFactory The factory for creating Demo moves.
46:      */
47:     MuenzwurfChooseRandomStrategy(final MuenzwurfMoveFactory moveFactory) {
48:         this.moveFactory = moveFactory;
49:     }
50: 
51:     @Override
52:     public Optional<MuenzwurfMove> computeNextMove(
53:             final int gameId,
54:             final MuenzwurfPlayer player,
55:             final MuenzwurfState state,
56:             final long maxComputationTimePerMove) {
57:         
58:         final int randomNumber = randomGenerator.nextInt(3);
59:         
60:•        if (randomNumber == 0) {
61:             return Optional.of(this.moveFactory.createHeadsMove()); 
62:•        } else if (randomNumber == 1) { 
63:             return Optional.of(this.moveFactory.createTailsMove()); 
64:•        } else if (randomNumber == 2) { 
65:             return Optional.of(this.moveFactory.createEdgeMove()); 
66:         } else  {
67:             throw new IndexOutOfBoundsException();
68:         }
69:         
70:     }
71: 
72:     @Override
73:     public String toString() {
74:         return MuenzwurfChooseRandomStrategy.class.getSimpleName();
75:     }
76: }