This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nextEmptyCell = puzzleArray => { | |
const emptyCell = {rowIndex: "", colIndex: ""} | |
puzzleArray.forEach( (row, rowIndex) => { | |
// If this key has already been assigned, skip iteration | |
if (emptyCell.colIndex !== "" ) return | |
// find first zero-element | |
let firstZero = row.find( col => col === 0) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pokeHoles = (startingBoard, holes) => { | |
const removedVals = [] | |
const val = shuffle( range(0,80) ) | |
while (removedVals.length < holes) { | |
const nextVal = val.pop() | |
if (nextVal === undefined) throw new Error ("Impossible Game") | |
const randomRowIndex = Math.floor(nextVal / 9) // Integer 0-8 for row index | |
const randomColIndex = nextVal % 9 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// startingBoard is a 9x9 matrix of zeros | |
const numArray = [1, 2, 3, 4, 5, 6, 7, 8, 9] | |
const shuffle = array => { | |
let newArray = [...array] | |
for ( let i = newArray.length - 1; i > 0; i-- ) { | |
const j = Math.floor( Math.random() * ( i + 1 ) ); | |
[ newArray[ i ], newArray[ j ] ] = [ newArray[ j ], newArray[ i ] ]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const safeToPlace = ( puzzleArray, emptyCell, num ) => { | |
return rowSafe(puzzleArray, emptyCell, num) && | |
colSafe(puzzleArray, emptyCell, num) && | |
boxSafe(puzzleArray, emptyCell, num) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// puzzleArray is the game board being solved. A 9x9 matrix | |
// emptyCell = {rowIndex: INT , colIndex: INT } INT = coordinates of currently empty cell | |
// num = integer value 1-9 being tested | |
const boxSafe = (puzzleArray, emptyCell, num) => { | |
// Define top left corner of box region for empty cell | |
boxStartRow = emptyCell.rowIndex - (emptyCell.rowIndex % 3) | |
boxStartCol = emptyCell.colIndex - (emptyCell.colIndex % 3) | |
let safe = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// puzzleArray is the game board being solved. A 9x9 matrix | |
// emptyCell = {rowIndex: INT , colIndex: INT } INT = coordinates of currently empty cell | |
// num = integer value 1-9 being tested | |
const colSafe = (puzzleArray, emptyCell, num) => { | |
return !puzzleArray.some(row => row[ emptyCell.colIndex ] == num ) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// puzzleArray is the game board being solved. A 9x9 matrix | |
// emptyCell = {rowIndex: INT , colIndex: INT } INT = coordinates of currently empty cell | |
// num = integer value 1-9 being tested | |
const rowSafe = (puzzleArray, emptyCell, num) => { | |
// -1 is return value of .find() if value not found | |
return puzzleArray[ emptyCell.rowIndex ].indexOf(num) == -1 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'tty-screen' | |
class String | |
def center_align | |
screen_width = TTY::Screen.width | |
screen_center = screen_width / 2 | |
string_length = self.length | |
string_center = string_length/2 | |
offset = screen_center - string_center | |
"#{sprintf("%#{offset}s" % self)}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler' | |
Bundler.require | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'db/development.db') | |
ActiveRecord::Base.logger = nil | |
require_relative '../app/tools/funstuff.rb' #=> Begin loading files from the bottom of the heirarchy | |
require_relative '../app/tools/cli_controls.rb' #=> Step 2 on the heirarchy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Module_A | |
def greeting_a | |
puts "Hello from Module A" | |
end | |
end | |
module Module_B | |
include Module_A |