quantum-chess

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

commit c0a3c7adffabdfcc2b175dcd77d6a185568e3050
parent adba60a24e036d67b402de60ff3a361f99b0c722
Author: Cody Lewis <luxdotsugi@gmail.com>
Date:   Tue, 31 Jul 2018 13:32:54 +1000

Started web app

Diffstat:
Asrc/WebApp/Main.py | 12++++++++++++
Asrc/WebApp/__init__.py | 31+++++++++++++++++++++++++++++++
Asrc/WebApp/__pycache__/Main.cpython-36.pyc | 0
Asrc/WebApp/__pycache__/__init__.cpython-36.pyc | 0
Asrc/WebApp/static/css/style.css | 4++++
Asrc/WebApp/static/images/background.png | 0
Asrc/WebApp/static/scripts/chess.js | 21+++++++++++++++++++++
Asrc/WebApp/templates/base.html | 45+++++++++++++++++++++++++++++++++++++++++++++
Asrc/WebApp/templates/error.html | 2++
Asrc/WebApp/templates/index.html | 17+++++++++++++++++
Asrc/cmd/__pycache__/bishop.cpython-36.pyc | 0
Asrc/cmd/__pycache__/board.cpython-36.pyc | 0
Asrc/cmd/__pycache__/functions.cpython-36.pyc | 0
Asrc/cmd/__pycache__/king.cpython-36.pyc | 0
Asrc/cmd/__pycache__/knight.cpython-36.pyc | 0
Asrc/cmd/__pycache__/pawn.cpython-36.pyc | 0
Asrc/cmd/__pycache__/piece.cpython-36.pyc | 0
Asrc/cmd/__pycache__/queen.cpython-36.pyc | 0
Asrc/cmd/__pycache__/rook.cpython-36.pyc | 0
Rsrc/bishop.py -> src/cmd/bishop.py | 0
Rsrc/board.py -> src/cmd/board.py | 0
Asrc/cmd/functions.py | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Rsrc/king.py -> src/cmd/king.py | 0
Rsrc/knight.py -> src/cmd/knight.py | 0
Rsrc/pawn.py -> src/cmd/pawn.py | 0
Asrc/cmd/piece.py | 42++++++++++++++++++++++++++++++++++++++++++
Rsrc/play.py -> src/cmd/play.py | 0
Rsrc/queen.py -> src/cmd/queen.py | 0
Rsrc/rook.py -> src/cmd/rook.py | 0
Dsrc/functions.py | 50--------------------------------------------------
Dsrc/piece.py | 42------------------------------------------
31 files changed, 224 insertions(+), 92 deletions(-)

