quantum-chess

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

commit 6bd2afc762310d51344abdbe0dd10150610d78d0
parent 4fae94676c64856b86dfbe92e3f01fb9ca5a0e11
Author: Cody Lewis <luxdotsugi@gmail.com>
Date:   Thu, 12 Apr 2018 14:37:56 +1000

Pieces no longer teleport through each other

Diffstat:
Dboard.py | 203-------------------------------------------------------------------------------
Asrc/__pycache__/bishop.cpython-36.pyc | 0
Asrc/__pycache__/board.cpython-36.pyc | 0
Asrc/__pycache__/functions.cpython-36.pyc | 0
Asrc/__pycache__/king.cpython-36.pyc | 0
Asrc/__pycache__/knight.cpython-36.pyc | 0
Asrc/__pycache__/pawn.cpython-36.pyc | 0
Asrc/__pycache__/piece.cpython-36.pyc | 0
Asrc/__pycache__/queen.cpython-36.pyc | 0
Asrc/__pycache__/rook.cpython-36.pyc | 0
Rbishop.py -> src/bishop.py | 0
Asrc/bishop.pyc | 0
Asrc/board.py | 231+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/board.pyc | 0
Rfunctions.py -> src/functions.py | 0
Asrc/functions.pyc | 0
Rking.py -> src/king.py | 0
Asrc/king.pyc | 0
Rknight.py -> src/knight.py | 0
Asrc/knight.pyc | 0
Rpawn.py -> src/pawn.py | 0
Asrc/pawn.pyc | 0
Rpiece.py -> src/piece.py | 0
Asrc/piece.pyc | 0
Asrc/play.py | 19+++++++++++++++++++
Rqueen.py -> src/queen.py | 0
Asrc/queen.pyc | 0
Rrook.py -> src/rook.py | 0
Asrc/rook.pyc | 0
29 files changed, 250 insertions(+), 203 deletions(-)

