0

I am in the process of updating my game to Java, but the only minor bug I have left is this:

Every time a dead player's name is "added" to the dead player name list, it adds the Player object's hashcode instead.

EDIT: Here is a link to a zip folder with the source code: https://dl.dropboxusercontent.com/u/98444970/KarmaSource.zip

The code in question is the two places where the line gets the player object and gets the object's name. When it is used in println, it works fine and prints the player's name. However, in the second part where it does the same thing, but it prints the hashcode of the player object instead of calling its get_name method and returning the String. I'm not sure if it has to do with the third part, where it adds the "name" to dead player list pdead.

If you'd like a link to the compiled version, let me know. It's compiled under JDK 7 Update 51 64-bit.

EDIT: I got it working, I was originally referencing the players list instead of the pdead list. Thanks to all who contributed to helping. If you still want the game, let me know and I'll put a download link :D

4
  • Shouldn't it just be System.out.println(pdead.get(index).getName());? Commented Mar 20, 2014 at 20:57
  • @Keppil I thought this at first as well, but pdead is a list of Strings. I think the important part we are missing is where instances of the Player class are created and their names are set. Commented Mar 20, 2014 at 20:59
  • This doesn't look right, your main method declaration isn't valid. Is this really the code you've seen the issue in? If not please provide an exact example of the code which produces the problem. Commented Mar 20, 2014 at 21:23
  • There are also semicolons in places where they aren't allowed. This code wouldn't even compile. The comments originally used backslashes. Show us your actual code if you expect an answer. Commented Mar 20, 2014 at 21:34

2 Answers 2

1

Answering your question:

This code is wrong: if (karma.pdead.isEmpty()) {System.out.println("None");} else for (int index = 0;index < karma.pdead.size();index++) System.out.println(pdead.get(index));

What is karma? Whatever that is, looks like you're referring to 2 different things there.

Try this:

if (pdead.isEmpty()) { System.out.println("None"); } else { for (String deadPlayer : pdead) { System.out.println(deadPlayer); } }

Pretty sure this will work :)

Some further, constructive advice:

Your code is breaking pretty much all conventions/good-practices I know in Java. But I am here to help, not to criticize, so let's try to improve this code.

  • Never keep state in static fields. This is a recipe for causing memory leaks.
  • your main function won't even compile. Should look like this:

    public static void main(String[] args)

  • Always wrap the body of for loops with braces.

  • Be consistent: if you open braces in a new line, then do it every time. NEVER write code on the same line as the opening bracket.

GOOD:

public void doSomething()
{
  // body
}

GOOD:

public void doSomething() {
  // body
}

BAD:

public void doSomething() {
  // body
}

public void somethingOther()
{
  // inconsistent!
}

public void terribleCode()
{ System.out.println("Never do this"); }
  • Do not use underscores to separate words. In Java, the favoured convention is to use camelCase. getName(), not get_name().
  • class names ALWAYS start with a capital letter, whereas variable names generally start with a lower-case letter.
  • if you're iterating over all items of a list, just use the forEach construct (shown above) not index navigation.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the tips. I found the piece of code I did wrong, but I will clean up the source using these specifications.
0

I wanted to check to see if there was some subtle syntax error, so I cut/paste your code into an editor and tried to massage it to get it running. After my massaging, I ran it and it ran fine. Here is the code I ran and the results I got:

Code: import java.util.ArrayList;

public class Game {

    private static ArrayList<Player> players = new ArrayList<Player>();
    private static ArrayList<String> pdead = new ArrayList<String>();

    public static void main(String[] args) {
        // Some Test Data
        Player p1 = new Player("George");
        p1.setHp(10);
        players.add(p1);

        Player p2 = new Player("Bob");
        p2.setHp(10);
        players.add(p2);

        // Print Current State of data
        System.out.println("Current Players");
        for(Player p: players) {
            System.out.println(p.getName() + ": " + p.getHp());
        }

        System.out.println("Dead Players");
        if (pdead.isEmpty()) {
            System.out.println("None");
        } else {
            for (int index=0; index < pdead.size(); index++) {
                System.out.println(pdead.get(index));
            }
        }

        // Kill Bob
        p2.setHp(0);

        // Do work to add names to dead players data structure
        for (int index2=0; index2 < players.size(); index2++) {
            if ((players.get(index2).getHp() <= 0) && !(pdead.contains(players.get(index2).getName()))) {
                pdead.add(players.get(index2).getName());
            }
        }

        // Print Current State of data
        System.out.println("Current Players");
        for(Player p: players) {
            System.out.println(p.getName() + ": " + p.getHp());
        }

        System.out.println("Dead Players");
        if (pdead.isEmpty()) {
            System.out.println("None");
        } else {
            for (int index=0; index < pdead.size(); index++) {
                System.out.println(pdead.get(index));
            }
        }
    }
}

class Player {
    private String name = "";
    private int hp = 0;
    public Player(String n) {
        name = n;
    }
    public String getName() {
        return name;
    }
    public int getHp() {
        return hp;
    }
    public void setHp(int h) {
        hp = h;
    }
}

Here are the results that code gives me:

javac Game.java
java Game
Current Players
George: 10
Bob: 10
Dead Players
None
Current Players
George: 10
Bob: 0
Dead Players
Bob

4 Comments

Thank you for the tip (and everyone as well) to use a for each loop. I'll add that in an updated version.
Does a for-each loop work with an ArrayList, or is it only for standard Arrays?
Yes the for each loop works for List and Sets both I believe. It does not work for Maps, you have to instead get the EntrySet from a Map then you can use the for each loop on that instead.
Essentially use the index for-loop similar to the one used above. Main reason I used the index for loop in most of the code is other parts require referring to the index (turn number, other variables with parallel structure, etc.). I updated the code as well to the new format if you'd like to see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.