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 ]