Committing before migrating to jQuery
authorjudge <judge@curious.ucc.gu.uwa.edu.au>
Thu, 20 Jun 2013 04:56:34 +0000 (12:56 +0800)
committerjudge <judge@curious.ucc.gu.uwa.edu.au>
Thu, 20 Jun 2013 04:56:34 +0000 (12:56 +0800)
Apparently I'm arrogant :(

web/qwebchess/js.js

index e02328b..7baea20 100644 (file)
@@ -5,24 +5,37 @@
 pieceSelected = ""; // currently selected piece\r
 piece = "";\r
 colour = "W"; // colour of this player\r
+canClick = true;\r
 \r
 // Unicode representations of chess pieces\r
 pieceChar = {"W" : { "p" : "\u2659", "h" : "\u2658", "b" : "\u2657", "r" : "\u2656", "q" : "\u2655", "k" : "\u2654", "?" : "?"},\r
             "B" : { "p" : "\u265F", "h" : "\u265E", "b" : "\u265D", "r" : "\u265C", "q" : "\u265B", "k" : "\u265A", "?" : "?"}};\r
 \r
+emptyHTML = "<!--0-->&nbsp; <big> <bold>&nbsp;</bold> </big> &nbsp;"\r
+\r
 // Select (or move) a piece\r
 function selectPiece(loc) {\r
+       if (!canClick)\r
+               return;\r
+\r
        x = (""+loc).charAt(1);\r
        y = (""+loc).charAt(0);\r
        //alert(loc);\r
 \r
        // work out whether to select or move based on the comment tag for the clicked location\r
        // It is either "<!--W-->" (white; select) or <!--B-->" (black) or "<!--0-->" (empty)\r
-       if (pieceSelected == "") {\r
-               if (document.getElementById(loc).innerHTML.charAt(4) == colour) {\r
+       if (pieceSelected == "") \r
+       {\r
+               square = document.getElementById(loc);\r
+               if (square.innerHTML.charAt(4) == colour) \r
+               {\r
                        console.log("Piece Selected: " + loc);\r
                        pieceSelected = loc;\r
                        ajaxUpdate("x=" + x + "&y=" + y);\r
+                       if ((+x + +y) % 2 == 0)\r
+                               square.style.background = "#DFD";\r
+                       else\r
+                               square.style.background = "#8F8";\r
                }\r
        }\r
        else {\r
@@ -38,19 +51,33 @@ function selectPiece(loc) {
        }\r
 }\r
 \r
+function resetColour(loc)\r
+{\r
+       square = document.getElementById(loc);\r
+       if ((+loc[loc.length-1] + +loc[loc.length-2]) % 2 == 0)\r
+               square.style.background = "#FFF";\r
+       else\r
+               square.style.background = "#DDD";\r
+               \r
+}\r
+\r
 function validMove(start, piece, end) {\r
        return true;\r
 }\r
 \r
 function doMove(start, end) {\r
-       begin = document.getElementById(start);\r
-       end = document.getElementById(end);\r
-       htmlToMove = begin.innerHTML;\r
-       end.innerHTML = htmlToMove;\r
-       begin.innerHTML = "<!--0--> <p>&nbsp;</p>";\r
-\r
-       if (end[end.length-1] % 2 == 0)\r
-               end.innerHTML.replace(/<big>.*<\/big>/i, "<big>?</big>");\r
+       alert("doMove("+start+","+end+")");\r
+       s1 = document.getElementById(start);\r
+       s2 = document.getElementById(end);\r
+       s2.innerHTML = s1.innerHTML;\r
+       s1.innerHTML = emptyHTML;\r
+\r
+       resetColour(start);\r
+\r
+       if ((+end[end.length-1] + +end[end.length-2]) % 2 == 1)\r
+       {\r
+               s2.innerHTML = s2.innerHTML.replace(/<bold>.*<\/bold>/i, "<bold>?</bold>");\r
+       }\r
        //console.log("Piece Moved");\r
 }\r
 \r
@@ -61,12 +88,8 @@ function boardLoad() {
        \r
        for (i = 0; i < 8; i++) {\r
                for (j = 0; j < 8; j++) {\r
-                       e = i + "" + j;\r
-                       elem = document.getElementById(e)\r
-                       if ((i + j) % 2 == 0)\r
-                               elem.style.background="#FFF";\r
-                       else\r
-                               elem.style.background="#DDD";\r
+                       e = ""+i + "" + j;\r
+                       resetColour(e);\r
                }\r
        }\r
        \r
@@ -99,7 +122,7 @@ function boardLoad() {
                for (j = 2; j < 6; j++)\r
                {\r
                        square = document.getElementById(""+j + i);\r
-                       square.innerHTML = "<!--0--> <p>&nbsp;</p>";\r
+                       square.innerHTML = emptyHTML;\r
                }\r
        }\r
        \r
@@ -141,20 +164,28 @@ function ajaxUpdate(queryString) {
                        for (var i = 0; i < lines.length; ++i)\r
                        {\r
                                tokens = lines[i].split(" ")\r
+                               x = Number(tokens[0]);\r
 \r
                                if (isNaN(tokens[0]) || isNaN(tokens[1]))\r
                                        continue;\r
 \r
-                               pieceSelected = ""+tokens[1]+""+tokens[0];\r
-                                square = document.getElementById(pieceSelected);\r
-                                html = square.innerHTML;\r
-                               c = html.charAt(4);\r
-                               if (tokens[2] == "->" && document.getElementById(""+tokens[4] + "" + tokens[3]).innerHTML.charAt(4) != colour)\r
+                                var s1 = document.getElementById("" + tokens[1] + "" + tokens[0]);\r
+                               var s2 = document.getElementById("" + tokens[4] + "" + tokens[3]);\r
+                               if (tokens[2] == "->" && s1.innerHTML.charAt(4) != '0')\r
                                {\r
-                                       doMove(""+tokens[1] + "" + tokens[0], ""+tokens[4] + "" + tokens[3]);\r
+                                       canClick = false;\r
+                                       if ((+tokens[0] + +tokens[1]) % 2 == 0)\r
+                                               s1.style.background = "#DFD";\r
+                                       else\r
+                                               s1.style.background = "#8F8";\r
+\r
+                                       var doThisMove = function(start, end) {doMove(start, end); canClick = true;}(""+tokens[1]+""+tokens[0], ""+tokens[4]+""+tokens[3]);\r
+                                       setTimeout(function() {doThisMove(); canClick = true;}, 500);\r
                                }\r
                                else if (tokens.length == 4 && !isNaN(tokens[0]) && !isNaN(tokens[1]) && !isNaN(tokens[2]) && isNaN(tokens[3]))\r
                                {\r
+                                       html = s1.innerHTML;\r
+                                       c = html.charAt(4);\r
                                        piece = tokens[3];\r
                                        if (piece == "knight") //HACK\r
                                                piece = "h";    \r
@@ -163,8 +194,7 @@ function ajaxUpdate(queryString) {
                                        if (tokens[2] == "1")\r
                                                html[html.length-1] = pieceChar[c][piece];\r
 \r
-                                       square.innerHTML = html.replace(/<big> <bold>.*<\/bold> <\/big>/i, "<big> <bold>"+pieceChar[c][piece]+"</bold> </big>");        \r
-                                       console.log("innerHTML = " + square.innerHTML);\r
+                                       s1.innerHTML = html.replace(/<bold>.*<\/bold>/i, "<bold>"+pieceChar[c][piece]+"</bold>");       \r
                                }\r
                        }\r
 \r

UCC git Repository :: git.ucc.asn.au