Skip to contentMethod: computeNextMove(int, FzgPlayer, FzgState)
      1: package de.fhdw.gaming.ipspiel23.freizeitgestaltung.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.freizeitgestaltung.domain.FzgPlayer;
8: import de.fhdw.gaming.ipspiel23.freizeitgestaltung.domain.FzgState;
9: import de.fhdw.gaming.ipspiel23.freizeitgestaltung.domain.FzgStrategy;
10: import de.fhdw.gaming.ipspiel23.freizeitgestaltung.move.Answer;
11: import de.fhdw.gaming.ipspiel23.freizeitgestaltung.move.AnswerOptions;
12: import de.fhdw.gaming.ipspiel23.freizeitgestaltung.move.factory.AnswerFactory;
13: 
14: /**
15:  * Implements a mixed strategy which aims to create a balance between the players outcomes. 
16:  */
17: public class MixedStrategy implements FzgStrategy {
18: 
19:     /**
20:      * Random number generator for implementing a mixed strategy.
21:      */
22:     private static final Random RANDOM = new Random();
23: 
24:     /**
25:      * Factory for creating answers.
26:      */
27:     private final AnswerFactory answerFactory;
28:     
29:     /**
30:      * Constructor.
31:      * @param answerFactory {@link AnswerFactory} factory to create the answer.
32:      */
33:     public MixedStrategy(final AnswerFactory answerFactory) {
34:         this.answerFactory = answerFactory;
35:     }
36:     
37:     @Override
38:     public Optional<Answer> computeNextMove(final int gameId, final FzgPlayer player, final FzgState state)
39:             throws GameException, InterruptedException {
40:         final FzgPlayer otherPlayer;
41:•        if (state.getFirstPlayer().equals(player)) {
42:             otherPlayer = state.getSecondPlayer();
43:         } else {
44:             otherPlayer = state.getFirstPlayer();
45:         }
46:         
47:         final var outcomes = otherPlayer.getPossibleOutcomes();
48:         final double a11 = outcomes.get(AnswerOptions.CINEMA).get(AnswerOptions.CINEMA);
49:         final double a12 = outcomes.get(AnswerOptions.CINEMA).get(AnswerOptions.FOOTBALL);
50:         final double a21 = outcomes.get(AnswerOptions.FOOTBALL).get(AnswerOptions.CINEMA);
51:         final double a22 = outcomes.get(AnswerOptions.FOOTBALL).get(AnswerOptions.FOOTBALL);
52:         
53:         final double cinemaLikeliness = (a12 + a22) / (a11 - a12 - a21 + a22);
54:         final double percentage = RANDOM.nextDouble();
55:•        if (percentage <= cinemaLikeliness) {
56:             return Optional.of(this.answerFactory.createCinemaAnswer());
57:         }
58:         return Optional.of(this.answerFactory.createFootballAnswer());
59:     }
60:     
61:     @Override
62:     public String toString() {
63:         return this.getClass().getSimpleName();
64:     }
65: }