Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package daily.challenge.sand;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class SandFall {
- public static String[][] grid; //Good Old 2D Arrays :D
- static Scanner in = new Scanner(System.in);
- static int gridX, gridY;
- static int steps = 0; //Track the Number of Updates
- static boolean settled = false; //Flag - Are all pieces of sand settled?
- static ArrayList<String> History = new ArrayList<String>();
- public static void main(String[] args){
- System.out.print("The Width and Height of the Map (X, Y): ");
- String str = in.nextLine();
- //How should the input be separated?
- String[] temp = (str.contains(", "))? str.split(", "):(!str.contains(","))?str.split(" "):str.split(",");
- gridX = Integer.parseInt(temp[0]);//String -> int (Readability)
- gridY = Integer.parseInt(temp[1]);//String -> int (Readability)
- grid = new String[gridX][gridY];
- System.out.println("Assemble Map (Items: '#' = Rock, '.' = Sand, 's' = Empty Space):");
- for(int y = 0; y< gridY; y++){
- for (int x = 0; x<gridX; x++){
- grid[x][y] = in.next(); //Item at x, y is the next typed submitted char
- if(grid[x][y].equalsIgnoreCase("s")) grid[x][y] = " ";
- //Due to Scanner taking input upon hitting "Enter"
- //I had to add a Space character, s, it does add a bit
- //of ease of use as well. Look at Usage
- }
- }
- //If Not Settled then Update
- while(!settled){
- update();
- }
- //Once Settled print the final grid once more
- System.out.println("\n\n\n");
- printGrid("");
- System.out.println("\nThe Sand has settled in "+steps+" steps...");
- printHistory();
- }
- static boolean cantSettle = false; //Changed to True if any piece of sand can still fall
- /** Update the position of the sand crystals according to the item below it.
- * Updates ALL items in one step.*/
- public static void update(){
- cantSettle = false;
- steps++;
- for(int y = 0;y<gridY;y++){
- for(int x = 0; x<gridX; x++){
- if(grid[x][y].equalsIgnoreCase(".")){
- if(y == gridY-1) settled = (cantSettle)?false:true; //Boundary Check
- else if(grid[x][y+1].equalsIgnoreCase(" ")){ //Space Check
- grid[x][y] = " "; grid[x][y+1] = ".";//Swap Places
- settled = false;
- cantSettle = true;
- printGrid("Sand Drop at ("+x+", "+y+")");//Print New Grid
- steps++;
- }else if(grid[x][y+1].equalsIgnoreCase("#")){//Rock Check
- settled = (cantSettle)?false:true; //If can Settle then Settle
- }else if(grid[x][y+1].equalsIgnoreCase(".")){ //Sand Check
- if(checkSandCollision(x,y)){ //Sand-Sand is Nuanced so pass to method
- settled = (cantSettle)?false:true; //If is settled then check if you can settle.
- }else{settled = false; cantSettle = true;} //If not settled then unsettle.
- }
- }
- }
- }
- }
- /** Print the Grid with the Change (msg) */
- public static void printGrid(String msg){
- System.out.println(msg.toUpperCase()+"--------------------------------------");
- for(int y = 0;y< gridY; y++){
- if(y>0)System.out.println();
- for (int x = 0; x<gridX; x++){
- System.out.print(grid[x][y] + " ");
- }
- }
- System.out.println("\n"+msg.toUpperCase()+"--------------------------------------\n");
- if(!msg.isEmpty()) History.add(steps +". "+msg);
- }
- /** If sand is colliding with Sand, scan a few pieces below to see if it is done */
- public static boolean checkSandCollision(int x, int y){
- if(grid[x][y+1].equalsIgnoreCase(".")){
- if(y+1 == gridY-1 || y == gridY-1) return true;
- if(grid[x][y+2].equalsIgnoreCase(".")){
- return checkSandCollision(x, y+1);//Woo recursion!
- }else if(grid[x][y+2].equalsIgnoreCase("#")) return true;
- else if(grid[x][y+2].equalsIgnoreCase(" ")) return false;
- //If the item is on the boundary, the movement IS complete (true)
- //If the item below is a rock, say the movement IS complete (true)
- //If the item below is Sand, call this method with the new X and Y
- //If the item below is Air, say the movement is INCOMPLETE (false)
- }
- return true; //If called for no reason
- //by accident then just return true
- }
- public static void printHistory(){ //Simple Prompting Method
- System.out.print("\nPrint History? (Y/N): ");
- in = new Scanner(System.in);
- String str = in.nextLine();
- if(str.equalsIgnoreCase("y")||str.equalsIgnoreCase("yes")){
- System.out.println("\n-----------------------------------------------------");
- System.out.println("SAND FALL HISTORY");
- System.out.println("-----------------------------------------------------");
- for(String s: History){
- System.out.println(s);
- }
- System.out.println("-----------------------------------------------------");
- }else if(!str.equalsIgnoreCase("n") && !str.equalsIgnoreCase("no")){
- System.out.println("Incorrect Input! \n");
- printHistory();
- }
- }
- }
- USAGE:
- The Width and Height of the Map (X, Y): 5 12
- Assemble Map (Items: '#' = Rock, '.' = Sand, 's' = Empty Space):
- . . . . .
- # # # s s
- . # # s .
- . # s # .
- # s . s s
- s s s # s
- . # # s s
- s # # s .
- s . . # #
- s s s s s
- s s s s s
- # . # s s
- SAND DROP AT (1, 9)--------------------------------------
- . . .
- # # # .
- . # # . .
- . # #
- #
- . #
- # # .
- # # .
- # #
- .
- . .
- # . #
- SAND DROP AT (1, 9)--------------------------------------
- SAND DROP AT (2, 9)--------------------------------------
- . . .
- # # # .
- . # # . .
- . # #
- #
- . #
- # # .
- # # .
- # #
- . . .
- # . #
- SAND DROP AT (2, 9)--------------------------------------
- SAND DROP AT (4, 2)--------------------------------------
- . . .
- # # # .
- . # # .
- . # # .
- #
- . #
- # # .
- # # .
- # #
- . . .
- # . #
- SAND DROP AT (4, 2)--------------------------------------
- SAND DROP AT (4, 3)--------------------------------------
- . . .
- # # # .
- . # # .
- . # #
- # .
- . #
- # # .
- # # .
- # #
- . . .
- # . #
- SAND DROP AT (4, 3)--------------------------------------
- SAND DROP AT (4, 4)--------------------------------------
- . . .
- # # # .
- . # # .
- . # #
- #
- . # .
- # # .
- # # .
- # #
- . . .
- # . #
- SAND DROP AT (4, 4)--------------------------------------
- SAND DROP AT (4, 1)--------------------------------------
- . . .
- # # #
- . # # . .
- . # #
- #
- . # .
- # # .
- # # .
- # #
- . . .
- # . #
- SAND DROP AT (4, 1)--------------------------------------
- SAND DROP AT (4, 2)--------------------------------------
- . . .
- # # #
- . # # .
- . # # .
- #
- . # .
- # # .
- # # .
- # #
- . . .
- # . #
- SAND DROP AT (4, 2)--------------------------------------
- SAND DROP AT (4, 3)--------------------------------------
- . . .
- # # #
- . # # .
- . # #
- # .
- . # .
- # # .
- # # .
- # #
- . . .
- # . #
- SAND DROP AT (4, 3)--------------------------------------
- --------------------------------------
- . . .
- # # #
- . # # .
- . # #
- # .
- . # .
- # # .
- # # .
- # #
- . . .
- # . #
- --------------------------------------
- The Sand has settled in 25 steps...
- Print History? (Y/N): Y
- -----------------------------------------------------
- SAND FALL HISTORY
- -----------------------------------------------------
- 1. Sand Drop at (3, 0)
- 2. Sand Drop at (4, 0)
- 3. Sand Drop at (3, 1)
- 4. Sand Drop at (4, 3)
- 5. Sand Drop at (2, 4)
- 6. Sand Drop at (4, 4)
- 7. Sand Drop at (4, 5)
- 8. Sand Drop at (0, 6)
- 9. Sand Drop at (0, 7)
- 10. Sand Drop at (0, 8)
- 11. Sand Drop at (1, 8)
- 12. Sand Drop at (2, 8)
- 13. Sand Drop at (0, 9)
- 14. Sand Drop at (1, 9)
- 15. Sand Drop at (2, 9)
- 17. Sand Drop at (4, 2)
- 18. Sand Drop at (4, 3)
- 19. Sand Drop at (4, 4)
- 21. Sand Drop at (4, 1)
- 22. Sand Drop at (4, 2)
- 23. Sand Drop at (4, 3)
- -----------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement