a Four By Four Box | ParrishCo.

Four By Four Box

March 16, 2007

In this bit of JavaScript code I had to create a four by four box in which I randomly placed numbers 1 to 16 in each box. When the user clicks on the box I have the number revealed and when the user releases the mouse button it hides the number again. When the user finds the correct number I display a message informing them. (This example will only work properly in Internet Explorer as of right now due to features only available in that browser.)

Here is an example of this code in action.

Here is the JavaScript code that I used:

<script type = “text/javascript”>
<!–
var cells, swapped, tries=0, randomNum;
var numbers = new Array();

function setup()
{
cells = new Array();
cells = [
[ cell00, cell01, cell02, cell03 ],
[ cell10, cell11, cell12, cell13 ],
[ cell20, cell21, cell22, cell23 ],
[ cell30, cell31, cell32, cell33 ]
];
placeNumbers();
//document.onclick = showCell(randomNum,this);
}

function placeNumbers()
{

var randomLoc,
temp;

for ( var i = 1; i <= 16; i++ )
numbers[ i ] = i;

for ( i = 1; i <= 16; ++i )
{
randomLoc = Math.floor( Math.random() * 16 + 1 );
temp = numbers[ i ];
numbers[ i ] = numbers[ randomLoc ];
numbers[ randomLoc ] = temp;
}

i = 1;
for ( var rows = 0; rows < 4; ++rows )
{
for ( var cols = 0; cols < 4; ++cols )
{
cells[rows][cols].innerText=numbers[i];
++i;
}
}

randomGuess();
tries=0;
/*
for ( var rows = 0; rows < 4; ++rows )
{
for ( var cols = 0; cols < 4; ++cols )
{
document.getElementById(’cell’+rows+”+cols).style.color=”white”;
}
} */
}

function showCell(button)
{
++tries;

if (button.innerText == randomNum)
{
button.style.color=”green”;
alert(’You Found the number ‘+randomNum+’ ! It took you ‘+tries+’ tries. Please click “Shuffle Numbers” to play again.’);
button.style.color=”white”;
}
else
{
button.style.color=”blue”;
}

}

function hideCell(button)
{
button.style.color=”white”;
}

function randomGuess()
{
randomNum = Math.floor( Math.random() * 16 + 1 );
document.getElementById(’ranNumSpan’).innerHTML=randomNum;
}

/*function processClick()
{
var top, bottom, left, right;

for ( var rows = 0; rows < 4; ++rows )
{
for ( var cols = 0; cols < 4; ++cols )
{

if ( cells[ rows ][ cols ].id == window.event.srcElement.id )
{
top = rows - 1;
bottom = rows + 1;
left = cols - 1;
right = cols + 1;

swapped = false;

// test cell above
if ( top != -1 && cells[ top ] [ cols ].innerText == ” ” )
swap( cells[ rows ][ cols ], cells[ top ][ cols ] );

// test cell right
else if ( right != 4 && cells[ rows ] [ right ].innerText == ” ” )
swap( cells[ rows ][ cols ], cells[ rows ][ right ] );

// test cell below
else if ( bottom != 4 && cells[ bottom ] [ cols ].innerText == ” ” )
swap( cells[ rows ][ cols ], cells[ bottom ][ cols ] );

// test cell left
else if ( left != -1 && cells[ rows ] [ left ].innerText == ” ” )
swap( cells[ rows ][ cols ], cells[ rows ][ left ] );

if ( swapped == false )
alert( “This move is not allowed” );
}
}
}
}

function swap( firstCell, secondCell )
{
swapped = true;
secondCell.innerText = firstCell.innerText;
firstCell.innerText = ” “;
}
*/
// –>
</script>

<h2>The random number to find is <span id=”ranNumSpan” style=”color: red; font-size: 1.5em;”></span> !</h2>
<p>To play, double click each box to reveal a number. Continue until you find the number.</p>

<table border = “1″>
<colgroup>
<col span = “4″ width = “80″ align = “center” valign = “middle” />
</colgroup>

<tr>
<td><span id = “cell00″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
<td><span id = “cell01″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
<td><span id = “cell02″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
<td><span id = “cell03″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
</tr>

<tr>
<td><span id = “cell10″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
<td><span id = “cell11″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
<td><span id = “cell12″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
<td><span id = “cell13″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
</tr>

<tr>
<td><span id = “cell20″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
<td><span id = “cell21″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
<td><span id = “cell22″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
<td><span id = “cell23″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
</tr>

<tr>
<td><span id = “cell30″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
<td><span id = “cell31″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
<td><span id = “cell32″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
<td><span id = “cell33″ onmousedown=”showCell(this)” onmouseup=”hideCell(this)”></span></td>
</tr>
</table>

<p><input type = “button” value = “Shuffle Numbers”
onclick = “placeNumbers()” />
</p>

Share This:
  • Digg
  • StumbleUpon
  • Facebook
  • Reddit
  • del.icio.us
  • Google
  • Slashdot
  • NewsVine
  • Technorati
  • E-mail this story to a friend!

Comments

Got something to say?