def index(): global gamestate global moveNum board = [ [5, 3, 4, 1000, 9, 4, 3, 5], [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], [-5, -3, -4, -1000, -9, -4, -3, -5], ] ep = {-1: [], 1: []} canCastle = { 1000: { 'queen': True, 'king': True }, -1000: { 'queen': True, 'king': True } } hasCastled = {1000: False, -1000: False} gamestate = gs.gamestate(board, ep, canCastle, hasCastled) '''loads index page''' html = open('app/board.html', 'r').read() return html
def playGameFromString(self,string, winner, print_positions=False, lineByLine=False): global fi self.board = [[5,3,4,1000,9,4,3,5], [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], [-5,-3,-4,-1000,-9,-4,-3,-5],] color=-1 ep={-1:[], 1:[]} array=[] move=0 for m in string.split(' '): print(m) i=input('press enter:') g=gs.gamestate(board, ep ,[],[]) g.getPinnedSquares() g.pinPieces() g.getAllMoves() g.representBoard() color=color*-1 new_move=MO(m) board=g.moveToInstruction(color,new_move) print('actual winner:', winner) print('Predicted winner', fi.evaluate(board,move)) ep=g.enpassants ao=an(g) ao.analyze() arr=ao.produceStateArray() array.append(arr) g=gs.gamestate(board, {-1:[], 1:[]},[],[]) g.getPinnedSquares() g.pinPieces() g.getAllMoves() return array
def playGame(string, winner): global fi board = [ [5, 3, 4, 1000, 9, 4, 3, 5], [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], [-5, -3, -4, -1000, -9, -4, -3, -5], ] color = -1 ep = {-1: [], 1: []} array = [] move = 0 canCastle = { 1000: { 'queen': True, 'king': True }, -1000: { 'queen': True, 'king': True } } hasCastled = {1000: False, -1000: False} for m in string.split(' '): print(m) i = input('press enter:') print('canCastle at miner level: ', canCastle) print('making new game state') g = gs.gamestate(board, ep, canCastle, hasCastled) g.getPinnedSquares() g.pinPieces() g.getAllMoves() g.representBoard() print(g.castles) color = color * -1 new_move = MO(m) board = g.moveToInstruction(color, new_move) print('actual winner:', winner) print('Predicted winner', fi.evaluate(board, move)) ep = g.enpassants canCastle = g.canCastle hasCastled = g.hasCastled ao = an(g) ao.analyze() arr = ao.produceStateArray() array.append(arr) return array
import gs as gs board = [[5,3,4,1000,9,4,3,5], [1,1,1,1,1,1,1,1], [0,0,0,0,0,0,0,0], [-4,0,0,-9,0,0,0,0], [-1,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [-1,-1,-1,-1,-1,-1,-1,-1], [-5,-3,-4,-1000,-9,-4,-3,-5],] g=gs.gamestate(board, {-1:[], 1:[]},[],[]) g.getPinnedSquares() g.pinPieces() g.getAllMoves() g.representBoard() print(g.scoreMaterial()) print(g.collection) t=g.returnNewGameState()
def FENtoGS(FENstring, oldBoard, color, hasCastled, canCastle): new_board = [[0 for x in range(8)] for y in range(8)] print(new_board) keys = { 'r': -5, 'R': 5, 'k': -1000, 'K': 1000, 'q': -9, 'Q': 9, 'p': -1, 'P': 1, 'b': -4, 'B': 4, 'n': -3, 'N': 3 } arr = FENstring.split('/') print(arr) ep = {-1: [], 1: []} for x in range(7, -1, -1): count = 0 for y in range(0, len(arr[x])): if arr[x][y] in keys: new_board[7 - x][count] = keys[arr[x][y]] count += 1 else: count += int(arr[x][y]) #compare boards differences = [] for x in range(8): for y in range(8): if board[x][y] != new_board[x][y]: differences.append({ 'pos': [x, y], 'newValue': new_board[x][y], 'oldValue': board[x][y] }) if len(differences) == 2: for difference in differences: if difference['newValue'] == 0: d1 = difference else: d2 = difference if color == 1 and d2['newValue'] == 1: if d1['pos'][0] == 1 and d2['pos'][0] == 3: ep[1].append(d2['pos']) elif color == -1 and d2['newValue'] == -1: if d1['pos'][0] == 6 and d2['pos'][0] == 4: ep[-1].append(d2['pos']) elif abs(d2['newValue']) == 1000: canCastle[color]['queen'] = False canCastle[color]['king'] = False elif abs(d1['oldValue'] == 5): piceColor = int(d1['oldValue'] / abs(d1['oldValue'])) if d1['pos'] == [0, 0]: canCastle[pieceColor]['king'] = False elif d1['pos'] == [7, 7]: canCastle[pieceColor]['queen'] = False elif d2['pos'] == [7, 0]: canCastle[pieceColor]['king'] = False elif d1['pos'] == [0, 7]: canCastle[pieceColor]['queen'] = False #just check for enpassants if this is the case else: for difference in differences: if difference['pos'] == [0, 4]: hasCastled[1] = True elif difference['pos'] == [7, 4]: hasCastled[-1] = True return gs.gamestate(new_board, ep, canCastle, hasCastled)
[-1, -1, -1, -1, -1, -1, -1, -1], [-5, -3, -4, -1000, -9, -4, -3, -5], ] ep = {-1: [], 1: []} canCastle = { 1000: { 'queen': True, 'king': True }, -1000: { 'queen': True, 'king': True } } hasCastled = {1000: False, -1000: False} gamestate = gs.gamestate(board, ep, canCastle, hasCastled) from flask import request import json #idea is to provide a visual interface with chess.js @app.route('/') def index(): '''loads index page''' html = open('app/board.html', 'r').read() return html @app.route('/move') def move(): global gamestate
move=0 canCastle = {1000:{'queen':True, 'king':True}, -1000:{'queen':True, 'king':True}} hasCastled = {1000:False, -1000:False} board = [[5,3,4,1000,9,4,3,5], [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], [-5,-3,-4,-1000,-9,-4,-3,-5],] while True: moveNumber+=1 color=color*-1 g=gamestate(board, ep, canCastle, hasCastled) g.getPinnedSquares() g.pinPieces() g.getAllMoves() g.representBoard() print(g.castles) b=copy.copy(g.board[:]) print(b) node1 = rootNode(g, moveNumber, forestmodel) new_move = node1.search(color) print(new_move) origin=new_move['origin'] destination = new_move['destination'] b[destination[0]][destination[1]]=b[origin[0]][origin[1]] b[origin[0]][origin[1]]=0 print(b)
def playGame(string, winner, printStates=False, printStats=False, wait=False, printPredict=False): '''Plays through a game and returns an array of gamestate arrays. Optional parameters for printing information and making predictions''' global fi board = [ [5, 3, 4, 1000, 9, 4, 3, 5], [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], [-5, -3, -4, -1000, -9, -4, -3, -5], ] ep = {-1: [], 1: []} canCastle = { 1000: { 'queen': True, 'king': True }, -1000: { 'queen': True, 'king': True } } hasCastled = {1000: False, -1000: False} color = -1 ep = {-1: [], 1: []} array = [] move = 0 for m in string.split(' '): if wait: i = input('press enter:') g = gs.gamestate(board, ep, canCastle, hasCastled) g.getPinnedSquares() g.pinPieces() g.getAllMoves() if printStates: g.representBoard() color = color * -1 new_move = MO(m) board = g.moveToInstruction(color, new_move) #if printPredict: #print('actual winner:', winner) #print('Predicted winnger', fi.evaluate(board,move)) ep = g.enpassants canCastle = g.canCastle hasCastled = g.hasCastled ao = an(g) ao.analyze() arr = ao.produceStateArray() array.append(arr) g = gs.gamestate(board, {-1: [], 1: []}, g.canCastle, g.hasCastled) g.getPinnedSquares() g.pinPieces() g.getAllMoves() return array