Results 1 to 3 of 3
  1. #1
    LoViNgHeArTy is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default Stack problem. Object array pushed into stack came out different.

    As stated, I seem to have problem with Stack. I'm using it for an undo function which when clicked, it retrieve and remove the object array from the stack and repopulate an array in the class but somehow it came out different.

    I'm making a chess game for my school assignment, require an undo function therefore a stack was required.

    I can't explain much here. I've attach a zip file which contain my codes.
    My stack is at the GameLogic Class.

    At line 90 is where I pushed the object array into the stack, below that is a for loop that generate a print out to the console of what it looks like.

    At line 440 is where I pop the stack which I attempt to retrieve the object array stored inside the stack and repopulate the array but like I said, it was different from when I pushed it in

    use GUILayout to start the game.

    I really appreciate any help I can get. I've been spending hours unable to find the problem.

    Attachment 2647.
    Last edited by LoViNgHeArTy; 01-14-2012 at 06:54 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Stack problem. Object array pushed into stack came out different.

    Quote Originally Posted by LoViNgHeArTy View Post
    I can't explain much here. I've attach a zip file which contain my codes.

    That may limit our ability to help you. Why this statement?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Stack problem. Object array pushed into stack came out different.

    For your benefit and ours, let's post the code for the file that is using a Stack:

    Java Code:
    import java.awt.Color;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Stack;
    
    public class GameLogic {
    	private Object[] cells;
    	private ArrayList<Integer> tempMoveKeys;
    	private HashMap<Integer, Object> whitePieces, blackPieces;
    	private Stack<Object[]> storage;
    	private Stack<Boolean> playerTurns;
    	private Stack<String> moveStorage;
    	private boolean player = true;
    	private HashMap<String, Integer> whiteScore, blackScore;
    
    	public GameLogic(int size) {
    		whiteScore = new HashMap<String, Integer>(); 
    		blackScore = new HashMap<String, Integer>();
    		storage = new Stack<Object[]>();
    		moveStorage = new Stack<String>();
    		playerTurns = new Stack<Boolean>();
    		tempMoveKeys = new ArrayList<Integer>();
    		whitePieces = new HashMap<Integer, Object>(); 
    		blackPieces = new HashMap<Integer, Object>();
    		cells = new Object[size*size];
    
    		for (int i = 0; i < cells.length; i++) {
    			cells[i] = new Blank("?", Color.MAGENTA);
    		}
    	}
    
    	public boolean getPlayer() {
    		return player;
    	}
    
    	public void addScore(String playerCol, String piece) {
    		if (playerCol.equalsIgnoreCase("W")) {
    			whiteScore.put(piece, whiteScore.get(piece)+1);
    		}
    		else {
    			blackScore.put(piece, blackScore.get(piece)+1);
    		}
    	}
    
    	public void switchPlayer() {
    		if (player) {
    			player = false;
    		} else {
    			player = true;
    		}
    	}
    
    	public void start() {
    
    		for (int i = 48; i < 56; i++) {
    			whitePieces.put(i, new Pawn("W", Color.BLUE));
    		}
    
    		for (int i = 8; i < 16; i++) {
    			blackPieces.put(i, new Pawn("B", Color.BLACK));
    		} 
    
    		whitePieces.put(63, new Rook("W", Color.BLUE));
    		whitePieces.put(56, new Rook("W", Color.BLUE));
    		blackPieces.put(0, new Rook("B", Color.BLACK));
    		blackPieces.put(7, new Rook("B", Color.BLACK)); 
    
    		whitePieces.put(62, new Knight("W", Color.BLUE));
    		whitePieces.put(57, new Knight("W", Color.BLUE));
    		blackPieces.put(1, new Knight("B", Color.BLACK));
    		blackPieces.put(6, new Knight("B", Color.BLACK));
    
    		whitePieces.put(61, new Bishop("W", Color.BLUE));
    		whitePieces.put(58, new Bishop("W", Color.BLUE)); 
    		blackPieces.put(2, new Bishop("B", Color.BLACK));
    		blackPieces.put(5, new Bishop("B", Color.BLACK));
    
    		whitePieces.put(60, new King("W", Color.BLUE)); 
    		whitePieces.put(59, new Queen("W", Color.BLUE));
    		blackPieces.put(3, new King("B", Color.BLACK));
    		blackPieces.put(4, new Queen("B", Color.BLACK));
    	}
    
    	public void movePiece(String espression) {
    		String[] pos;
    		int indexOne, indexTwo, score;
    		String piece;
    		Object[] cellStore = cells;
    		storage.push(cellStore);
    		
    		
    		Object[] tempCells = storage.peek();
    
    		int counter = 1;
    
    		for (Object i : tempCells) {
    			System.out.print(((ChessPiece)i).getPiece());
    			if (counter%8 == 0) {
    				counter = 1;
    				System.out.println();
    			}
    			else {
    				counter++;	
    			}
    		}
    		
    		
    		playerTurns.push(player);
    		moveStorage.push(espression);
    		if (espression.contains("x")) {
    			pos = espression.split("x");
    			indexOne = getCellIndex(pos[0].toUpperCase());
    			indexTwo = getCellIndex(pos[1].toUpperCase());
    			if (player) {
    				piece = ((ChessPiece)blackPieces.remove(indexTwo)).getPiece();
    
    				if (whiteScore.containsKey(piece)) {
    					score = whiteScore.get(piece);
    
    					whiteScore.put(piece, score+1);	
    				}
    				else {
    					whiteScore.put(piece, 1);
    				}
    
    				((ChessPiece)whitePieces.get(indexOne)).setStartUsed(true);
    				whitePieces.put(indexTwo, whitePieces.remove(indexOne));
    			}
    			else {
    				piece = ((ChessPiece)whitePieces.remove(indexTwo)).getPiece();
    
    				if (blackScore.containsKey(piece)) {
    					score = blackScore.get(piece);
    					blackScore.put(piece, score+1);	
    				}
    				else {
    					blackScore.put(piece, 1);
    				}
    				((ChessPiece)blackPieces.get(indexOne)).setStartUsed(true);
    				blackPieces.put(indexTwo, blackPieces.remove(indexOne));
    			}
    		}
    		else {
    			pos = espression.split("-");
    			indexOne = getCellIndex(pos[0].toUpperCase());
    			indexTwo = getCellIndex(pos[1].toUpperCase());
    			if (player) {
    				((ChessPiece)whitePieces.get(indexOne)).setStartUsed(true);
    				whitePieces.put(indexTwo, whitePieces.remove(indexOne));
    			}
    			else {
    				((ChessPiece)blackPieces.get(indexOne)).setStartUsed(true);
    				blackPieces.put(indexTwo, blackPieces.remove(indexOne));
    			}
    		}
    		cells[getCellIndex(pos[1].toUpperCase())] = cells[getCellIndex(pos[0].toUpperCase())];
    		cells[getCellIndex(pos[0].toUpperCase())] = new Blank("", Color.MAGENTA);
    	}
    
    	public int getCellIndex(String pos) {
    		int letterNum = LettersLookUp.valueOf(Character.toString(pos.charAt(0))).getValue();
    		int number = Integer.parseInt(Character.toString(pos.charAt(1))) - 1;
    		return letterNum + ((7-number)*8);
    	}
    
    	public boolean checkCapture(int startIndex, int moveIndex, String playerCol) {
    		ArrayList<Coordinate> coor;
    		boolean capture = false, specific = false;
    		int index = 0, counter = 1;
    
    		if (playerCol.equalsIgnoreCase("W")) {
    			specific = ((ChessPiece)whitePieces.get(startIndex)).getSpecificLocation();
    			coor = ((ChessPiece)whitePieces.get(startIndex)).captureCoordinate();
    		}
    		else {
    			specific = ((ChessPiece)blackPieces.get(startIndex)).getSpecificLocation();
    			coor = ((ChessPiece)blackPieces.get(startIndex)).captureCoordinate();
    		}
    		for (Coordinate i : coor) {
    			if (specific) {
    				index = calculateDirections(i.getDirection(), startIndex, i.getAmount(), i.getSpecific() ,playerCol);
    				if (index == moveIndex && containOpponentPiece(moveIndex, playerCol)) {
    					capture = true;
    					break;
    				}
    			}
    			else {
    				while((i.getAmount() != 0 ? i.getAmount() >= counter : true)) {
    					index = calculateDirections(i.getDirection(), startIndex, 
    							(i.getAmount() != 0 ? counter : i.getAmount()+ counter), i.getSpecific() + counter ,playerCol);
    
    					if (index == moveIndex && containOpponentPiece(moveIndex, playerCol)) {
    						capture = true;
    						break;
    					}
    					counter++;
    				}
    				if (capture) {
    					break;
    				}
    			}
    		}
    		return capture;
    	}
    
    	public void reveal(String playerCol) {
    		if (playerCol.equalsIgnoreCase("W")) {
    			revealMap(whitePieces, "W");
    		}
    		else {
    			revealMap(blackPieces, "B");
    		}
    	}
    
    	private void revealMap(HashMap<Integer, Object> pos, String playerCol) {
    		ArrayList<Coordinate> dir;
    		for (Map.Entry<Integer, Object> i : pos.entrySet()) {
    			dir = new ArrayList<Coordinate>();
    			if (!((ChessPiece)i.getValue()).getStartUsed()) {
    				dir.addAll(((ChessPiece)i.getValue()).getSpecialStart());
    				dir.addAll(((ChessPiece)i.getValue()).revealCoordinates());
    			}
    			else {
    				dir.addAll(((ChessPiece)i.getValue()).revealCoordinates());
    			}
    			
    			for (Coordinate ai : dir) {
    				revealDirections(i.getKey(), ai, playerCol, false);
    			}
    		}
    	}
    
    	public void revealMove(int cellIndex, String playerCol) {
    		ArrayList<Coordinate> move;		
    		
    		if (!((ChessPiece)cells[cellIndex]).getStartUsed()) {
    			move = ((ChessPiece)cells[cellIndex]).getSpecialStart();
    		}
    		else {	
    			move = ((ChessPiece)cells[cellIndex]).moveCoordinate();
    		}
    
    		for (Coordinate i : move) {
    			revealDirections(cellIndex, i, playerCol, true);
    		}
    	}
    
    	private void revealDirections(int startPos, Coordinate ai ,String player, boolean move) {
    		int cellIndex = 0, counter = 1;
    		boolean breakOut = false;
    		try {
    			Coordinate tempCoor = ai;
    
    			if (player.equalsIgnoreCase("W")) {
    				cells[startPos] = whitePieces.get(startPos);
    			}
    			else {
    				cells[startPos] = blackPieces.get(startPos);
    			}
    
    			if (((ChessPiece)cells[startPos]).getSpecificLocation()) {
    
    				cellIndex = calculateDirections(tempCoor.getDirection(), startPos, tempCoor.getAmount(), 
    						tempCoor.getSpecific(), player);
    
    				if (whitePieces.containsKey(cellIndex)) {
    					cells[cellIndex] = whitePieces.get(cellIndex);
    				}
    				else if (blackPieces.containsKey(cellIndex)) {
    
    					cells[cellIndex] = blackPieces.get(cellIndex);
    				}
    				else {
    					cells[cellIndex] = new Blank("", Color.BLACK);
    				}
    
    				if (move) {
    					tempMoveKeys.add(cellIndex);
    				}
    			}
    			else {
    
    				while((tempCoor.getAmount() != 0 ? tempCoor.getAmount() >= counter : true)) {
    
    					cellIndex = calculateDirections(tempCoor.getDirection(), startPos, 
    							(tempCoor.getAmount() != 0 ? counter : tempCoor.getAmount()+ counter), tempCoor.getSpecific() + counter ,player);
    
    					if (whitePieces.containsKey(cellIndex)) {
    						cells[cellIndex] = whitePieces.get(cellIndex);
    						breakOut = true;;
    					}
    					else if (blackPieces.containsKey(cellIndex)) {
    						cells[cellIndex] = blackPieces.get(cellIndex);
    						breakOut = true;;
    					}
    					else {
    						cells[cellIndex] = new Blank("", Color.MAGENTA);
    					}
    
    					if (move) {
    						tempMoveKeys.add(cellIndex);
    					}
    
    					if (breakOut) {
    						break;
    					}
    					counter++;
    				}
    			}
    		} catch(IndexOutOfBoundsException e) {}
    	}
    
    	private int calculateDirections(Directions dir, int start, int amount, int specific ,String player) {
    		int previous = start / 8;
    		int current = 0;
    
    		if (player.equalsIgnoreCase("W")) {
    			switch(dir) {
    			case FORWARD: return start - (amount*8);
    
    			case BACKWARD: return start + (amount*8);
    
    			case LEFT: 
    				current = (start - amount) / 8; 
    				return previous == current  ? start - amount : -1;
    
    			case RIGHT: 
    				current = (start + amount) / 8; 
    				return previous == current ? start + amount : -1;
    
    			case FORWARD_LEFT: 
    				current = (start-(amount*8)-specific)%8;
    				previous = start%8; 
    				return previous >= current ? start - (amount*8) - specific : -1;
    
    			case FORWARD_RIGHT: 
    				current = (start - (amount*8) + specific)%8;
    				previous = start%8;
    				return previous <= current ? start - (amount*8) + specific : -1;
    
    			case BACKWARD_LEFT: 
    				current = (start + (amount*8) - specific)%8;
    				previous = start%8; 
    				return previous >= current ? start + (amount*8) - specific : -1;
    
    			case BACKWARD_RIGHT: 
    				current = (start + (amount*8) + specific)%8;
    				previous = start%8;
    				return previous <= current ? start + (amount*8) + specific : -1;
    			}
    		}
    		else {
    			switch(dir) {
    			case FORWARD: return start + (amount*8);
    
    			case BACKWARD: return start - (amount*8);
    
    			case LEFT: 
    				current = (start + amount) / 8; 
    				return previous == current  ? start + amount : -1;
    
    			case RIGHT: 
    				current = (start - amount) / 8; 
    				return previous == current ? start - amount : -1;
    
    			case FORWARD_LEFT: 
    				current = (start + (amount*8) + specific)%8;
    				previous = start%8; 
    				return previous <= current ? start + (amount*8) + specific : -1;
    
    			case FORWARD_RIGHT: 
    				current = (start + (amount*8) - specific)%8;
    				previous = start%8; 
    				return previous >= current ? start + (amount*8) - specific : -1;
    
    			case BACKWARD_LEFT: 
    				current = (start - (amount*8) + specific)%8;
    				previous = start%8;
    				return previous <= current ? start - (amount*8) + specific : -1;
    			case BACKWARD_RIGHT: 
    				current = (start - (amount*8) - specific)%8;
    				previous = start%8;
    				return start - (amount*8) - specific;
    			}
    		}
    		return -1;
    	}
    
    	public void clearMove() {
    		tempMoveKeys.clear();
    	}
    
    	public void clearMap() {
    		for (int i = 0; i < cells.length; i++) {
    			cells[i] = new Blank("?", Color.MAGENTA);
    		}
    	}
    
    	public Object[] getBoard() {
    		return cells;
    	}
    
    	public HashMap<Integer, Object> getWhitePieces() {
    		return whitePieces;
    	}
    
    	public HashMap<Integer, Object> getBlackPieces() {
    		return blackPieces;
    	}
    
    	public boolean containOpponentPiece(int index, String playerCol) {
    		if (playerCol.equalsIgnoreCase("W")) {
    			if (blackPieces.containsKey(index)) {
    				return true;
    			}
    			else {
    				return false;
    			}
    		}
    		else {
    			if (whitePieces.containsKey(index)) {
    				return true;
    			}
    			else {
    				return false;
    			}			
    		}
    	}
    
    	public ArrayList<Integer> getMoveKeys() {
    		return tempMoveKeys;
    	}
    
    	public boolean undo() {
    		ChessPiece piece;
    		
    		if (storage.isEmpty()) {
    			return false;
    		}
    		else {
    			//cells = storage.pop();
    			
    			Object[] tempCells = storage.peek();
    
    			int counter = 1;
    
    			for (Object i : tempCells) {
    				System.out.print(((ChessPiece)i).getPiece());
    				if (counter%8 == 0) {
    					counter = 1;
    					System.out.println();
    				}
    				else {
    					counter++;	
    				}
    			} 
    			
    			moveStorage.pop();
    			player = playerTurns.pop();
    			whitePieces.clear();
    			blackPieces.clear();
    
    			for (int i = 0; i < cells.length; i++) {
    				piece = ((ChessPiece)cells[i]);
    				if (!piece.getPiece().isEmpty()) {
    					if (Character.toString(piece.getPiece().charAt(0)).equalsIgnoreCase("W")) {
    						whitePieces.put(i, cells[i]);
    					}
    					else if (Character.toString(piece.getPiece().charAt(0)).equalsIgnoreCase("B")) {
    						blackPieces.put(i, cells[i]);
    					}
    				}
    			}
    			return true;
    		}
    	}
    
    	public String[] getMoves() {
    		return moveStorage.toArray(new String[moveStorage.size()]);
    	}
    }
    
    /*Object[] tempCells = storage.peek();
    
    int counter = 1;
    
    for (Object i : tempCells) {
    	System.out.print(((ChessPiece)i).getPiece());
    	if (counter%8 == 0) {
    		counter = 1;
    		System.out.println();
    	}
    	else {
    		counter++;	
    	}
    } */

Similar Threads

  1. Implementing a stack as an array
    By fam2315 in forum New To Java
    Replies: 2
    Last Post: 06-22-2011, 03:55 PM
  2. trouble creating stack using array
    By shashankc in forum New To Java
    Replies: 5
    Last Post: 01-20-2011, 12:49 PM
  3. object stack memory id apeears instead of object itself
    By Embercloud in forum New To Java
    Replies: 7
    Last Post: 01-02-2011, 05:06 PM
  4. Stack problem..pls help
    By Mika in forum New To Java
    Replies: 1
    Last Post: 02-16-2009, 08:10 AM
  5. Replies: 1
    Last Post: 03-25-2008, 05:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •