Tags: Java Script, Squares, Using Java
This entry was posted on December 31, 2009, 5:17 pm and is filed under Uncategorized. You can follow any responses to this entry through RSS 2.0. You can leave a response, or trackback from your own site.
Arclite theme by digitalnature | powered by WordPress
#1 by Rafal on January 3, 2010 - 2:17 am
Quote
You need to declare variables names for each possible 5 in a row.
row1, row2, row3, row4, row5, col1, col2, col3, col4, col5, diag1, diag2
and set each variable to equal 0 except for row3, col3, diag1, and diag2 which should be set to 1 because they contain the free square.
as part of the onclick function for each squares, add 1 to each of the rows, columns or diagonals it affects. So, for example, if you call out N32 and 32 is in the second row of the third( or N) column, then when you click on that box, it should add a value of 1 to row2 and col3.
The function for each of the squares should call a second function to check all the rows, columns and diagonals to see if there value is equal to 5. and if so, alert the player.
The method should work but i haven’t done javascript for a long time. I already know the following code doesn’t work but your code should look similar:
>> Create these variables:
var row1=0
var row2=0
var row3=1
var row4=0
var row5=0
var col1=0
var col2=0
var col3=1
var col4=0
var col5=0
var diag1=1
var diag2=1
>> Assuming the function 24SquareHit is assigned to square 4 in column 2, add this to that function:
function 24SquareHit()
{
col2 = col2 + 1;
row4 = row4 + 1;
check();
}
>> Do that for all of the onclick functions, but of course have them change the correct variables.
>> Create a function that checks if you have 5 in a row:
check()
{
if (row1==5 || row2==5 || row3==5 || row4==5 || row5==5 || col1==5 || col2==5 || col3==5 || col4==5 || col5==5 || diag1==5 || diag2==5)
{
alert(”you win”);
}
}
Also you will need to find a way to disable clicking the same square more than once.
I hope that can help you a bit