java 中的井字游戏,覆盖问题

Tic tac toe in java, overwrite issues

这是一个简单的井字游戏,每件事都是对的,但有一个问题,任何人都可以覆盖以前的标记并把他的

我想禁用覆盖,我该怎么做?

我们必须使用 final 语法吗?

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");
        }
    }
}
}

这很学术,所以我只能给你一些建议,而不是解决方案。 当你输入新的移动时,输入一个 returns 布尔值的方法(比如 isValid(int move)),如果该区域被占用,它 return false。比在等待有效移动时提出移动请求