Hello,
I’m in the midst of creating a puzzle game, one that has different modes and some extras to unlock. The idea was to create something that would give a person incentive to play a game that’s otherwise common (no doubt).
One of the modes I’m creating is a challenge type mode where the player has to solve the puzzle within a certain amount of moves. While this in and of itself is not very complex to create, I am stuck on trying to get puzzle pieces into a fixed position on the board. I know how to randomize tiles, as randomly generated puzzles are used in another area. But for some reason, intentionally placing the tiles where I want them is not as intuitive as I would hope.
I feel that the solution is in a new or separate array. I have tried a few solutions on my own, but I either get errors, no changes at all or you will have to have a mixed up picture to solve the puzzle.
Overall, I guess the question is as follows; How would I shift the pieces in the array to be placed where I want them, without the new order being the “correct” order, and without doing it randomly?
Here’s what my code looks like (sans failed attempts at creating a fixed array):
import fl.transitions.*;
import fl.transitions.easing.*;
var pieces:Array = [{className:Piece1},{className:Piece2},{className:Piece3},{className:Piece4},{className:Piece5},{className:Piece6},{className:Piece7},{className:Piece8}]
var allTiles:Array = new Array();
var tileWidth:uint = 100;
var pieceIndex:uint = 0;
var tileContainer:MovieClip = new MovieClip();
var slideTween:Tween;
function makeMap():void
{
var tile:MovieClip
var tileClass:Class;
for (var i:uint = 0; i < allTiles.length; i++)
{
for (var j:uint = 0; j < allTiles[0].length; j++)
{
if(allTiles*[j] != 0)
{
tileClass = Class(pieces[pieceIndex].className);
tile = new tileClass();
tile.x = tileWidth * j;
tile.y = tileWidth * i;
tile.name = "tile_" + pieces[pieceIndex].targetX + "_" + pieces[pieceIndex].targetY;
trace(tile.name);
tile.addEventListener(MouseEvent.CLICK, clickTile);
tileContainer.addChild(tile);
pieceIndex ++;
}
}
}
container_mc.addChild(tileContainer);
}
function initializeGame():void
{
allTiles[0] = [1,1,1];
allTiles[1] = [1,1,1];
allTiles[2] = [1,1,0];
setTargetPositions();
//fixed pieces function
//pieces = fixedArray(pieces);
makeMap();
}
function setTargetPositions():void
{
var tileIndex:uint = 0;
for (var i:uint = 0; i < allTiles.length; i++)
{
for (var j:uint = 0; j < allTiles[0].length; j++)
{
pieces[tileIndex].targetX = j;
pieces[tileIndex].targetY = i;
tileIndex ++;
if (tileIndex >= pieces.length)
{
break;
}
}
}
}
function checkBoundaries(tile:MovieClip, xPos:Number, yPos:Number):void
{
if (allTiles.indexOf(allTiles[yPos - 1]) != -1 && allTiles[yPos - 1][xPos] == 0)
{
allTiles[yPos - 1][xPos] = 1;
allTiles[yPos][xPos] = 0;
slideTile(tile,"y",tile.y,tile.y - tileWidth);
}
else if (allTiles.indexOf(allTiles[yPos + 1]) != -1 && allTiles[yPos + 1][xPos] == 0)
{
allTiles[yPos + 1][xPos] = 1;
allTiles[yPos][xPos] = 0;
slideTile(tile,"y",tile.y,tile.y + tileWidth);
}
else if (allTiles[yPos].indexOf(allTiles[yPos][xPos - 1]) != -1 && allTiles[yPos][xPos - 1] == 0)
{
allTiles[yPos][xPos - 1] = 1;
allTiles[yPos][xPos] = 0;
slideTile(tile,"x",tile.x,tile.x - tileWidth);
}
else if (allTiles[yPos].indexOf(allTiles[yPos][xPos + 1]) != -1 && allTiles[yPos][xPos + 1] == 0)
{
allTiles[yPos][xPos + 1] = 1;
allTiles[yPos][xPos] = 0;
slideTile(tile,"x",tile.x,tile.x + tileWidth);
}
}
function fixedArray(ary:Array):Array
{
//scramble the puzzle pieces somehow here
}
//keyword: WIN
function checkSolution(event:TweenEvent):void
{
var thisTile:MovieClip;
var thisName:String;
var thisTargetX:Number;
var thisTargetY:Number;
var numCorrect:uint = 0;
for(var i:uint = 0; i <tileContainer.numChildren; i++)
{
thisTile = MovieClip(tileContainer.getChildAt(i));
thisName = thisTile.name;
thisTargetX = Number(thisName.slice(5,6)) * tileWidth;
thisTargetY = Number(thisName.slice(7,8)) * tileWidth;
if(thisTile.x == thisTargetX && thisTile.y == thisTargetY)
{
numCorrect ++;
if(numCorrect >= tileContainer.numChildren)
{
removeChild(container_mc);
}
}
}
}
function clickTile(event:MouseEvent):void
{
var tileX:Number = event.currentTarget.x / tileWidth;
var tileY:Number = event.currentTarget.y / tileWidth;
var thisTile:MovieClip = MovieClip(event.currentTarget);
checkBoundaries(thisTile, tileX, tileY);
}
function slideTile(tileToSlide:MovieClip,slideProperty:String,startValue:Number,slideFinalValue:Number):void
{
slideTween = new Tween(tileToSlide,slideProperty,Regular.easeOut,startValue,slideFinalValue,3,false);
slideTween.addEventListener(TweenEvent.MOTION_FINISH, checkSolution);
}
initializeGame();
Here’s an example of how one of the fixed puzzles would look/work.
[1][3][6]
[5][2][8]
[4][7]
The blank spot is where you slide adjacent pieces. This can be solved in 8 moves. =)
Assume the solution to a 9x9 puzzle is:
[1][2][3]
[4][5][6]
[7][8]
So, according to the array of tiles in the initializeGame function, I want to get keep Piece1 at 0,0, move Piece2 to 1,1, Piece3 at 0,1 and so forth.