Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tictactoe;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Main {
- static boolean isGameRunning = true;
- public static void main(String[] args) {
- // write your code here
- Grid grid = new Grid();
- grid.fillGrid();
- grid.showGrid();
- while (isGameRunning) {
- grid.makeMove();
- }
- }
- }
- class Grid{
- public static final char EMPTY_CELL = ' ';
- public static final char O_CELL = 'O';
- public static final char X_CELL = 'X';
- public static int moveCounter = 0;
- static String input;
- private char[][] field = new char[3][3];
- // Scanner s = new Scanner(System.in);
- void fillGrid () {
- for (int i = 0; i < field.length; i++) {
- for (int j = 0; j < field[i].length; j++)
- field[i][j] = Grid.EMPTY_CELL;
- }
- }
- public void makeMove () {
- Scanner s = new Scanner(System.in);
- try {
- int x = s.nextInt();
- int y = s.nextInt();
- x--;
- y--;
- if (EMPTY_CELL == field[x][y] && moveCounter % 2 == 0) {
- field[x][y] = Grid.X_CELL;
- showGrid();
- moveCounter++;
- checkGameState();
- } else if (EMPTY_CELL == field[x][y] && moveCounter % 2 != 0) {
- field[x][y] = Grid.O_CELL;
- showGrid();
- moveCounter++;
- checkGameState();
- } else if (EMPTY_CELL != field[x][y]) {
- System.out.println("This cell is occupied! Choose another one!");
- }
- } catch (InputMismatchException e) {
- System.out.println("You should enter numbers!");
- } catch (IndexOutOfBoundsException e) {
- System.out.println("Coordinates should be from 1 to 3!");
- }
- }
- void showGrid () {
- System.out.print("---------\n" +
- "| " + field[0][0] + " " + field[0][1] + " " + field[0][2] + " |\n" +
- "| " + field[1][0] + " " + field[1][1] + " " + field[1][2] + " |\n" +
- "| " + field[2][0] + " " + field[2][1] + " " + field[2][2] + " |\n" +
- "---------\n");
- }
- private boolean isPlayerWin (char player) {
- return (field[0][0] == player && field[1][1] == player && field[2][2] == player) ||
- ((field[0][2] == player && field[1][1] == player && field[2][0] == player) ||
- (field[0][0] == player && field[1][0] == player && field[2][0] == player) ||
- (field[0][1] == player && field[1][1] == player && field[2][1] == player) ||
- (field[0][2] == player && field[1][2] == player && field[2][2] == player) ||
- (field[0][0] == player && field[0][1] == player && field[0][2] == player) ||
- (field[1][0] == player && field[1][1] == player && field[1][2] == player) ||
- (field[2][0] == player && field[2][1] == player && field[2][2] == player));
- }
- public void checkGameState() {
- int xCnt = 0;
- int oCnt = 0;
- for (int i = 0; i < field.length; i++) {
- for (int j = 0; j < field[i].length; j++) {
- if (field[i][j] == X_CELL) {
- xCnt++;
- } else if (field[i][j] == Grid.O_CELL) {
- oCnt++;
- }
- }
- }
- int moveCnt = xCnt + oCnt;
- boolean isImpossQntDiff = xCnt > oCnt + 1 || xCnt < oCnt - 1 || oCnt > xCnt + 1 || oCnt < xCnt - 1;
- char player = (moveCounter - 1) % 2 == 0 ? 'X' : 'O';
- for (;;) {
- if (isPlayerWin(player)) {
- System.out.println(player + " wins");
- Main.isGameRunning = false;
- return;
- } else if (isPlayerWin(player) || isImpossQntDiff) {
- System.out.println("Impossible");
- return;
- } else if (9 == moveCnt){
- System.out.println("Draw");
- Main.isGameRunning = false;
- return;
- } else {
- return;
- }
- }
- }
- }
- // public void gameStartInput () {
- // final Scanner s = new Scanner(System.in);
- // Grid.input = s.nextLine().replaceAll("_", " ");
- // int counter = 0;
- // for (int i = 0; i < field.length; i++) {
- // for (int j = 0; j < field[i].length; j++) {
- // field[i][j] = Grid.input.charAt(counter);
- // counter++;
- // }
- // }
- // showGrid();
- // checkGameState();
- // }
Advertisement
Comments
-
- https://hyperskill.org/projects/48?track=8
- Learning outcomes
- After finishing this project, you'll get to know a lot about planning and developing a complex program from scratch, using methods, nested lists, list comprehension, handling errors, and processing user input.
Add Comment
Please, Sign In to add comment
Advertisement