import java.util.*; import java.io.*; public class Player { // 入力のための Reader private BufferedReader reader; public Player(){ reader = new BufferedReader(new InputStreamReader(System.in)); } // 次の手を提示する public PileMovementInfo nextStep(List towers) { while(true){ String inputText = null; try{ System.out.print("どこのリングを動かしますか? [0-2] "); inputText = reader.readLine(); int source = Integer.parseInt(inputText); System.out.print("どこへ動かしますか? [0-2] "); inputText = reader.readLine(); int destination = Integer.parseInt(inputText); if(isMovable(towers, source, destination)){ return new PileMovementInfo(source, destination); }else{ System.out.println("動かせません。もう一度入力してください。"); continue; } }catch(IOException ex){ ex.printStackTrace(); System.exit(1); }catch(NumberFormatException ex){ System.out.println("入力が間違っています。もう一度入力してください。"); continue; } } } // 円盤が移動できるかどうかのチェック private boolean isMovable(List towers, int source, int destination){ try{ // 移動元の塔に円盤がなければ false if(((Stack)towers.get(source)).empty()){ return false; } }catch(IndexOutOfBoundsException ex){ // 移動元の塔がなければ false return false; } Integer diameterSource = (Integer)((Stack)towers.get(source)).peek(); try{ // 移動先の塔に円盤がなければ true if(((Stack)towers.get(destination)).empty()){ return true; } }catch(IndexOutOfBoundsException ex){ // 移動先の塔がなければ false return false; } Integer diameterDestination = (Integer)((Stack)towers.get(destination)).peek(); // 円盤の大きさの比較 if(diameterSource.intValue() < diameterDestination.intValue()){ return true; }else{ return false; } } }