ActionManager.java

1
package com.takenoko.engine;
2
3
import com.takenoko.actions.Action;
4
import com.takenoko.actions.ActionResult;
5
import com.takenoko.actions.DefaultAction;
6
import com.takenoko.actions.actors.MoveGardenerAction;
7
import com.takenoko.actions.actors.MovePandaAction;
8
import com.takenoko.actions.annotations.ActionAnnotation;
9
import com.takenoko.actions.annotations.ActionCanBePlayedMultipleTimesPerTurn;
10
import com.takenoko.actions.annotations.ActionType;
11
import com.takenoko.actions.improvement.ApplyImprovementFromInventoryAction;
12
import com.takenoko.actions.irrigation.DrawIrrigationAction;
13
import com.takenoko.actions.irrigation.PlaceIrrigationFromInventoryAction;
14
import com.takenoko.actions.objective.DrawObjectiveAction;
15
import com.takenoko.actions.objective.RedeemObjectiveAction;
16
import com.takenoko.actions.tile.DrawTileAction;
17
import com.takenoko.weather.Windy;
18
import java.util.ArrayList;
19
import java.util.List;
20
import java.util.Objects;
21
22
/** This class is used to manage the actions of a bot. */
23
public class ActionManager {
24
    private static final int DEFAULT_NUMBER_OF_ACTIONS = 2;
25
26
    private static final List<Class<? extends DefaultAction>> DEFAULT_AVAILABLE_ACTIONS =
27
            List.of(
28
                    DrawObjectiveAction.class,
29
                    DrawTileAction.class,
30
                    DrawIrrigationAction.class,
31
                    MoveGardenerAction.class,
32
                    MovePandaAction.class,
33
                    RedeemObjectiveAction.class,
34
                    PlaceIrrigationFromInventoryAction.class,
35
                    ApplyImprovementFromInventoryAction.class);
36
37
    private int numberOfActions;
38
    private List<Class<? extends Action>> availableActions;
39
    private List<Class<? extends Action>> alreadyDoneActions = new ArrayList<>();
40
41
    public ActionManager() {
42
        this.numberOfActions = DEFAULT_NUMBER_OF_ACTIONS;
43
        this.availableActions = new ArrayList<>();
44
    }
45
46
    public ActionManager(ActionManager actionManager) {
47
        this.numberOfActions = actionManager.numberOfActions;
48
        this.availableActions = new ArrayList<>(actionManager.availableActions);
49
        this.alreadyDoneActions = new ArrayList<>(actionManager.alreadyDoneActions);
50
    }
51
52
    public ActionManager(int numberOfActions, List<Class<? extends Action>> availableActions) {
53
        this.numberOfActions = numberOfActions;
54
        this.availableActions = availableActions;
55
    }
56
57
    /**
58
     * Set the number of actions the bot can do in a turn.
59
     *
60
     * @param numberOfActions
61
     */
62
    public void setNumberOfActions(int numberOfActions) {
63
        this.numberOfActions = numberOfActions;
64
    }
65
66
    /**
67
     * @return number of actions the bot can do in a turn
68
     */
69
    protected int getNumberOfActions() {
70 1 1. getNumberOfActions : replaced int return with 0 for com/takenoko/engine/ActionManager::getNumberOfActions → KILLED
        return numberOfActions;
71
    }
72
73
    /**
74
     * Return the list of available actions. If actions of FORCED type are available, only these
75
     * actions are returned else all available actions are returned.
76
     *
77
     * @return the list of available actions
78
     */
79
    public List<Class<? extends Action>> getAvailableActions() {
80
        List<Class<? extends Action>> forcedActions =
81
                availableActions.stream()
82
                        .filter(
83
                                action ->
84 2 1. lambda$getAvailableActions$0 : negated conditional → KILLED
2. lambda$getAvailableActions$0 : replaced boolean return with true for com/takenoko/engine/ActionManager::lambda$getAvailableActions$0 → KILLED
                                        action.getAnnotation(ActionAnnotation.class).value()
85
                                                == ActionType.FORCED)
86
                        .toList();
87
88 1 1. getAvailableActions : negated conditional → KILLED
        if (forcedActions.isEmpty()) {
89 1 1. getAvailableActions : replaced return value with Collections.emptyList for com/takenoko/engine/ActionManager::getAvailableActions → KILLED
            return availableActions;
90
        } else {
91 1 1. getAvailableActions : replaced return value with Collections.emptyList for com/takenoko/engine/ActionManager::getAvailableActions → KILLED
            return forcedActions;
92
        }
93
    }
94
95
    /**
96
     * add an action to the list of available actions
97
     *
98
     * @param action the action to add
99
     */
100
    public void addAvailableAction(Class<? extends Action> action) {
101
        this.availableActions.add(action);
102
    }
103
104
    /**
105
     * add a list of actions to the list of available actions
106
     *
107
     * @param actions the list of actions to add
108
     */
109
    public void addAvailableActions(List<Class<? extends Action>> actions) {
110
        this.availableActions.addAll(actions);
111
    }
112
113
    /** add an action to the number of actions to plau this turn */
114
    public void addAction() {
115 1 1. addAction : Replaced integer addition with subtraction → TIMED_OUT
        numberOfActions++;
116
    }
117
118
    /** clear the list of available actions of the FORCED type */
119
    private void clearForcedActions() {
120
        availableActions.removeIf(
121
                action ->
122 2 1. lambda$clearForcedActions$1 : negated conditional → KILLED
2. lambda$clearForcedActions$1 : replaced boolean return with true for com/takenoko/engine/ActionManager::lambda$clearForcedActions$1 → KILLED
                        action.getAnnotation(ActionAnnotation.class).value() == ActionType.FORCED);
123
    }
124
125
    /**
126
     * update an action in available actions
127
     *
128
     * @param action the action to update
129
     * @param actionResult the result of the action
130
     */
131
    public void updateAvailableActions(Action action, ActionResult actionResult) {
132
        this.availableActions.remove(action.getClass());
133
        this.alreadyDoneActions.add(action.getClass());
134 1 1. updateAvailableActions : removed call to com/takenoko/engine/ActionManager::clearForcedActions → KILLED
        this.clearForcedActions();
135 1 1. updateAvailableActions : removed call to com/takenoko/engine/ActionManager::addAvailableActions → TIMED_OUT
        this.addAvailableActions(actionResult.availableActions());
136 2 1. updateAvailableActions : Replaced integer subtraction with addition → TIMED_OUT
2. updateAvailableActions : removed call to com/takenoko/engine/ActionManager::setNumberOfActions → TIMED_OUT
        this.setNumberOfActions(this.getNumberOfActions() - actionResult.cost());
137
    }
138
139
    /**
140
     * update to the defaults actions
141
     *
142
     * @param board the board
143
     */
144
    public void updateDefaultActions(Board board, BotState botState) {
145
        availableActions.removeIf(
146
                action ->
147 2 1. lambda$updateDefaultActions$2 : replaced boolean return with true for com/takenoko/engine/ActionManager::lambda$updateDefaultActions$2 → TIMED_OUT
2. lambda$updateDefaultActions$2 : negated conditional → KILLED
                        action.getAnnotation(ActionAnnotation.class).value() == ActionType.DEFAULT);
148
        DEFAULT_AVAILABLE_ACTIONS.stream()
149
                .filter(
150
                        actionClass ->
151 2 1. lambda$updateDefaultActions$4 : negated conditional → KILLED
2. lambda$updateDefaultActions$4 : replaced boolean return with true for com/takenoko/engine/ActionManager::lambda$updateDefaultActions$4 → KILLED
                                DefaultAction.canBePlayed(board, botState, actionClass)
152 1 1. lambda$updateDefaultActions$4 : negated conditional → TIMED_OUT
                                        && (actionClass.isAnnotationPresent(
153
                                                        ActionCanBePlayedMultipleTimesPerTurn.class)
154 1 1. lambda$updateDefaultActions$4 : negated conditional → KILLED
                                                || !alreadyDoneActions.contains(actionClass)
155
                                                || board.getWeather()
156 2 1. lambda$updateDefaultActions$3 : replaced Boolean return with False for com/takenoko/engine/ActionManager::lambda$updateDefaultActions$3 → TIMED_OUT
2. lambda$updateDefaultActions$3 : replaced Boolean return with True for com/takenoko/engine/ActionManager::lambda$updateDefaultActions$3 → TIMED_OUT
                                                        .map(v -> v.getClass().equals(Windy.class))
157 1 1. lambda$updateDefaultActions$4 : negated conditional → TIMED_OUT
                                                        .orElse(false)))
158 1 1. updateDefaultActions : removed call to java/util/stream/Stream::forEach → KILLED
                .forEach(actionClass -> availableActions.add(actionClass));
159
    }
160
161
    /**
162
     * reset the available actions
163
     *
164
     * @param board the board
165
     */
166
    public void resetAvailableActions(Board board, BotState botState) {
167
        this.availableActions = new ArrayList<>(DEFAULT_AVAILABLE_ACTIONS);
168
        this.alreadyDoneActions = new ArrayList<>();
169 1 1. resetAvailableActions : removed call to com/takenoko/engine/ActionManager::updateDefaultActions → TIMED_OUT
        updateDefaultActions(board, botState);
170
    }
171
172
    /**
173
     * get the list of already done actions
174
     *
175
     * @return the list of already done actions
176
     */
177
    public List<Class<? extends Action>> getAlreadyDoneActions() {
178 1 1. getAlreadyDoneActions : replaced return value with Collections.emptyList for com/takenoko/engine/ActionManager::getAlreadyDoneActions → TIMED_OUT
        return new ArrayList<>(alreadyDoneActions);
179
    }
180
181
    public void reset() {
182
        this.numberOfActions = DEFAULT_NUMBER_OF_ACTIONS;
183
        this.availableActions = new ArrayList<>();
184
        this.alreadyDoneActions = new ArrayList<>();
185
    }
186
187
    @Override
188
    public boolean equals(Object o) {
189 2 1. equals : negated conditional → SURVIVED
2. equals : replaced boolean return with false for com/takenoko/engine/ActionManager::equals → NO_COVERAGE
        if (this == o) return true;
190 3 1. equals : replaced boolean return with true for com/takenoko/engine/ActionManager::equals → NO_COVERAGE
2. equals : negated conditional → KILLED
3. equals : negated conditional → KILLED
        if (o == null || getClass() != o.getClass()) return false;
191
        ActionManager that = (ActionManager) o;
192 2 1. equals : replaced boolean return with true for com/takenoko/engine/ActionManager::equals → SURVIVED
2. equals : negated conditional → KILLED
        return numberOfActions == that.numberOfActions
193 1 1. equals : negated conditional → KILLED
                && Objects.equals(availableActions, that.availableActions)
194 1 1. equals : negated conditional → KILLED
                && Objects.equals(alreadyDoneActions, that.alreadyDoneActions);
195
    }
196
197
    @Override
198
    public int hashCode() {
199 1 1. hashCode : replaced int return with 0 for com/takenoko/engine/ActionManager::hashCode → SURVIVED
        return Objects.hash(numberOfActions, availableActions, alreadyDoneActions);
200
    }
201
}

