|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Java help - not sure if HashMap is working right
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||||
|
Photography nut
![]() |
Well for my final class project I'm making the game mancala. It works and now I'm just adding the features, and the undo feature is what has me stuck.
I'm using a HashMap list to keep track of what the board looked like on any given previous move. Here is what that class looks like. Inside the class MancalaBoard is what keeps track of the current board. The locations on the board is an array. Code:
public class MovesLogger {
private HashMap<Integer, MancalaBoard> previousMoves =
new HashMap<Integer, MancalaBoard>();
private int index=0;
public MovesLogger(){}
public void add(MancalaBoard mB){
previousMoves.put(index, mB);
index++;
}
public MancalaBoard getABoard(int movesAgo){
return previousMoves.get(index-movesAgo);
}
public int getNumberOfMoves(){
return index;
}
public void clear(){
previousMoves.clear();
}
}
Code:
if(mFrame.getUndoLastItem()==event.getSource()){
mancalaBoard=moveLog.getABoard(1);
upDateBoard();
}
moveLog.getABoard() returns a board and I want to set the current board that is being used to be used. But its not working. It appears that the board that is returned is the same as the current , Even if I make a few moves and then just send it the board at index 0. I'm in desperate need of help. Thanks!
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
||||||||||||||
|
|
|
|
#2 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
There are two possibilities I see from a quick scan of the code. First off you can try to cast movesAgo from int to Integer (doubt that matters - but w/e)
Also, does the hashmap return an Object or a mancalaBoard?
|
|||||||||||||
|
|
|
|
#3 (permalink) | ||||||||||||||
|
Photography nut
![]() |
From examples I've found it seems it returns as a mancalaBoard.
And doesn't Auto-boxing take care of the whole int to Integer thing now? I haven't any luck with help on the Java forums even. ![]() So I hope you can help me figure it out ![]() I'm not messing with it now, as I need to reorganize some of my code, as I'm having scope issues ![]()
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
Last edited by dangerousHobo : 04-22-07 at 11:28 PM. |
||||||||||||||
|
|
|
|
#4 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
I would suggest using an ArrayList rather than a hashMap if even just for debug purposes
|
|||||||||||||
|
|
|
|
#5 (permalink) | ||||||||||||||
|
Photography nut
![]() |
Yeah that is what I was thinking of doing tomorrow in still no luck with HashMap by then.
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
||||||||||||||
|
|
|
|
#6 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
Here's the code if you want to try it out
Code:
public class MovesLogger {
private ArrayList previousMoves =
new ArrayList();
public MovesLogger(){}
public void add(MancalaBoard mB){
previousMoves.add(mB);
}
public MancalaBoard getABoard(int movesAgo){
if(previousMoves.size() - movesAgo >= 0) return (MancalaBoard)(previousMoves.get(previousMoves.size()-movesAgo));
else return null;
}
public int getNumberOfMoves(){
return previousMoves.size();
}
public void clear(){
previousMoves = new ArrayList();
}
}
The get method needs its return value cast to MancalaBoard
Last edited by rabidgnome229 : 04-23-07 at 03:43 AM. |
|||||||||||||
|
|
|
|
#7 (permalink) | ||||||||||||||
|
Photography nut
![]() |
Well I've ditched that class MoveLogger since it was made to undo both 1 move and "N" move. Now I'm only concerned about undoing the last move. So I have the following code.
Problem -> still not working and I'm clueless. Part of One Class Code:
public class MancalaBoard implements Serializable{
private static final long serialVersionUID = 1132708152997695271L;
private int[] board = new int[14];
private boolean isPlayer1Turn=true, winner=false;
/**
*
*
*/
public MancalaBoard(){
newGame();
}
/**
*Copy Constructor
* @param newBoard
*/
public MancalaBoard(MancalaBoard newBoard){
this.board=newBoard.getBoard();
this.isPlayer1Turn=newBoard.isPlayer1Turn();
this.winner=newBoard.winner;
}
/**
*
*
*/
public void newGame(){
board[0]=0;
board[7]=0;
for(int i=1;i<14;++i){
if(i!=7)
board[i]=3;
}
winner=false;
}
}
Code:
public class MancalaManager implements Serializable{
private static final long serialVersionUID = 8647303086339783792L;
private MancalaBoard mancalaBoard = new MancalaBoard();
private String player1Name=" Player 1", player2Name="Player 2 ";
private boolean singlePlayer=true, twoPlayer=false, computerOnly=false;
private MancalaBoard previousBoard;
/**
* Default Constructor
*
*/
public MancalaManager(){}
public MancalaManager(MancalaManager newMancalaManager){
this.mancalaBoard=newMancalaManager.mancalaBoard;
this.player1Name=newMancalaManager.player1Name;
this.player2Name=newMancalaManager.player2Name;
}
public void setGamePlayType(boolean single, boolean dual, boolean comp){
this.singlePlayer=single;
this.twoPlayer=dual;
this.computerOnly=comp;
}
public void undoLastMove(){
mancalaBoard=new MancalaBoard(previousBoard);
}
public void upDataPreviousBoard(){
previousBoard=new MancalaBoard(mancalaBoard);
}
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
||||||||||||||
|
|
|
|
#8 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
What happens when you call the function?
Just a few style things tho The variables should be initialized in the constructors rather than when they are declared. Also - ditch the single/dual/comp variables and use an int numPlayers that goes from 0-2 (0 for computer only)
|
|||||||||||||
|
|
|
|
#9 (permalink) | |||||||||||||||
|
Photography nut
![]() |
Quote:
However I found my silly flaw. In the copy constructor that I was calling, it trying to set the newArray equal to the current one, instead of stepping through it. So it was just some silly mistake I over looked. Thanks for the style tips though. I know that is an area I could use some pointers on.
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
|||||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|