Skip to content

Content of file C4GSTKopplung.java

package de.fhdw.gaming.ipspiel23.c4.gststrategy;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;

import de.fhdw.gaming.core.domain.Move;
import de.fhdw.gaming.core.domain.PlayerState;
import de.fhdw.gaming.ipspiel23.c4.domain.IC4Player;
import de.fhdw.gaming.ipspiel23.c4.domain.IC4Position;
import de.fhdw.gaming.ipspiel23.c4.domain.IC4State;
import de.fhdw.gaming.ipspiel23.c4.moves.factory.IC4MoveFactory;
import de.fhdw.gaming.ipspiel23.gst.domain.IKopplung;

/**
 * The C4-GST-Kopplung.
 */
public class C4GSTKopplung implements IKopplung<IC4Player, IC4State> {

    
    /**
     * TODO: Strategy-Pattern
     */
    private SimpleC4GSTEvaluation evaluationStrategy;
Private field 'evaluationStrategy' could be made final; it is only initialized in the declaration or constructor.
Reports non-final fields whose value never changes once object initialization ends, and hence may be marked final. Note that this rule does not enforce that the field value be deeply immutable itself. An object can still have mutable state, even if all its member fields are declared final. This is referred to as shallow immutability. For more information on mutability, see *Effective Java, 3rd Edition, Item 17: Minimize mutability*. Limitations: We can only check private fields for now.
    
        

public class Foo {
  private int x; // could be final
  public Foo() {
      x = 7;
  }
  public void foo() {
     int a = x + 2;
  }
}

        
    
See PMD documentation.
/** * The Movefactory. */ private IC4MoveFactory moveFactory; /** * Ctr. * @param moveFactory */ public C4GSTKopplung(IC4MoveFactory moveFactory) { this.moveFactory = moveFactory; this.evaluationStrategy = new SimpleC4GSTEvaluation(); } @Override public Optional<Collection<Move<IC4Player, IC4State>>> getPossibleMoves(IC4State state) { // seems to work as intended final IC4Position[] positions = state.getBoard().getLegalPositions(); final Collection<Move<IC4Player, IC4State>> possibleMoves = new ArrayList<>(); for (IC4Position position : positions) { possibleMoves.add(moveFactory.createMove(state.getCurrentPlayer(), position)); } // return Optional.of(possibleMoves); } @Override public Optional<Integer> evalState(IC4State state) { return Optional.of(evaluationStrategy.evalC4State(state)); } @Override public Optional<IC4Player> getCurrentPlayer(IC4State state) { return Optional.of(state.getCurrentPlayer()); } @Override public Optional<Boolean> getIsGameOver(IC4State state) { return Optional.of(state.getPlayers().values().stream() .filter(p -> p.getState() == PlayerState.PLAYING) .findAny() .isEmpty() ); //return Optional.of(false); //return Optional.of(state.getPlayers().values() // .stream() // .filter(p -> p.getOutcome().get() > 0) // .count() > 0); } }