Mutations

70

1.1
Location : getNumberOfActions
Killed by : com.takenoko.bot.utils.HistoryAnalysisTest.[engine:junit-jupiter]/[class:com.takenoko.bot.utils.HistoryAnalysisTest]/[nested-class:IntegrationTest]/[method:test1()]
replaced int return with 0 for com/takenoko/engine/ActionManager::getNumberOfActions → KILLED

84

1.1
Location : lambda$getAvailableActions$0
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestGetAvailableActions]/[method:getAvailableActions_shouldReturnOnlyForcedActions()]
negated conditional → KILLED

2.2
Location : lambda$getAvailableActions$0
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestGetAvailableActions]/[method:getAvailableActions_shouldReturnOnlyForcedActions()]
replaced boolean return with true for com/takenoko/engine/ActionManager::lambda$getAvailableActions$0 → KILLED

88

1.1
Location : getAvailableActions
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestGetAvailableActions]/[method:getAvailableActions_shouldReturnOnlyForcedActions()]
negated conditional → KILLED

89

1.1
Location : getAvailableActions
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestUpdateAvailableActions]/[method:updateAvailableActions_shouldKeepPersistantActions()]
replaced return value with Collections.emptyList for com/takenoko/engine/ActionManager::getAvailableActions → KILLED

91

1.1
Location : getAvailableActions
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestGetAvailableActions]/[method:getAvailableActions_shouldReturnOnlyForcedActions()]
replaced return value with Collections.emptyList for com/takenoko/engine/ActionManager::getAvailableActions → KILLED

115

1.1
Location : addAction
Killed by : none
Replaced integer addition with subtraction → TIMED_OUT

122

1.1
Location : lambda$clearForcedActions$1
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestUpdateAvailableActions]/[method:updateAvailableActions_shouldKeepPersistantActions()]
negated conditional → KILLED

2.2
Location : lambda$clearForcedActions$1
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestUpdateAvailableActions]/[method:updateAvailableActions_shouldKeepPersistantActions()]
replaced boolean return with true for com/takenoko/engine/ActionManager::lambda$clearForcedActions$1 → KILLED

134

1.1
Location : updateAvailableActions
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestUpdateAvailableActions]/[method:updateAvailableActions_shouldRemoveForcedActions()]
removed call to com/takenoko/engine/ActionManager::clearForcedActions → KILLED

135

1.1
Location : updateAvailableActions
Killed by : none
removed call to com/takenoko/engine/ActionManager::addAvailableActions → TIMED_OUT

136

1.1
Location : updateAvailableActions
Killed by : none
Replaced integer subtraction with addition → TIMED_OUT

