7baea20bd8f86ce938ed984ba18ea58083cc2399
[progcomp2013.git] / web / qwebchess / js.js
1 //progcomp.ucc.asn.au/cgi-bin/qchess.cgi?r=start\r
2 //progcomp.ucc.asn.au/cgi-bin/qchess.cgi?r=quit\r
3 //progcomp.ucc.asn.au/cgi-bin/qchess.cgi?x=X&y=Y (0 indexed)\r
4 \r
5 pieceSelected = ""; // currently selected piece\r
6 piece = "";\r
7 colour = "W"; // colour of this player\r
8 canClick = true;\r
9 \r
10 // Unicode representations of chess pieces\r
11 pieceChar = {"W" : { "p" : "\u2659", "h" : "\u2658", "b" : "\u2657", "r" : "\u2656", "q" : "\u2655", "k" : "\u2654", "?" : "?"},\r
12              "B" : { "p" : "\u265F", "h" : "\u265E", "b" : "\u265D", "r" : "\u265C", "q" : "\u265B", "k" : "\u265A", "?" : "?"}};\r
13 \r
14 emptyHTML = "<!--0-->&nbsp; <big> <bold>&nbsp;</bold> </big> &nbsp;"\r
15 \r
16 // Select (or move) a piece\r
17 function selectPiece(loc) {\r
18         if (!canClick)\r
19                 return;\r
20 \r
21         x = (""+loc).charAt(1);\r
22         y = (""+loc).charAt(0);\r
23         //alert(loc);\r
24 \r
25         // work out whether to select or move based on the comment tag for the clicked location\r
26         // It is either "<!--W-->" (white; select) or <!--B-->" (black) or "<!--0-->" (empty)\r
27         if (pieceSelected == "") \r
28         {\r
29                 square = document.getElementById(loc);\r
30                 if (square.innerHTML.charAt(4) == colour) \r
31                 {\r
32                         console.log("Piece Selected: " + loc);\r
33                         pieceSelected = loc;\r
34                         ajaxUpdate("x=" + x + "&y=" + y);\r
35                         if ((+x + +y) % 2 == 0)\r
36                                 square.style.background = "#DFD";\r
37                         else\r
38                                 square.style.background = "#8F8";\r
39                 }\r
40         }\r
41         else {\r
42                 //alert("pieceMoved");\r
43                 if (validMove(pieceSelected, piece, loc)) {\r
44                         doMove(pieceSelected, loc);\r
45                         ajaxUpdate("x=" + x + "&y=" + y);\r
46                         pieceSelected = "";\r
47                 }\r
48                 else {\r
49                         console.log("Invalid Move");\r
50                 }\r
51         }\r
52 }\r
53 \r
54 function resetColour(loc)\r
55 {\r
56         square = document.getElementById(loc);\r
57         if ((+loc[loc.length-1] + +loc[loc.length-2]) % 2 == 0)\r
58                 square.style.background = "#FFF";\r
59         else\r
60                 square.style.background = "#DDD";\r
61                 \r
62 }\r
63 \r
64 function validMove(start, piece, end) {\r
65         return true;\r
66 }\r
67 \r
68 function doMove(start, end) {\r
69         alert("doMove("+start+","+end+")");\r
70         s1 = document.getElementById(start);\r
71         s2 = document.getElementById(end);\r
72         s2.innerHTML = s1.innerHTML;\r
73         s1.innerHTML = emptyHTML;\r
74 \r
75         resetColour(start);\r
76 \r
77         if ((+end[end.length-1] + +end[end.length-2]) % 2 == 1)\r
78         {\r
79                 s2.innerHTML = s2.innerHTML.replace(/<bold>.*<\/bold>/i, "<bold>?</bold>");\r
80         }\r
81         //console.log("Piece Moved");\r
82 }\r
83 \r
84 function boardLoad() {\r
85         ajaxUpdate("r=force_quit");\r
86         \r
87         \r
88         \r
89         for (i = 0; i < 8; i++) {\r
90                 for (j = 0; j < 8; j++) {\r
91                         e = ""+i + "" + j;\r
92                         resetColour(e);\r
93                 }\r
94         }\r
95         \r
96         //Place pieces on the board\r
97         for (i = 0; i < 8; i++) {\r
98                 black = document.getElementById("1" + i);\r
99                 white = document.getElementById("6" + i);\r
100                 //pawns\r
101                 black.innerHTML = "<!--B--> " + pieceChar["B"]["p"] + " <big> <bold>?</bold> </big> ?";\r
102                 white.innerHTML = "<!--W--> " + pieceChar["W"]["p"] + " <big> <bold>?</bold> </big> ?";\r
103                 \r
104                 black = document.getElementById("0" + i);\r
105                 white = document.getElementById("7" + i);\r
106                 piece = "p";\r
107                 if (i == 0 || i == 7)\r
108                         piece = "r";\r
109                 if (i == 1 || i == 6)\r
110                         piece = "h";\r
111                 if (i == 2 || i == 5)\r
112                         piece = "b";\r
113                 if (i == 3)\r
114                         piece = "k";\r
115                 if (i == 4)\r
116                         piece = "q";\r
117                 //major pieces\r
118                 black.innerHTML = "<!--B--> " + pieceChar["B"][piece] + "<big> <bold>?</bold> </big> ?";\r
119                 white.innerHTML = "<!--W--> " + pieceChar["W"][piece] + "<big> <bold>?</bold> </big> ?";\r
120 \r
121                 // empty squares\r
122                 for (j = 2; j < 6; j++)\r
123                 {\r
124                         square = document.getElementById(""+j + i);\r
125                         square.innerHTML = emptyHTML;\r
126                 }\r
127         }\r
128         \r
129         setTimeout(function(){ajaxUpdate("r=start");}, 1000);\r
130 }\r
131 \r
132 //AJAX Stuff\r
133 function ajaxUpdate(queryString) {\r
134         var ajaxRequest;  // The variable that makes Ajax possible!\r
135 \r
136         try {\r
137                 // Opera 8.0+, Firefox, Safari\r
138                 ajaxRequest = new XMLHttpRequest();\r
139         } catch (e) {\r
140                 // Internet Explorer Browsers\r
141                 try {\r
142                         ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");\r
143                 } catch (e) {\r
144                         try {\r
145                                 ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");\r
146                         } catch (e) {\r
147                                 // Something went wrong\r
148                                 alert("Your Browser is not Ajax Compatible, Please Upgrade to Google Chrome.");\r
149                                 return false;\r
150                         }\r
151                 }\r
152         }\r
153         \r
154         //alert(queryString);\r
155         \r
156         // Create a function that will receive data sent from the server\r
157         ajaxRequest.onreadystatechange = function () \r
158         {\r
159                 //alert("RS" + ajaxRequest.readyState);\r
160                 if (ajaxRequest.readyState == 4) {\r
161                         console.log("AJAX Response: " + ajaxRequest.responseText);\r
162                         lines = ajaxRequest.responseText.split("\n");\r
163 \r
164                         for (var i = 0; i < lines.length; ++i)\r
165                         {\r
166                                 tokens = lines[i].split(" ")\r
167                                 x = Number(tokens[0]);\r
168 \r
169                                 if (isNaN(tokens[0]) || isNaN(tokens[1]))\r
170                                         continue;\r
171 \r
172                                 var s1 = document.getElementById("" + tokens[1] + "" + tokens[0]);\r
173                                 var s2 = document.getElementById("" + tokens[4] + "" + tokens[3]);\r
174                                 if (tokens[2] == "->" && s1.innerHTML.charAt(4) != '0')\r
175                                 {\r
176                                         canClick = false;\r
177                                         if ((+tokens[0] + +tokens[1]) % 2 == 0)\r
178                                                 s1.style.background = "#DFD";\r
179                                         else\r
180                                                 s1.style.background = "#8F8";\r
181 \r
182                                         var doThisMove = function(start, end) {doMove(start, end); canClick = true;}(""+tokens[1]+""+tokens[0], ""+tokens[4]+""+tokens[3]);\r
183                                         setTimeout(function() {doThisMove(); canClick = true;}, 500);\r
184                                 }\r
185                                 else if (tokens.length == 4 && !isNaN(tokens[0]) && !isNaN(tokens[1]) && !isNaN(tokens[2]) && isNaN(tokens[3]))\r
186                                 {\r
187                                         html = s1.innerHTML;\r
188                                         c = html.charAt(4);\r
189                                         piece = tokens[3];\r
190                                         if (piece == "knight") //HACK\r
191                                                 piece = "h";    \r
192                                         else\r
193                                                 piece = ""+piece.charAt(0);\r
194                                         if (tokens[2] == "1")\r
195                                                 html[html.length-1] = pieceChar[c][piece];\r
196 \r
197                                         s1.innerHTML = html.replace(/<bold>.*<\/bold>/i, "<bold>"+pieceChar[c][piece]+"</bold>");       \r
198                                 }\r
199                         }\r
200 \r
201                         /*\r
202                         if (ret.charAt(4) == "-" && ret.charAt(5) == ">") {\r
203                                 //Piece has been moved\r
204                                 //console.log("Moving other piece");\r
205                                 lines = ret.split("\n");\r
206                                 //if (lines[3] != "SELECT?") {\r
207                                 if (lines[2] != "SELECT?") {\r
208                                         x1 = lines[2].charAt(0);\r
209                                         y1 = lines[2].charAt(2);\r
210                                         x2 = lines[2].charAt(7);\r
211                                         y2 = lines[2].charAt(9);\r
212                                         console.log("Black Move: " + x1 + "" + y1 + " -> " + x2 + "" + y2);\r
213                                         doMove(y1 + "" + x1, y2 + "" + x2);\r
214                                 }\r
215                                 else {\r
216                                         console.log("Black Unable to move");\r
217                                 }\r
218                         }\r
219                         else {\r
220                                 lines = ret.split("\n");\r
221                                 if (lines[1] == "MOVE?") {\r
222                                         //We selected a piece\r
223                                         //console.log("choose where to move our piece");\r
224                                         piece = lines[0].charAt(6);\r
225                                         //console.log("Piece: " + piece);\r
226                                         content = document.getElementById(pieceSelected);\r
227                                         contentHTML = content.innerHTML;\r
228                                         //contentHTML = contentHTML.replace("?", piece);\r
229                                         //"W<br /><small>p</small> <bold>?</bold> <small>?</small></span>";\r
230                                         if (lines[0].charAt(4) == "1") {\r
231                                                 //console.log("changing quantum piece");\r
232                                                 contentHTML = replaceAt(contentHTML, 44, piece);\r
233                                         }\r
234                                         contentHTML = replaceAt(contentHTML, 28, piece);\r
235                                         //console.log(contentHTML);\r
236                                         //contentHTML = "CHANGED" + contentHTML;\r
237                                         content.innerHTML = contentHTML;\r
238                                 }\r
239                         }\r
240                         */\r
241                         //alert(ret);\r
242                 }\r
243         }\r
244         \r
245         //ar = "http://progcomp.ucc.asn.au/cgi-bin/qchess.cgi?" + queryString;\r
246         ar = "/../../../cgi-bin/qchess.cgi?" + queryString;\r
247         \r
248         console.log("AJAX Request: " + ar);\r
249         \r
250         ajaxRequest.open("GET", ar, true);\r
251         ajaxRequest.send();\r
252 }\r
253 \r
254 \r
255 \r
256 \r
257 \r
258 \r
259 function replaceAt(s, n, t) {\r
260         //console.log(s.substring(0, n) + "\n" + t + "\n" + s.substring(n + 1) + "\n");\r
261         return (s.substring(0, n) + t + s.substring(n + 1));\r
262 }\r

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