Advertisement
Guest User

MapLoader.java

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