Ver Fonte

Add function to parse input code box

ApisNecros há 1 ano atrás
pai
commit
4cb14d15fa
1 ficheiros alterados com 58 adições e 0 exclusões
  1. 58 0
      starfish.js

+ 58 - 0
starfish.js

@@ -76,6 +76,64 @@ class CodeBox {
         }
     }
 
+    /**
+     * Parse the initial code box
+     * 
+     * Transforms the textual code box into usable matrix
+     */
+    ParseCodeBox() {
+        const cbRaw = this.codeBoxDOM.value;
+        const rows = cbRaw.split("\n").filter((r) => r.length);
+
+        let maxRowLength = 0;
+        for(const row of rows) {
+            const rowSplit = row.split("");
+
+            // Store this for later processing
+            while(rowSplit.length > maxRowLength) {
+                maxRowLength = rowSplit.length
+            }
+
+            this.box.push(rowSplit);
+        }
+
+        this.EqualizeBoxWidth(maxRowLength);
+        
+        let fin = null;
+
+        try {
+            while(!fin) {
+                fin = this.Swim();
+            }
+        }
+        catch(e) {
+            console.error(e);
+        }
+    }
+
+    /**
+     * Make all the rows in the code box the same length
+     *
+     * All rows not long enough will have NOPs added until they're uniform in size.
+     *
+     * @param {int} [rowLength] The longest row in the code box
+     */
+    EqualizeBoxWidth(rowLength = null) {
+        if(!rowLength) {
+            for(const row of this.box) {
+                if(row.length > rowLength) {
+                    rowLength = row.length;
+                }
+            }
+        }
+
+        for(const row of this.box) {
+            while(row.length < rowLength) {
+                row.push(" ");
+            }
+        }
+    }
+
     /**
      * Print the value to the display 
      *