Puzzle Algorithms

Puzzle Algorithms
-----------------

The goal is to have slots for each piece, this way an image piece belongs to a slot at
a given time. The piece is a specific subsection of an image and its x, y 
coordinate in relation to the section of the puzzle never change. The location of the piece may 
change depending on the slot it belongs to and if its moving at the time of 
the screen refresh.

//Objects used
var PIECES = 9;
var SIDES = 3;

Piece {
	pieceNumber;
	x;
	y;
}

pieceArray = new Piece[];

Slot {
	slotNumber;
	x;
	y;
	Piece;
}

slotArray = new Slot[];

//psuedocode for creating the arrays for slots and pieces
for i = 0; i < PIECES; i++
    do:
	pieceArray[i] = new Piece();
	pieceArray.pieceNumber = i;
	slotArray[i] = new Slot();
	slotArray[i].slotNumber = i;
		
//pseudocode for dividing the original bitmap and assigning x,y 
//coordinates to slots and pieces and assign the index since the
//shape we are after is a tiled image with 
//numbering moving across horizontally (3 x 3) example.		
	-------------------------
	-   0   -   1   -   2   - 
	-------------------------
	-   3   -   4   -   5   - 
	-------------------------
	-   6   -   7   -   8   - 
	-------------------------

//each section is length / Number of sides	
sectionWidth = imageWidth / SIDES;
sectionHeight = imageHeight / SIDES;

//this loop creates the x an y coordinates for a solved puzzle
//we will need to swap pieces to make it jumbled		
for i = 0; i < SIDES; i++
	for j = 0; j < SIDES; j++
		do:
		    index = i + (y * sides);
		    width = i * sectionWitdh;
		    height = j * sectionHeight;
		    slotArray[index].x = width;
		    slotArray[index].y = height;
		    pieceArray[index].x = width;
		    pieceArray[index].y = height;

//swap pieces on moving piece to new slot or setting random puzzle
//keep a reference to the one being overwritten then move it into 
//the one replacing it
tempPiece = slotArray[newSlotIndex].piece;
slotArray[newSlotIndex].piece = slotArray[oldSlotIndex].piece;
slotArray[oldSlotIndex].piece = tempPiece;

//drawing the puzzle to your canvas means to draw the piece coordinates 
//relative to the full image to the slot coordinates on the canvas
for i=0; i < PIECES; i++
    do: 		
        slot = slotArray[i]; 	
	piece = slot.piece; 	
	movingPiece = null; 	
	if(currentMovingPieceIndex != i){ 	
		//draw piece in the slot  location
                draw: 				
                    piece from piece.x to sectionWidth;  	
  		    and from piece.y to sectionHeight;
 		into: 	
		    slot.x to sectionWidth; 	
                    and slot.y to sectionHeight;	
	}else{ 		
              movingPiece = piece; 	
	} 	
	 	
	// if there is a piece moving draw outside of its slot location
 	if(movingPiece != null){ 	
	      //center piece on mouse click 	
              centerX = mouseClickX - sectionWidth / 2; 	
              centerY = mouseClickY - sectionHeight / 2; 	
   	      draw: 	
                   movingPiece from movingPiece.x to sectionWidth; 	
                   and from movingPiece.y to sectionHeight; 	
              into: 			
                   centerX to sectionWidth;  	
                   and from centerY to sectionHeight; 	
	} 	 
        
//get index of current piece clicked down or up based on (x,y) coords
// of click event 
xIndex = 0;
tempWidth = sectionWidth;
while(mouseClickX >= tempSectionWidth){
     xIndex++;
     tempWidth += sectionWidth;
}

yIndex = 0;
tempHeight = sectionHeight;
while(mouseClickY >= tempHeight){
     yIndex++;
     tempHeight += sectionHeight;
}
	
return xIndex + (yIndex * SIDES);

// random algorithm to make sure that all pieces are out of place
var isRandom = false;
var attempts = 0;
var moves = 0;
if (PIECES % 2 == 0) {
	moves = PIECES / 2;
} else {
	moves = (PIECES / 2) + 1;
}
while isRandom == false do
	attempts++;

	//only switch every piece / 2 + 1 to give every piece a chance at random
	for from = 0; from < ((PIECES / 2) + 1); from++)
		do:
		   var to = Math.floor(Math.random() * PIECES);
		   while (from == to) {
			to = Math.floor(Math.random() * PIECES);
		   }
		   switchPieces(from, to);
	
	//check for even one piece to be in its original place
	isRandom = true;
	for i = 0; int < PIECES; i++) {
		if (slotArray[i].slotNumber == slotArray[i].piece.pieceNumber) {
			isRandom = false;
		}
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 2 other subscribers