Last active
August 29, 2015 14:10
-
-
Save tomasos/6d2eec797852767c99fa to your computer and use it in GitHub Desktop.
springer
This file contains 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
function generateGrid() { | |
console.time('generate grid'); | |
var grid = []; | |
for(var i = 0; i < 10; i++) { | |
grid[i] = []; | |
for(var j = 0; j < 10; j++){ | |
grid[i][j] = {color: 'white'}; | |
} | |
} | |
console.timeEnd('generate grid'); | |
return grid; | |
} | |
var grid = generateGrid(); | |
var moves = [ | |
{x: -2, y: -1}, | |
{x: -1, y: -2}, | |
{x: 1, y: -2}, | |
{x: 2, y: -1}, | |
{x: 2, y: 1}, | |
{x: 1, y: 2}, | |
{x: -1, y: 2}, | |
{x: -2, y: 1} | |
]; | |
function possibleMoves(x, y) { | |
console.time('possible moves'); | |
var possible = []; | |
for(var i = 0; i < 8; i++) { | |
var newX = x + moves[i].x; | |
var newY = y + moves[i].y; | |
if((newX >= 0 && newX < 10) | |
&& (newY >= 0 && newY < 10)) { | |
possible.push({x: newX, y: newY, num: newX*10 + newY}); | |
} | |
} | |
console.timeEnd('possible moves'); | |
return possible; | |
} | |
function possibleColorMoves(x, y, color) { | |
console.time('possible color moves'); | |
var possible = []; | |
for(var i = 0; i < 8; i++) { | |
var newX = x + moves[i].x; | |
var newY = y + moves[i].y; | |
if((newX >= 0 && newX < 10) | |
&& (newY >= 0 && newY < 10) | |
&& (grid[newX][newY].color === color)) { | |
possible.push({x: newX, y: newY, num: newX*10 + newY}); | |
} | |
} | |
console.timeEnd('possible color moves'); | |
return possible; | |
} | |
function nextMove(x, y, color) { | |
console.time('next move'); | |
var possible = possibleColorMoves(x, y, color); | |
var next = {}; | |
if(possible.length > 0) { | |
var lowest = {}; | |
var lowestNumber = 100; | |
for(var i = 0; i < possible.length; i++) { | |
if(possible[i].num < lowestNumber) { | |
lowestNumber = possible[i].num; | |
lowest = possible[i]; | |
} | |
} | |
lowest.color = grid[lowest.x][lowest.y].color; | |
next = lowest; | |
} else { | |
var possibleTotal = possibleMoves(x, y); | |
var highest = {}; | |
var highestNumber = 0; | |
for(var i = 0; i < possibleTotal.length; i++) { | |
if(possibleTotal[i].num > highestNumber) { | |
highestNumber = possibleTotal[i].num; | |
highest = possibleTotal[i]; | |
} | |
} | |
highest.color = grid[highest.x][highest.y].color; | |
next = highest; | |
} | |
console.timeEnd('next move'); | |
return next; | |
} | |
function flipColor(x, y) { | |
grid[x][y].color = grid[x][y].color === 'white' ? 'black' : 'white'; | |
} | |
function countBlacks() { | |
var res = 0; | |
for(var i = 0; i < 10; i++) { | |
for(var j = 0; j < 10; j++) { | |
if(grid[i][j].color === 'black') { | |
res = res + 1; | |
} | |
} | |
} | |
return res; | |
} | |
function printGrid() { | |
console.log('- - - - - - - - - - ') | |
for(var i = 0; i < 10; i++) { | |
var row = ''; | |
for(var j = 0; j < 10; j++) { | |
if(grid[i][j].color === 'black') { | |
row = row + 'x '; | |
} else { | |
row = row + 'o '; | |
} | |
} | |
console.log(' '); | |
console.log(row); | |
} | |
} | |
function moveKnight(x, y, color, i) { | |
// console.log('x: ' + x + ' - y: ' + y); | |
// console.log('color: ' + color); | |
// printGrid(); | |
if(i === 0) { | |
console.log('number of blacks: ' + countBlacks()) | |
printGrid(); | |
return countBlacks(); | |
} | |
var next = nextMove(x, y, color); | |
flipColor(x, y); | |
return moveKnight(next.x, next.y, next.color, i-1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment