quantum-chess

git clone git://git.codymlewis.com/quantum-chess.git
Log | Files | Refs | LICENSE

commit 84a754b816dc39aed9318d56795ebddc04988fc1
parent 13b5128fe83295099c7a5de7e3414fd44e84f482
Author: Cody Lewis <luxdotsugi@gmail.com>
Date:   Wed,  8 Aug 2018 15:41:28 +1000

The prototype gui works

Diffstat:
Msrc/WebApp/Board.py | 32++++++++++++++++++--------------
Msrc/WebApp/Main.py | 7++++---
Msrc/WebApp/__pycache__/Board.cpython-36.pyc | 0
Msrc/WebApp/__pycache__/Main.cpython-36.pyc | 0
Msrc/WebApp/__pycache__/Piece.cpython-36.pyc | 0
Msrc/WebApp/static/scripts/chess.js | 57+++++++++++++++++++++++++++++++++++++++------------------
Msrc/WebApp/templates/index.html | 11+++++++----
Msrc/cmd/__pycache__/board.cpython-36.pyc | 0
Msrc/cmd/__pycache__/piece.cpython-36.pyc | 0
Msrc/cmd/board.py | 2+-
Msrc/cmd/piece.py | 1+
11 files changed, 70 insertions(+), 40 deletions(-)

