0

I'm working on a Connect 4 game. So the issue I'm having is that for some reason I can't use getIcon as a String and compare it to another String. Essentially, what I've done is that whenever someone presses the first column, I use a loop to check if the bottom piece has a blankImage slot, and replace it. But the issue is that it even though it always starts as true, (meaning I can place a piece), it returns the if statement as false.

Here's the code for what happens if the first column is pressed.

//Executes this code if the first column is activated
if (e.getActionCommand ().equals ("a")) {

  //Checks if there is a possible move to place a piece from the bottom, moving it's way up
  for (int i = boardHeight - 1; ((i >= 0) || (exitStatement == "exit")); i--) {

    //Sets the image source for the piece equal to tempImage
    String tempImage = "" + boardSlots[0][i].getIcon();

    //If the image is blank, we set the image to be the appropriate image for the player's piece (either player 1 or player 2)
    if (tempImage == "blankSlotImage.png") { //<-----THIS IS ALWAYS FALSE??? WHYYY?!?!

      //boardSlots and playerImage are already a globally declared variable
      boardSlots[0][i].setIcon(new ImageIcon(playerImage));

      //An exit variable is provided to end the loop so other pieces aren't added
      exitStatement = "exit";

    //Otherwise this should mean the column is full
    } else {

      //Checks if the top slot is empty or not; if it is the user is indicated that they cannot go there
      if (blankSlots[0][0].getIcon() != (new ImageIcon("blankSlotImage.png")) {

        //A JOptionPane goes here telling the user they can't place their piece in this column

      }
    }
  }
}

Something I've tried is instead of comparing the slots' given image as a string, my comparison looked like this:

if (boardSlots[0][i].getIcon() == (new ImageIcon("/Users/rishabchopra/Desktop/BlankSlot.png"))))

But that hasn't worked either:/

So once again, if you could enlighten me on what I'm doing wrong or a way I could improve this, I would really appreciate it. Thanks!

P.s. I realized that a slight flaw is that if the column is full, we get about 7 or so JOptionPanes, but that shouldn't really matter...

Inferfire
  • 11
  • 2

0 Answers0