Pregunta

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

¿Fue útil?

Solución

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.

Otros consejos

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top