package de.fesere.tictactoe.ui;

import de.fesere.tictactoe.Board;
import de.fesere.tictactoe.Move;
import de.fesere.tictactoe.Player;
import de.fesere.tictactoe.UI;
import de.fesere.tictactoe.board.Line;

import java.io.*;
import java.util.List;

public class ConsoleUI implements UI {

    BufferedReader reader;
    PrintStream printer;
    private Board board;

    protected ConsoleUI(InputStream input, OutputStream output) {
        reader = new BufferedReader(new InputStreamReader(input));
        printer = new PrintStream(output);
    }

    public ConsoleUI() {
      this(System.in, System.out);
    }

    @Override
    public void use(Board board) {
        this.board = board;
    }

    @Override
    public void displayBoard(Board board) {
        for(Line row : board.getRows()){
            printRow(row);
        }
    }

    private void printRow(Line row) {
        printer.println("[" + row.getMarker(0) + "][" + row.getMarker(1) + "][" + row.getMarker(2) + "]");
    }

    @Override
    public void displayMoves(List<Move> moves) {
        for (int i= 0; i < moves.size(); i++) {
            Move move = moves.get(i);
            printer.println("Move (" + i + ") :" + move.getRow() + ", " + move.getColumn());
        }
    }

    @Override
    public int getSelectedMove() {
        printer.println("Your choice: ");

        int choice = -1;
        while(!isValid(choice))  {
            String input = readInput();
            choice = extract(input);
        }
        return choice;
    }

    private boolean isValid(int choice) {
        return 0 <= choice && choice < board.getPossibleMoves().size();
    }

    private int extract(String input) {
        try {
            return Integer.parseInt(input);
        }
        catch (NumberFormatException nfe) {
            return -1;
        }
    }

    private String readInput() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);

        }
    }

    @Override
    public void showWinner(Player player) {
        printer.println("Player " + player + " has won");
    }
}

@@ -336,33 +336,8 @@
 ter;
-%0A    private Board board;
 %0A%0A  
@@ -588,92 +588,8 @@
  %7D%0A%0A
-    @Override%0A    public void use(Board board) %7B%0A        this.board = board;%0A    %7D%0A%0A
     
@@ -1183,16 +1183,40 @@
 tedMove(
+List%3CMove%3E possibleMoves
 ) %7B%0A    
@@ -1250,24 +1250,71 @@
 hoice: %22);%0A%0A
+        int upperLimit = possibleMoves.size();%0A
         int 
@@ -1355,16 +1355,28 @@
 d(choice
+, upperLimit
 ))  %7B%0A  
@@ -1528,16 +1528,32 @@
 t choice
+, int upperLimit
 ) %7B%0A    
@@ -1591,39 +1591,18 @@
 e %3C 
-board.getPossibleMoves().size()
+upperLimit
 ;%0A  

Simplify the UI part #2