Results 1 to 2 of 2
  1. #1
    iCon09 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default Help with a method

    I have been assigned to code a clear cell game, and am stuck on a method.
    Java Code:
    /**
    	 * This method will turn to BoardCell.EMPTY any cells immediately surrounding the
    	 * cell at position [rowIndex][colIndex] if and only if those surrounding 
    	 * cells have the same color as the selected cell.  The selected cell will
    	 * also be turned into a BoardCell.EMPTY.  If after processing the surrounding
    	 * cells any rows in the board are empty then those rows will collapse, moving non-empty
    	 * rows upward. If [rowIndex][colIndex] corresponds to an empty cell no 
    	 * action will take place. 
    	 * @throws IllegalArgumentException with message "Invalid row index" for invalid row
    	 * or "Invalid column index" for invalid column.  We check for row validity first.
    	 */
    And from that I have interpreted it thusly:
    Java Code:
    if (board[rowIndex][colIndex] != BoardCell.EMPTY){
    			if (rowIndex >= board.length){
    				throw new IllegalArgumentException("Invalid row index");
    			}
    			if (colIndex >= board[0].length){
    				throw new IllegalArgumentException("Invalid column index");
    			}
    if (colIndex-1 >= 0){
    				if (board[rowIndex][colIndex-1] == board[rowIndex][colIndex]){
    					board[rowIndex][colIndex-1] = BoardCell.EMPTY;
    					score++;
    				}
    			}
    			if (colIndex+1 >= 0 && colIndex+1 < board[0].length){
    				if (board[rowIndex][colIndex+1] == board[rowIndex][colIndex]){
    					board[rowIndex][colIndex+1] = BoardCell.EMPTY;
    					score++;
    				}
    			}
    if (rowIndex-1 >= 0 && rowIndex-1 < board.length){
    				if (board[rowIndex-1][colIndex] == board[rowIndex][colIndex]){
    					board[rowIndex-1][colIndex] = BoardCell.EMPTY;
    					score++;
    				}
    			}
    			if (rowIndex+1 >= 0 && rowIndex+1 < board.length){
    				if (board[rowIndex+1][colIndex] == board[rowIndex][colIndex]){
    					board[rowIndex+1][colIndex] = BoardCell.EMPTY;
    					score++;
    				}
    			}
    I'm lost after that :/, especially the collapsing aspect.

    Comments/criticism?

    Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help with a method

    I can not easily see the alignment of the statement with the opening { and the ending } in your code.
    That makes it very hard to read and understand your code.

    I'm lost after that :/, especially the collapsing aspect.
    Where are you lost? What part is done and what part is needs work?
    Last edited by Norm; 09-22-2011 at 12:04 AM.

Similar Threads

  1. Replies: 18
    Last Post: 04-18-2011, 05:39 AM
  2. Declaring a method inside a method: Runnable?
    By africanhacker in forum New To Java
    Replies: 8
    Last Post: 03-23-2011, 04:30 AM
  3. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  4. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 PM

Posting Permissions

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