Advertisement
elltyl325

puzzlestate

Dec 7th, 2020
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function PuzzleState(tiles) {
  2.  
  3.     this.tiles = tiles;
  4.    
  5.        
  6.     this.toString = function() {
  7.  
  8.         var zeroLoc = this.getLocation(0);
  9.         tiles[zeroLoc.row][zeroLoc.column] = " ";
  10.        
  11.     var top = "+---+---+---+\n";
  12.         var topRow = "| " + tiles[0][0] + " | " + tiles[0][1] + " | " + tiles[0][2] + " |\n";
  13.         var topMid = "+---+---+---+\n";
  14.         var midRow = "| " + tiles[1][0] + " | " + tiles[1][1] + " | " + tiles[1][2] + " |\n";
  15.         var botMid = "+---+---+---+\n";
  16.         var botRow = "| " + tiles[2][0] + " | " + tiles[2][1] + " | " + tiles[2][2] + " |\n";
  17.         var bot = "+---+---+---+\n";
  18.        
  19.         tiles[zeroLoc.row][zeroLoc.column] = 0;
  20.         return top + topRow + topMid + midRow + botMid + botRow + bot;
  21.     };
  22.  
  23.     this.equals = function(other) {
  24.        
  25.         if(other === null) return false;
  26.  
  27.         if (this.toString() === other.toString()) return true;
  28.         else return false;
  29.     };
  30.  
  31.     // Other properties and methods here
  32.    
  33.     //Gets location of tileNum assuming array is 3x3
  34.     this.getLocation = function(tileNum){
  35.         for (var r = 0; r < 3; r++) {
  36.             for (var c = 0; c < 3; c++) {
  37.                 if(tiles[r][c] === tileNum){
  38.                     return {row: r, column: c};
  39.                 }
  40.             }
  41.         }
  42.         return null;
  43.     };
  44.    
  45.     this.makeCanvas = function() {
  46.     return this.makeDefaultCanvas(this);
  47.     };
  48.    
  49. }
  50.  
  51. PuzzleState.prototype = STATE_PROTO;
  52.  
  53. // Helper functions here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement