Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- private static Scanner scanner = new Scanner(System.in);
- private static char[][] initiateGameField() {
- char[][] gameField = new char[3][3];
- int inputIndex = 0;
- // System.out.print("Enter cells: ");
- // String inputString = scanner.nextLine();
- String inputString = "_________";
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j <3; j++) {
- gameField[i][j] = inputString.charAt(inputIndex);
- inputIndex++;
- }
- }
- drawGameField(gameField);
- return gameField;
- }
- private static void drawGameField(char[][] gameField) {
- System.out.print("---------\n");
- for (int i = 0; i < 3; i++) {
- System.out.printf("| %s %s %s |\n",
- gameField[i][0], gameField[i][1], gameField[i][2]);
- }
- System.out.print("---------\n");
- }
- private static String analyzeGameField(char[][] gameField) {
- String outcome = "Game not finished";
- int wins = 0;
- int countX = 0;
- int countY = 0;
- int emptyCells = 0;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (gameField[i][j] == 'X') {
- countX++;
- }
- if (gameField[i][j] == 'O') {
- countY++;
- }
- if (gameField[i][j] == '_') {
- emptyCells++;
- }
- }
- }
- for (int i = 0; i < 3; i++) {
- if (gameField[i][0] == gameField[i][1] && gameField[i][1] == gameField[i][2] && gameField[i][0] != '_') {
- outcome = String.format("%s wins", gameField[i][0]);
- wins++;
- }
- if (gameField[0][i] == gameField[1][i] && gameField[1][i] == gameField[2][i] && gameField[0][i] != '_') {
- outcome = String.format("%s wins", gameField[0][i]);
- wins++;
- }
- }
- if (gameField[0][0] == gameField[1][1] && gameField[1][1] == gameField[2][2] && gameField[1][1] != '_') {
- outcome = String.format("%s wins", gameField[0][0]);
- wins++;
- }
- if (gameField[0][2] == gameField[1][1] && gameField[1][1] == gameField[2][0] && gameField[1][1] != '_') {
- outcome = String.format("%s wins", gameField[0][2]);
- wins++;
- }
- if (emptyCells == 0 && wins == 0) {
- outcome = "Draw";
- }
- if (wins > 2 || Math.abs(countX - countY) > 1) {
- outcome = "Impossible";
- }
- return outcome;
- }
- private static char[][] playerMove(char[][] gameField, char player) { // player - X or O
- boolean moveFinished = false;
- int x;
- int y;
- while (!moveFinished) {
- System.out.print("Enter the coordinates: ");
- String input = scanner.nextLine();
- try {
- x = Integer.parseInt(input.substring(0, 1));
- y = Integer.parseInt(input.substring(2, 3));
- if (x < 0 || x > 3 || y < 0 || y > 3) {
- System.out.println("Coordinates should be from 1 to 3!");
- continue;
- }
- // I Have no idea how to an make algorithm for that
- if (x == 1 && y == 1) {x = 2; y = 0;}
- if (x == 1 && y == 2) {x = 1; y = 0;}
- if (x == 1 && y == 3) {x = 0; y = 0;}
- if (x == 2 && y == 1) {x = 2; y = 1;}
- if (x == 2 && y == 2) {x = 1; y = 1;}
- if (x == 2 && y == 3) {x = 0; y = 1;}
- if (x == 3 && y == 1) {x = 2; y = 2;}
- if (x == 3 && y == 2) {x = 1; y = 2;}
- if (x == 3 && y == 3) {x = 0; y = 2;}
- } catch (Exception e){
- System.out.println("You should enter numbers!");
- continue;
- }
- if (gameField[x][y] == '_') {
- gameField[x][y] = player;
- } else {
- System.out.println("This cell is occupied! Choose another one!");
- continue;
- }
- moveFinished = true;
- }
- return gameField;
- }
- static void clear() {
- try {
- new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
- } catch (Exception E) {
- System.out.println(E);
- }
- }
- private static void game() {
- char[][] gameField = initiateGameField();
- boolean inProgress = true;
- char[] players = new char[] {'X', 'O'};
- while (inProgress) {
- for (char player : players) {
- gameField = playerMove(gameField, player);
- clear();
- drawGameField(gameField);
- String outcome = analyzeGameField(gameField);
- if (!outcome.equals("Game not finished")) {
- System.out.println(outcome);
- inProgress = false;
- break;
- }
- }
- }
- }
- public static void main(String[] args) {
- game();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment