Mr. Martin's programming school

JavaScript

Merlin

<html>
    <head>
        <script type="text/javascript">
            var pattern = [0,0,0,0,0,0,0,0,0];
            var solved = [1,0,1,0,1,0,1,0,1];
            var count = 0;
            
            function shuffle()
            {
                for(var i = 0; i < 9; i++)
                {
                    pattern[i] = Math.round(Math.random());
                }
                update_board();
                count = 0;
            }
            
            function update_board()
            {
                document.getElementById("0").innerHTML = "<img src='" + pattern[0] + ".png'>";
                document.getElementById("1").innerHTML = "<img src='" + pattern[1] + ".png'>";
                document.getElementById("2").innerHTML = "<img src='" + pattern[2] + ".png'>";
                document.getElementById("3").innerHTML = "<img src='" + pattern[3] + ".png'>";
                document.getElementById("4").innerHTML = "<img src='" + pattern[4] + ".png'>";
                document.getElementById("5").innerHTML = "<img src='" + pattern[5] + ".png'>";
                document.getElementById("6").innerHTML = "<img src='" + pattern[6] + ".png'>";
                document.getElementById("7").innerHTML = "<img src='" + pattern[7] + ".png'>";
                document.getElementById("8").innerHTML = "<img src='" + pattern[8] + ".png'>";
            }
            
            function switch_pattern(btn)
            {
                switch(btn)
                {
                    case 0:
                        toggle(0);
                        toggle(5);
                        toggle(7);
                    break;
                    case 1:
                        toggle(1);
                        toggle(6);
                        toggle(8);
                    break;
                    case 2:
                        toggle(2);
                        toggle(3);
                        toggle(7);
                    break;
                    case 3:
                        toggle(3);
                        toggle(2);
                        toggle(8);
                    break;
                    case 4:
                        toggle(0);
                        toggle(1);
                        toggle(2);
                        toggle(3);
                        toggle(4);
                        toggle(5);
                        toggle(6);
                        toggle(7);
                        toggle(8);
                    break;
                    case 5:
                        toggle(5);
                        toggle(0);
                        toggle(6);
                    break;
                    case 6:
                        toggle(6);
                        toggle(1);
                        toggle(5);
                    break;
                    case 7:
                        toggle(7);
                        toggle(0);
                        toggle(2);
                    break;
                    case 8:
                        toggle(8);
                        toggle(1);
                        toggle(3);
                    break;
                }
                
                update_board();
                count++;
                var all_done = true;
                var i = 0;
                while(all_done && i < 9)
                {
                    if(pattern[i] != solved[i])
                    {
                        all_done = false;
                    }
                    i++;
                }
                
                if(all_done)
                {
                    alert("Congratulations! You made it in " + count + " tries!");
                    shuffle();
                }
            }
            
            function toggle(btn)
            {
                pattern[btn] = Math.abs(pattern[btn] - 1);
            }
        </script>
    </head>
    
    <body onload="JavaScript:shuffle()">
        <table>
            <tr>
                <td><label id="0" onClick="JavaScript:switch_pattern(0)"/></td>
                <td><label id="1" onClick="JavaScript:switch_pattern(1)"/></td>
                <td><label id="2" onClick="JavaScript:switch_pattern(2)"/></td>
            </tr>
            <tr>
                <td><label id="3" onClick="JavaScript:switch_pattern(3)"/></td>
                <td><label id="4" onClick="JavaScript:switch_pattern(4)"/></td>
                <td><label id="5" onClick="JavaScript:switch_pattern(5)"/></td>
            </tr>
            <tr>
                <td><label id="6" onClick="JavaScript:switch_pattern(6)"/></td>
                <td><label id="7" onClick="JavaScript:switch_pattern(7)"/></td>
                <td><label id="8" onClick="JavaScript:switch_pattern(8)"/></td>
            </tr>
        </table>
    <body>
</html>