Trouble getting value from a HashMap

I'm working on the undo last move feature for a game I'm working on. I'm storing all the previous moves in a HashMap. My problem is that what ever boad I try to get out of the HashMap it appears to be the current board and not the previous one like it should be. So I'm guessing I still don't understand how HashMap works or its not sending anything back.

Part of the class is below.

publicclass MovesLogger{

private HashMap<Integer, MancalaBoard> previousMoves =

new HashMap<Integer, MancalaBoard>();

privateint index=0;

public MovesLogger(){

}

publicvoid add(MancalaBoard mB){

previousMoves.put(index, mB);

index++;

}

public MancalaBoard getABoard(int movesAgo){

return previousMoves.get(index-movesAgo);

}

This is the code that is executed when the user clicks on the undo last move option.

It is in a separate class than the code above. The mancalaBoard that is used here is the current board, and I'm trying to set is equal to the board that came before it.

elseif(mFrame.getUndoLastItem()==event.getSource()){

mancalaBoard=moveLog.getABoard(1);

upDateBoard();

}

Thank you for any help or suggestions.

[1959 byte] By [slackware_javaa] at [2007-11-27 1:54:49]
# 1

Hi,

A HashMap is a little big for an undo situation, I suggest to use

a Vector or a Stack. Anyway, one of your problem is that you didn't

(in getABoard) remove the object you get so the stack keep track

of the move you just undid.

Another thing to check is when you stack your move, use a different

instance of MancalaBoard. Otherwise, all the move contained in your

HashMap will be the same (i.e. the last one).

Hope that help,

Jack

jack@square6a at 2007-7-12 1:27:02 > top of Java-index,Java Essentials,Java Programming...
# 2

So would something like this work:

public void add(MancalaBoard mB){

previousMoves.put(index, new MancalaBoard(mB));

index++;

}

public MancalaBoard getABoard(int movesAgo){

MancalaBoard newBoard = previousMoves.get(index-movesAgo);

index--;

return newBoard;

}

So now everytime it adds to the HashMap it make a new instance of the MancalaBoard.

And if I subtract one from the index then later when I got to add to it again, will it replace what ever is at that index?

I'm not very familiar with HashMap and I think thats partly whats leading to my confusion. I'm not familiar with a Stack either but I'll look into it.

slackware_javaa at 2007-7-12 1:27:02 > top of Java-index,Java Essentials,Java Programming...
# 3

>

> public MancalaBoard getABoard(int movesAgo){

>

> MancalaBoard newBoard =

> = previousMoves.get(index-movesAgo);

> index--;

> return newBoard;

> }[/code]

>

The problem I see now is when this function is called with

an argument different of 1... (index--).

> And if I subtract one from the index then later when

> I got to add to it again, will it replace what ever

> is at that index?

Yep

> I'm not very familiar with HashMap and I think thats

> partly whats leading to my confusion. I'm not

> familiar with a Stack either but I'll look into it.

The Stack is, to my opinion, better suited for your need.

Hope that help,

Jack

jack@square6a at 2007-7-12 1:27:02 > top of Java-index,Java Essentials,Java Programming...