Advertisement
Guest User

DailyProgrammer Easy Challenge: Falling Sand

a guest
Jan 5th, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.11 KB | None | 0 0
  1. package daily.challenge.sand;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class SandFall {
  7.    
  8.     public static String[][] grid; //Good Old 2D Arrays :D
  9.    
  10.     static Scanner in = new Scanner(System.in);
  11.     static int gridX, gridY;
  12.     static int steps = 0; //Track the Number of Updates
  13.     static boolean settled = false; //Flag - Are all pieces of sand settled?
  14.     static ArrayList<String> History = new ArrayList<String>();
  15.    
  16.     public static void main(String[] args){
  17.         System.out.print("The Width and Height of the Map (X, Y): ");
  18.         String str = in.nextLine();
  19.        
  20.         //How should the input be separated?
  21.         String[] temp = (str.contains(", "))? str.split(", "):(!str.contains(","))?str.split(" "):str.split(",");
  22.        
  23.         gridX = Integer.parseInt(temp[0]);//String -> int (Readability)
  24.         gridY = Integer.parseInt(temp[1]);//String -> int (Readability)
  25.         grid = new String[gridX][gridY];
  26.        
  27.         System.out.println("Assemble Map (Items: '#' = Rock, '.' = Sand, 's' = Empty Space):");
  28.         for(int y = 0; y< gridY; y++){
  29.             for (int x = 0; x<gridX; x++){
  30.                 grid[x][y] = in.next(); //Item at x, y is the next typed submitted char
  31.                 if(grid[x][y].equalsIgnoreCase("s")) grid[x][y] = " ";
  32.                 //Due to Scanner taking input upon hitting "Enter"
  33.                 //I had to add a Space character, s, it does add a bit
  34.                 //of ease of use as well. Look at Usage
  35.             }
  36.         }
  37.        
  38.         //If Not Settled then Update
  39.         while(!settled){
  40.             update();
  41.         }
  42.        
  43.         //Once Settled print the final grid once more
  44.         System.out.println("\n\n\n");
  45.         printGrid("");
  46.         System.out.println("\nThe Sand has settled in "+steps+" steps...");
  47.         printHistory();
  48.        
  49.        
  50.     }
  51.     static boolean cantSettle = false; //Changed to True if any piece of sand can still fall
  52.     /** Update the position of the sand crystals according to the item below it.
  53.      *  Updates ALL items in one step.*/
  54.     public static void update(){
  55.         cantSettle = false;
  56.         steps++;
  57.         for(int y = 0;y<gridY;y++){
  58.             for(int x = 0; x<gridX; x++){
  59.                 if(grid[x][y].equalsIgnoreCase(".")){
  60.                     if(y == gridY-1) settled = (cantSettle)?false:true; //Boundary Check
  61.                     else if(grid[x][y+1].equalsIgnoreCase(" ")){ //Space Check
  62.                         grid[x][y] = " "; grid[x][y+1] = ".";//Swap Places
  63.                         settled = false;
  64.                         cantSettle = true;
  65.                         printGrid("Sand Drop at ("+x+", "+y+")");//Print New Grid
  66.                         steps++;
  67.                     }else if(grid[x][y+1].equalsIgnoreCase("#")){//Rock Check
  68.                         settled = (cantSettle)?false:true; //If can Settle then Settle
  69.                     }else if(grid[x][y+1].equalsIgnoreCase(".")){ //Sand Check
  70.                         if(checkSandCollision(x,y)){ //Sand-Sand is Nuanced so pass to method
  71.                             settled = (cantSettle)?false:true; //If is settled then check if you can settle.
  72.                         }else{settled = false; cantSettle = true;} //If not settled then unsettle.
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.     }
  78.    
  79.     /** Print the Grid with the Change (msg) */
  80.     public static void printGrid(String msg){
  81.         System.out.println(msg.toUpperCase()+"--------------------------------------");
  82.         for(int y = 0;y< gridY; y++){
  83.             if(y>0)System.out.println();
  84.             for (int x = 0; x<gridX; x++){
  85.                 System.out.print(grid[x][y] + " ");
  86.             }
  87.         }
  88.         System.out.println("\n"+msg.toUpperCase()+"--------------------------------------\n");
  89.         if(!msg.isEmpty()) History.add(steps +". "+msg);
  90.     }
  91.    
  92.     /** If sand is colliding with Sand, scan a few pieces below to see if it is done */
  93.     public static boolean checkSandCollision(int x, int y){
  94.         if(grid[x][y+1].equalsIgnoreCase(".")){
  95.             if(y+1 == gridY-1 || y == gridY-1) return true;
  96.             if(grid[x][y+2].equalsIgnoreCase(".")){
  97.                 return checkSandCollision(x, y+1);//Woo recursion!
  98.             }else if(grid[x][y+2].equalsIgnoreCase("#")) return true;
  99.             else if(grid[x][y+2].equalsIgnoreCase(" ")) return false;
  100.             //If the item is on the boundary, the movement IS complete (true)
  101.             //If the item below is a rock, say the movement IS complete (true)
  102.             //If the item below is Sand, call this method with the new X and Y
  103.             //If the item below is Air, say the movement is INCOMPLETE (false)
  104.         }
  105.        
  106.         return true; //If called for no reason
  107.                      //by accident then just return true
  108.     }
  109.    
  110.     public static void printHistory(){ //Simple Prompting Method
  111.         System.out.print("\nPrint History? (Y/N): ");
  112.         in = new Scanner(System.in);
  113.         String str = in.nextLine();
  114.         if(str.equalsIgnoreCase("y")||str.equalsIgnoreCase("yes")){
  115.             System.out.println("\n-----------------------------------------------------");
  116.             System.out.println("SAND FALL HISTORY");
  117.             System.out.println("-----------------------------------------------------");
  118.             for(String s: History){
  119.                 System.out.println(s);
  120.             }
  121.             System.out.println("-----------------------------------------------------");
  122.            
  123.         }else if(!str.equalsIgnoreCase("n") && !str.equalsIgnoreCase("no")){
  124.             System.out.println("Incorrect Input! \n");
  125.             printHistory();
  126.         }
  127.     }
  128.    
  129. }
  130.  
  131.  
  132. USAGE:
  133.  
  134. The Width and Height of the Map (X, Y): 5 12
  135. Assemble Map (Items: '#' = Rock, '.' = Sand, 's' = Empty Space):
  136. . . . . .
  137. # # # s s
  138. . # # s .
  139. . # s # .
  140. # s . s s
  141. s s s # s
  142. . # # s s
  143. s # # s .
  144. s . . # #
  145. s s s s s
  146. s s s s s
  147. # . # s s
  148.  
  149. SAND DROP AT (1, 9)--------------------------------------
  150. . . .    
  151. # # #   .
  152. . # # . .
  153. . #   #  
  154. #        
  155.     . #  
  156.   # #   .
  157.   # #   .
  158.       # #
  159.     .    
  160. . .      
  161. # . #    
  162. SAND DROP AT (1, 9)--------------------------------------
  163.  
  164. SAND DROP AT (2, 9)--------------------------------------
  165. . . .    
  166. # # #   .
  167. . # # . .
  168. . #   #  
  169. #        
  170.     . #  
  171.   # #   .
  172.   # #   .
  173.       # #
  174.          
  175. . . .    
  176. # . #    
  177. SAND DROP AT (2, 9)--------------------------------------
  178.  
  179. SAND DROP AT (4, 2)--------------------------------------
  180. . . .    
  181. # # #   .
  182. . # # .  
  183. . #   # .
  184. #        
  185.     . #  
  186.   # #   .
  187.   # #   .
  188.       # #
  189.          
  190. . . .    
  191. # . #    
  192. SAND DROP AT (4, 2)--------------------------------------
  193.  
  194. SAND DROP AT (4, 3)--------------------------------------
  195. . . .    
  196. # # #   .
  197. . # # .  
  198. . #   #  
  199. #       .
  200.     . #  
  201.   # #   .
  202.   # #   .
  203.       # #
  204.          
  205. . . .    
  206. # . #    
  207. SAND DROP AT (4, 3)--------------------------------------
  208.  
  209. SAND DROP AT (4, 4)--------------------------------------
  210. . . .    
  211. # # #   .
  212. . # # .  
  213. . #   #  
  214. #        
  215.     . # .
  216.   # #   .
  217.   # #   .
  218.       # #
  219.          
  220. . . .    
  221. # . #    
  222. SAND DROP AT (4, 4)--------------------------------------
  223.  
  224. SAND DROP AT (4, 1)--------------------------------------
  225. . . .    
  226. # # #    
  227. . # # . .
  228. . #   #  
  229. #        
  230.     . # .
  231.   # #   .
  232.   # #   .
  233.       # #
  234.          
  235. . . .    
  236. # . #    
  237. SAND DROP AT (4, 1)--------------------------------------
  238.  
  239. SAND DROP AT (4, 2)--------------------------------------
  240. . . .    
  241. # # #    
  242. . # # .  
  243. . #   # .
  244. #        
  245.     . # .
  246.   # #   .
  247.   # #   .
  248.       # #
  249.          
  250. . . .    
  251. # . #    
  252. SAND DROP AT (4, 2)--------------------------------------
  253.  
  254. SAND DROP AT (4, 3)--------------------------------------
  255. . . .    
  256. # # #    
  257. . # # .  
  258. . #   #  
  259. #       .
  260.     . # .
  261.   # #   .
  262.   # #   .
  263.       # #
  264.          
  265. . . .    
  266. # . #    
  267. SAND DROP AT (4, 3)--------------------------------------
  268.  
  269.  
  270. --------------------------------------
  271. . . .    
  272. # # #    
  273. . # # .  
  274. . #   #  
  275. #       .
  276.     . # .
  277.   # #   .
  278.   # #   .
  279.       # #
  280.          
  281. . . .    
  282. # . #    
  283. --------------------------------------
  284.  
  285.  
  286. The Sand has settled in 25 steps...
  287.  
  288. Print History? (Y/N): Y
  289.  
  290. -----------------------------------------------------
  291. SAND FALL HISTORY
  292. -----------------------------------------------------
  293. 1. Sand Drop at (3, 0)
  294. 2. Sand Drop at (4, 0)
  295. 3. Sand Drop at (3, 1)
  296. 4. Sand Drop at (4, 3)
  297. 5. Sand Drop at (2, 4)
  298. 6. Sand Drop at (4, 4)
  299. 7. Sand Drop at (4, 5)
  300. 8. Sand Drop at (0, 6)
  301. 9. Sand Drop at (0, 7)
  302. 10. Sand Drop at (0, 8)
  303. 11. Sand Drop at (1, 8)
  304. 12. Sand Drop at (2, 8)
  305. 13. Sand Drop at (0, 9)
  306. 14. Sand Drop at (1, 9)
  307. 15. Sand Drop at (2, 9)
  308. 17. Sand Drop at (4, 2)
  309. 18. Sand Drop at (4, 3)
  310. 19. Sand Drop at (4, 4)
  311. 21. Sand Drop at (4, 1)
  312. 22. Sand Drop at (4, 2)
  313. 23. Sand Drop at (4, 3)
  314. -----------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement