-
Notifications
You must be signed in to change notification settings - Fork 1
EC Hash Edits #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: jc-hash-url
Are you sure you want to change the base?
EC Hash Edits #80
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ BoardSolvedService = require '../services/BoardSolvedService' | |
| ClickHandler = require '../services/ClickHandler' | ||
| DFS = require '../services/DFS' | ||
| ExpressionGenerator = require '../services/ExpressionGenerator' | ||
| HashingService = require '../services/HashingService' | ||
| HowToPlay = require '../services/HowToPlay' | ||
| InputSolver = require '../services/InputSolver' | ||
| RandomizedFitLength = require '../services/RandomizedFitLength' | ||
|
|
@@ -33,26 +34,25 @@ class MathSwipeController | |
| TrackingService.desktopView() | ||
| @cursorToPointer() | ||
| ShareGameService.setMessage() | ||
| @initialize window.location.hash | ||
| @initialize window.location.hash, 3 | ||
|
|
||
| # # Uncomment the following line to perform general tests | ||
| # GeneralTests.tests @board | ||
|
|
||
| initialize: (hash) -> | ||
| initialize: (hash, length = 3) -> | ||
| solutionPlacements = [] | ||
| goals = [] | ||
| inputLengths = [] | ||
| boardValues = [] | ||
| hasCompleteBoard = false | ||
| if hash? and hash isnt '' | ||
| hasCompleteBoard = ShareGameService.decode boardValues, goals, solutionPlacements | ||
| unless hasCompleteBoard | ||
| length = 3 | ||
| goals = [] | ||
| solutionPlacements = [] | ||
| inputs = [] | ||
| inputLengths = RandomizedFitLength.generate length * length | ||
| @generateInputs inputLengths, inputs, goals | ||
| boardValues = @generateBoard inputs, length, solutionPlacements | ||
| inputs = [] | ||
| goals = [] | ||
|
|
||
| decoded = HashingService.decodeMap() | ||
| [boardValues, goals, solutionPlacements] = HashingService.parse decoded | ||
|
|
||
| if @malformedDecode boardValues, goals, solutionPlacements | ||
| inputLengths = RandomizedFitLength.generate length ** 2 | ||
| [inputs, goals] = @generateInputs inputLengths | ||
| boardValues = @generateBoard inputs, length, solutionPlacements | ||
|
|
||
| @goalContainer = new GoalContainer goals, Colors | ||
| @board = new Board boardValues, @gameScene, goals, @symbols, | ||
|
|
@@ -62,8 +62,9 @@ class MathSwipeController | |
|
|
||
| ResetButton.bindClick @board, RunningSum | ||
| RunningSum.empty() | ||
| @createNewGame() unless ShareGameService.reloadPageWithHash(@board, | ||
| solutionPlacements, SolutionService) | ||
|
|
||
| @createNewGame() unless HashingService.reloadPageWithHash(@board, | ||
| solutionPlacements, SolutionService) | ||
|
|
||
| isMobile: () -> | ||
| Android: () -> | ||
|
|
@@ -124,14 +125,18 @@ class MathSwipeController | |
| generateBoard: (inputs, length, solutionPlacements) -> | ||
| DFS.setEquationsOnGrid length, inputs, AdjacentCellsCalculator, solutionPlacements | ||
|
|
||
| generateInputs: (inputLengths, inputs, goals) -> | ||
| generateInputs: (inputLengths, goals = [], inputs = []) -> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather have |
||
| for inputSize in inputLengths | ||
| value = -1 | ||
| while value < 1 or value > 300 | ||
| expression = ExpressionGenerator.generate inputSize | ||
| value = InputSolver.compute expression | ||
| goals.push (InputSolver.compute expression) | ||
| inputs.push expression.split('') | ||
| inputs.push (expression.split('')) | ||
| [inputs, goals] | ||
|
|
||
| malformedDecode: (boardValues, goals, solutionPlacements) -> | ||
| boardValues.length < 1 or goals.length < 1 or solutionPlacements.length < 1 | ||
|
|
||
| randExpression: (length) -> | ||
| ExpressionGenerator.generate length | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| class HashingService | ||
|
|
||
| @reloadPageWithHash: (board, solutionPlacements, SolutionService) -> | ||
| unless @checkSolutionPlacements board, solutionPlacements, SolutionService | ||
| @emptyHash() | ||
| return false | ||
| HashingService.setHash board.initialValues, board.goals, solutionPlacements | ||
|
|
||
| @emptyHash: -> window.location.hash = '' | ||
|
|
||
| @setHash: (boardValues, goals, solutionPlacements) -> | ||
| window.location.hash = @encode boardValues, goals, solutionPlacements | ||
|
|
||
| @encode: (boardValues, goals, solutionPlacements) -> | ||
| boardValues = (JSON.stringify boardValues).replace(/(\[|\]|"|,|{|})*/g, '') | ||
|
|
||
| length = Math.sqrt boardValues.length | ||
| for list in [0...solutionPlacements.length] | ||
| for pos in [0...solutionPlacements[list].length] | ||
| solutionPlacements[list][pos] = | ||
| solutionPlacements[list][pos][0] * length + | ||
| solutionPlacements[list][pos][1] | ||
|
|
||
| btoa(JSON.stringify {b: boardValues, g: goals, p: solutionPlacements}) | ||
|
|
||
| @decodeMap: () -> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for extra |
||
| try | ||
| decoded_s = atob window.location.hash.substr(1, window.location.hash.length) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be the first time using the underscore naming convention. |
||
| decoded = JSON.parse decoded_s | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could just make it one line instead of two. We'll just add a newline halfway through to keep to the 80 char limit. This will also take care of creating a new variable |
||
| catch e | ||
| decoded = null | ||
| return decoded | ||
|
|
||
| @parse: (decoded) -> | ||
| return [[],[],[]] unless @successfulDecode decoded | ||
| length = Math.sqrt decoded.b.length | ||
| b = @decodeBoardValues decoded.b, length | ||
| g = @decodeGoals decoded.g | ||
| p = @decodeSolutionPlacements decoded.p, length | ||
| [b, g, p] | ||
|
|
||
| @successfulDecode: (decoded)-> | ||
| return decoded? and | ||
| decoded.p? and | ||
| decoded.g? and | ||
| decoded.p? and | ||
| @regexPass decoded | ||
|
|
||
| @regexPass: (decoded) -> | ||
| alphabet = ['"', '{', '}', '[', ']', ',', ':', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it could be better for us to put |
||
| 'b', 'g', 'p', '1', '2', '3', '4', | ||
| '5', '6', '7', '8', '9', '0', | ||
| '+', '-', '*'] | ||
| for char in decoded | ||
| return false if char not in alphabet | ||
| true | ||
|
|
||
| @decodeBoardValues: (copy, length, boardValues=[] ) -> | ||
| index = 0 | ||
| for i in [0...length] | ||
| row = [] | ||
| for j in [0...length] | ||
| row.push copy[index++] | ||
| boardValues.push row | ||
| boardValues | ||
|
|
||
| @decodeGoals: (toCopy) -> toCopy[..] | ||
|
|
||
| @decodeSolutionPlacements: (copy, length, solutionPlacements=[]) -> | ||
| for list in [0...copy.length] | ||
| expression = [] | ||
| for coord in [0...copy[list].length] | ||
| expression.push [(Math.floor copy[list][coord] / length), | ||
| (copy[list][coord] % length)] | ||
| solutionPlacements.push expression | ||
| solutionPlacements | ||
|
|
||
| @checkSolutionPlacements: (board, solutionPlacements, SolutionService) -> | ||
| @initializeTempBoard board | ||
| @solutionService = new SolutionService @tempBoard, board.goals | ||
|
|
||
| inputs = [] | ||
| for expression in solutionPlacements | ||
| clickedCells = [] | ||
| for index in [0...expression.length] | ||
| cell = expression[index] | ||
| clickedCells.push {row: cell[0], col: cell[1]} | ||
| @solutionService.initialize clickedCells | ||
|
|
||
| solution = [] | ||
| for cell in clickedCells | ||
| solution.push @tempBoard.boardValues[cell.row][cell.col] | ||
| @tempBoard.boardValues[cell.row][cell.col] = ' ' | ||
| @pushDownTempBoard() | ||
|
|
||
| return false unless @solutionService.isSolution() | ||
| inputs.push solution | ||
|
|
||
| console.log expression for expression in inputs | ||
| console.log '\n' | ||
| true | ||
|
|
||
| @initializeTempBoard: (board) -> | ||
| @tempBoard = {} | ||
| @tempBoard.boardValues = [] | ||
| for row, i in board.initialValues | ||
| @tempBoard.boardValues.push [] | ||
| for col in row | ||
| @tempBoard.boardValues[i].push col | ||
|
|
||
| @pushDownTempBoard: -> | ||
| for row in [@tempBoard.boardValues.length-1..1] | ||
| for col in [@tempBoard.boardValues.length-1..0] | ||
| if @tempBoard.boardValues[row][col] is ' ' | ||
| for up in [row-1..0] | ||
| unless @tempBoard.boardValues[up][col] is ' ' | ||
| @swapCells row, col, up, col | ||
| break | ||
|
|
||
| @swapCells: (r1, c1, r2, c2) -> | ||
| temp = @tempBoard.boardValues[r1][c1] | ||
| @tempBoard.boardValues[r1][c1] = @tempBoard.boardValues[r2][c2] | ||
| @tempBoard.boardValues[r2][c2] = temp | ||
|
|
||
| module.exports = HashingService | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pesky newline |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,25 +3,24 @@ InputSolver = require './InputSolver' | |
| class SolutionService | ||
|
|
||
| constructor: (@board, goals, @RunningSum) -> | ||
| @goals = [] | ||
| @goals.push g for g in goals | ||
| @goals = goals[..] | ||
|
|
||
| initialize: (clickedCells) -> | ||
| @setSolutionString clickedCells | ||
| @value = InputSolver.compute @solution | ||
|
|
||
| isSolution: -> | ||
| return false unless @solution? | ||
| return false if @solution[@solution.length - 1] is '+' or | ||
| @solution[@solution.length - 1] is '-' or | ||
| @solution[@solution.length - 1] is '*' | ||
| return false unless @value in @goals | ||
| if not @isCompleteExpression() | ||
| return false unless @solution? and @finished and @value in @goals | ||
|
|
||
| if @isCompleteExpression() | ||
| @valueIndex = @goals.indexOf @value | ||
| @goals[@valueIndex] = ' ' | ||
| true | ||
| else | ||
| @RunningSum.display @RunningSum.solutionOperatorString | ||
| return false | ||
| @valueIndex = @goals.indexOf @value | ||
| @goals[@valueIndex] = ' ' | ||
| true | ||
| false | ||
|
|
||
| finished: -> @solution[@solution.length - 1] not in "+-*" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have to use |
||
|
|
||
| isCompleteExpression: -> @solution.search(/-?\d+[-+\*]\d+/g) is 0 | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets change this to
@initialize window.location.hash, lengthwhere length is an updated constant so it can work better with the leveling system