quantum-chess

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

commit 24c60df5d92316806393195ca04a128c43f4d804
parent 3e4b2dfe1c24980cb59a0639a83886db89c888ec
Author: Cody Lewis <luxdotsugi@gmail.com>
Date:   Sun,  5 Aug 2018 21:18:47 +1000

Completed conversion to Enums for the direction

Diffstat:
Msrc/WebApp/Main.py | 86+++----------------------------------------------------------------------------
Msrc/cmd/__pycache__/bishop.cpython-36.pyc | 0
Msrc/cmd/__pycache__/board.cpython-36.pyc | 0
Msrc/cmd/__pycache__/functions.cpython-36.pyc | 0
Msrc/cmd/__pycache__/piece.cpython-36.pyc | 0
Msrc/cmd/bishop.py | 3++-
Msrc/cmd/board.py | 142++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/cmd/functions.py | 1-
Msrc/cmd/king.py | 9+++++----
Msrc/cmd/knight.py | 25+++++++++++++------------
Msrc/cmd/pawn.py | 21+++++++++++----------
Msrc/cmd/piece.py | 9++++-----
Msrc/cmd/play.py | 64++++++++++++++++++++++++++++++++--------------------------------
Msrc/cmd/queen.py | 12++++++------
Msrc/cmd/rook.py | 14+++++++-------
15 files changed, 154 insertions(+), 232 deletions(-)

