0

So I am having trouble adding actionlisteners using the method addActionListeners(), which is between some system.out.printlns so that I could tell that the method was actually working.

protected void whoFirst(String first) {
    int currPlayer = 0;
    System.out.println("Hello");
    addActionListeners();
    System.out.println("How are you?");
    if(first == "player1") {
        player1.setVisible(true);
        currPlayer = 1;
    }
    if(first == "player2") {
        player2.setVisible(true);
        currPlayer = 2;
    }
}

The add actionlistener method I have tried many different ways such as making the class implement an actionListener, and using player1Cards[i].addActionListener(this);... This didn't work so I changed to this:

private void addActionListeners() {
            System.out.println("Number of players = : " + players );
            for(int i = 0; i == player1Cards.length ; i++) {
            if(players == 2) {
                player1Cards[i].addActionListener(e -> cardActions());
                player2Cards[i].addActionListener(e -> cardActions());
            }
            if(players == 3) {
                player1Cards[i].addActionListener(e -> cardActions());
                player2Cards[i].addActionListener(e -> cardActions());
                player3Cards[i].addActionListener(e -> cardActions());
            }
            if(players == 4) {
                player1Cards[i].addActionListener(e -> cardActions());
                player2Cards[i].addActionListener(e -> cardActions());
                player3Cards[i].addActionListener(e -> cardActions());
                player4Cards[i].addActionListener(e -> cardActions());
            }
        }
    }

This is how it is currently, after finding a java 8 tutorial (I am using java 8, so should be fine?) If its not obvious the JButtons are in a collection and all the same size as all the players get the same amount of cards to start with. This is my method which is supposed to call no matter which player goes first... But it never prints a line to the console...

private void cardActions() {
    System.out.println("Whats up?");
}

I feel like this should have worked in either of the cases but if anyone has any suggestions that would help that would be fantastic. Thanks in advance.

bradimus
  • 2,472
  • 1
  • 16
  • 23
Tim Weeks
  • 9
  • 1

1 Answers1

1

Some things are not quite right in your code.

  1. Your for loop is not correct:

    for (int i = 0; i == player1Cards.length; i++)
    

    must be

    for (int i = 0; i < player1Cards.length; i++)
    

    Your for loop can be rewritten to:

    {
        int i = 0;
        while (i == player1Cards.length) {
            // code inside for loop
            i++;
        }
    }
    

    Because apparently, the length of player1Cards is always greater than 0, the condition i == player1Cards.length is false at the first loop, causing the for loop to immediately abort.

  2. You are comparing strings with ==. Never do that! Always use equals() to compare strings. That is because for object references, == compares the identity (memory location) of the objects. For strings, it's the same. That's why a string with value "player1" has not always the same identity as another string with the same value. The equals() method is designed to compare the values of the objects being compared.

    As hinted by Zabuza, this answer on StackOverflow explains more about what's the difference between == and .equals().


You should also avoid variable repetion like player1Cards, player2Cards et cetera. What if you extend the game and allow 16 players? You have to copy-paste a lot of things. One way to address that problem is to use an array for the players, for example playerCards[]. Also, you should read up a little bit more about object-orientation in Java. It'll guide you how en when to use classes and objects.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • To extend the answer: Also see https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java for a good explanation about `==` versus `equals` on `String`s. – Zabuzard Jul 14 '17 at 21:29
  • I only have 4 players because they have 11 cards each. There are not 55 cards in a deck. Thank you for your answer. I will change some things around. :) – Tim Weeks Jul 15 '17 at 09:03