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
5 pieceSelected = ""; // currently selected piece
\r
7 colour = "W"; // colour of this player
\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
14 emptyHTML = "<!--0--> <big> <bold> </bold> </big> "
\r
16 // Select (or move) a piece
\r
17 function selectPiece(loc) {
\r
21 x = (""+loc).charAt(1);
\r
22 y = (""+loc).charAt(0);
\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
29 square = document.getElementById(loc);
\r
30 if (square.innerHTML.charAt(4) == colour)
\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
38 square.style.background = "#8F8";
\r
42 //alert("pieceMoved");
\r
43 if (validMove(pieceSelected, piece, loc)) {
\r
44 doMove(pieceSelected, loc);
\r
45 ajaxUpdate("x=" + x + "&y=" + y);
\r
49 console.log("Invalid Move");
\r
54 function resetColour(loc)
\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
60 square.style.background = "#DDD";
\r
64 function validMove(start, piece, end) {
\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
77 if ((+end[end.length-1] + +end[end.length-2]) % 2 == 1)
\r
79 s2.innerHTML = s2.innerHTML.replace(/<bold>.*<\/bold>/i, "<bold>?</bold>");
\r
81 //console.log("Piece Moved");
\r
84 function boardLoad() {
\r
85 ajaxUpdate("r=force_quit");
\r
89 for (i = 0; i < 8; i++) {
\r
90 for (j = 0; j < 8; j++) {
\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
101 black.innerHTML = "<!--B--> " + pieceChar["B"]["p"] + " <big> <bold>?</bold> </big> ?";
\r
102 white.innerHTML = "<!--W--> " + pieceChar["W"]["p"] + " <big> <bold>?</bold> </big> ?";
\r
104 black = document.getElementById("0" + i);
\r
105 white = document.getElementById("7" + i);
\r
107 if (i == 0 || i == 7)
\r
109 if (i == 1 || i == 6)
\r
111 if (i == 2 || i == 5)
\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
122 for (j = 2; j < 6; j++)
\r
124 square = document.getElementById(""+j + i);
\r
125 square.innerHTML = emptyHTML;
\r
129 setTimeout(function(){ajaxUpdate("r=start");}, 1000);
\r
133 function ajaxUpdate(queryString) {
\r
134 var ajaxRequest; // The variable that makes Ajax possible!
\r
137 // Opera 8.0+, Firefox, Safari
\r
138 ajaxRequest = new XMLHttpRequest();
\r
140 // Internet Explorer Browsers
\r
142 ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
\r
145 ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
\r
147 // Something went wrong
\r
148 alert("Your Browser is not Ajax Compatible, Please Upgrade to Google Chrome.");
\r
154 //alert(queryString);
\r
156 // Create a function that will receive data sent from the server
\r
157 ajaxRequest.onreadystatechange = function ()
\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
164 for (var i = 0; i < lines.length; ++i)
\r
166 tokens = lines[i].split(" ")
\r
167 x = Number(tokens[0]);
\r
169 if (isNaN(tokens[0]) || isNaN(tokens[1]))
\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
177 if ((+tokens[0] + +tokens[1]) % 2 == 0)
\r
178 s1.style.background = "#DFD";
\r
180 s1.style.background = "#8F8";
\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
185 else if (tokens.length == 4 && !isNaN(tokens[0]) && !isNaN(tokens[1]) && !isNaN(tokens[2]) && isNaN(tokens[3]))
\r
187 html = s1.innerHTML;
\r
188 c = html.charAt(4);
\r
190 if (piece == "knight") //HACK
\r
193 piece = ""+piece.charAt(0);
\r
194 if (tokens[2] == "1")
\r
195 html[html.length-1] = pieceChar[c][piece];
\r
197 s1.innerHTML = html.replace(/<bold>.*<\/bold>/i, "<bold>"+pieceChar[c][piece]+"</bold>");
\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
216 console.log("Black Unable to move");
\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
234 contentHTML = replaceAt(contentHTML, 28, piece);
\r
235 //console.log(contentHTML);
\r
236 //contentHTML = "CHANGED" + contentHTML;
\r
237 content.innerHTML = contentHTML;
\r
245 //ar = "http://progcomp.ucc.asn.au/cgi-bin/qchess.cgi?" + queryString;
\r
246 ar = "/../../../cgi-bin/qchess.cgi?" + queryString;
\r
248 console.log("AJAX Request: " + ar);
\r
250 ajaxRequest.open("GET", ar, true);
\r
251 ajaxRequest.send();
\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