diff --git a/src/WebApp/Main.py b/src/WebApp/Main.py @@ -8,86 +8,6 @@ bp = Blueprint("Main", __name__, url_prefix="/") def index(): return redirect(url_for("Main.home")) -@bp.route("/home/") +@bp.route("/home") def home(): - return render_template("index.html") - -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 -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 evalQubit(self.superposNo)- \ No newline at end of file + return render_template("index.html")+ \ 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__/piece.cpython-36.pyc b/src/cmd/__pycache__/piece.cpython-36.pyc Binary files differ. diff --git a/src/cmd/bishop.py b/src/cmd/bishop.py @@ -3,6 +3,7 @@ # Date: 24-FEB-2018 import piece import functions +from functions import Direction class Bishop(piece.Piece): def __init__(self, superposNum, frstSuperPos, col, idT): idT = 'B ' + str(idT) @@ -11,7 +12,7 @@ class Bishop(piece.Piece): def canMove(self,movement): moveArr = functions.splitMovement(movement) direction = moveArr[0] - if direction == functions.Direction.UPLEFT or direction == functions.Direction.UPRIGHT or direction == functions.Direction.DOWNLEFT or direction == functions.Direction.DOWNRIGHT: + if direction in [Direction.UPLEFT, Direction.UPRIGHT, Direction.DOWNLEFT, Direction.DOWNRIGHT]: for i in range(1, len(moveArr)): if(direction != moveArr[i]): return False diff --git a/src/cmd/board.py b/src/cmd/board.py @@ -1,7 +1,6 @@ # board.py - QuantumChess # Author: Cody Lewis # Date: 26-FEB-2018 -# Mod.: 04-MAY-2018 # Description: # The board for the Quantum Chess # the board is indexed with a birds eye view with the white pieces on the bottom @@ -12,54 +11,55 @@ import bishop import knight import queen import king +from functions import Direction class Board: def __init__(self): self.playBoard = dict() self.rows = 8 self.columns = 8 - for i in range(97,98+self.rows): # letters for columns, with 'a' at the top and 'i' at the bottom + 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): colour = 'B' - elif(i == 96+self.rows or i == 97+self.rows): + elif(i == 96 + self.rows or i == 97 + self.rows): colour = 'W' - for j in range(1,self.columns+1): # numbers for rows, with 1 at the left and 8 at the right + for j in range(1, self.columns+1): # numbers for rows, with 1 at the left and 8 at the right index = str(chr(i)) + str(j) - if(i == 97 or i==97+self.rows): # essentially a factory method + if(i == 97 or i == 97 + self.rows): # essentially a factory method if(j == 1 or j == 8): - self.playBoard[index] = rook.Rook(0,True,colour,j) + self.playBoard[index] = rook.Rook(0, True, colour, j) elif(j == 2 or j == 7): - self.playBoard[index] = knight.Knight(0,True,colour,j) + self.playBoard[index] = knight.Knight(0, True, colour, j) elif(j == 3 or j == 6): - self.playBoard[index] = bishop.Bishop(0,True,colour,j) + self.playBoard[index] = bishop.Bishop(0, True, colour, j) elif(j == 4): - self.playBoard[index] = queen.Queen(0,True,colour,j) + self.playBoard[index] = queen.Queen(0, True, colour, j) else: - self.playBoard[index] = king.King(0,True,colour,j) - elif(i == 98 or i==96+self.rows): - self.playBoard[index] = pawn.Pawn(0,True,colour,j) + self.playBoard[index] = king.King(0, True, colour, j) + elif(i == 98 or i == 96 + self.rows): + self.playBoard[index] = pawn.Pawn(0, True, colour, j) else: self.playBoard[index] = '0' - def addMovement(self,i,move): # add an atomic movement unit to the index - if(move == 'u'): + def addMovement(self, i, move): # add an atomic movement unit to the index + if(move == Direction.UP): i = chr(ord(i[0:1])-1) + str(int(i[1:])) - elif(move == 'd'): + elif(move == Direction.DOWN): i = chr(ord(i[0:1])+1) + str(int(i[1:])) - elif(move == 'r'): + elif(move == Direction.RIGHT): i = i[0:1] + str(int(i[1:])-1) - elif(move == 'l'): + elif(move == Direction.LEFT): i = i[0:1] + str(int(i[1:])+1) - elif(move == 'q'): + elif(move == Direction.UPRIGHT): i = chr(ord(i[0:1])-1) + str(int(i[1:])-1) - elif(move == 'g'): + elif(move == Direction.DOWNRIGHT): i = chr(ord(i[0:1])+1) + str(int(i[1:])-1) - elif(move == 'h'): + elif(move == Direction.DOWNLEFT): i = chr(ord(i[0:1])+1) + str(int(i[1:])+1) else: i = chr(ord(i[0:1])-1) + str(int(i[1:])+1) return i - def play(self,start,end,colour,sp): + 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'): @@ -68,14 +68,14 @@ class Board: # evaluate the path travelled dy = ord(start[0:1]) - ord(end[0:1]) # +ve: up, -ve: down dx = int(start[1:]) - int(end[1:]) # +ve: right, -ve: left - movement = self.pathToString(dx,dy) + 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]) + 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 # output path as string into piece @@ -94,9 +94,9 @@ class Board: 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 + 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) + kill, supKill = self.playBoard[start].attack(self.playBoard[end], movement) if(kill): if(supKill): self.findAndDestroyParent(end) @@ -108,15 +108,15 @@ class Board: return False return False - def checkPoint(self,p): # check whether a point is on the board - charNum = ord(p[0:1]) - if(charNum > 96 and charNum < 98+self.rows): - num = int(p[1:]) + def checkPoint(self, p): # check whether a point is on the board + charNum = ord(p[0 : 1]) + if(charNum > 96 and charNum < 98 + self.rows): + num = int(p[1 : ]) if(num > 0 and num < 9): return True return False - def pathToString(self,dx,dy): + def pathToString(self, dx, dy): string = '' if(dx > 0): # goes right if(dy > 0): # goes up @@ -126,21 +126,21 @@ class Board: string = 'uq' elif(dx > dy): diff = dx - dy - for i in range(dy): + for _i in range(dy): string = string + 'q' - for i in range(diff): + for _i in range(diff): string = string + 'r' elif(dx < dy): diff = dy - dx - for i in range(dx): + for _i in range(dx): string = string + 'q' - for i in range(diff): + for _i in range(diff): string = string + 'u' else: - for i in range(dx): + for _i in range(dx): string = string + 'q' elif(dy == 0): - for i in range(dx): + for _i in range(dx): string = string + 'r' else: # goes down if(dx == 2 and dy == -1): @@ -149,25 +149,25 @@ class Board: string = 'dg' elif(dx > abs(dy)): diff = dx - abs(dy) # dy is -ve - for i in range(abs(dy)): + for _i in range(abs(dy)): string = string + 'g' - for i in range(diff): + for _i in range(diff): string = string + 'r' elif(dx < abs(dy)): diff = abs(dy) - dx - for i in range(dx): + for _i in range(dx): string = string + 'g' - for i in range(diff): + for _i in range(diff): string = string + 'd' else: - for i in range(dx): + for _i in range(dx): string = string + 'g' elif(dx == 0): if(dy > 0): # up - for i in range(dy): + for _i in range(dy): string = string + 'u' else: # down (no change is an invalid input) - for i in range(abs(dy)): + for _i in range(abs(dy)): string = string + 'd' else: # goes left if(dy > 0): # goes up @@ -177,21 +177,21 @@ class Board: string = 'uf' elif(abs(dx) > dy): diff = abs(dx) - dy - for i in range(dy): + for _i in range(dy): string = string + 'f' - for i in range(diff): + for _i in range(diff): string = string + 'l' elif(abs(dx) < dy): diff = dy - abs(dx) - for i in range(abs(dx)): + for _i in range(abs(dx)): string = string + 'f' - for i in range(diff): + for _i in range(diff): string = string + 'u' else: - for i in range(abs(dx)): + for _i in range(abs(dx)): string = string + 'f' elif(dy == 0): - for i in range(abs(dx)): + for _i in range(abs(dx)): string = string + 'l' else: # goes down if(dx == -2 and dy == -1): @@ -200,33 +200,33 @@ class Board: string = 'dh' elif(abs(dx) > abs(dy)): diff = abs(dx) - abs(dy) # dy is -ve - for i in range(abs(dy)): + for _i in range(abs(dy)): string = string + 'h' - for i in range(diff): + for _i in range(diff): string = string + 'l' elif(abs(dx) < abs(dy)): diff = abs(dy) - abs(dx) - for i in range(abs(dx)): + for _i in range(abs(dx)): string = string + 'h' - for i in range(diff): + for _i in range(diff): string = string + 'd' else: - for i in range(abs(dx)): + for _i in range(abs(dx)): string = string + 'h' return string - def findAndDestroyParent(self,point): + def findAndDestroyParent(self, point): # search board for all matching pieces and destroy them ident = self.playBoard[point].getId() # identifier - 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): + 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())): + if(index != point and re.match(parent, self.playBoard[index].getId())): otherId = self.playBoard[index].getId() - otherNum = int(otherId[len(otherId)-1:len(otherId)]) + otherNum = int(otherId[len(otherId) - 1 : len(otherId)]) if(otherNum >= num): self.playBoard[index] = '0' @@ -235,14 +235,14 @@ class Board: bkPat = 'BKi.*' whiteWin = True wkPat = 'WKi.*' - for i in range(97,98+self.rows): - for j in range(1,self.columns+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'): currId = self.playBoard[index].getId() - if(re.match(bkPat,currId)): + if(re.match(bkPat, currId)): whiteWin = False - elif(re.match(wkPat,currId)): + elif(re.match(wkPat, currId)): blackWin = False if(whiteWin): return 'W' @@ -253,18 +253,18 @@ class Board: def toString(self): string = '' - for i in range(97,99+self.rows): # iterate through the letter part of the index - if(i < 98+self.rows): + for i in range(97, 99 + self.rows): # iterate through the letter part of the index + if(i < 98 + self.rows): string = string + chr(i) string = string + ' ' - for j in range(1,self.columns+1): + for j in range(1, self.columns+1): index = str(chr(i)) + str(j) - if(i == 98+self.rows): + if(i == 98 + self.rows): string = string + ' ' + str(j) elif(self.playBoard[index] == '0'): string = string + '| ' else: - idTag = self.playBoard[index].getId()[:3] + idTag = self.playBoard[index].getId()[ : 3] string = string + '|' + idTag if(i < 98+self.rows): # put a pipe at the end of the line string = string + '|\n' diff --git a/src/cmd/functions.py b/src/cmd/functions.py @@ -1,7 +1,6 @@ # functions.py - QuantumChess # Author: Cody Lewis # Date: 23-FEB-2018 -# Mod.: 04-MAY-2018 # Description: # defines misc. functions for the Quantum Chess program from enum import Enum diff --git a/src/cmd/king.py b/src/cmd/king.py @@ -4,15 +4,16 @@ # Mod.: 24-FEB-2018 import piece import functions +from functions import Direction class King(piece.Piece): - def __init__(self,superposNum,frstSuperPos,col,idT): + def __init__(self, superposNum, frstSuperPos, col, idT): idT = 'Ki' + str(idT) - piece.Piece.__init__(self,superposNum,frstSuperPos,col,idT) + piece.Piece.__init__(self, superposNum, frstSuperPos, col, idT) - def canMove(self,movement): + def canMove(self, movement): moveArr = functions.splitMovement(movement) direction = moveArr[0] if(len(moveArr) == 1): - if(direction == 'd' or direction == 'u' or direction == 'r' or direction == 'l' or direction == 'f' or direction == 'q' or direction == 'h' or direction == 'g'): + if direction in [Direction.DOWN, Direction.UP, Direction.RIGHT, Direction.LEFT, Direction.DOWNRIGHT, Direction.DOWNLEFT, Direction.UPRIGHT, Direction.UPLEFT]: return True return False diff --git a/src/cmd/knight.py b/src/cmd/knight.py @@ -4,30 +4,31 @@ # Mod.: 24-FEB-2018 import piece import functions +from functions import Direction class Knight(piece.Piece): - def __init__(self,superposNum,frstSuperPos,col,idT): + def __init__(self, superposNum, frstSuperPos, col, idT): idT = 'Kn' + str(idT) - piece.Piece.__init__(self,superposNum,frstSuperPos,col,idT) + piece.Piece.__init__(self, superposNum, frstSuperPos, col, idT) - def canMove(self,movement): + def canMove(self, movement): moveArr = functions.splitMovement(movement) if(len(moveArr) == 2): # need the L movement if(self.__isL(moveArr)): return True return False - def __isL(self,moveArr): - if(moveArr[0] == 'u'): - if(moveArr[1] == 'f' or moveArr[1] == 'q'): + def __isL(self, moveArr): + if(moveArr[0] == Direction.UP): + if(moveArr[1] == Direction.UPLEFT or moveArr[1] == Direction.UPRIGHT): return True - elif(moveArr[0] == 'd'): - if(moveArr[1] == 'h' or moveArr[1] == 'g'): + elif(moveArr[0] == Direction.DOWN): + if(moveArr[1] == Direction.DOWNLEFT or moveArr[1] == Direction.DOWNRIGHT): return True - elif(moveArr[0] == 'l'): - if(moveArr[1] == 'q' or moveArr[1] == 'g'): + elif(moveArr[0] == Direction.LEFT): + if(moveArr[1] == Direction.UPLEFT or moveArr[1] == Direction.DOWNLEFT): return True - elif(moveArr[0] == 'r'): - if(moveArr[1] == 'f' or moveArr[1] == 'h'): + elif(moveArr[0] == Direction.RIGHT): + if(moveArr[1] == Direction.UPRIGHT or moveArr[1] == Direction.DOWNRIGHT): return True else: return False diff --git a/src/cmd/pawn.py b/src/cmd/pawn.py @@ -4,13 +4,14 @@ # Mod.: 23-FEB-2018 import piece import functions +from functions import Direction class Pawn(piece.Piece): - def __init__(self,superposNum,frstSuperPos,col,idT): + def __init__(self, superposNum, frstSuperPos, col, idT): idT = 'P ' + str(idT) - piece.Piece.__init__(self,superposNum,frstSuperPos,col,idT) + piece.Piece.__init__(self, superposNum, frstSuperPos, col, idT) self.firstMove = True - def canMove(self,movement): + def canMove(self, movement): moveArr = functions.splitMovement(movement) if(self.firstMove): moveNum = 2 @@ -19,23 +20,23 @@ class Pawn(piece.Piece): moveNum = 1 if(len(moveArr) <= moveNum): if(self.colour == 'W'): - direction = 'u' + direction = Direction.UP else: - direction = 'd' + direction = Direction.DOWN for i in range(len(moveArr)): if(not moveArr[i] == direction): return False return True return False - def canAttack(self,movement): + def canAttack(self, movement): moveArr = functions.splitMovement(movement) if(self.colour == 'W'): - dir1 = 'f' - dir2 = 'q' + dir1 = Direction.UPLEFT + dir2 = Direction.UPRIGHT else: - dir1 = 'h' - dir2 = 'g' + dir1 = Direction.DOWNLEFT + dir2 = Direction.DOWNRIGHT if(len(moveArr) == 1 and (moveArr[0] == dir1 or moveArr[0] == dir2)): return True else: diff --git a/src/cmd/piece.py b/src/cmd/piece.py @@ -1,7 +1,6 @@ # 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: @@ -15,7 +14,7 @@ class Piece: return self.idTag def getSuperPosNum(self): - return self.superPosNo + return self.superposNo def superposition(self): self.superposNo += 1 @@ -23,13 +22,13 @@ class Piece: if self.firstSuperPos: self.firstSuperPos = False - def attack(self,enemy,movement): + def attack(self, enemy, movement): if self.canAttack(movement): return enemy.die() else: - return False,False + return False, False - def canAttack(self,movement): + def canAttack(self, movement): return self.canMove(movement) def die(self): diff --git a/src/cmd/play.py b/src/cmd/play.py @@ -3,38 +3,38 @@ # Description: The main game flow of the quantum chess game import re import board -b = board.Board() -i = 0 -col = 'B' -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") +if __name__ == "__main__": + b = board.Board() + i = 0 + col = 'B' + pattern = '[Yy]e?s?' + sp = False 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,sp)): - sp = False + winVal = b.win() + if(winVal == 'W'): + print("White wins!") + break + elif(winVal == 'B'): + print("Black wins!") break - else: - print("Your move was invalid, try again") + 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,sp)): + sp = False + break + else: + print("Your move was invalid, try again") diff --git a/src/cmd/queen.py b/src/cmd/queen.py @@ -1,19 +1,19 @@ # queen.py - QuantumChess # Author: Cody Lewis # Date: 24-FEB-2018 -# Mod.: 24-FEB-2018 import piece import functions +from functions import Direction class Queen(piece.Piece): - def __init__(self,superposNum,frstSuperPos,col,idT): + def __init__(self, superposNum, frstSuperPos, col, idT): idT = 'Q ' + str(idT) - piece.Piece.__init__(self,superposNum,frstSuperPos,col,idT) + piece.Piece.__init__(self, superposNum, frstSuperPos, col, idT) - def canMove(self,movement): + def canMove(self, movement): moveArr = functions.splitMovement(movement) direction = moveArr[0] - if(direction == 'd' or direction == 'u' or direction == 'r' or direction == 'l' or direction == 'f' or direction == 'q' or direction == 'h' or direction == 'g'): - for i in range(1,len(moveArr)): + if direction in [Direction.DOWN, Direction.UP, Direction.RIGHT, Direction.LEFT, Direction.DOWNRIGHT, Direction.DOWNLEFT, Direction.UPRIGHT, Direction.UPLEFT]: + for i in range(1, len(moveArr)): if(direction != moveArr[i]): return False return True diff --git a/src/cmd/rook.py b/src/cmd/rook.py @@ -1,20 +1,20 @@ # rook.py - QuantumChess # Author: Cody Lewis # Date: 24-FEB-2018 -# Mod.: 24-FEB-2018 import piece import functions +from functions import Direction class Rook(piece.Piece): - def __init__(self,superposNum,frstSuperPos,col,idT): + def __init__(self, superposNum, frstSuperPos, col, idT): idT = 'R ' + str(idT) - piece.Piece.__init__(self,superposNum,frstSuperPos,col,idT) + piece.Piece.__init__(self, superposNum, frstSuperPos, col, idT) - def canMove(self,movement): + def canMove(self, movement): moveArr = functions.splitMovement(movement) direction = moveArr[0] - if(direction == 'u' or direction == 'r' or direction == 'l' or direction == 'd'): - for i in range(1,len(moveArr)): - if(direction != moveArr[i]): + if direction in [Direction.UP, Direction.DOWN, Direction.LEFT, Direction.RIGHT]: + for i in range(1, len(moveArr)): + if direction != moveArr[i]: return False return True return False