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

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

最佳答案

这是非常学术性的,所以我只能给你一些建议,而不是解决方案。 当您输入新的移动时,放置一个返回 boolean 值的方法(如 isValid(int move)),如果该区域被占用,则返回 false。然后将移动请求放在等待有效移动的时间

关于java - java中的井字游戏,覆盖问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36885495/

相关文章:

artificial-intelligence - 带有 alpha-beta 修剪的量子井字棋 - 状态的最佳表示?

java - DAO 中的结果集问题

java - 如何使用 Java 在 Excel 文件中存储 BigDecimal

java - TicTacToe 人工智能 Java

C++ MiniMax算法陷入井字棋板的无限循环

java - 在简单的井字游戏中是否有任何紧凑的方法来检查获胜者?

java - 如何使用 Apache Jena Java API 创建 Fuseki SPARQL 服务器?

java - 使用 Apache HttpClient 定义源 IP 地址

java - selenium webdriver触发chrome但不打开获取url

c# - Tic Tac Toe完美AI算法: deeper in "create fork" step