def __init__(self): self.stateSave = ChessState(Common.initChessFEN) self.stateCurr = ChessState(Common.initChessFEN) self.moves = [] self.lastOcc = [ \ 1, 1, 1, 1, 1, 1, 1, 1, \ 1, 1, 1, 1, 1, 1, 1, 1, \ 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, 0, \ 1, 1, 1, 1, 1, 1, 1, 1, \ 1, 1, 1, 1, 1, 1, 1, 1]
def __init__(self): self.initState = ChessState(Common.initChessFEN) self.moves = [] self.tags = {} self.comments = [] self.states = [self.initState] self.result = None
def __init__(self, parent, squareWidth=64, squareHeight=64): self.player = 'w' self.moveNum = 1 Tkinter.Frame.__init__(self, parent) self.parent = parent self.chessState = ChessState(Common.initChessFEN) self.cb = ChessBoard(self) self.cb.setState(self.chessState) self.cb.refreshCanvasFromState() self.cb.pack() self.b = Tkinter.Button(self, text="flip", command=self.flipIt) self.b.pack() self.b2 = Tkinter.Button(self, text="clear", command=self.clearIt) self.b2.pack() #self.b = Tkinter.Button(self, text="html", command=self.html) #self.b.pack() self.moveEntry = Tkinter.Entry(self) self.moveEntry.pack() self.execMove = Tkinter.Button(self, text="execute move", command=self.executeMove) self.execMove.pack()
def parsePgn(self, text): tokens = PgnTokenizer.tokenize(text) currMoveNum = 0 player = 'W' while tokens: token = tokens.pop(0) #print "on token: -%s-" % token # tag tokens eg: [Event "May 2013 Tourney"] m = re.match(r'\[(.*?) "(.*?)"\]', token) if m: self.tags[m.group(1)] = m.group(2) continue # comment tokens eg: { good move! also consider Rxe8 } m = re.match('^{(.*)}$', token) if m: # if we're in the moves section, comment applies to a move if self.moves: self.moves[-1].addComment(m.group(1)) # else it applies to the match comments else: self.comments.append(m.group(1)) continue # result tokens eg: 0-1 m = re.match(Common.regexResults, token) if m: self.result = token if tokens: raise Exception("result token was not the final token! next is: " + tokens[0]) continue # move number token eg: 34. m = re.match(r'(\d+)\.', token) if m: if currMoveNum + 1 != int(m.group(1)): raise Exception("out of order move number: " + token) player = 'w' currMoveNum += 1 # normal move (SAN) m = re.match(Common.regexSanChess, token) if m: move = ChessMove.ChessMove() move.moveNum = currMoveNum move.player = player move.san = token self.moves.append(move) player = {'w':'b', 'b':'w'}[player] # calculate all board states # # initial state? or special state? (Fischer960, etc.) if 'SetUp' in self.tags and self.tags['SetUp'] == '1': if 'FEN' in self.tags: self.initState = ChessState(self.tags['FEN']) self.states = [self.initState] # loop over all moves... for move in self.moves: # exceptions (repeated moves due to time forfeiture, etc.) just carry state along... if 'TIME_FORFEIT' in move.flags: self.states.append(self.states[-1]) continue currState = self.states[-1] nextState = currState.transition(move) self.states.append(nextState)
def setFEN(self, fen): self.chessState = ChessState(fen) self.refreshCanvasFromState()
color = ["black", "white"] print("This is a text-based chess interface, where the computer will play against the user or itself.") #Prompts user for game type type_of_game = None while True: type_of_game = input("In order of white versus black, where c means computer and h means human, type cvc, hvc, or cvh: ") if type_of_game in ["cvc", "cvh", "hvc"]: break #Prompts user for starting position while True: fen = input("Type 'n' for the standard starting position or a specific FEN: ") if fen is 'n': board = ChessState() break else: try: board = ChessState(fen=fen) break except ValueError: print("Invalid FEN") #Plays calculated best move in given position def play_engine_move(): engine_move = board.san(player.best_move(board, board.turn)) print("Computer move for " + color[board.turn] + " is " + engine_move) board.push_san(engine_move) #Prompts and plays user's move
def __init__(self, initState, movesToMate): self.headState = ChessState(initState) self.depth = movesToMate + 1 self.lowestRow = [] self.lowestRow.append(self.headState) self.nodesExplored = 0
def __init__(self, chessBoard): self._boardWidth = 8 self._boardHeight = 8 self._state = ChessState(chessBoard)