0

I've been trying to build an Elo style ranking system for myself and a few friends so that we could keep track of our performance in just our games together. But I've been having some trouble with what to call the references to both the players and the game objects to keep it as readable as possible. Intuitively, it seems that the reference to the game should be the game number, and the reference to the player should be the playername. Yet as the specifics of each change every game, I can't hardwire these things into the code. So, is there a way to make the object references a user input?

Here are a few things I've tried that have made the compiler yell at me:

    Scanner input = new Scanner(System.in);
    Game input.next() = new Game();  //doesn't work

    String playername;
    for(int i = 0; i < numberOfPlayers; ++i){
        System.out.print("Player name: ");
        playerName = input.next();
        Player playerName = new Player();  //compiler still isn't happy
    }

    //then there's also this-
    String playerName = input.next();
    Player /* ????? */ = new Player(Playername);
    //it seems like a good idea at first, but it doesn't solve the problem at all.

If there is a way to do what I'm trying to get at here, I have no clue how to do it. And if there isn't a way to do this, what am I supposed to call the variables that isn't a completely ambiguous/unreadable reference name?

EDIT: This question was apparently marked as a duplicate of another question asking how to use System.in with the scanner correctly for a variety of things, not including reference names. As this question is asking a completely different thing(this question is asking primarily about reference names, not System.in or Scanner), I assume that the duplicate mark is either an amusing troll or a misunderstanding of my question.

narf0708
  • 100
  • 7
  • haw about assigning each player an id? (and storing them in a file, for instance) – eis Feb 16 '15 at 22:56
  • I will be storing them in a file for the sake of persistence, but giving each a numeric ID is exactly what I meant by ambiguous/unreadable. – narf0708 Feb 17 '15 at 00:33
  • I also disagree with the marking of this as duplicate, but whatever -- glad you got your question answered :) – ThisClark Feb 17 '15 at 00:39

1 Answers1

0

Maybe the Map data structure will serve you better. Take a look at this code and see if it helps you. You may store identifiers as the key and reference to the game or players as the value. Note: If you enter the same key twice you rewrite its value.

Scanner input = new Scanner(System.in);

HashMap<String, Game> games = new HashMap<String, Game>();
HashMap<String, Player> players = new HashMap<String, Player>();

games.put(input.next(), new Game());
players.put(input.next(), new Player());

//do something with game and player properties

System.out.println(games.get("game 1"));
System.out.println(players.get("john"));
ThisClark
  • 12,145
  • 9
  • 61
  • 89