Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Still broke

  • Loading branch information...
commit d6e33c59f467b741b9a3527c80f80486a6c9e93d 1 parent f047712
M W authored
Showing with 154 additions and 43 deletions.
  1. +110 −27 TowerLogic.js
  2. +43 −15 TowerLogic.ls
  3. +1 −1  index.html
View
137 TowerLogic.js
@@ -1,4 +1,4 @@
-var bestMove, isDoneOrEmpty, findSolution, keyBrick, brickInStack, TowerLogic;
+var moveTower, bestMoves, isDoneOrEmpty, findSolution, keyBrick, brickInStack, freeTowerPit, subOptimalMove, TowerLogic;
angular.module('SomeApp', []).filter('numeric', function(){
return function(input){
if (input === '?') {
@@ -8,7 +8,73 @@ angular.module('SomeApp', []).filter('numeric', function(){
};
});
import$(this, prelude);
-bestMove = function(puzzle){};
+moveTower = function(puzzle, step){
+ puzzle[step.iTo].bricks = puzzle[step.iFrom].bricks.splice(0, step.depth).concat(puzzle[step.iTo].bricks);
+ return puzzle;
+};
+bestMoves = function(puzzle){
+ var keyBricks, possibleTos, i, j, better, good;
+ keyBricks = map(keyBrick)(
+ map(function(it){
+ return it.bricks;
+ }, puzzle));
+ possibleTos = map(head)(
+ map(function(it){
+ return it.bricks;
+ }, puzzle));
+ i = 0;
+ while (i < 8) {
+ j = 0;
+ while (j < 8) {
+ if (brickInStack(keyBricks[i], possibleTos[j])) {
+ return [{
+ penalty: 0,
+ iFrom: i,
+ iTo: j,
+ depth: keyBricks[i].depth
+ }];
+ }
+ j++;
+ }
+ i++;
+ }
+ return [];
+ better = [];
+ i = 0;
+ while (i < 8) {
+ j = 0;
+ while (j < 8) {
+ if (freeTowerPit(keyBricks[i], possibleTos[j])) {
+ better.concat([{
+ penalty: 0,
+ iFrom: i,
+ iTo: j,
+ depth: keyBricks[i].depth
+ }]);
+ }
+ j++;
+ }
+ i++;
+ }
+ good = [];
+ i = 0;
+ while (i < 8) {
+ j = 0;
+ while (j < 8) {
+ if (subOptimalMove(keyBricks[i], possibleTos[j])) {
+ good.concat([{
+ penalty: 1,
+ iFrom: i,
+ iTo: j,
+ depth: keyBricks[i].depth
+ }]);
+ }
+ j++;
+ }
+ i++;
+ }
+ return better.concat(good);
+};
isDoneOrEmpty = function(towerBricks){
if (towerBricks.length === 0) {
return true;
@@ -27,6 +93,8 @@ isDoneOrEmpty = function(towerBricks){
return false;
};
findSolution = function(puzzle, steps){
+ var possibleNextSteps, i, results$ = [];
+ window.globalsBad.callCount += 1;
if (window.globalsBad.solution !== [] && steps.length > window.globalsBad.solution.length) {
return;
}
@@ -41,22 +109,22 @@ findSolution = function(puzzle, steps){
}, puzzle))) {
window.globalsBad.solution = steps;
}
- return (function($_){
- if ($_) {
- return findSolution($_.puzzle, steps.concat($_.step));
- }
- }.call(this, bestMove(puzzle)));
+ debugger;
+ possibleNextSteps = bestMoves(puzzle);
+ i = 0;
+ while (i < possibleNextSteps.length) {
+ results$.push(findSolution(moveTower(puzzle, possibleNextSteps[i]), steps.concat(possibleNextSteps[i])));
+ }
+ return results$;
};
-keyBrick = function(towerIndex){
- var brick, fromBricks;
- brick = {
- depth: 1
- };
- fromBricks = this.towers[towerIndex].bricks;
- while (brick.depth < fromBricks.length && brickInStack(fromBricks[brick.depth - 1], fromBricks[brick.depth])) {
- brick.depth += 1;
+keyBrick = function(towerBricks){
+ var depth;
+ depth = 1;
+ while (depth < towerBricks.length && brickInStack(towerBricks[depth - 1], towerBricks[depth])) {
+ depth += 1;
}
- return brick;
+ towerBricks[depth - 1].depth = depth;
+ return towerBricks[depth - 1];
};
brickInStack = function(currentBrick, nextBrick){
false;
@@ -64,10 +132,26 @@ brickInStack = function(currentBrick, nextBrick){
return true;
}
};
+freeTowerPit = function(currentBrick, nextBrick){
+ if (nextBrick === void 8 && currentBrick.n === 6) {
+ return true;
+ }
+ return false;
+};
+subOptimalMove = function(currentBrick, nextBrick){
+ if (nextBrick === void 8) {
+ return true;
+ }
+ if (nextBrick.color === currentBrick.color && nextBrick.n > currentBrick.n) {
+ return true;
+ }
+ return false;
+};
TowerLogic = function($scope){
$scope.solving = 0;
- $scope.maxPenalty = 25;
+ $scope.penaltyMax = 25;
$scope.penaltyPoints = 0;
+ $scope.callCount = 0;
$scope.solution = [];
$scope.towers = [
{
@@ -239,19 +323,21 @@ TowerLogic = function($scope){
}
];
$scope.solve = function(){
+ debugger;
window.globalsBad = $scope;
$scope.solving = 1;
$scope.solution = [];
+ debugger;
$scope.solved = function(it){
return it.length;
}(
- findSolution(this.towers, []));
+ findSolution($scope.towers, []));
return $scope.solving = 0;
};
$scope.aiMove = function(){
var nextMove;
nextMove = this.bestMove();
- return $scope.moveBricks(nextMove.iFrom, nextMove.iTo);
+ return $scope.solution = moveBricks(nextMove.iFrom, nextMove.iTo);
};
$scope.bestMove = function(){
return this.fake.shift();
@@ -347,14 +433,11 @@ TowerLogic = function($scope){
$scope.towers[iTo].bricks = (function($_){
return $_.splice(0, keyBrick($_).depth);
}.call(this, $scope.towers[iFrom].bricks)).concat($scope.towers[iTo].bricks);
- $scope.solution.push({
- fromTower: iFrom,
- toTower: iTo,
- penalty: 0
- });
- return $scope.penaltyPoints = sum(map(function(it){
- return it.penalty;
- }($scope.solution)));
+ if (!this.solved) {
+ return $scope.penaltyPoints = sum(map(function(it){
+ return it.penalty;
+ }($scope.solution)));
+ }
};
};
function import$(obj, src){
View
58 TowerLogic.ls
@@ -6,12 +6,8 @@
import prelude
moveTower = (puzzle, step) ->
- $scope.towers[iTo].bricks = (let $_ = $scope.towers[iFrom].bricks
- $_.splice 0, (keyBrick $_).depth) +++ $scope.towers[iTo].bricks
-
- if not @solved
- #$scope.solution.push fromTower: iFrom, toTower: iTo, penalty: 0
- $scope.penaltyPoints = sum map (.penalty) $scope.solution
+ puzzle[step.iTo].bricks = (puzzle[step.iFrom].bricks.splice 0, step.depth) +++ puzzle[step.iTo].bricks
+ puzzle
bestMoves = (puzzle) ->
keyBricks = map (.bricks), puzzle |> map keyBrick
@@ -22,14 +18,30 @@ bestMoves = (puzzle) ->
while i < 8
j = 0
while j < 8
- return [penalty: 0 iFrom: i iTo: j] if brickInStack keyBricks[i], possibleTos[j]
+ return [penalty: 0, iFrom: i, iTo: j, depth: keyBricks[i].depth] if brickInStack keyBricks[i], possibleTos[j]
+ j++
+ i++
+
+ return []
+ better = []
+ i = 0
+ while i < 8
+ j = 0
+ while j < 8
+ better+++ [penalty: 0, iFrom: i, iTo: j, depth: keyBricks[i].depth] if freeTowerPit keyBricks[i], possibleTos[j]
+ j++
+ i++
+
+ good= []
+ i = 0
+ while i < 8
+ j = 0
+ while j < 8
+ good+++ [penalty: 1, iFrom: i, iTo: j, depth: keyBricks[i].depth] if subOptimalMove keyBricks[i], possibleTos[j]
j++
i++
- #better
-
- #good
- []
+ better +++ good
isDoneOrEmpty = (towerBricks) ->
return true if towerBricks.length is 0
@@ -37,13 +49,17 @@ isDoneOrEmpty = (towerBricks) ->
false
findSolution = (puzzle, steps) ->
+ window.globalsBad.callCount += 1
return if window.globalsBad.solution is not [] and steps.length > window.globalsBad.solution.length
return if window.globalsBad.penaltyMax < sum map (.penalty), steps
if map (.bricks), puzzle |> all isDoneOrEmpty
window.globalsBad.solution = steps
- let $_ = bestMoves puzzle
- findSolution moveTower $_, steps+++ $_ if $_
+ debugger
+ possibleNextSteps = bestMoves puzzle
+ i = 0
+ while i < possibleNextSteps.length
+ findSolution (moveTower puzzle, possibleNextSteps[i]), steps+++ possibleNextSteps[i]
keyBrick = (towerBricks) ->
depth = 1
@@ -56,11 +72,21 @@ brickInStack = (currentBrick, nextBrick) ->
false
true if nextBrick.color is currentBrick.color and nextBrick.n is currentBrick.n + 1
+freeTowerPit = (currentBrick, nextBrick) ->
+ return true if nextBrick is void and currentBrick.n is 6
+ false
+
+subOptimalMove = (currentBrick, nextBrick) ->
+ return true if nextBrick is void
+ return true if nextBrick.color is currentBrick.color and nextBrick.n > currentBrick.n
+ false
+
TowerLogic = ($scope) ->
$scope.solving = 0
- $scope.maxPenalty = 25
+ $scope.penaltyMax = 25
$scope.penaltyPoints = 0
+ $scope.callCount = 0
$scope.solution = []
$scope.towers = [
{index: 0, bricks: [{n:3 color:\orange},{n:6 color:\orange},{n:1 color:\red },{n:0 color:\red },{n:0 color:\blue },{n:4 color:\cyan }]},
@@ -73,10 +99,12 @@ TowerLogic = ($scope) ->
{index: 7, bricks: [{n:2 color:\cyan },{n:6 color:\green },{n:2 color:\blue },{n:6 color:\red },{n:1 color:\blue }]}]
$scope.solve = ->
+ debugger
window.globalsBad = $scope
$scope.solving = 1
$scope.solution = []
- $scope.solved = findSolution @towers, [] |> (.length)
+ debugger
+ $scope.solved = findSolution $scope.towers, [] |> (.length)
$scope.solving = 0
$scope.aiMove = ->
View
2  index.html
@@ -23,7 +23,7 @@ <h3 style="margin-bottom:{{390-tower.bricks.length*30}}px"># {{tower.index+1}}
<div style="float:left;width:300px;margin-left:8px;">
<h1></h1>
- <h2>Graph Size: {{}}</h2>
+ <h2>Call Count: {{callCount}}</h2>
<p><a href="" ng-click="newPuzzle()">Clear Board for Input</a></p>
<p><a href="" ng-click="solve()">Solve</a></p>
<p><a href="" ng-click="aiMove()">Move Once</a></p>
Please sign in to comment.
Something went wrong with that request. Please try again.