|
@@ -1,114 +1,75 @@
|
|
|
-import java.util.Scanner;
|
|
|
-
|
|
|
public class Main {
|
|
|
+ public static void main(String[] args) {
|
|
|
+ PlayField field = new PlayField();
|
|
|
+ try(ConsoleReader cr = new ConsoleReader()) {
|
|
|
+ do {
|
|
|
+ doGame(cr, field);
|
|
|
+ } while(askPlayAgain(cr, field));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void doGame(ConsoleReader cr, PlayField field) {
|
|
|
+ while(true) {
|
|
|
+ int nextPos = getPlayerTurn(cr, field);
|
|
|
+
|
|
|
+ field.setPos(nextPos, 1);
|
|
|
+ field.print();
|
|
|
+ if(hasGameEnded(field, 1)) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Opponent's turn
|
|
|
+ System.out.println("Opponents turn:");
|
|
|
+ field.oppMove();
|
|
|
+ field.print();
|
|
|
+ if(hasGameEnded(field, 2)) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean hasGameEnded(PlayField field, int player) {
|
|
|
+ if(field.hasWon(player)) {
|
|
|
+ printWinner(player);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ if(field.isDraw()) {
|
|
|
+ System.out.println("Draw.");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getPlayerTurn(ConsoleReader cr, PlayField field) {
|
|
|
+ String s = "Where do you want to set your 'x'? (1-9)";
|
|
|
+ while(true) {
|
|
|
+ System.out.println(s);
|
|
|
+ int nextPos = cr.readInt(s) - 1;
|
|
|
+ if(nextPos >= 0 && nextPos <= 8 && field.isFree(nextPos)) {
|
|
|
+ return nextPos;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
-
|
|
|
- PlayField field = new PlayField();
|
|
|
- int nextPos;
|
|
|
- boolean won;
|
|
|
- Scanner myScanner = new Scanner(System.in);
|
|
|
-
|
|
|
- do {
|
|
|
-
|
|
|
- System.out.println("Where do you want to set your 'x'? (1-9)");
|
|
|
-
|
|
|
- do {
|
|
|
- //Check input
|
|
|
- if(!myScanner.hasNextInt()) {
|
|
|
- System.out.println("Invalid input. Try again.");
|
|
|
- myScanner.nextLine();
|
|
|
- continue;
|
|
|
- }
|
|
|
- nextPos = myScanner.nextInt();
|
|
|
- if(nextPos < 1 || nextPos > 9) {
|
|
|
- System.out.println("Invalid int. 1 to 9 expected.");
|
|
|
- myScanner.nextLine();
|
|
|
- continue;
|
|
|
- }
|
|
|
- if(field.getPos(nextPos) != 0) {
|
|
|
- System.out.println("Position not free.");
|
|
|
- myScanner.nextLine();
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- //Player's turn
|
|
|
- won = field.updatePos(nextPos, 1);
|
|
|
- if(won) {
|
|
|
- printWinner(1);
|
|
|
- if(!askPlayAgain(myScanner)) {
|
|
|
- myScanner.close();
|
|
|
- System.exit(0);
|
|
|
- }
|
|
|
- field.resetField();
|
|
|
- break;
|
|
|
-
|
|
|
- }
|
|
|
- if(field.isDraw()) {
|
|
|
- System.out.println("Draw.");
|
|
|
- if(!askPlayAgain(myScanner)) {
|
|
|
- System.out.println("Game closed.");
|
|
|
- myScanner.close();
|
|
|
- System.exit(0);
|
|
|
- }
|
|
|
- field.resetField();
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- //Opponent's turn
|
|
|
- System.out.println("Opponents turn:");
|
|
|
- won = field.oppMove();
|
|
|
- if(won) {
|
|
|
- printWinner(2);
|
|
|
- if(!askPlayAgain(myScanner)) {
|
|
|
- System.out.println("Game closed.");
|
|
|
- myScanner.close();
|
|
|
- System.exit(0);
|
|
|
- }
|
|
|
- field.resetField();
|
|
|
- break;
|
|
|
-
|
|
|
- }
|
|
|
- if(field.isDraw()) {
|
|
|
- System.out.println("Draw.");
|
|
|
- if(!askPlayAgain(myScanner)) {
|
|
|
- System.out.println("Game closed.");
|
|
|
- myScanner.close();
|
|
|
- System.exit(0);
|
|
|
- }
|
|
|
- field.resetField();
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- break;
|
|
|
-
|
|
|
- } while(true);
|
|
|
+ public static void printWinner(int player) {
|
|
|
+ if(player == 1) {
|
|
|
+ System.out.println("Player 'x' has won.");
|
|
|
+ } else {
|
|
|
+ System.out.println("Player 'o' has won.");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- } while (true);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public static void printWinner(int player) {
|
|
|
- if(player == 1) {
|
|
|
- System.out.println("Player 'x' has won.");
|
|
|
- } else {
|
|
|
- System.out.println("Player 'o' has won.");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static boolean askPlayAgain(Scanner in) {
|
|
|
- String again;
|
|
|
- System.out.println("New game? Y/N");
|
|
|
- do {
|
|
|
- again = in.next();
|
|
|
- if(again.equals("Y")) {
|
|
|
- return true;
|
|
|
- } else if(again.equals("N")) {
|
|
|
- return false;
|
|
|
- } else {
|
|
|
- System.out.println("Invalid input. Try again.");
|
|
|
- }
|
|
|
- } while(true);
|
|
|
- }
|
|
|
-
|
|
|
+ private static boolean askPlayAgain(ConsoleReader cr, PlayField field) {
|
|
|
+ while(true) {
|
|
|
+ System.out.println("New game? Y/N");
|
|
|
+ String s = cr.readLine();
|
|
|
+ if(s.equals("Y")) {
|
|
|
+ field.reset();
|
|
|
+ return true;
|
|
|
+ } else if(s.equals("N")) {
|
|
|
+ System.out.println("Game closed.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|