Magic Squares | JavaScriptSource

Magic Squares

Roger Zeitel Sep 9, 2000
The JavaScript Source: Games: Magic Squares 0 && pos[num-1]==0) { pos[num-1]= pos[num]; pos[num]= 0; nummoves++; } else if (num > 2 && pos[num-3]==0) { pos[num-3]= pos[num]; pos[num]= 0; nummoves++; } else if (num < 6 && pos[num+3]==0 ) { pos[num+3]= pos[num]; pos[num]= 0; nummoves++; } display(pos); } function win() { if (pos[0]== 1 & pos[1]== 2 & pos[2]== 3 & pos[3]== 4 & pos[4]== 5 & pos[5]== 6 & pos[6]== 7 & pos[7]== 8 & pos[8]== 0) { if (confirm('You did it! Do you want to restart?')) newgame(); } } function newgame() { var x=1; for (var i=0; i<9; i++) { pos[i]=9; } for (var i=0; i<9;i++) { randomnum=random(); if (randomnum==9) randomnum=8; x=1; for (var j=0; j<9; j++) { if (j==i) continue; if (randomnum==pos[j]) { x=0; break; } } if (x==0) { i ; continue; } pos[i]=randomnum; } nummoves=0; display(pos); } // -->

Here is a fun JavaScript game. Try to put the squares back in order. The '0' repesents the empty spot, and click a square next to the 0 to make them trade places! Get the squares back in order and you win!

Magic Squares

 



 

Put the numbers in order so that they read 1-8.
The 0 is the 'empty' place. Click on any number
next to 0 and they will switch places.
# of moves:

 


The JavaScript Source: Games: Magic Squares

Simply click inside the window below, use your cursor to highlight the script, and copy (type Control-c or Apple-c) the script into a new file in your text editor (such as NotePad or SimpleText) and save (Control-s or Apple-s). The script is yours!

    

 
Did you use this script?  Do you like this site?  Please link to us!

Leave a Response

(0 comments)