2.2
Location : updateAvailableActions
Killed by : none
removed call to com/takenoko/engine/ActionManager::setNumberOfActions → TIMED_OUT

147

1.1
Location : lambda$updateDefaultActions$2
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestUpdateAvailableActions]/[method:updateAvailableActions_shouldNotRemoveAlreadyExecutedActionsIfWindy()]
negated conditional → KILLED

2.2
Location : lambda$updateDefaultActions$2
Killed by : none
replaced boolean return with true for com/takenoko/engine/ActionManager::lambda$updateDefaultActions$2 → TIMED_OUT

151

1.1
Location : lambda$updateDefaultActions$4
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestGetObjectiveScore]/[method:update_should_addDrawObjectiveActionIfCanDrawObjective()]
negated conditional → KILLED

2.2
Location : lambda$updateDefaultActions$4
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestUpdateAvailableActions]/[method:updateAvailableActions_shouldNotRemoveAlreadyExecutedActionsIfWindy()]
replaced boolean return with true for com/takenoko/engine/ActionManager::lambda$updateDefaultActions$4 → KILLED

152

1.1
Location : lambda$updateDefaultActions$4
Killed by : none
negated conditional → TIMED_OUT

154

1.1
Location : lambda$updateDefaultActions$4
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestGetObjectiveScore]/[method:update_should_addDrawObjectiveActionIfCanDrawObjective()]
negated conditional → KILLED

156

1.1
Location : lambda$updateDefaultActions$3
Killed by : none
replaced Boolean return with False for com/takenoko/engine/ActionManager::lambda$updateDefaultActions$3 → TIMED_OUT

2.2
Location : lambda$updateDefaultActions$3
Killed by : none
replaced Boolean return with True for com/takenoko/engine/ActionManager::lambda$updateDefaultActions$3 → TIMED_OUT

157

1.1
Location : lambda$updateDefaultActions$4
Killed by : none
negated conditional → TIMED_OUT

158

1.1
Location : updateDefaultActions
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestGetObjectiveScore]/[method:update_should_addDrawObjectiveActionIfCanDrawObjective()]
removed call to java/util/stream/Stream::forEach → KILLED

169

1.1
Location : resetAvailableActions
Killed by : none
removed call to com/takenoko/engine/ActionManager::updateDefaultActions → TIMED_OUT

178

1.1
Location : getAlreadyDoneActions
Killed by : none
replaced return value with Collections.emptyList for com/takenoko/engine/ActionManager::getAlreadyDoneActions → TIMED_OUT

189

1.1
Location : equals
Killed by : none
negated conditional → SURVIVED

2.2
Location : equals
Killed by : none
replaced boolean return with false for com/takenoko/engine/ActionManager::equals → NO_COVERAGE

190

1.1
Location : equals
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestEquals]/[method:equals_shouldReturnTrueWhenEqual()]
negated conditional → KILLED

2.2
Location : equals
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestEquals]/[method:equals_shouldReturnTrueWhenEqual()]
negated conditional → KILLED

3.3
Location : equals
Killed by : none
replaced boolean return with true for com/takenoko/engine/ActionManager::equals → NO_COVERAGE

192

1.1
Location : equals
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestEquals]/[method:equals_shouldReturnTrueWhenEqual()]
negated conditional → KILLED

2.2
Location : equals
Killed by : none
replaced boolean return with true for com/takenoko/engine/ActionManager::equals → SURVIVED

193

1.1
Location : equals
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestEquals]/[method:equals_shouldReturnTrueWhenEqual()]
negated conditional → KILLED

194

1.1
Location : equals
Killed by : com.takenoko.engine.BotStateTest.[engine:junit-jupiter]/[class:com.takenoko.engine.BotStateTest]/[nested-class:TestEquals]/[method:equals_shouldReturnTrueWhenEqual()]
negated conditional → KILLED

199

1.1
Location : hashCode
Killed by : none
replaced int return with 0 for com/takenoko/engine/ActionManager::hashCode → SURVIVED

Active mutators

Tests examined


Report generated by PIT 1.8.0