• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Referencing an array via a method.

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As above, I'm trying to reference an array via a method in the following code, but it is not acting as I expect it to.

I am trying to update array element zero only, but I'm either updating both, or referencing both somehow, and for the life of me I can't see how

The following code results in the following output:

!!! BEFORE + GOLD
PlayerGold 0 = 30
PlayerGold 1 = 30
PlayerGold 2 = 30
!!! AFTER + GOLD
PlayerGold 0 = 40
PlayerGold 1 = 40
PlayerGold 2 = 30

i.e. both PlayerGold 1 & PlayerGold 2 seem to be getting updated



pD is an array of class Player which has been created and populated at startup e.g.
public Players pD = new Players();
pD.loadPlayers();



final boolean FoundGold() {
System.out.println("!!! BEFORE + GOLD");

System.out.println("PlayerGold 0 = " + pD.Invent(0)[gV.GOLD]);
System.out.println("PlayerGold " + pD.playerNum + " = " + pD.Invent(pD.playerNum)[gV.GOLD]);
System.out.println("PlayerGold " + (pD.playerNum+1) + " = " + pD.Invent(pD.playerNum+1)[gV.GOLD]);

pD.InventAdd(gV.GOLD, 10);
System.out.println("!!! AFTER + GOLD");

System.out.println("PlayerGold 0 = " + pD.Invent(0)[gV.GOLD]);
System.out.println("PlayerGold " + pD.playerNum + " = " + pD.Invent(pD.playerNum)[gV.GOLD]);
System.out.println("PlayerGold " + (pD.playerNum+1) + " = " + pD.Invent(pD.playerNum+1)[gV.GOLD]);


boolean fGold = true;
}








public class Players {

GlobalVariables gV = new GlobalVariables();
private static final int NUMPLAYERS = 6; //4PLAYERS+TEMP+DRAGON

public Player[] players;
int numPlayers = 1;
int playerNum;
int cursedPlayer = 1;

/** Creates a new instance of Players */
public Players() {
}

public void loadPlayers(){
players = new Player[NUMPLAYERS];

for (int i=0; i<players.length; ++i) {
players[i] = new Player();
}

players[5].invent[gV.GOLD] = 0; //Dragon!!!
players[5].invent[gV.WARRIOR] = 0; //Dragon!!!

}

public int[] Invent(int inventPlayerNum) {
return players[inventPlayerNum].invent;
}

public void InventAdd(int itemID, int incAmount) {
//players[0].invent[itemID] += incAmount;
//if (players[0].invent[itemID] > 99) players[0].invent[itemID] = 99;
//if (players[0].invent[itemID] < 0) players[0].invent[itemID] = 0;
//Doesn't work the above way either

Invent(0)[itemID] += incAmount;
if (Invent(0)[itemID] > 99) Invent(0)[itemID] = 99;
if (Invent(0)[itemID] < 0) Invent(0)[itemID] = 0;
}


public class Player {
public int score = 0;
public int[] foundItems = null;

//public int gold = 30;
//public int warriors = 10;
//public int food = 25;
//public boolean[] boughtItems = new boolean[3];
//public int[] invent = new int[6];

public int[] invent = {0,0,0,10,25,30,0,0,0,0,0};

public int kingdom = 0;
public int moves = 0;
public int lastLocation = 0;
public boolean cursed = false;

public void SetTheScore () {
int score = 140 - (moves + 1 + invent[gV.WARRIOR]);
//Add extra 1 to move as move @ end of turn

if (score > 99) score = 99;
if (score < 0) score = 0;
}
}
}
[ January 15, 2007: Message edited by: Paul Carter ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



For starters, it looks like your debugging code may not be showing you what you think it is; it looks like you are actually retrieving the values for PlayerGold 0 and PlayerGold 1 from the exact same place: pD.Invent(pD.playerNum)[gV.GOLD]

Since (as far as I can tell) you do not change pD.playerNum between those two System.out.println() statements, it makes sense that they are printing out the same value. If I'm understanding it correctly, you'll need to fix that before you can say for sure whether the pD.InventAdd() method call is doing what you want it to do.
 
Paul Carter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry - note to self "must not edit code in posts" :roll:

Your right the first line should be showing zero, but the result is the same

Regards

Paul.
 
reply
    Bookmark Topic Watch Topic
  • New Topic