diff --git a/src/WebApp/Board.py b/src/WebApp/Board.py @@ -4,7 +4,7 @@ from . import Functions class Board: def __init__(self): self.playBoard = dict() - self.rows = 8 + self.rows = 7 self.columns = 8 for i in range(97, 98+self.rows): # letters for columns, with 'a' at the top and 'i' at the bottom if(i == 97 or i == 98): @@ -29,6 +29,9 @@ class Board: else: self.playBoard[index] = '0' + def reset(self): + self.__init__() + def addMovement(self, i, move): # add an atomic movement unit to the index if(move == Functions.Direction.UP.value): i = chr(ord(i[0:1])-1) + str(int(i[1:])) @@ -50,6 +53,7 @@ class Board: def play(self, start, end, colour, sp): # check if inputs are valid + supKill = False if(end != start and self.checkPoint(start) and self.checkPoint(end)): if(self.playBoard[start] != '0'): # check if colour matches @@ -60,13 +64,11 @@ class Board: movement = self.pathToString(dx, dy) i = start i = self.addMovement(i, movement[0:1]) - # if((self.playBoard[start].getId()[1:3] != "Kn" and (movement[0:1] in ['u', 'd', 'l', 'r'])) and i != end and self.playBoard[i] != '0'): # make sure nothing is in the way - # return False if(len(movement) > 1): for j in range(1, len(movement)): i = self.addMovement(i, movement[j : j + 1]) if(i != end and self.playBoard[i] != '0'): # check whether there are pieces in the way - return False + return False, supKill # output path as string into piece if(self.playBoard[end] == '0'): if(self.playBoard[start].canMove(movement)): @@ -76,9 +78,9 @@ class Board: else: self.playBoard[end] = self.playBoard[start] self.playBoard[start] = '0' - return True + return True, supKill else: - return False + return False, supKill # check for attack else: # ends at another piece if(sp): @@ -91,11 +93,11 @@ class Board: self.findAndDestroyParent(end) self.playBoard[end] = self.playBoard[start] self.playBoard[start] = '0' - return True - return False + return True, supKill + return False, supKill else: - return False - return False + return False, supKill + return False, supKill def checkPoint(self, p): # check whether a point is on the board charNum = ord(p[0 : 1]) @@ -260,7 +262,9 @@ class Board: return string INSTANCEBOARD = Board() + +def reset_board(): + INSTANCEBOARD.reset() + def play(start, end, colour, sp): - if INSTANCEBOARD.play(start, end, colour, sp): - return INSTANCEBOARD.toString() - return False- \ No newline at end of file + return INSTANCEBOARD.play(start, end, colour, sp)+ \ No newline at end of file diff --git a/src/WebApp/Main.py b/src/WebApp/Main.py @@ -12,10 +12,11 @@ def index(): @bp.route("/home", methods=["GET", "POST"]) def home(): if request.method == "POST": - sp = True if request.form["sp"] == "checked" else False + sp = True if request.form["sp"] == "True" else False colour = clean(request.form["colour"]) start = clean(request.form["start"]) end = clean(request.form["end"]) - return "success" if Board.play(start, end, colour, sp) else "fail" - # return "sp: {}, colour: {}, start: {}, end: {}".format(sp, colour, start, end) + success, sup_kill = Board.play(start, end, colour, sp) + return "{},{}".format("success" if success else "fail", "success" if sup_kill else "fail") + Board.reset_board() return render_template("index.html") diff --git a/src/WebApp/__pycache__/Board.cpython-36.pyc b/src/WebApp/__pycache__/Board.cpython-36.pyc Binary files differ. diff --git a/src/WebApp/__pycache__/Main.cpython-36.pyc b/src/WebApp/__pycache__/Main.cpython-36.pyc Binary files differ. diff --git a/src/WebApp/__pycache__/Piece.cpython-36.pyc b/src/WebApp/__pycache__/Piece.cpython-36.pyc Binary files differ. diff --git a/src/WebApp/static/scripts/chess.js b/src/WebApp/static/scripts/chess.js @@ -93,28 +93,49 @@ function movement(id, classes) { // post plays to backend, then change board based on response function postMovement(start, end) { + document.getElementById("error").innerHTML = ""; var turn = document.getElementById("turn"); var colour = turn.innerHTML == "Blue's Turn" ? "W" : "B"; - var validMove; - superpos = sp.value == "checked" ? "True" : "False"; + var superpos = sp.checked ? "True" : "False"; $.post("/home", { "sp" : superpos, "colour" : colour, "start" : start, "end" : end }, function(data) { - // validMove = data; - alert("start: " + start + " end: " + end + " sp: " + superpos + " colour: " + colour); - alert(data); - }); - if(validMove) { - // if backend function returns true - var startSquare = document.getElementById(start); - var endSquare = document.getElementById(end); - endSquare.innerHTML = startSquare.innerHTML; - startSquare.innerHTML = ""; - if(turn.innerHTML == "Blue's Turn") { - turn.innerHTML = "Red's Turn"; - turn.className = "pc-black text-center"; + result = data.split(","); + if(result[0] == "success") { // if backend function returns true + if(result[1] == "success") { + findAndDestroy(document.getElementById(end).getElementsByClassName("piece")[0].id); + } + makeMove(start, end); } else { - turn.innerHTML = "Blue's Turn"; - turn.className = "pc-white text-center"; + document.getElementById("error").innerHTML = "Your move was invalid"; } - } + }); convertToStart(); +} + +function findAndDestroy(tag) { + tag = tag.substring(0, tag.length - 1); + var pieces = document.getElementsByClassName("piece"); + for(var i = 0; i < pieces.length; i++) { + if(pieces[i].id.indexOf(tag) > -1) { + pieces[i].innerHTML = ""; + } + } +} + +function makeMove(start, end) { + var startSquare = document.getElementById(start); + var endSquare = document.getElementById(end); + endSquare.innerHTML = startSquare.innerHTML; + if(!sp.checked) { + startSquare.innerHTML = ""; + } else { + var endPiece = endSquare.getElementsByClassName("piece")[0]; + endPiece.id += "1"; + } + if(turn.innerHTML == "Blue's Turn") { + turn.innerHTML = "Red's Turn"; + turn.className = "pc-black text-center"; + } else { + turn.innerHTML = "Blue's Turn"; + turn.className = "pc-white text-center"; + } } \ No newline at end of file diff --git a/src/WebApp/templates/index.html b/src/WebApp/templates/index.html @@ -10,7 +10,7 @@ {% block contents %} <div class="container-fluid bg-light"> - <!-- <h1 class="display-1 text-center">Quantum Chess</h1> --> + <h1 class="display-1 text-center">Quantum Chess</h1> <br /> <p class="pc-white text-center" id="turn">Blue's Turn</p> <div class="container"> @@ -19,9 +19,9 @@ <div class="row"> <div class="col-sm-2 text-right"></div> {% for j in range(0, 8) %} - <div class="col-1 {% if ((j % 2) == (i % 2)) %}bg-dark{% else %}bg-light{% endif %} square" id="{{ "abcgefgh"[i] }}{{ j + 1 }}"> + <div class="col-1 {% if ((j % 2) == (i % 2)) %}bg-dark{% else %}bg-light{% endif %} square" id="{{ "abcdefgh"[i] }}{{ j + 1 }}"> {% if i in [0, 1, 6, 7] %} - <span class="piece text-center fa + <span id="{{ 'abcdefgh'[i] }}{{ j + 1 }}" class="piece text-center fa {% if i in [0, 7] %} {% if j in [0, 7] %} fa-chess-rook @@ -50,11 +50,14 @@ </div> {% endfor %} <br /> + <div class="text-center text-danger" id="error"></div> + <br /> <div class="col-sm-4 mx-auto checkbox"> <label class="" for="sp">Super-position</label> - <input type="checkbox" data-toggle="toggle" data-onstyle="success" data-offstyle="danger" class="" name="sp" id="sp" value="checked"> + <input type="checkbox" data-toggle="toggle" data-onstyle="success" data-offstyle="danger" class="" name="sp" id="sp" value="superpos"> </div> </div> </div> </div> + <br /> {% endblock %} \ No newline at end of file diff --git a/src/cmd/__pycache__/board.cpython-36.pyc b/src/cmd/__pycache__/board.cpython-36.pyc Binary files differ. diff --git a/src/cmd/__pycache__/piece.cpython-36.pyc b/src/cmd/__pycache__/piece.cpython-36.pyc Binary files differ. diff --git a/src/cmd/board.py b/src/cmd/board.py @@ -15,7 +15,7 @@ from functions import Direction class Board: def __init__(self): self.playBoard = dict() - self.rows = 8 + self.rows = 7 self.columns = 8 for i in range(97, 98+self.rows): # letters for columns, with 'a' at the top and 'i' at the bottom if(i == 97 or i == 98): diff --git a/src/cmd/piece.py b/src/cmd/piece.py @@ -19,6 +19,7 @@ class Piece: def superposition(self): self.superposNo += 1 self.idTag = self.idTag + str(self.superposNo) + print(self.idTag) if self.firstSuperPos: self.firstSuperPos = False