Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 04-21-07   #1 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,470

FAQs Submitted: 7
Folding Team Rank: 390
Trader Rating: 0
Default Java help - not sure if HashMap is working right

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();
	}
}
So when the user clicks the undo move action, this is the code that is executed:
Code:
if(mFrame.getUndoLastItem()==event.getSource()){
				mancalaBoard=moveLog.getABoard(1);
				upDateBoard();
}
My problem is that the board is not changing.
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:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

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

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64
Monitor
Acer AL2216W 22" WS LCD
dangerousHobo is offline I fold for Overclock.net Overclocked Account dangerousHobo's Gallery   Reply With Quote
Old 04-22-07   #2 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,975
Blog Entries: 1

Rep: 564 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

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?
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 04-22-07   #3 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,470

FAQs Submitted: 7
Folding Team Rank: 390
Trader Rating: 0
Default

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:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

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

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64
Monitor
Acer AL2216W 22" WS LCD

Last edited by dangerousHobo : 04-22-07 at 11:28 PM.
dangerousHobo is offline I fold for Overclock.net Overclocked Account dangerousHobo's Gallery   Reply With Quote
Old 04-22-07   #4 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,975
Blog Entries: 1

Rep: 564 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

I would suggest using an ArrayList rather than a hashMap if even just for debug purposes
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 04-23-07   #5 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,470

FAQs Submitted: 7
Folding Team Rank: 390
Trader Rating: 0
Default

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:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

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

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64
Monitor
Acer AL2216W 22" WS LCD
dangerousHobo is offline I fold for Overclock.net Overclocked Account dangerousHobo's Gallery   Reply With Quote
Old 04-23-07   #6 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,975
Blog Entries: 1

Rep: 564 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

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();
	}
}
**EDIT**
The get method needs its return value cast to MancalaBoard
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw

Last edited by rabidgnome229 : 04-23-07 at 03:43 AM.
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 04-24-07   #7 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,470

FAQs Submitted: 7
Folding Team Rank: 390
Trader Rating: 0
Default

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;
	}

}
Part of another class where the undo last move is happening
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:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

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

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64
Monitor
Acer AL2216W 22" WS LCD
dangerousHobo is offline I fold for Overclock.net Overclocked Account dangerousHobo's Gallery   Reply With Quote
Old 04-24-07   #8 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,975
Blog Entries: 1

Rep: 564 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

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)
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 04-24-07   #9 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,470

FAQs Submitted: 7
Folding Team Rank: 390
Trader Rating: 0
Default

Quote:
What happens when you call the function?
It was only changing the title that said who's turn it is.

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:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

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

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64
Monitor
Acer AL2216W 22" WS LCD
dangerousHobo is offline I fold for Overclock.net Overclocked Account dangerousHobo's Gallery   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 01:28 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.35981 seconds with 8 queries