package is.ru.tictactoe;

import java.awt.Point;

public class Application {
	private Game game;
	private UI ui;

	public Application (Game game, UI ui) {
		this.game = game;
		this.ui = ui;
	}

	public void runApp () {
		while (true) {
			if (quitApp(getMenuChoice())) {
				break;
			}
			else {
				playGame();
				ui.drawBoard(game.getBoard());
				if (game.getWinner()) {
					game.switchPlayer();
					(game.getCurrPlayer()).incrementScore();
					ui.printWinner(game.getCurrPlayer());
				}
				else if (game.getDraw()) {
					ui.printDraw();
				}
				ui.printScore(game.getPlayerO(), game.getPlayerX());
			}
		}
	}

	private int getMenuChoice () {
		int choice;
		boolean validChoice;
		do {
			choice = ui.getChoice();
			validChoice = game.validMenuInput(choice);
			if (!validChoice) {
				ui.printInvalidInput(0, 1);
			}
		} while(!validChoice);
		return choice;
	}

	private void playGame () {
		game.newGame();
		while (!game.getWinner() && !game.getDraw()) {
			ui.drawBoard(game.getBoard());
			int input;
			boolean validInput, isFree;
			do{
				input = ui.getNextMove(game.getCurrPlayer());
				validInput = game.validInput(input);
				isFree = game.getBoard().isFree(game.convertToPoint(input));
				if(!isFree) {
					ui.printFieldTaken();
				}
				else if (!validInput) {
					ui.printInvalidInput(1, 9);
				}
			} while (!validInput);
			game.makeMove(input);
			game.switchPlayer();
		}
	}
	private boolean quitApp (int choice) {
		return (choice == 0);
	}


	public static void main (String[] args) {
		Game game = new Game();
		UI ui = new UI();
		Application app = new Application(game,ui);

		app.runApp();
	}
}

@@ -653,19 +653,22 @@
  () %7B%0A%09%09
+Str
 in
-t
+g
  choice;
@@ -691,16 +691,27 @@
 Choice;%0A
+%09%09int num;%0A
 %09%09do %7B%0A%09
@@ -737,16 +737,53 @@
 oice();%0A
+%09%09%09num = convertStringToInt(choice);%0A
 %09%09%09valid
@@ -811,22 +811,19 @@
 nuInput(
-choice
+num
 );%0A%09%09%09if
@@ -911,22 +911,19 @@
 %09return 
-choice
+num
 ;%0A%09%7D%0A%0A%09p
@@ -1052,19 +1052,22 @@
 ());%0A%09%09%09
+Str
 in
-t
+g
  input;%0A
@@ -1097,16 +1097,28 @@
 isFree;%0A
+%09%09%09int num;%0A
 %09%09%09do%7B%0A%09
@@ -1162,24 +1162,61 @@
 rPlayer());%0A
+%09%09%09%09num = convertStringToInt(input);%0A
 %09%09%09%09validInp
@@ -1236,24 +1236,45 @@
 idInput(
-i
+num);%0A%09%09%09%09if (validI
 nput)
-;%0A
+ %7B%0A%09
 %09%09%09%09isFr
@@ -1325,17 +1325,16 @@
 int(
-input
+num
 ));%0A
+%09
 %09%09%09%09
@@ -1343,24 +1343,25 @@
 (!isFree) %7B%0A
+%09
 %09%09%09%09%09ui.prin
@@ -1371,24 +1371,31 @@
 eldTaken();%0A
+%09%09%09%09%09%7D%0A
 %09%09%09%09%7D%0A%09%09%09%09el
@@ -1400,25 +1400,8 @@
 else
- if (!validInput)
  %7B%0A%09
@@ -1481,21 +1481,19 @@
 akeMove(
-input
+num
 );%0A%09%09%09ga
@@ -1518,16 +1518,118 @@
 %0A%09%09%7D%0A%09%7D%0A
+%0A%09private int convertStringToInt(String str) %7B%0A%09%09return Character.getNumericValue(str.charAt(0));%0A%09%7D%0A%0A
 %09private

convert first char of string input from ui to int and fix minor bugs