首页 > 解决方案 > Java hashmap算法检查值

问题描述

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

每个点或 X 或 O 是哈希图中的一个键,例如,键 A1 的值为 。键 D4 的值为 O。游戏由两个用户玩,每个用户根据玩家的不同给出 X 或 O 的输入。其他两个玩家之间的每一块石头(或一系列石头)水平,垂直或对角线都需要更改为它所包围的石头,并且玩家只有在可以抓住另一个的情况下才能给出输入玩家的石头,否则他跳过一个回合。例如: Board2

如果玩家 O 要在 b2 放置一颗棋子,则 c2 处的 X 也需要更改为 O,因为它位于另一玩家的 2 个棋子之间: Board3

我需要创建一个算法,每轮检查玩家是否可以放置有效的石头,并检查需要转动哪些石头。它需要在各个方向上都这样做,但我不知道如何创建这样的算法。我知道的唯一方法是在 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;
        }
        

标签: javaalgorithmhashmap

解决方案


我建议您应该将字母逻辑存储为数字,例如“a2”到“02”,因为您可以使用坐标数字更轻松地在桌子上“行走”。这些字母只是用户界面上的标签。


推荐阅读