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
8 function selectPiece(loc) {
\r
9 x = (""+loc).charAt(1);
\r
10 y = (""+loc).charAt(0);
\r
12 if (pieceSelected == "") {
\r
13 if (document.getElementById(loc).innerHTML.charAt(0) == "W") {
\r
14 console.log("Piece Selected: " + loc);
\r
15 pieceSelected = loc;
\r
16 ajaxUpdate("x=" + x + "&y=" + y);
\r
20 //alert("pieceMoved");
\r
21 if (validMove(pieceSelected, piece, loc)) {
\r
22 ajaxUpdate("x=" + x + "&y=" + y);
\r
23 doMove(pieceSelected, loc);
\r
27 console.log("Invalid Move");
\r
32 function validMove(start, piece, end) {
\r
36 function doMove(start, end) {
\r
37 begin = document.getElementById(start);
\r
38 end = document.getElementById(end);
\r
39 htmlToMove = begin.innerHTML;
\r
40 end.innerHTML = htmlToMove;
\r
41 begin.innerHTML = "";
\r
42 //console.log("Piece Moved");
\r
45 function boardLoad() {
\r
46 ajaxUpdate("r=force_quit");
\r
50 for (i = 0; i < 8; i++) {
\r
51 for (j = 0; j < 8; j++) {
\r
53 elem = document.getElementById(e)
\r
54 if ((i + j) % 2 == 0)
\r
55 elem.style.background="#FFF";
\r
57 elem.style.background="#DDD";
\r
61 //Place pieces on the board
\r
63 for (i = 0; i < 8; i++) {
\r
64 black = document.getElementById("1" + i);
\r
65 white = document.getElementById("6" + i);
\r
66 black.innerHTML = "B<br /><small>p</small> <bold>?</bold> <small>?</small></span>";
\r
67 white.innerHTML = "W<br /><small>p</small> <bold>?</bold> <small>?</small></span>";
\r
69 black = document.getElementById("0" + i);
\r
70 white = document.getElementById("7" + i);
\r
72 if (i == 0 || i == 7)
\r
74 if (i == 1 || i == 6)
\r
76 if (i == 2 || i == 5)
\r
83 black.innerHTML = "B<br /><small>" + piece + "</small> <bold>?</bold> <small>?</small>";
\r
84 white.innerHTML = "W<br /><small>" + piece + "</small> <bold>?</bold> <small>?</small>";
\r
87 setTimeout(function(){ajaxUpdate("r=start");}, 1000);
\r
91 function ajaxUpdate(queryString) {
\r
92 var ajaxRequest; // The variable that makes Ajax possible!
\r
95 // Opera 8.0+, Firefox, Safari
\r
96 ajaxRequest = new XMLHttpRequest();
\r
98 // Internet Explorer Browsers
\r
100 ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
\r
103 ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
\r
105 // Something went wrong
\r
106 alert("Your Browser is not Ajax Compatible, Please Upgrade to Google Chrome.");
\r
112 //alert(queryString);
\r
114 // Create a function that will receive data sent from the server
\r
115 ajaxRequest.onreadystatechange = function () {
\r
116 //alert("RS" + ajaxRequest.readyState);
\r
117 if (ajaxRequest.readyState == 4) {
\r
118 console.log("AJAX Response: " + ajaxRequest.responseText);
\r
119 ret = ""+ajaxRequest.responseText;
\r
120 if (ret.charAt(4) == "-" && ret.charAt(5) == ">") {
\r
121 //Piece has been moved
\r
122 //console.log("Moving other piece");
\r
123 lines = ret.split("\n");
\r
124 //if (lines[3] != "SELECT?") {
\r
125 if (lines[2] != "SELECT?") {
\r
126 x1 = lines[2].charAt(0);
\r
127 y1 = lines[2].charAt(2);
\r
128 x2 = lines[2].charAt(7);
\r
129 y2 = lines[2].charAt(9);
\r
130 console.log("Black Move: " + x1 + "" + y1 + " -> " + x2 + "" + y2);
\r
131 doMove(y1 + "" + x1, y2 + "" + x2);
\r
134 console.log("Black Unable to move");
\r
138 lines = ret.split("\n");
\r
139 if (lines[1] == "MOVE?") {
\r
140 //We selected a piece
\r
141 //console.log("choose where to move our piece");
\r
142 piece = lines[0].charAt(6);
\r
143 //console.log("Piece: " + piece);
\r
144 content = document.getElementById(pieceSelected);
\r
145 contentHTML = content.innerHTML;
\r
146 //contentHTML = contentHTML.replace("?", piece);
\r
147 //"W<br /><small>p</small> <bold>?</bold> <small>?</small></span>";
\r
148 if (lines[0].charAt(4) == "1") {
\r
149 //console.log("changing quantum piece");
\r
150 contentHTML = replaceAt(contentHTML, 44, piece);
\r
152 contentHTML = replaceAt(contentHTML, 28, piece);
\r
153 //console.log(contentHTML);
\r
154 //contentHTML = "CHANGED" + contentHTML;
\r
155 content.innerHTML = contentHTML;
\r
163 //ar = "http://progcomp.ucc.asn.au/cgi-bin/qchess.cgi?" + queryString;
\r
164 ar = "/../../../cgi-bin/qchess.cgi?" + queryString;
\r
166 console.log("AJAX Request: " + ar);
\r
168 ajaxRequest.open("GET", ar, true);
\r
169 ajaxRequest.send();
\r
177 function replaceAt(s, n, t) {
\r
178 //console.log(s.substring(0, n) + "\n" + t + "\n" + s.substring(n + 1) + "\n");
\r
179 return (s.substring(0, n) + t + s.substring(n + 1));
\r