package TicTacToe.console;

import TicTacToe.board.Board;
import TicTacToe.player.Player;
import TicTacToe.player.Position;

import java.util.*;

/**
 * Created by bubblebitoey on 2/23/2017 AD.
 */
public class Game implements Runnable {
	private Board board;
	private int currentPlayer;
	private Player[] players;
	
	public Game(Board board, Player... players) {
		this.board = board;
		this.players = players;
	}
	
	private Position input() {
		Scanner input = new Scanner(System.in);
		
		System.out.println();
		System.out.println(String.format("Player %s(%s)'s turn.", getCurrentPlayer(), getCurrentPlayer().getSymbol()));
		try {
			System.out.print("Please select row: ");
			int row = input.nextInt();
			System.out.print("Please select column: ");
			int col = input.nextInt();
			
			return new Position(row, col);
		} catch (InputMismatchException e) {
			return null;
		}
	}
	
	private void swapPlayer() {
		// next
		currentPlayer++;
		// if out of player length
		if (currentPlayer == players.length) currentPlayer = 0;
	}
	
	private Player getCurrentPlayer() {
		return players[currentPlayer];
	}
	
	@Override
	public void run() {
		System.out.println(board);
		while (board.state == Board.Condition.PLAYING) {
			Position p = input();
			if (p == null) System.out.println("Accepted only number");
			boolean isSuccess = board.insert(getCurrentPlayer(), p);
			if (isSuccess) {
				System.out.println(board);
				if (board.state == Board.Condition.PLAYING) swapPlayer();
			} else {
				System.out.println("Invalid row or column");
			}
		}
		
		if (board.state == Board.Condition.DRAW) {
			System.out.println("Draw.");
		} else if (board.state == Board.Condition.WIN) {
			System.out.println(board.winner + " winner.");
		}
	}
}

@@ -1,25 +1,19 @@
 package 
-TicTacToe
+com
 .console
@@ -22,25 +22,19 @@
 %0Aimport 
-TicTacToe
+com
 .board.B
@@ -42,33 +42,27 @@
 ard;%0Aimport 
-TicTacToe
+com
 .player.Play
@@ -76,17 +76,11 @@
 ort 
-TicTacToe
+com
 .pla
@@ -94,16 +94,44 @@
 ition;%0A%0A
+import java.io.InputStream;%0A
 import j
@@ -235,16 +235,111 @@
 nable %7B%0A
+%09public static final InputStream DEFAULT_INPUT_STREAM = System.in;%0A%09%0A%09private Scanner input;%0A%09%0A
 %09private
@@ -456,16 +456,205 @@
 yers) %7B%0A
+%09%09input = new Scanner(DEFAULT_INPUT_STREAM);%0A%09%09this.board = board;%0A%09%09this.players = players;%0A%09%7D%0A%09%0A%09public Game(InputStream s, Board board, Player... players) %7B%0A%09%09input = new Scanner(s);%0A%09%09%0A
 %09%09this.b
@@ -730,77 +730,8 @@
 ) %7B%0A
-%09%09Scanner input = new Scanner(System.in);%0A%09%09%0A%09%09System.out.println();%0A
 %09%09Sy
@@ -922,16 +922,20 @@
 tInt();%0A
+%09%09%09%0A
 %09%09%09Syste
@@ -1417,33 +1417,29 @@
 te == Board.
-Condition
+State
 .PLAYING) %7B%0A
@@ -1665,25 +1665,21 @@
 = Board.
-Condition
+State
 .PLAYING
@@ -1794,25 +1794,21 @@
 = Board.
-Condition
+State
 .DRAW) %7B
@@ -1878,17 +1878,13 @@
 ard.
-Condition
+State
 .WIN

refactor some naming, add optional inputStream to input value to play the game