
Welcome Guest
|
#1
|
|||
|
|||
|
Hi all,
Apologies in advance if this is the wrong forum for this Q, and thanks in advance for your help. Using Flash 8, I've been working on a basic image-based 4x4 slide puzzle for a while, there's surprisingly little info on the net about them and I've scoured the forums here without luck finding answers to this problem. I found some code for a 3x3 puzzle, and modified it a bit to work on a 4x4 basis. Original code appears to be from the MX or earlier eras. The problem: sometimes (but not always, it's inconsistent) this code generates an unsolvable puzzle. You can fit the whole thing together, but you'll wind up with pieces 14 and 15 mixed up, rendering the entire puzzle impossible to solve. Sometimes the code will generate a puzzle that works. For the heck of it, I checked the old sample puzzle from Flash 4 and it does the same thing - it sometimes creates an unsolvable puzzle where the last two pieces (14 and 15) are swapped. Is there a way to modify this code to fix that issue? And if not, can someone point me toward working AS2 (or 1) code that solves this issue? Thanks again in advance, code follows below: =-=-= Actionscript:
if (!initialized) {
Initialize();
initialized = true;
}
function PieceX(col)
{
return xBase + col * xSpace;
}
function PieceY(row)
{
return yBase + row * ySpace;
}
// Subroutine Initialize
// Puzzle Initialization Code
function Initialize()
{
Congratulations.stop();
// Set dimensions of game grid
numRows = 4;
numCols = 4;
numCells = 16;
xBase = p0._x;
yBase = p0._y;
xSpace = p0._width * 1.0;
ySpace = p0._height * 1.0;
// Create pieces
var c = 1;
var r = 0;
for (var i=1; i<15; i++) {
var name = "p" + i;
var newPiece = eval(name);
newPiece._x = PieceX(c);
newPiece._y = PieceY(r);
newPiece.PieceNumber = i+1;
if (++c >= 4) {
c = 0;
r++;
}
}
// Set initial positions of each cell, including empty space
posArray = [];
for (i=0; i<15; i++) {
posArray[i] = i;
}
empty = 15;
}
// Subroutine Winner
// Tests whether the current piece positions is a winning position (that is, every piece in sequence)
function isWinner()
{
for (var i = 0; i<15; i++) {
if (posArray[i] != i) {
return false;
}
}
return true;
}
// Subroutine Click
// Puzzle click handler.When a piece is clicked, determine if it is adjacent to the empty space. if it is, move it into the empty space.
// Check if this is a winner
function Click(clicked)
{
clicked--;
if (isWinner()) {
// Start a new game
shuffle();
// Get rid of the congratulations message
Congratulations.gotoAndStop(1);
} else {
// Get the position of the clicked piece
pos = posArray[clicked];
// Get row, column of empty spot
emptyRow = Math.floor(empty/4);
emptyCol = empty-emptyRow*4;
// Get row, column of clicked piece
clickedRow = Math.floor(pos/4);
clickedCol = pos-clickedRow*4;
// Test adjacency
// adjacent(i, j) = i is above j or i is below j, or i is left of j or i is right of j
rowDiff = Math.abs(clickedRow-emptyRow);
colDiff = Math.abs(clickedCol-emptyCol);
adjacent = (rowDiff+colDiff) == 1;
if (adjacent) {
// Move the movie clip for the piece
var piece = eval("/p" + clicked);
piece._x = PieceX(emptyCol);
piece._y = PieceY(emptyRow);
// Swap the clicked piece with the empty space
posArray[clicked] = empty;
empty = pos;
// If this is a winner, start the winning movie clip
if (isWinner()) {
Congratulations.play();
}
}
}
}
function Shuffle()
{
// We want to arrange the cells in a random
// order. We do this by generating a random
// number r(i) for each cell i, and sorting
// the pairs (i, r(i)) using r(i) as the key.
// Comparison function for the sort
var cf = function (x, y) {
if (x[1] < y[1]) {
return -1;
} else if (x[1] > y[1]) {
return 1;
} else {
return 0;
}
}
var i;
var cell = [];
for (i=0; i<16; i++) {
cell.push([i, Math.random()]);
}
cell.sort(cf);
// We've sorted the ordered pairs...
// Now position the pieces according
// to the new order.
var r = 0, c = 0;
for (i=0; i<16; i++) {
var piece = cell[i][0];
if (piece == 15) {
empty = i;
} else {
posArray[piece] = i;
var p = eval("/p" + piece);
p._x = PieceX(c);
p._y = PieceY(r);
}
if (++c >= 4) {
c = 0;
r++;
}
}
}Last edited by jfox3000 : 09-28-2007 at 02:24 PM Reason: tag code |
«
Previous Thread
|
Next Thread
»
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Website using flash slide presentation | John S. | General Flash | 0 | 12-04-2007 06:12 PM |
| slide show preloaders | jagrivera | Flash MX 2004 |
3 | 03-19-2006 06:00 PM |
| Slide In Background (Need Advice on Code) | TymeAw8z | Flash MX |
5 | 07-25-2005 03:55 PM |
| Slide show with audio per slide | Daf | General Flash | 0 | 04-05-2005 02:56 PM |
| Slide tile puzzle | nessiii | Flash MX |
3 | 05-20-2003 10:00 AM |

Programming Languages



