Package squaresgame

Source Code of squaresgame.HumanPlayer

package squaresgame;

import framework.event.Event;
import framework.event.EventListener;
import framework.event.EventSystem;
import generators.CursorGenerator.PlayerClickedOnLineEvent;

public class HumanPlayer extends Player implements EventListener{

  public HumanPlayer(int id, String name) {
    super(id, name);
  }

  private Line nextMove = null;
  private static final int sleepTime = 100;

  @Override
  public Line getNextMove(Grid grid) {
    if(grid != null){
      grid.unlock();
      nextMove = null;

      EventSystem.getInstance().registerEventListener(this, "PlayerClickedOnLine", null);

      while(nextMove == null){
        try {
          Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      grid.lock();
      return nextMove;
    }
    return null;
  }

  @Override
  public void onEvent(Event e) {
    if(e != null && e instanceof PlayerClickedOnLineEvent && e.getSender() != null){
      PlayerClickedOnLineEvent clickEvent = (PlayerClickedOnLineEvent) e;
      if(clickEvent.getLine() != null){
        EventSystem.getInstance().removeListener(this);
        nextMove = clickEvent.getLine();
      }
    }
  }


  @Override
  public int getUniqueID() {
    return getId();
  }

  @Override
  public String getType() {
    return "Player";
  }
}
TOP

Related Classes of squaresgame.HumanPlayer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.