Advertisement
Guest User

MapLoader.java

a guest
May 17th, 2021
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. //  Sokoban brute solver.
  2. //  2021, May, B@R5uk
  3. //  Discussion: https://dxdy.ru/topic144781.html
  4.  
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. public class MapLoader {
  9.    
  10.     private boolean isOpen, hasLines;
  11.     private Scanner inputFile;
  12.     private List <String> linesList;
  13.    
  14.     public MapLoader () {
  15.         isOpen = false;
  16.         hasLines = false;
  17.         linesList = new ArrayList <> ();
  18.     }
  19.    
  20.     public boolean hasLines () {
  21.         return hasLines;
  22.     }
  23.    
  24.     public String [] getMapLines () {
  25.         if (hasLines) {
  26.             return linesList .toArray (new String [linesList .size ()]);
  27.         } else {
  28.             return null;
  29.         }
  30.     }
  31.    
  32.     public boolean openFile (String fileName) {
  33.         if (isOpen) {
  34.             inputFile .close ();
  35.             linesList .clear ();
  36.             hasLines = false;
  37.         }
  38.         try {
  39.             inputFile = new Scanner (new File (fileName));
  40.         } catch (IOException ex) {
  41.             ex .printStackTrace ();
  42.             isOpen = false;
  43.             return false;
  44.         }
  45.         isOpen = true;
  46.         return true;
  47.     }
  48.    
  49.     public void closeFile () {
  50.         if (isOpen) {
  51.             inputFile .close ();
  52.             isOpen = false;
  53.         }
  54.     }
  55.    
  56.     public void skip (int arg) {
  57.         while (0 < arg && loadNext ()) {
  58.             --arg;
  59.         }
  60.     }
  61.    
  62.     public boolean loadNext () {
  63.         String fileLine;
  64.        
  65.         linesList .clear ();
  66.         hasLines = false;
  67.        
  68.         if (!isOpen) {
  69.             return false;
  70.         }
  71.        
  72.         while (true) {
  73.             if (!inputFile .hasNextLine ()) {
  74.                 break;
  75.             }
  76.             fileLine = inputFile .nextLine ();
  77.             if (isMapString (fileLine)) {
  78.                 linesList .add (fileLine);
  79.                 continue;
  80.             }
  81.             if (2 < linesList .size ()) {
  82.                 hasLines = true;
  83.                 return true;
  84.             }
  85.             linesList .clear ();
  86.         }
  87.        
  88.         hasLines = 2 < linesList .size ();
  89.         return hasLines;
  90.     }
  91.    
  92.     private static boolean isMapString (String arg) {
  93.         int k, length, count;
  94.        
  95.         length = arg .length ();
  96.         if (3 > length) {
  97.             return false;
  98.         }
  99.         count = 0;
  100.         for (k = 0; length > k; ++k) {
  101.             switch (arg .charAt (k)) {
  102.             case '#':
  103.                 ++count;
  104.             case ' ':
  105.             case '$':
  106.             case '*':
  107.             case '+':
  108.             case '.':
  109.             case '@':
  110.                 break;
  111.             default:
  112.                 return false;
  113.             }
  114.         }
  115.         return 1 < count;
  116.     }
  117.    
  118.     public static void displayLines (String [] arg) {
  119.         System .out .println ();
  120.         for (int k = 0; arg .length > k; ++k) {
  121.             System .out .println (arg [k]);
  122.         }
  123.     }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement