自定义状态机设计(棋盘)

uyto3xhc  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(201)

给定一个棋盘,如果玩家从一个位置移动到另一个位置,我必须在某些条件下改变游戏状态。为此,我提出了自己的状态机实现。

public enum GameState {
  CREATED,
  IN_PROGRESS,
  BLACK_WIN,
  WHITE_WIN,
  STALEMATE,
  RESIGNATION;

  static Map<GameState, List<GameState>> moveToState = new HashMap<>();

  static {
    moveToState.put(CREATED, Arrays.asList(IN_PROGRESS,RESIGNATION));
    moveToState.put(IN_PROGRESS,Arrays.asList(BLACK_WIN,WHITE_WIN,STALEMATE,RESIGNATION));
    moveToState.put(BLACK_WIN, Collections.emptyList());
    moveToState.put(WHITE_WIN, Collections.emptyList());
    moveToState.put(STALEMATE, Collections.emptyList());
    moveToState.put(RESIGNATION, Collections.emptyList());
  }

  public boolean canMoveToState(GameState fromGameState,GameState toGameState){
    List<GameState> states = moveToState
        .getOrDefault(fromGameState, new ArrayList<>());
    return states.contains(toGameState);
  }
}

所以我的问题是这是实现状态机的正确方法,如果不是的话,这种实现可能有什么缺点?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题