Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sokoban brute solver.
- // 2021, May, B@R5uk
- // Discussion: https://dxdy.ru/topic144781.html
- import java.io.*;
- import java.util.*;
- public class MapLoader {
- private boolean isOpen, hasLines;
- private Scanner inputFile;
- private List <String> linesList;
- public MapLoader () {
- isOpen = false;
- hasLines = false;
- linesList = new ArrayList <> ();
- }
- public boolean hasLines () {
- return hasLines;
- }
- public String [] getMapLines () {
- if (hasLines) {
- return linesList .toArray (new String [linesList .size ()]);
- } else {
- return null;
- }
- }
- public boolean openFile (String fileName) {
- if (isOpen) {
- inputFile .close ();
- linesList .clear ();
- hasLines = false;
- }
- try {
- inputFile = new Scanner (new File (fileName));
- } catch (IOException ex) {
- ex .printStackTrace ();
- isOpen = false;
- return false;
- }
- isOpen = true;
- return true;
- }
- public void closeFile () {
- if (isOpen) {
- inputFile .close ();
- isOpen = false;
- }
- }
- public void skip (int arg) {
- while (0 < arg && loadNext ()) {
- --arg;
- }
- }
- public boolean loadNext () {
- String fileLine;
- linesList .clear ();
- hasLines = false;
- if (!isOpen) {
- return false;
- }
- while (true) {
- if (!inputFile .hasNextLine ()) {
- break;
- }
- fileLine = inputFile .nextLine ();
- if (isMapString (fileLine)) {
- linesList .add (fileLine);
- continue;
- }
- if (2 < linesList .size ()) {
- hasLines = true;
- return true;
- }
- linesList .clear ();
- }
- hasLines = 2 < linesList .size ();
- return hasLines;
- }
- private static boolean isMapString (String arg) {
- int k, length, count;
- length = arg .length ();
- if (3 > length) {
- return false;
- }
- count = 0;
- for (k = 0; length > k; ++k) {
- switch (arg .charAt (k)) {
- case '#':
- ++count;
- case ' ':
- case '$':
- case '*':
- case '+':
- case '.':
- case '@':
- break;
- default:
- return false;
- }
- }
- return 1 < count;
- }
- public static void displayLines (String [] arg) {
- System .out .println ();
- for (int k = 0; arg .length > k; ++k) {
- System .out .println (arg [k]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement