java中的Tic tac toe,覆盖问题

时间:2016-04-27 09:19:51

标签: java tic-tac-toe

这是一个简单的tic tac toe游戏,每件事情都是正确的,但有一个问题,任何人都可以覆盖以前的标记,并把他的

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

我们必须使用最终语法吗?

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

1 个答案:

答案 0 :(得分:1)

这是非常有学问的,所以我可以给你一些建议,而不是解决方案。 当您输入新移动时,放置一个返回布尔值的方法(如isValid(int move)),如果该区域被占用,则返回false。比移动请求暂时等待有效移动