diff --git a/src/WebApp/Main.py b/src/WebApp/Main.py @@ -0,0 +1,12 @@ +from flask import ( + Blueprint, render_template, url_for, redirect, current_app, g, session, request, flash +) +bp = Blueprint("Main", __name__, url_prefix="/") + +@bp.route("/") +def index(): + return redirect(url_for("Main.home")) + +@bp.route("/home/") +def home(): + return render_template("index.html") diff --git a/src/WebApp/__init__.py b/src/WebApp/__init__.py @@ -0,0 +1,31 @@ +import os +from flask import Flask, render_template +def create_app(test_config=None): + app = Flask(__name__, instance_relative_config=True) + app.config.from_mapping(SECRET_KEY='dev') + if test_config is None: + # load the instance config, if it exists, when not testing + app.config.from_pyfile('config.py', silent=True) + else: + # load the test config if passed in + app.config.from_mapping(test_config) + # ensure the instance folder exists + try: + os.makedirs(app.instance_path) + except OSError: + pass + # blueprint registration + from . import Main + app.register_blueprint(Main.bp) + + return app + +RAS_APP = create_app() + +@RAS_APP.errorhandler(404) +def page_not_found(e): + return render_template("error.html", error=404), 404 + +@RAS_APP.errorhandler(500) +def internal_error(e): + return render_template("error.html", error=500), 500 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__/__init__.cpython-36.pyc b/src/WebApp/__pycache__/__init__.cpython-36.pyc Binary files differ. diff --git a/src/WebApp/static/css/style.css b/src/WebApp/static/css/style.css @@ -0,0 +1,4 @@ +.bg-img { + background: linear-gradient(rgb(0, 72, 0), rgb(28, 100, 28), rgb(56, 128, 56), rgb(84, 156, 84), rgb(255, 255, 255)); + background-repeat: no-repeat; +} diff --git a/src/WebApp/static/images/background.png b/src/WebApp/static/images/background.png Binary files differ. diff --git a/src/WebApp/static/scripts/chess.js b/src/WebApp/static/scripts/chess.js @@ -0,0 +1,20 @@ +var Piece = { + id : "", + color : "", + firstSuperPos : true, + superPosNum : 0, + superPosition : function() { + if(this.firstSuperPos) { + this.firstSuperPos = false; + } + }, + // attack : attack(), +}; +$(document).ready(function() { + var canvas = document.getElementById("qccanvas"); + var ctx = canvas.getContext("2d"); + for(var i = 0; i < 300; i += 20) { + ctx.fillRect(i, i, i + 40, i + ); + } + ctx.fillRect(0, 0, 280, 138); +});+ \ No newline at end of file diff --git a/src/WebApp/templates/base.html b/src/WebApp/templates/base.html @@ -0,0 +1,44 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="description" content="Quantum Chess using Quantum Programming"> + <meta name="keywords" content=""> + <meta name="format-detection" content="telephone=no" /> + <meta name="msapplication-tap-highlight" content="no" /> + <!-- <meta http-equiv="Content-Security-Policy" content="default-src 'self'; worker-src 'none'; frame-src 'none'"> --> + <meta name="author" content="Cody Lewis"> + <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" /> + <link rel="stylesheet" href={{ url_for("static", filename="css/style.css") }}> + <link rel="shortcut icon" href={{ url_for("static", filename="images/icons/favicon.ico") }} type="image/x-icon"> + <title>{% block title %}{% endblock %} - / && STEM</title> + <!-- Bootstrap 4 --> + <!-- Latest compiled and minified CSS --> + <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"> + <!-- jQuery library --> + <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> + <!-- Popper JS --> + <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script> + <!-- Latest compiled JavaScript --> + <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script> + {% block scripts %}{% endblock %} +</head> +<body class="bg-img"> + <div class="container-fluid text-body"> + <nav class="navbar navbar-expand-sm bg-dark navbar-dark sticky-top"> + <ul class="navbar-nav"> + <li class="nav-item"> + <a class="nav-link selected" href="/home/">Home</a> + </li> + <li class="nav-item"> + <a class="nav-link">Other</a> + </li> + <li class="nav-item"> + <a class="nav-link" href="https://github.com/luxdosugi">Git</a> + </li> + </ul> + </nav> + {% block contents %}{% endblock %} + </div> +</body> +</html>+ \ No newline at end of file diff --git a/src/WebApp/templates/error.html b/src/WebApp/templates/error.html @@ -0,0 +1 @@ +{% extends "base.html" %}+ \ No newline at end of file diff --git a/src/WebApp/templates/index.html b/src/WebApp/templates/index.html @@ -0,0 +1,16 @@ +{% extends "base.html" %} + +{% block title %}Home{% endblock %} + +{% block scripts %} + <script src={{ url_for("static", filename="scripts/chess.js") }}></script> +{% endblock %} + +{% block contents %} + <div class="container-fluid bg-light"> + <div class="container"> + <!-- <h1 class="display-1">Quantum Chess</h1> --> + <canvas class="col-12" id="qccanvas"></canvas> + </div> + </div> +{% endblock %}+ \ No newline at end of file diff --git a/src/cmd/__pycache__/bishop.cpython-36.pyc b/src/cmd/__pycache__/bishop.cpython-36.pyc Binary files differ. 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__/functions.cpython-36.pyc b/src/cmd/__pycache__/functions.cpython-36.pyc Binary files differ. diff --git a/src/cmd/__pycache__/king.cpython-36.pyc b/src/cmd/__pycache__/king.cpython-36.pyc Binary files differ. diff --git a/src/cmd/__pycache__/knight.cpython-36.pyc b/src/cmd/__pycache__/knight.cpython-36.pyc Binary files differ. diff --git a/src/cmd/__pycache__/pawn.cpython-36.pyc b/src/cmd/__pycache__/pawn.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/__pycache__/queen.cpython-36.pyc b/src/cmd/__pycache__/queen.cpython-36.pyc Binary files differ. diff --git a/src/cmd/__pycache__/rook.cpython-36.pyc b/src/cmd/__pycache__/rook.cpython-36.pyc Binary files differ. diff --git a/src/bishop.py b/src/cmd/bishop.py diff --git a/src/board.py b/src/cmd/board.py diff --git a/src/cmd/functions.py b/src/cmd/functions.py @@ -0,0 +1,50 @@ +# functions.py - QuantumChess +# Author: Cody Lewis +# Date: 23-FEB-2018 +# Mod.: 04-MAY-2018 +# Description: +# defines misc. functions for the Quantum Chess program +from qiskit import QuantumProgram +def evalQubit(qrNo): # check if the most occurring qubit is all ones + qp = QuantumProgram() + try: + qr = qp.create_quantum_register("qr",qrNo) + cr = qp.create_classical_register("cr",qrNo) + qc = qp.create_circuit("superposition", [qr], [cr]) + + isPiece = '' + for i in range(qrNo): + qc.h(qr[i]) + isPiece = isPiece + '1' + qc.measure(qr, cr) + result = qp.execute(["superposition"], backend="local_qasm_simulator", shots=1024) + counts = result.get_counts("superposition") + return isPiece == getGreatestCount(qrNo, counts) + except QISKITError as ex: + print("There was an error in the circuit! Error = {}".format(ex)) + except RegisterSizeError as ex: + print("Error in the number of registers! Error = {}".format(ex)) + +def getGreatestCount(qrNo,counts): + # increment throught dict, find greatest value, return index + greatestCount = '' + gc = 0 + perms = 2**qrNo + for i in range(0,perms): + index = bin(i)[2:] + if(len(index) < qrNo): + while(len(index) < qrNo): + index = '0' + index + if(counts[index] > gc): + greatestCount = index + gc = counts[index] + return greatestCount + +def splitMovement(movement): + # split the string movement into individual characters and store them in the returned array + # movement key: f = up dia left, u = up, q = up dia right, r = right, l = left, h = down dia left, d = down, g = down dia right + # Note: The move movement directions are from a birds eye view with the white pieces on the bottom + moveArr = [] + for i in range(len(movement)): + moveArr.append(movement[i:i+1]) + return moveArr diff --git a/src/king.py b/src/cmd/king.py diff --git a/src/knight.py b/src/cmd/knight.py diff --git a/src/pawn.py b/src/cmd/pawn.py diff --git a/src/cmd/piece.py b/src/cmd/piece.py @@ -0,0 +1,42 @@ +# piece.py - QuantumChess +# Author: Cody Lewis +# Date: 21-FEB-2018 +# Mod.: 12-APR-2018 +# Description: Defines the super class of a piece in Quantum Chess +import functions +class Piece: + def __init__(self,superposNum,frstSuperPos,col,idT): + self.superposNo = superposNum + self.firstSuperPos = frstSuperPos + self.colour = col + self.idTag = self.colour + idT + + def getId(self): + return self.idTag + + def getSuperPosNum(self): + return self.superPosNo + + def superposition(self): + self.superposNo += 1 + self.idTag = self.idTag + str(self.superposNo) + if(self.firstSuperPos): + self.firstSuperPos = False + + def attack(self,enemy,movement): + if(self.canAttack(movement)): + return enemy.die() + else: + return False,False + + def canAttack(self,movement): + return self.canMove(movement) + + def die(self): + if(self.superposNo > 0): + return True,self.observe() + return True,False + + def observe(self): + # check the Qubit stored in this piece + return functions.evalQubit(self.superposNo) diff --git a/src/play.py b/src/cmd/play.py diff --git a/src/queen.py b/src/cmd/queen.py diff --git a/src/rook.py b/src/cmd/rook.py diff --git a/src/functions.py b/src/functions.py @@ -1,50 +0,0 @@ -# functions.py - QuantumChess -# Author: Cody Lewis -# Date: 23-FEB-2018 -# Mod.: 04-MAY-2018 -# Description: -# defines misc. functions for the Quantum Chess program -def evalQubit(qrNo): # check if the most occurring qubit is all ones - from qiskit import QuantumProgram - qp = QuantumProgram() - try: - qr = qp.create_quantum_register("qr",qrNo) - cr = qp.create_classical_register("cr",qrNo) - qc = qp.create_circuit("superposition", [qr], [cr]) - - isPiece = '' - for i in range(qrNo): - qc.h(qr[i]) - isPiece = isPiece + '1' - qc.measure(qr, cr) - result = qp.execute(["superposition"], backend="local_qasm_simulator", shots=1024) - counts = result.get_counts("superposition") - return isPiece == getGreatestCount(qrNo,counts) - except QISKITError as ex: - print("There was an error in the circuit! Error = {}".format(ex)) - except RegisterSizeError as ex: - print("Error in the number of registers! Error = {}".format(ex)) - -def getGreatestCount(qrNo,counts): - # increment throught dict, find greatest value, return index - greatestCount = '' - gc = 0 - perms = 2**qrNo - for i in range(0,perms): - index = bin(i)[2:] - if(len(index) < qrNo): - while(len(index) < qrNo): - index = '0' + index - if(counts[index] > gc): - greatestCount = index - gc = counts[index] - return greatestCount - -def splitMovement(movement): - # split the string movement into individual characters and store them in the returned array - # movement key: f = up dia left, u = up, q = up dia right, r = right, l = left, h = down dia left, d = down, g = down dia right - # Note: The move movement directions are from a birds eye view with the white pieces on the bottom - moveArr = [] - for i in range(len(movement)): - moveArr.append(movement[i:i+1]) - return moveArr diff --git a/src/piece.py b/src/piece.py @@ -1,42 +0,0 @@ -# piece.py - QuantumChess -# Author: Cody Lewis -# Date: 21-FEB-2018 -# Mod.: 12-APR-2018 -# Description: Defines the super class of a piece in Quantum Chess -import functions -class Piece: - def __init__(self,superposNum,frstSuperPos,col,idT): - self.superposNo = superposNum - self.firstSuperPos = frstSuperPos - self.colour = col - self.idTag = self.colour + idT - - def getId(self): - return self.idTag - - def getSuperPosNum(self): - return self.superPosNo - - def superposition(self): - self.superposNo += 1 - self.idTag = self.idTag + str(self.superposNo) - if(self.firstSuperPos): - self.firstSuperPos = False - - def attack(self,enemy,movement): - if(self.canAttack(movement)): - return enemy.die() - else: - return False,False - - def canAttack(self,movement): - return self.canMove(movement) - - def die(self): - if(self.superposNo > 0): - return True,self.observe() - return True,False - - def observe(self): - # check the Qubit stored in this piece - return functions.evalQubit(self.superposNo)