diff --git a/board.py b/board.py @@ -1,203 +0,0 @@ -# board.py - QuantumChess -# Author: Cody Lewis -# Date: 26-FEB-2018 -# Mod.: 03-MAR-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 pawn -import rook -import bishop -import knight -import queen -import king -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 - if(i == 97 or i == 98): - colour = 'B' - 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 - index = str(chr(i)) + str(j) - if(i == 97 or i==97+self.rows): - if(j == 1 or j == 8): - self.playBoard[index] = rook.Rook(0,True,colour,j) - elif(j == 2 or j == 7): - self.playBoard[index] = knight.Knight(0,True,colour,j) - elif(j == 3 or j == 6): - self.playBoard[index] = bishop.Bishop(0,True,colour,j) - elif(j == 4): - 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) - else: - self.playBoard[index] = '0' - - def play(self,start,end,colour): # TODO: stop pieces from teleporting through eachother, modify the entanglement, add the superposition move - # check if inputs are valid - if(end != start and self.checkPoint(start) and self.checkPoint(end)): - if(self.playBoard[start] != '0'): - # check if colour matches - if(colour == self.playBoard[start].getId()[0:1]): - # 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) - # 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' - return True - else: - return False - # check for attack - else: # ends at another piece - 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): # maybe modify attack return values - self.playBoard[end] = self.playBoard[start] - self.playBoard[start] = '0' - if(supKill): - self.findAndDestroyAllId(end) - return True - return False - else: - return False - return False - - def checkPoint(self,p): - 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): - string = '' - if(dx > 0): # goes right - if(dy > 0): # goes up - if(dx == 2 and dy == 1): # L move - string = 'rq' - elif(dx == 1 and dy == 2): - string = 'uq' - elif(dx > dy): - diff = dx - dy - for i in range(dy): - string = string + 'q' - for i in range(diff): - string = string + 'r' - elif(dx < dy): - diff = dy - dx - for i in range(dx): - string = string + 'q' - for i in range(diff): - string = string + 'u' - else: - for i in range(dx): - string = string + 'q' - elif(dy == 0): - for i in range(dx): - string = string + 'r' - else: # goes down - if(dx == 2 and dy == -1): - string = 'rg' - elif(dx == 1 and dy == -2): - string = 'dg' - elif(dx > abs(dy)): - diff = dx - abs(dy) # dy is -ve - for i in range(abs(dy)): - string = string + 'g' - for i in range(diff): - string = string + 'r' - elif(dx < abs(dy)): - diff = abs(dy) - dx - for i in range(dx): - string = string + 'g' - for i in range(diff): - string = string + 'd' - else: - for i in range(dx): - string = string + 'g' - elif(dx == 0): - if(dy > 0): # up - for i in range(dy): - string = string + 'u' - else: # down (no change is an invalid input) - for i in range(abs(dy)): - string = string + 'd' - else: # goes left - if(dy > 0): # goes up - if(dx == -2 and dy == 1): # L move - string = 'lf' - elif(dx == -1 and dy == 2): - string = 'uf' - elif(abs(dx) > dy): - diff = abs(dx) - dy - for i in range(dy): - string = string + 'f' - for i in range(diff): - string = string + 'l' - elif(abs(dx) < dy): - diff = dy - abs(dx) - for i in range(abs(dx)): - string = string + 'f' - for i in range(diff): - string = string + 'u' - else: - for i in range(abs(dx)): - string = string + 'f' - elif(dy == 0): - for i in range(abs(dx)): - string = string + 'l' - else: # goes down - if(dx == -2 and dy == -1): - string = 'lh' - elif(dx == -1 and dy == -2): - string = 'dh' - elif(abs(dx) > abs(dy)): - diff = abs(dx) - abs(dy) # dy is -ve - for i in range(abs(dy)): - string = string + 'h' - for i in range(diff): - string = string + 'l' - elif(abs(dx) < abs(dy)): - diff = abs(dy) - abs(dx) - for i in range(abs(dx)): - string = string + 'h' - for i in range(diff): - string = string + 'd' - else: - for i in range(abs(dx)): - string = string + 'h' - return string - def findAndDestroyAllId(self,index): - # search board for all matching pieces and destroy them - return '' - - def toString(self): - string = '' - for i in range(97,99+self.rows): - if(i < 98+self.rows): - string = string + chr(i) - string = string + ' ' - for j in range(1,self.columns+1): - index = str(chr(i)) + str(j) - if(i == 98+self.rows): - string = string + ' ' + str(j) - elif(self.playBoard[index] == '0'): - string = string + '| ' - else: - idTag = self.playBoard[index].getId()[:3] - string = string + '|' + idTag - if(i < 98+self.rows): - string = string + '|\n' - return string diff --git a/src/__pycache__/bishop.cpython-36.pyc b/src/__pycache__/bishop.cpython-36.pyc Binary files differ. diff --git a/src/__pycache__/board.cpython-36.pyc b/src/__pycache__/board.cpython-36.pyc Binary files differ. diff --git a/src/__pycache__/functions.cpython-36.pyc b/src/__pycache__/functions.cpython-36.pyc Binary files differ. diff --git a/src/__pycache__/king.cpython-36.pyc b/src/__pycache__/king.cpython-36.pyc Binary files differ. diff --git a/src/__pycache__/knight.cpython-36.pyc b/src/__pycache__/knight.cpython-36.pyc Binary files differ. diff --git a/src/__pycache__/pawn.cpython-36.pyc b/src/__pycache__/pawn.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/__pycache__/queen.cpython-36.pyc b/src/__pycache__/queen.cpython-36.pyc Binary files differ. diff --git a/src/__pycache__/rook.cpython-36.pyc b/src/__pycache__/rook.cpython-36.pyc Binary files differ. diff --git a/bishop.py b/src/bishop.py diff --git a/src/bishop.pyc b/src/bishop.pyc Binary files differ. diff --git a/src/board.py b/src/board.py @@ -0,0 +1,231 @@ +# board.py - QuantumChess +# Author: Cody Lewis +# Date: 26-FEB-2018 +# Mod.: 03-MAR-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 pawn +import rook +import bishop +import knight +import queen +import king +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 + if(i == 97 or i == 98): + colour = 'B' + 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 + index = str(chr(i)) + str(j) + if(i == 97 or i==97+self.rows): + if(j == 1 or j == 8): + self.playBoard[index] = rook.Rook(0,True,colour,j) + elif(j == 2 or j == 7): + self.playBoard[index] = knight.Knight(0,True,colour,j) + elif(j == 3 or j == 6): + self.playBoard[index] = bishop.Bishop(0,True,colour,j) + elif(j == 4): + 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) + 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'): + i = chr(ord(i[0:1])+1) + str(int(i[1:])) + elif(move == 'r'): + i = i[0:1] + str(int(i[1:])-1) + elif(move == 'l'): + i = i[0:1] + str(int(i[1:])+1) + elif(move == 'q'): + i = chr(ord(i[0:1])-1) + str(int(i[1:])-1) + elif(move == 'g'): + i = chr(ord(i[0:1])+1) + str(int(i[1:])-1) + elif(move == 'h'): + 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): # TODO: modify the entanglement, add the superposition move + # check if inputs are valid + if(end != start and self.checkPoint(start) and self.checkPoint(end)): + if(self.playBoard[start] != '0'): + # check if colour matches + if(colour == self.playBoard[start].getId()[0:1]): + # 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) + i = start + i = self.addMovement(i,movement[0:1]) + if(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'): + return False + # 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' + return True + else: + return False + # check for attack + else: # ends at another piece + 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): + self.playBoard[end] = self.playBoard[start] + self.playBoard[start] = '0' + if(supKill): + self.findAndDestroyAllId(end) + return True + return False + else: + return False + return False + + def checkPoint(self,p): + 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): + string = '' + if(dx > 0): # goes right + if(dy > 0): # goes up + if(dx == 2 and dy == 1): # L move + string = 'rq' + elif(dx == 1 and dy == 2): + string = 'uq' + elif(dx > dy): + diff = dx - dy + for i in range(dy): + string = string + 'q' + for i in range(diff): + string = string + 'r' + elif(dx < dy): + diff = dy - dx + for i in range(dx): + string = string + 'q' + for i in range(diff): + string = string + 'u' + else: + for i in range(dx): + string = string + 'q' + elif(dy == 0): + for i in range(dx): + string = string + 'r' + else: # goes down + if(dx == 2 and dy == -1): + string = 'rg' + elif(dx == 1 and dy == -2): + string = 'dg' + elif(dx > abs(dy)): + diff = dx - abs(dy) # dy is -ve + for i in range(abs(dy)): + string = string + 'g' + for i in range(diff): + string = string + 'r' + elif(dx < abs(dy)): + diff = abs(dy) - dx + for i in range(dx): + string = string + 'g' + for i in range(diff): + string = string + 'd' + else: + for i in range(dx): + string = string + 'g' + elif(dx == 0): + if(dy > 0): # up + for i in range(dy): + string = string + 'u' + else: # down (no change is an invalid input) + for i in range(abs(dy)): + string = string + 'd' + else: # goes left + if(dy > 0): # goes up + if(dx == -2 and dy == 1): # L move + string = 'lf' + elif(dx == -1 and dy == 2): + string = 'uf' + elif(abs(dx) > dy): + diff = abs(dx) - dy + for i in range(dy): + string = string + 'f' + for i in range(diff): + string = string + 'l' + elif(abs(dx) < dy): + diff = dy - abs(dx) + for i in range(abs(dx)): + string = string + 'f' + for i in range(diff): + string = string + 'u' + else: + for i in range(abs(dx)): + string = string + 'f' + elif(dy == 0): + for i in range(abs(dx)): + string = string + 'l' + else: # goes down + if(dx == -2 and dy == -1): + string = 'lh' + elif(dx == -1 and dy == -2): + string = 'dh' + elif(abs(dx) > abs(dy)): + diff = abs(dx) - abs(dy) # dy is -ve + for i in range(abs(dy)): + string = string + 'h' + for i in range(diff): + string = string + 'l' + elif(abs(dx) < abs(dy)): + diff = abs(dy) - abs(dx) + for i in range(abs(dx)): + string = string + 'h' + for i in range(diff): + string = string + 'd' + else: + for i in range(abs(dx)): + string = string + 'h' + return string + def findAndDestroyAllId(self,index): + # search board for all matching pieces and destroy them + return '' + + def toString(self): + string = '' + for i in range(97,99+self.rows): + if(i < 98+self.rows): + string = string + chr(i) + string = string + ' ' + for j in range(1,self.columns+1): + index = str(chr(i)) + str(j) + if(i == 98+self.rows): + string = string + ' ' + str(j) + elif(self.playBoard[index] == '0'): + string = string + '| ' + else: + idTag = self.playBoard[index].getId()[:3] + string = string + '|' + idTag + if(i < 98+self.rows): + string = string + '|\n' + return string diff --git a/src/board.pyc b/src/board.pyc Binary files differ. diff --git a/functions.py b/src/functions.py diff --git a/src/functions.pyc b/src/functions.pyc Binary files differ. diff --git a/king.py b/src/king.py diff --git a/src/king.pyc b/src/king.pyc Binary files differ. diff --git a/knight.py b/src/knight.py diff --git a/src/knight.pyc b/src/knight.pyc Binary files differ. diff --git a/pawn.py b/src/pawn.py diff --git a/src/pawn.pyc b/src/pawn.pyc Binary files differ. diff --git a/piece.py b/src/piece.py diff --git a/src/piece.pyc b/src/piece.pyc Binary files differ. diff --git a/src/play.py b/src/play.py @@ -0,0 +1,19 @@ +import board +b = board.Board() +i = 0 +col = 'B' +while(i != 10): + print(b.toString()) + i+=1 + if(col == 'W'): + col = 'B' + else: + col = 'W' + while(True): + start = str(input("Choose your starting piece: ")) + end = str(input("Choose your end place: ")) + if(b.play(start,end,col)): + break + else: + print("Your move was invalid, try again") + diff --git a/queen.py b/src/queen.py diff --git a/src/queen.pyc b/src/queen.pyc Binary files differ. diff --git a/rook.py b/src/rook.py diff --git a/src/rook.pyc b/src/rook.pyc Binary files differ.