DEV Community

Nina Hwang
Nina Hwang

Posted on

Tic-tac-toe with Java

After studying Java arrays, I created this simple tic-tac-toe application.
Alt Text

Code

import java.util.Scanner;
import java.util.Arrays;

public class game {
    public static void main(String[] args)  {
        // get the name of players
        Scanner playersInfo = new Scanner(System.in);
        String player1;
        String player2;
        System.out.println("Player 1: ");
        player1 = playersInfo.nextLine();
        System.out.println("Player 2: ");
        player2 = playersInfo.nextLine();

        int game[][] = {{0,0,0},{0,0,0},{0,0,0}};
        boolean isEnd = false;
        boolean isFirstTurn = true;
        String winner = null;

        // repeat until the game's over
        while (!isEnd) {
            if (!!isFirstTurn) {
                System.out.println(player1 + "'s turn");
            } else {
                System.out.println(player2 + "'s turn");
            }

            // get the number b/w 1 & 9
            int coordinates[][] = {
                    {0,0},
                    {0,1},
                    {0,2},
                    {1,0},
                    {1,1},
                    {1,2},
                    {2,0},
                    {2,1},
                    {2,2}
            };
            Scanner userInput = new Scanner(System.in);
            int n = userInput.nextInt();
            if (n < 1 || n > 9) {
                System.out.println("The number should be between 1 and 9");
                continue;
            }
            int coordinate[] = coordinates[n-1];

            if (game[coordinate[0]][coordinate[1]] != 0) {
                System.out.println("Select another spot");
                continue;
            }

            // change the value of game array
            if (!!isFirstTurn) {
                game[coordinate[0]][coordinate[1]] = 1;
                isFirstTurn = false;
            } else {
                game[coordinate[0]][coordinate[1]] = -1;
                isFirstTurn = true;
            }

            int h_sum = 0;
            int v_sum = 0;
            int right_diagonal_sum = 0;
            int left_diagonal_sum = 0;
            int zero_cnt = 0;

            for (int i = 0; i < 3; i++) {
                h_sum = 0;
                v_sum = 0;
                for (int j = 0; j < 3; j++) {
                    h_sum += game[i][j];
                    v_sum += game[j][i];
                    if (i + j == 2) {
                        left_diagonal_sum += game[i][j];
                    }
                    if (game[i][j] == 0) {
                        zero_cnt += 1;
                    }
                }

                if (h_sum == 3 || v_sum == 3) {
                    isEnd = true;
                    winner = player1;
                } else if (h_sum == -3 || v_sum == -3) {
                    isEnd = true;
                    winner = player2;
                }
                right_diagonal_sum += game[i][i];
            }

            if (isEnd == false) {
                if (left_diagonal_sum == 3 || right_diagonal_sum == 3) {
                    isEnd = true;
                    winner = player1;
                } else if (left_diagonal_sum == -3 || right_diagonal_sum == -3) {
                    isEnd = true;
                    winner = player2;
                } else if (zero_cnt == 0) {
                    isEnd = true;
                }
            }

            // print the board
            for (int i = 0; i < 3; i++) {
                String show[] = new String[3];
                for (int j=0; j<3; j++){
                    if (game[i][j] == 0) {
                        show[j] = "  ";
                    } else if (game[i][j] == 1) {
                        show[j] = "X ";
                    } else {
                        show[j] = "O ";
                    }
                }
                System.out.println(Arrays.toString(show));
            }
        }

        // print the final result
        if (winner == null) {
            System.out.println("Draw");
        } else {
            System.out.println("The winner is " + winner);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)