From f49e23900da0401aa811e3d523c7d2e3bc74ae72 Mon Sep 17 00:00:00 2001 From: Peter Jablonski Date: Sun, 6 Jan 2019 03:27:53 +0000 Subject: [PATCH] create random pattern generator --- cross.js | 4 +-- patterns.js | 77 ++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 61 insertions(+), 20 deletions(-) diff --git a/cross.js b/cross.js index a3fc49c..45d4ff3 100644 --- a/cross.js +++ b/cross.js @@ -684,7 +684,7 @@ function generatePattern() { xw.title = title; xw.author = author; - const pattern = patterns[randomNumber(0, patterns.length)]; // select random pattern + const pattern = createRandomPattern(xw.rows, xw.cols); // create random pattern if (!isSymmetrical) { // patterns are encoded as only one half of the grid... toggleSymmetry(); // so symmetry needs to be on to populate correctly } @@ -865,4 +865,4 @@ function randomNumber(min, max) { function randomLetter() { let alphabet = "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLLMMNNNNNNOOOOOOOOPPQRRRRRRSSSSSSTTTTTTUUUUVVWWXYYZ"; return alphabet[randomNumber(0, alphabet.length)]; -} +} \ No newline at end of file diff --git a/patterns.js b/patterns.js index 533dab7..d0f2c97 100644 --- a/patterns.js +++ b/patterns.js @@ -13,21 +13,62 @@ // limitations under the License. // ------------------------------------------------------------------------ -const patterns = [ - [ - [0,4], [1,4], [2,4], [12,4], [13,4], [14,4], - [4,0], [4,1], [4,2], [4,12], [4,13], [4,14], - [8,3], [7,4], [6,5], [5,6], [4,7], [3,8] - ], - [ - [0,4], [1,4], [2,4], [12,4], [13,4], [14,4], - [6,0], [10,0], [6,1], [10,1], [10,2], [8,4], - [5,3], [9,3], [4,5], [11,5], [6,6], [7,7] - ], - [ - [0,5], [1,5], [2,5], [12,4], [13,4], [14,4], - [5,0], [5,1], [5,2], [4,3], [3,13], [3,14], - [5,6], [4,7], [4,8], [6,9], [7,10], [5,11] - ] -]; -console.log("Loaded", patterns.length, "patterns."); +function createRandomPattern(numRows, numColumns) { + pattern = [] + for (let i = 0; i < numRows; i++){ + for (let j = 0; j < numColumns/2 ; j++){ + let canFillSquare = canSquareBeFilled(pattern, i, j); + let shouldFillSquare = Math.random() < 0.75; + if (shouldFillSquare && canFillSquare) { + pattern.push([i, j]); + } + } + } + console.log("Generated pattern."); + return pattern; +} + +function isFilled(pattern, row, column){ + return pattern.some(function(square) { + return (square[0] === row && square[1] === column) + || (square[0] === 14 - row && square[1] === 14 - column); + }); +} + +function canSquareBeFilled(pattern, i, j){ + let leftClear = sideIsClear(pattern, i, j, 0, j, 0, -1) + let rightClear = sideIsClear(pattern, i, j, 7, j, 0 , 1) + let upClear = sideIsClear(pattern, i, j, 0, i, -1, 0) + let downClear = sideIsClear(pattern, i, j, 14, i, 1, 0) + if (leftClear && rightClear && upClear && downClear) { + return true; + } + return false; +} + +function sideIsClear(pattern, i, j, bound, pivot, imultiplier, jmultiplier){ + if (pivot === 0) { + return true + } else if (pivot + ((imultiplier + jmultiplier) * 1) === bound || pivot + ((imultiplier + jmultiplier) * 2) === bound) { + if (isFilled(pattern, i + (imultiplier * 1), j + (jmultiplier * 1))){ + return true; + } + return false; + } else if (pivot + (imultiplier + jmultiplier) * 3 === bound) { + if ((!isFilled(pattern, i + (imultiplier * 1), j + (jmultiplier * 1)) && + !isFilled(pattern, i + (imultiplier * 2), j + (jmultiplier * 2)) && + !isFilled(pattern, i + (imultiplier * 3), j + (jmultiplier * 3)) ) + ) { + return true; + } + return false; + } else { + if ( + isFilled(pattern, i + (imultiplier * 2), j + (jmultiplier * 2)) || + isFilled(pattern, i + (imultiplier * 3), j + (jmultiplier * 3)) + ){ + return false; + } + return true; + } +}