Search code examples
javatic-tac-toe

Tic tac toe in java, overwrite issues


This is a simple tic tac toe game,every thing is right but with a problem that anyone can overwrite the previous marker and put his

I want to disable overwrite, How can I do it ?

Do we have to use final syntax ?

import java.util.*;
class TicTacToe {
static String Side;
static int NumberOfMoves=0;
public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    Functions fc = new Functions();
    int move;
    System.out.println("Enter your Side :\n1) X\n2) O");
    Side = sc.next();
    if(Side.equals("1") ||Side.equals("x") ||Side.equals("X")) {
        Side = "X";
    } else if(Side.equals("2") ||Side.equals("o") ||Side.equals("O")) {
        Side = "O";
    }
    fc.initialize();
    fc.DisplayBoard();
    while(fc.CheckIfWin() == 0) {
        System.out.println("Its "+Side+"'s turn");
        move = sc.nextInt();
        fc.Move(move);
        NumberOfMoves++;
        fc.CheckIfWin();
        fc.DisplayBoard();
        fc.SideChange();
    }
}
static class Functions {
    String Row[] = new String[9];
    public void initialize() {
        for(int i = 1;i<=9;i++) {
            Row[i-1] = Integer.toString(i);
        }
    }
    public void DisplayBoard() {
        System.out.print('\u000C');
        System.out.println(" "+Row[0]+" | "+Row[1]+" | "+Row[2]);
        System.out.println("---|---|---");
        System.out.println(" "+Row[3]+" | "+Row[4]+" | "+Row[5]);
        System.out.println("---|---|---");
        System.out.println(" "+Row[6]+" | "+Row[7]+" | "+Row[8]);
    }
    public int CheckIfWin() {
        /* Possible wins :-
         * Horzontal : ( 0=1=2 ), (3, 4, 5), (6, 7, 8)
         * Vertical : ( 0=3=6 ), (1=4=7), (2=5=8)
         * Croswards : (0=4=8 ), (2=4=6)
         */
        if(Row[0].equals(Row[1]) && Row[1].equals(Row[2])) {
            System.out.println(Side+" wins.");
            Row[0] = "--";Row[1] = "--";Row[2] = "--";
            return 1;
        } else if(Row[3].equals(Row[4]) && Row[4].equals(Row[5])) {
            System.out.println(Side+" wins.");
            Row[3] = "--";Row[4] = "--";Row[5] = "--";
            return 1;
        } else if(Row[6].equals(Row[7]) && Row[7].equals(Row[8])) {
            System.out.println(Side+" wins.");
            Row[6] = "--";Row[7] = "--";Row[8] = "--";
            return 1;
        } else if(Row[0].equals(Row[3]) && Row[3].equals(Row[6])) {
            System.out.println(Side+" wins.");
            Row[0] = "|";Row[3] = "|";Row[6] = "|";
            return 1;
        } else if(Row[1].equals(Row[4]) && Row[4].equals(Row[7])) {
            System.out.println(Side+" wins.");
            Row[1] = "|";Row[4] = "|";Row[7] = "|";
            return 1;
        } else if(Row[2].equals(Row[5]) && Row[5].equals(Row[8])) {
            System.out.println(Side+" wins.");
            Row[2] = "|";Row[5] = "|";Row[8] = "|";
            return 1;
        } else if(Row[0].equals(Row[4]) && Row[4].equals(Row[8])) {
            System.out.println(Side+" wins.");
            Row[0] = "\\";Row[4] = "\\";Row[8] = "\\";
            return 1;
        } else if(Row[2].equals(Row[4]) && Row[4].equals(Row[6])) {
            System.out.println(Side+" wins.");
            Row[2] = "/";Row[4] = "/";Row[6] = "/";
            return 1;
        }
        return 0;
    }
    public void SideChange() {
        if(Side.equals("O")) {
            Side = "X";
        } else {
            Side = "O";
        }
    }
    public void Move(int move) {
        try {
            Row[move-1] = Side;
        } catch(Exception e) {
            System.out.println("Tried to cheat\n Move will not be accepted");
        }
    }
}
}

Solution

  • This is pretty scholastic so I can just give you some advice and not the solution. When you enter the new move, put a method that returns a Boolean (like isValid(int move)) and if that area is occupied it return false. Than put the moves request in a while waiting for a valid move