Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function PuzzleState(tiles) {
- this.tiles = tiles;
- this.toString = function() {
- var zeroLoc = this.getLocation(0);
- tiles[zeroLoc.row][zeroLoc.column] = " ";
- var top = "+---+---+---+\n";
- var topRow = "| " + tiles[0][0] + " | " + tiles[0][1] + " | " + tiles[0][2] + " |\n";
- var topMid = "+---+---+---+\n";
- var midRow = "| " + tiles[1][0] + " | " + tiles[1][1] + " | " + tiles[1][2] + " |\n";
- var botMid = "+---+---+---+\n";
- var botRow = "| " + tiles[2][0] + " | " + tiles[2][1] + " | " + tiles[2][2] + " |\n";
- var bot = "+---+---+---+\n";
- tiles[zeroLoc.row][zeroLoc.column] = 0;
- return top + topRow + topMid + midRow + botMid + botRow + bot;
- };
- this.equals = function(other) {
- if(other === null) return false;
- if (this.toString() === other.toString()) return true;
- else return false;
- };
- // Other properties and methods here
- //Gets location of tileNum assuming array is 3x3
- this.getLocation = function(tileNum){
- for (var r = 0; r < 3; r++) {
- for (var c = 0; c < 3; c++) {
- if(tiles[r][c] === tileNum){
- return {row: r, column: c};
- }
- }
- }
- return null;
- };
- this.makeCanvas = function() {
- return this.makeDefaultCanvas(this);
- };
- }
- PuzzleState.prototype = STATE_PROTO;
- // Helper functions here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement