-1

First of all thanks everyone who is willing to read all this code and help me.

I have a Hashmap<String,Square> that simply prints a board to the console and it goes as follows:

public class Square {
private char status;

public Square(String statusp) {
    if(statusp.equals("empty")) {
        this.status = '.';
                    
    }else if(statusp.equals("black")) {
        this.status = 'X';
        
    }else if(statusp.equals("white")) {
        this.status = 'O';
        
    }else {
        System.out.println("ERROR: status can only be: empty, black, white");
    }
}

public char getStatus() {
    return status;
} 

}

public class Board {
public HashMap<String, Square> board;

public Board() {
    board = new HashMap<String, Square>();
    Square empty = new Square("empty");
    Square black = new Square("black");
    Square white = new Square("white");
    
    
    board.put("a1", empty);
    board.put("a2", empty);
    board.put("a3", empty);
    board.put("a4", empty);
    board.put("a5", empty);
    board.put("a6", empty);
    board.put("a7", empty);
    board.put("a8", empty);
    
    board.put("b1", empty);
    board.put("b2", empty);
    board.put("b3", empty);
    board.put("b4", empty);
    board.put("b5", empty);
    board.put("b6", empty);
    board.put("b7", empty);
    board.put("b8", empty);
    
    board.put("c1", empty);
    board.put("c2", empty);
    board.put("c3", empty);
    board.put("c4", empty);
    board.put("c5", empty);
    board.put("c6", empty);
    board.put("c7", empty);
    board.put("c8", empty);
    
    board.put("d1", empty);
    board.put("d2", empty);
    board.put("d3", empty);
    board.put("d4", white);
    board.put("d5", black);
    board.put("d6", empty);
    board.put("d7", empty);
    board.put("d8", empty);
    
    board.put("e1", empty);
    board.put("e2", empty);
    board.put("e3", empty);
    board.put("e4", black);
    board.put("e5", white);
    board.put("e6", empty);
    board.put("e7", empty);
    board.put("e8", empty);
    
    board.put("f1", empty);
    board.put("f2", empty);
    board.put("f3", empty);
    board.put("f4", empty);
    board.put("f5", empty);
    board.put("f6", empty);
    board.put("f7", empty);
    board.put("f8", empty);
    
    board.put("g1", empty);
    board.put("g2", empty);
    board.put("g3", empty);
    board.put("g4", empty);
    board.put("g5", empty);
    board.put("g6", empty);
    board.put("g7", empty);
    board.put("g8", empty);
    
    board.put("h1", empty);
    board.put("h2", empty);
    board.put("h3", empty);
    board.put("h4", empty);
    board.put("h5", empty);
    board.put("h6", empty);
    board.put("h7", empty);
    board.put("h8", empty);
    
    
    
}

public void printBoard() {
    System.out.println("+----------------+");
    System.out.println("|    abcdefgh    |");
    
    
    System.out.println("|  1 " + board.get("a1").getStatus() + "" + board.get("b1").getStatus() + board.get("c1").getStatus() + board.get("d1").getStatus()
                     + board.get("e1").getStatus() + board.get("f1").getStatus() + board.get("g1").getStatus() + board.get("h1").getStatus() + " 1  |");
    
    System.out.println("|  2 " + board.get("a2").getStatus() + "" + board.get("b2").getStatus() + board.get("c2").getStatus() + board.get("d2").getStatus()
             + board.get("e2").getStatus() + board.get("f2").getStatus() + board.get("g2").getStatus() + board.get("h2").getStatus() + " 2  |");
    
    System.out.println( "|  3 " + board.get("a3").getStatus() + "" + board.get("b3").getStatus() + board.get("c3").getStatus() + board.get("d3").getStatus()
             + board.get("e3").getStatus() + board.get("f3").getStatus() + board.get("g3").getStatus() + board.get("h3").getStatus() + " 3  |");
    
    System.out.println("|  4 " + board.get("a4").getStatus() + "" + board.get("b4").getStatus() + board.get("c4").getStatus() + board.get("d4").getStatus()
             + board.get("e4").getStatus() + board.get("f4").getStatus() + board.get("g4").getStatus() + board.get("h4").getStatus() + " 4  |");
    
    System.out.println("|  5 " + board.get("a5").getStatus() + "" + board.get("b5").getStatus() + board.get("c5").getStatus() + board.get("d5").getStatus()
             + board.get("e5").getStatus() + board.get("f5").getStatus() + board.get("g5").getStatus() + board.get("h5").getStatus() + " 5  |");
    
    System.out.println("|  6 " + board.get("a6").getStatus() + "" + board.get("b6").getStatus() + board.get("c6").getStatus() + board.get("d6").getStatus()
             + board.get("e6").getStatus() + board.get("f6").getStatus() + board.get("g6").getStatus() + board.get("h6").getStatus() + " 6  |");
    
    System.out.println("|  7 " + board.get("a7").getStatus() + "" + board.get("b7").getStatus() + board.get("c7").getStatus() + board.get("d7").getStatus()
             + board.get("e7").getStatus() + board.get("f7").getStatus() + board.get("g7").getStatus() + board.get("h7").getStatus() + " 7  |");
    
    System.out.println("|  8 " + board.get("a8").getStatus() + "" + board.get("b8").getStatus() + board.get("c8").getStatus() + board.get("d8").getStatus()
             + board.get("e8").getStatus() + board.get("f8").getStatus() + board.get("g8").getStatus() + board.get("h8").getStatus() + " 8  |");
    
    System.out.println("|    abcdefgh    |");
    System.out.println("+----------------+");
}

}

And it basically prints this: Board

The idea is that when the user gives an input that is equal to one of the keys of the HashMap the value of that key will change to either Square black or Square white (X or O) AND the value only changes if the users inputted key doesnt already have the value of Square black or Square white. The first check works fine but i cant seem to get the second check working. It always overwrites the value even if it already was X or O.

To perfom the above checks i have the following code:

System.out.println(p1.getName().toUpperCase() + ", please enter your move:");
    String move = io.readInput();

        //check if the hashmap contains the key of move
    if (board.board.containsKey(move)) {
        
        //check if the value of key move isnt equal to Square black or white (X or O)
        if(board.board.get(move) != black || board.board.get(move) != white ) {
        
        board.board.replace(move, black);
        }else {
            System.out.println("that spot is already taken");
        }

        board.printBoard();
    }
    

I tried changing the || to && but no results. i also tried:

if(board.board.get(move) == empty)  {
        
        board.board.replace(move, black);
        board.printBoard();
        }else {
            System.out.println("that spot is already taken");
        }

But then no matter what input i give it always returns the else statement and the if statement is never true even if move equals empty.

Any idea why the second if statement doesnt work? it gives me zero erros.

1
  • 2
    If you use a || it'll always be true. Because if it's black it's different of white, and vice-versa Commented Jan 9, 2021 at 17:21

1 Answer 1

1

There are two problems: the if will always be true because empty differs from both black and white and you need to get the board position's status and compare that to "empty", like

if (board.get(move).getStatus().equals("empty")) {
    //Code here...
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.