Thank you for visiting Blazing Games

Coding Cubes

While face information is handled by the cube class, the CubePuzzleBoard handles the bulk of the game. To make sure that it is always possible to solve the puzzle, each cube has one side dedicated to each of the puzzles. The actual chunk of the image that is placed on each side is randomly determined. This leads to the potential problem of all the faces being predictable, so once the image segments have been distributed onto the cubes, the order of the faces are also scrambled to make sure there are no give-aways.

public function createPuzzle()
{
var cntrCube:int, cntrSide:int, temp:int, swapIndex:int;

// first reset the puzzle so the puzzle we create will be valid
for (cntrSide = 0; cntrSide < 6; ++cntrSide)
for (cntrCube = 0; cntrCube < 9; ++cntrCube)
_puzzleData[cntrSide][cntrCube] = cntrSide*9 + cntrCube + 1;

// next scrable the six pictures
for (cntrSide = 0; cntrSide < 6; ++cntrSide) {
for (cntrCube = 0; cntrCube < 9; ++cntrCube) {
swapIndex = Math.floor(Math.random()*9);
temp = _puzzleData[cntrSide][swapIndex];
_puzzleData[cntrSide][swapIndex] =
_puzzleData[cntrSide][cntrCube];
_puzzleData[cntrSide][cntrCube] = temp;
}
}

// now scramble each of the cubes
for (cntrCube = 0; cntrCube < 9; ++cntrCube) {
for (cntrSide = 0; cntrSide < 6; ++cntrSide) {
swapIndex = Math.floor(Math.random()*6);
temp = _puzzleData[swapIndex][cntrCube];
_puzzleData[swapIndex][cntrCube] =
_puzzleData[cntrSide][cntrCube];
_puzzleData[cntrSide][cntrCube] = temp;
}
}

// finally set the faces of the nine cubes
for (cntrCube = 0; cntrCube < 9; ++cntrCube) {
for (cntrSide = 0; cntrSide < 6; ++cntrSide) {
_cubes[cntrCube].setFaceTile(cntrSide,
_puzzleData[cntrSide][cntrCube]);
}
}
}

Selecting the cube, rotating and swapping cubes are all handled by event handlers.

// **************************************
// ***** Functions - Event handlers *****
// **************************************

public function handleSelect(evt:Event)
{
_lastSelected = evt.currentTarget.getID();
trace ("Cube " + _lastSelected + " selected");
for (var cntr:int = 0; cntr < 9; ++cntr) {
if (cntr != _lastSelected)
_cubes[cntr].setState(Cube.STATE_SWAP);
}
}

public function handleSwap(evt:Event)
{
var selected:int = evt.currentTarget.getID();
var cntr:int, temp:int;

trace ("swapping " + _lastSelected + " and " + selected);
for (cntr = 0; cntr < 6; ++cntr) {
temp = _cubes[_lastSelected].getFaceTile(cntr);
_cubes[_lastSelected].setFaceTile(cntr, _cubes[selected].getFaceTile(cntr));
_cubes[selected].setFaceTile(cntr, temp);
}
for (cntr = 0; cntr < 9; ++cntr) {
_cubes[cntr].setState(Cube.STATE_NORMAL);
}
checkIfWon();
}

public function handleDeselect(evt:Event)
{
for (var cntr:int = 0; cntr < 9; ++cntr) {
_cubes[cntr].setState(Cube.STATE_NORMAL);
}
checkIfWon();
}

Finally, deteriming if the game has been one is simply a matter of looping through all of the cubes. If the cube has the correct face being shown and is in the proper position, it is counted. If the total count of correct cubes equals the total number of cubes than the game has been won as all the cubes are correct.

protected function checkIfWon():Boolean
{
var matches:int = 0;
for (var cntr:int = 0; cntr < 9; ++cntr) {
if (_cubes[cntr].getFaceTile(Cube.SIDE_FRONT) == (cntr+1))
++matches;
}

if (matches == 9) {
var evnt:Event = new Event(PUZZLE_SOLVED_EVENT);
this.dispatchEvent(evnt);
return true;
} else
return false;
}

Previous page
Chapter 33 Page 5

About - Privacy Policy - Contact - Links - FAQ
Copyright © 2009 Blazing Games Inc. All Rights Reserved