Tic-Tac-Toe Game in Java

Tic-Tac-Toe Java Code

Tic-Tac-Toe Java Code


import java.util.*;
class tic_tac_toe {
    Character a[][];
    boolean right() {
        int n = 3;
        char ch = 'X';
        int c = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i + j == 2) {
                    if (a[i][j].equals(ch))
                        c++;
                }
            }
        }

        char ch1 = 'O';
        int c1 = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i + j == 2) {
                    if (a[i][j].equals(ch1))
                        c1++;
                }
            }
        }
        return c == 3 || c1 == 3;
    }

    boolean left() {
        int n = 3;
        char ch = 'X';
        int c = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j) {
                    if (a[i][j].equals(ch))
                        c++;
                }
            }
        }

        char ch1 = 'O';
        int c1 = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j) {
                    if (a[i][j].equals(ch1))
                        c1++;
                }
            }
        }
        return c == 3 || c1 == 3;
    }

    boolean row() {
        int n = 3;
        int c = 0;
        boolean b = false;
        for (int i = 0; i < n; i++) {
            char ch = 'X';
            for (int j = 0; j < n; j++) {
                if (a[i][j].equals(ch))
                    c++;
            }
            if (c == 3) {
                b = true;
                break;
            } else
                c = 0;
        }
        for (int i = 0; i < n; i++) {
            char ch = 'O';
            for (int j = 0; j < n; j++) {
                if (a[i][j].equals(ch))
                    c++;
            }
            if (c == 3) {
                b = true;
                break;
            } else
                c = 0;
        }
        return b;
    }

    boolean column() {
        int n = 3;
        int c = 0;
        boolean b = false;
        for (int i = 0; i < n; i++) {
            char ch = 'X';
            for (int j = 0; j < n; j++) {
                if (a[j][i].equals(ch))
                    c++;
            }
            if (c == 3) {
                b = true;
                break;
            } else
                c = 0;
        }
        for (int i = 0; i < n; i++) {
            char ch = 'O';
            for (int j = 0; j < n; j++) {
                if (a[j][i].equals(ch))
                    c++;
            }
            if (c == 3) {
                b = true;
                break;
            } else
                c = 0;
        }
        return b;
    }

    void friends() {
        Scanner sc = new Scanner(System.in);
        a = new Character[3][3];
        int n = 3;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                a[i][j] = '-';
            }
        }
        System.out.println("Game Starts!");
        for (int l = 0; l < 9; l++) {
            System.out.println("Player 1");
            System.out.println("Enter coordinates of X");
            int x = sc.nextInt();
            int y = sc.nextInt();
            if(x>2 || y>2 || x<0 || y<0){
                System.out.println("Wrong input. Replay!");
                System.exit(0);
            }
            else{
                while (x < 0 || x >= 3 || y < 0 || y >= 3 || a[x][y] != '-') {
                    System.out.println("Invalid move! Try again.");
                    x = sc.nextInt();
                    y = sc.nextInt();
                }
                a[x][y] = 'X';
                if (right() || left() || row() || column()) {
                    for (int i = 0; i < n; i++) {
                        for (int j = 0; j < n; j++) {
                            System.out.print(" "+a[i][j]+" ");
                        }
                        System.out.println();
                    }
                    System.out.println("Player 1 Won!");
                    break;
                }
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        System.out.print(" "+a[i][j]+" ");
                    }
                    System.out.println();
                }
                System.out.println("Player 2");
                System.out.println("Enter coordinates of O");
                x = sc.nextInt();
                y = sc.nextInt();
                if (x > 2 || y > 2 || x < 0 || y < 0) {
                    System.out.println("Wrong input. Replay!");
                    System.exit(0);
                }
                while (x < 0 || x >= 3 || y < 0 || y >= 3 || a[x][y] != '-') {
                    System.out.println("Invalid move! Try again.");
                    x = sc.nextInt();
                    y = sc.nextInt();
                }
                a[x][y] = 'O';
                if (right() || left() || row() || column()) {
                    for (int i = 0; i < n; i++) {
                        for (int j = 0; j < n; j++) {
                            System.out.print(" "+a[i][j]+" ");
                        }
                        System.out.println();
                    }
                    System.out.println("Player 2 Won!");
                    break;
                }
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        System.out.print(" "+a[i][j]+" ");
                    }
                    System.out.println();
                }
            }
        }
    }

    void computer() {
        Scanner sc = new Scanner(System.in);
        a = new Character[3][3];
        int n = 3;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                a[i][j] = '-';
            }
        }
        System.out.println("Game Starts!");
        for (int l = 0; l < 9; l++) {
            System.out.println("Player 1");
            System.out.println("Enter coordinates of X");
            int x = sc.nextInt();
            int y = sc.nextInt();
            if(x>2 || y>2 || x<0 || y<0){
                System.out.println("Wrong input. Replay!");
                System.exit(0);
            }
            else{
                while (x < 0 || x >= 3 || y < 0 || y >= 3 || a[x][y] != '-') {
                    System.out.println("Invalid move! Try again.");
                    x = sc.nextInt();
                    y = sc.nextInt();
                }
                a[x][y] = 'X';
                if (right() || left() || row() || column()) {
                    for (int i = 0; i < n; i++) {
                        for (int j = 0; j < n; j++) {
                            System.out.print(" "+a[i][j]+" ");
                        }
                        System.out.println();
                    }
                    System.out.println("Player 1 Won!");
                    break;
                }
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        System.out.print(" "+a[i][j]+" ");
                    }
                    System.out.println();
                }

                if (l == 8) break;

                System.out.println("Computer's turn...");
                x = (int)(Math.random()*3);
                y = (int)(Math.random()*3);
                while (a[x][y] != '-') {
                    x = (int)(Math.random()*3);
                    y = (int)(Math.random()*3);
                }
                a[x][y] = 'O';
                if (right() || left() || row() || column()) {
                    for (int i = 0; i < n; i++) {
                        for (int j = 0; j < n; j++) {
                            System.out.print(" "+a[i][j]+" ");
                        }
                        System.out.println();
                    }
                    System.out.println("Computer Won!");
                    break;
                }
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        System.out.print(" "+a[i][j]+" ");
                    }
                    System.out.println();
                }
            }
        }
    }

    public static void main(String[] args) {
        tic_tac_toe t = new tic_tac_toe();
        Scanner sc = new Scanner(System.in);
        System.out.println("Choose Option:");
        System.out.println("1. Play with Friends");
        System.out.println("2. Play with Computer");
        int choice = sc.nextInt();
        if (choice == 1) {
            t.friends();
        } else {
            t.computer();
        }
    }
}


Comments

Popular posts from this blog

Trisanjit Number

Finding Subarrays with a Target Sum in Java: A Beginner's Guide