Java hashmap算法检查值

Java hashmap算法检查值,java,algorithm,hashmap,Java,Algorithm,Hashmap,我是一名CS一年级的学生,我正在制作一个简单的棋盘游戏,名为Reversi/Otherlo,作为Java的一个学校项目。 为了打印电路板,我使用了HashMap,它打印出以下内容: 每个点或X或O都是hashmap中的一个键,例如,键A1的值为。键D4的值为O。 游戏由两个用户进行,每个用户根据玩家的不同输入X或O。 每一块石头(或一系列石头)在其他玩家的两块石头之间,无论是水平的、垂直的还是对角的,都需要换成它周围的石头,一个玩家只有在他能抓住其他玩家的石头时才能输入,否则他会跳过一个回合。

我是一名CS一年级的学生,我正在制作一个简单的棋盘游戏,名为Reversi/Otherlo,作为Java的一个学校项目。 为了打印电路板,我使用了HashMap,它打印出以下内容:

每个点或X或O都是hashmap中的一个键,例如,键A1的值为。键D4的值为O。 游戏由两个用户进行,每个用户根据玩家的不同输入X或O。 每一块石头(或一系列石头)在其他玩家的两块石头之间,无论是水平的、垂直的还是对角的,都需要换成它周围的石头,一个玩家只有在他能抓住其他玩家的石头时才能输入,否则他会跳过一个回合。例如:

如果玩家O在b2处放置一块石头,c2处的X也需要在a O中改变,因为它位于另一玩家的两块石头之间:

我需要创建一个算法,每个回合检查玩家是否可以放置有效的石头,并检查需要转动的石头。它需要在各个方向都这样做,但我不知道如何创建这样的算法。我知道的唯一方法是在if语句中硬编码任何单一的解决方案,但这不是很有效。比如:
if(board.board.get(“c2”)==board.BLACK和board.board.get(“d2”)==board.WHITE和board.board.get(“b2”)==board.WHITE){board.board.replace(“c2”,board.WHITE);board.printBoard();}
代码如下:

    private boolean p1Start = false;
private boolean p2Start = false;
public final Player P1 = new Player();
public final Player P2 = new Player();


public void play(int amountofstones) { 
    ConsoleInteractor io = new ConsoleInteractor();

    // create players
    P1.setName1();
    P2.setName2();
    
    
    //welcome message
    System.out.println("");
    System.out.println("Welcome to a game of Reversi " + P1.getName().toUpperCase() + " and "
            + P2.getName().toUpperCase() + "!");
    System.out.println("");

    // create and print board
    Board board = new Board();
    board.printBoard();

    // play a game of Reversi
    //create a boolean that returns true when a stone has been succesfuly placed
    boolean succes;
    
    for (int i = 1; i < amountofstones; i = i + 2) { // eruit
        if(p1Start) {
        succes = false;
        while (!succes) {
            System.out.println(P1.getName().toUpperCase() + ", please enter your move:");
            String move1 = io.readInput();

            // check if the hashmap contains the key of move
            if (board.board.containsKey(move1)) {

                // check if the value of key move is equal to empty
                if (board.board.get(move1) == board.EMPTY) {
                    //place a stone
                    board.board.replace(move1, board.BLACK);
                    succes = true;
                    board.printBoard();
                } else {
                    //return error message when users tries to place a stone on a place thats already taken
                    System.out.println("that spot is already taken");
                }

            } else {
                //return error message when user wants to put stone outside of board
                System.out.println("That spot doesnt exist");
            }
        }
            p2Start = true;
        }
        //same for player 2
        succes = false;
        if(p2Start) {
        while (!succes) {
            System.out.println(P2.getName().toUpperCase() + ", please enter your move:");
            String move2 = io.readInput();
            // check if the hashmap contains the key of move
            if (board.board.containsKey(move2)) {

                // check if the value of key move is equal to empty
                if (board.board.get(move2) == board.EMPTY) {

                    board.board.replace(move2, board.WHITE);
                    succes = true;
                    board.printBoard();
                } else {
                    //return error message when users tries to place a stone on a place thats already taken
                    System.out.println("that spot is already taken");
                }

            } else {
                //return error message when user wants to put stone outside of board
                System.out.println("That spot doesnt exist");
            }
        }
        p1Start = true;
        }
        //if board is full or no white or black stones are left end game and print winner
        if(!board.board.containsValue(board.EMPTY) || !board.board.containsValue(board.BLACK) || !board.board.containsValue(board.WHITE)) {
            System.out.println("Game over");
            
            
            int white = Collections.frequency( board.board.values(), board.WHITE);
            int black = Collections.frequency( board.board.values(), board.BLACK);
            int numOfWinsWhite = 0;
            int numOfWinsBlack = 0;
            
            if(white > black) {
                numOfWinsWhite++;
                System.out.println("The winner is: " + P2.getName());
                
            }else if(black > white) {
                System.out.println("The winner is: " + P1.getName());
                numOfWinsBlack++;
            }else {
                System.out.println("Its a tie");
            }
            System.out.println(P1.getName() + ": " + numOfWinsBlack + " - " + P2.getName() + ": " + numOfWinsWhite);
            break;
        }
        
private boolean p1Start=false;
私有布尔值p2Start=false;
公开决赛选手P1=新选手();
公开决赛选手P2=新选手();
公开无效播放(int-amountofstones){
ConsoleInteractor io=新的ConsoleInteractor();
//创建玩家
P1.setName1();
P2.setName2();
//欢迎辞
System.out.println(“”);
System.out.println(“欢迎来到Reversi游戏”+P1.getName().toUpperCase()+“和”
+P2.getName().toUpperCase()+“!”;
System.out.println(“”);
//创建和打印电路板
线路板=新线路板();
印制板();
//玩反转游戏
//创建一个布尔值,该布尔值在成功放置石头时返回true
布尔成功;
对于(int i=1;i黑色){
numOfWinsWhite++;
System.out.println(“获胜者是:+P2.getName());
}else if(黑色>白色){
System.out.println(“获胜者是:+P1.getName());
numOfWinsBlack++;
}否则{
System.out.println(“这是一个平局”);
}
System.out.println(P1.getName()+“:”+numOfWinsBlack+“-”+P2.getName()+“:”+numOfWinsWhite);
打破
}

我建议您将字母逻辑存储为数字,例如“a2”到“02”,因为您可以更轻松地“行走”在表格上用坐标数字编程。字母只是用户界面上的标签。

问题是什么?你能解释什么不起作用,什么不明白吗?你做了什么调试?这些都是你应该问自己的问题,并在帖子中解释。请访问,阅读,特别是问题是我如何创建这样一个算法