Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Row is column and vice versa?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Row is column and vice versa?

    For some reason, my code is that column is acting as a row and row acting as a column. Row should be the horizontal count, column being the vertical (as specified by my professor), however they have flipped. If I change the scanner so they take col first then it works correctly.

    import java.util.*;
    public class Bejeweled {
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		System.out.println("How to play: type a row and column index, followed by the direction to move it: u (up), r (right), d (down), l (left)");
    		char[][] gameChars = new char[8][8];
    		char[] options = new char[] {'*', '$', '@', '+', '!', '&'};
    		populateRandom(gameChars, options);
    		drawGrid(gameChars);
    		System.out.println("Enter <row> <column> <direction to move>, or q to quit");
    		if (scan.hasNextInt()) {
    			int row = scan.nextInt();
    			int col = scan.nextInt();
    			String direction = scan.next();
    			while (isWithinGrid(row,col,direction,gameChars) == false) {
    				System.out.println("Enter <row> <column> <direction to move>, or q to quit");
    				row = scan.nextInt();
    				col = scan.nextInt();
    				direction = scan.next();
    			}
    			swap((row-1), (col-1), direction, gameChars);
    			drawGrid(gameChars);
    			}
    		else {System.out.print("Adios!");
    		}
    	}
    	public static void drawGrid(char[][] grid) {
    		for (int i = 0; i < grid.length; i++) {
    			for (int j = 0; j < grid[i].length; j++) {
    				System.out.print(grid[i][j] + " ");
    			}
    			System.out.println();
    		}
    	}
    	public static void populateRandom(char[][] grid, char[] options) {
    		Random randomNum = new Random();
    		for (int i = 0; i < grid.length; i++) {
    			for (int j = 0; j < grid[i].length; j++) {
    				int randomChar = randomNum.nextInt(5);
    				grid[i][j] = options[randomChar];
    			}
    		}
    	}
    	public static boolean isWithinGrid(int row, int col, String cmd, char[][] grid) {
    		if (row > 1 && row < 8) {
    			if (col > 1 && col < 8) {
    				if (cmd.equals("u") | cmd.equals("l") | cmd.equals("r") | cmd.equals("d")) {
    					return true;
    				}
    				else {return false;}
    			}
    			else if (col == 8) {
    				if (cmd.equals("u") | cmd.equals("l") | cmd.equals("r")) {
    					return true;
    				}
    				else {return false;}
    			}
    			else if (col == 1) {
    				if (cmd.equals("d") | cmd.equals("l") | cmd.equals("r")) {
    					return true;
    				}
    				else {return false;}
    			}
    			else {return false;}
    		}
    		else if (row == 8) {
    			if (col > 1 && col > 8) {
    				if (cmd.equals("u") | cmd.equals("l") | cmd.equals("d")) {
    						return true;
    				}
    				else {return false;}
    			}
    			else if (col == 8) {
    				if (cmd.equals("u") | cmd.equals("l")) {
    					return true;
    				}
    				else {return false;}
    			}
    			else if (col == 1) {
    				if (cmd.equals("d") | cmd.equals("r") | cmd.equals("l")) {
    					return true;
    				}
    				else {return false;}
    			}
    		}
     
    		else if (row == 1) {
    			if (col > 1 && col < 8) {
    				if (cmd.equals("u") | cmd.equals("r") | cmd.equals("d")) {
    					return true;
    				}
    				else {return false;}
    			}
    			else if (col == 1) {
    				if (cmd.equals("r") | cmd.equals("d")) {
    					return true;
    				}
    			else if (col == 8) {
    				if (cmd.equals("r") | cmd.equals("u")) {
    					return true;
    				}
    			}
    			else {return false;}
    			}
    		} else {
    			return false;
    		}
    		return false;
     
    	}
    	public static void swap(int row, int col, String cmd, char[][] grid) {
    		if (cmd.equals("u")) {
    			char temp = grid[row][col];
    			grid[row][col] = grid[row-1][col];
    			grid[row-1][col] = temp;
    		}
    		if (cmd.equals("d")) {
    			char temp = grid[row][col];
    			grid[row][col] = grid[row+1][col];
    			grid[row+1][col] = temp;
    		}
    		if (cmd.equals("l")) {
    			char temp = grid[row][col];
    			grid[row][col] = grid[row][col-1];
    			grid[row][col-1] = temp;
    		}
    		if (cmd.equals("r")) {
    			char temp = grid[row][col];
    			grid[row][col] = grid[row][col+1];
    			grid[row][col+1] = temp;
    		}
    	}
     
    }\


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Row is column and vice versa?

    Is there so output that shows what you are talking about? Post the output and add some comments explaining what the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Row is column and vice versa?

    EDIT: Figured it out, my rows/cols were swapped in the swap method. Oops.

Similar Threads

  1. How do I convert a char into a BigInteger and vice versa?
    By ineedahero in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 12th, 2013, 09:53 PM
  2. Celsius to fahrenheit vice versa
    By Onlyname in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 16th, 2013, 11:39 PM
  3. sum of each row & column in 2-dimensional array.
    By muhammad waqar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2013, 08:09 AM
  4. [SOLVED] how to get the row and column of a component gridlayout
    By prettynew in forum AWT / Java Swing
    Replies: 0
    Last Post: March 13th, 2011, 06:39 PM
  5. Highlighting both row and column in JTable
    By bschneider14 in forum AWT / Java Swing
    Replies: 4
    Last Post: May 29th, 2010, 09:14 AM