quantum-chess

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

commit c15b98a8b0e02f9aa46bf117e7d79bf655c0146c
parent 6bd2afc762310d51344abdbe0dd10150610d78d0
Author: Cody Lewis <luxdotsugi@gmail.com>
Date:   Thu, 12 Apr 2018 23:59:35 +1000

The game is now playable

Diffstat:
MREADME.md | 2+-
Msrc/__pycache__/board.cpython-36.pyc | 0
Msrc/__pycache__/piece.cpython-36.pyc | 0
Msrc/board.py | 57++++++++++++++++++++++++++++++++++++++++++++++++---------
Msrc/board.pyc | 0
Msrc/piece.py | 13+++++++++----
Msrc/piece.pyc | 0
Msrc/play.py | 25+++++++++++++++++++++++--
8 files changed, 81 insertions(+), 16 deletions(-)

diff --git a/README.md b/README.md @@ -1,6 +1,6 @@ # QuantumChess Quantum Chess by Cody Lewis -Last Modified: 26-FEB-2018 +Last Modified: 12-APR-2018 This is a python implementation of quantum chess using IBM's Qiskit for the case of piece superposition. That is, to calculate the probability of the diff --git a/src/__pycache__/board.cpython-36.pyc b/src/__pycache__/board.cpython-36.pyc Binary files differ. diff --git a/src/__pycache__/piece.cpython-36.pyc b/src/__pycache__/piece.cpython-36.pyc Binary files differ. diff --git a/src/board.py b/src/board.py @@ -1,10 +1,11 @@ # board.py - QuantumChess # Author: Cody Lewis # Date: 26-FEB-2018 -# Mod.: 03-MAR-2018 +# Mod.: 12-APR-2018 # Description: # The board for the Quantum Chess # the board is indexed with a birds eye view with the white pieces on the bottom +import re import pawn import rook import bishop @@ -39,7 +40,6 @@ class Board: else: self.playBoard[index] = '0' def addMovement(self,i,move): - i = str(i) if(move == 'u'): i = chr(ord(i[0:1])-1) + str(int(i[1:])) elif(move == 'd'): @@ -58,7 +58,7 @@ class Board: i = chr(ord(i[0:1])-1) + str(int(i[1:])+1) return i - def play(self,start,end,colour): # TODO: modify the entanglement, add the superposition move + def play(self,start,end,colour,sp): # check if inputs are valid if(end != start and self.checkPoint(start) and self.checkPoint(end)): if(self.playBoard[start] != '0'): @@ -80,21 +80,27 @@ class Board: # output path as string into piece if(self.playBoard[end] == '0'): if(self.playBoard[start].canMove(movement)): - self.playBoard[end] = self.playBoard[start] - self.playBoard[start] = '0' + if(sp): + self.playBoard[start].superposition() + self.playBoard[end] = self.playBoard[start] + else: + self.playBoard[end] = self.playBoard[start] + self.playBoard[start] = '0' return True else: return False # check for attack else: # ends at another piece + if(sp): + return False if(self.playBoard[start].getId()[0:1] != self.playBoard[end].getId()[0:1]): # canMove function is contained in attack # then do the attack kill,supKill = self.playBoard[start].attack(self.playBoard[end],movement) if(kill): + if(supKill): + self.findAndDestroyParent(end) self.playBoard[end] = self.playBoard[start] self.playBoard[start] = '0' - if(supKill): - self.findAndDestroyAllId(end) return True return False else: @@ -207,9 +213,42 @@ class Board: for i in range(abs(dx)): string = string + 'h' return string - def findAndDestroyAllId(self,index): + def findAndDestroyParent(self,point): # search board for all matching pieces and destroy them - return '' + ident = self.playBoard[point].getId() + num = int(ident[len(ident)-1:len(ident)]) + parent = ident[0:len(ident)-1] + '.' + for i in range(97,98+self.rows): + for j in range(1,self.columns+1): + index = chr(i) + str(j) + if(self.playBoard[index] != '0'): + if(index != point and re.match(parent,self.playBoard[index].getId())): + otherId = self.playBoard[index].getId() + otherNum = int(otherId[len(otherId)-1:len(otherId)]) + if(otherNum >= num): + self.playBoard[index] = '0' + + def win(self): + blackWin = True + bkPat = 'BKi.*' + whiteWin = True + wkPat = 'WKi.*' + for i in range(97,98+self.rows): + for j in range(1,self.columns+1): + index = chr(i) + str(j) + if(self.playBoard[index] != '0'): + currId = self.playBoard[index].getId() + if(re.match(bkPat,currId)): + whiteWin = False + elif(re.match(wkPat,currId)): + blackWin = False + if(whiteWin): + return 'W' + elif(blackWin): + return 'B' + else: + return '0' + def toString(self): string = '' diff --git a/src/board.pyc b/src/board.pyc Binary files differ. diff --git a/src/piece.py b/src/piece.py @@ -1,7 +1,7 @@ # piece.py - QuantumChess # Author: Cody Lewis # Date: 21-FEB-2018 -# Mod.: 28-FEB-2018 +# Mod.: 12-APR-2018 # Description: Defines the super class of a piece in Quantum Chess import functions class Piece: @@ -14,15 +14,20 @@ class Piece: def getId(self): return self.idTag - def superpostion(self): + def getSuperPosNum(self): + return self.superPosNo + + def superposition(self): + self.superposNo += 1 + self.idTag = self.idTag + str(self.superposNo) if(self.firstSuperPos): - self.__init__(1,False,self.colour,self.idTag) + self.firstSuperPos = False def attack(self,enemy,movement): if(self.canAttack(movement)): return enemy.die() else: - return False + return False,False def canAttack(self,movement): return self.canMove(movement) diff --git a/src/piece.pyc b/src/piece.pyc Binary files differ. diff --git a/src/play.py b/src/play.py @@ -1,18 +1,39 @@ +# Author: Cody Lewis +# Date: 12-APR-2018 +# Description: The main game flow of the quantum chess game +import re import board b = board.Board() i = 0 col = 'B' -while(i != 10): +pattern = '[Yy]e?s?' +sp = False +while(True): + winVal = b.win() + if(winVal == 'W'): + print("White wins!") + break + elif(winVal == 'B'): + print("Black wins!") + break + print(b.toString()) i+=1 if(col == 'W'): col = 'B' + print("Blacks turn") else: col = 'W' + print("Whites turn") while(True): + superPos = str(input("Do you want to super-position (y/n)? ")) + if(re.match(pattern,superPos)): + print('Super-position mode on') + sp = True start = str(input("Choose your starting piece: ")) end = str(input("Choose your end place: ")) - if(b.play(start,end,col)): + if(b.play(start,end,col,sp)): + sp = False break else: print("Your move was invalid, try again")