Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

Java hashmap algorithm checking values

I am a first year CS student and i am making a simple board game called Reversi/othello as a school project in Java. To print the board i used a HashMap and it prints out this: board

With each dot or X or O being a key in the hashmap, for example key A1 has a value of . and key D4 has a value of O. the game is played by two users who each give an input of X or O depending on the player. Every stone (or series of stones) that is between 2 of the other players stones either horizontally, vertically or diagonally needs to change to the stones it is surrounded by, and a player can only give an input if he can grab one of the other players stones, otherwise he skips a turn. For example: Board2

If player O were to place a stone at b2 the X at c2 needs to change in a O too because it is between 2 stones of the other player: Board3

I need to create an algorithm that with every turn checks if the player can place a valid stone, and that checks which stone(s) need to be turned. it needs to do this in every direction, but i have no idea how to create such algorithm. The only way i know is hardcoding e ve ry single solution in if-statements, but thats not very efficient. Like: 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(); } Here is the code:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I suggest you should store logically letters as numbers for example "a2" to "02", because you can easier "walk" on table programatically with coordinate numbers. The letters are only labels on userinterface.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...