Skip to content

Instantly share code, notes, and snippets.

@jsettlem
Last active January 13, 2020 05:16
Show Gist options
  • Save jsettlem/ae11564ef9294b51f5cc7cbcbd35da58 to your computer and use it in GitHub Desktop.
Save jsettlem/ae11564ef9294b51f5cc7cbcbd35da58 to your computer and use it in GitHub Desktop.
it's like chess, but good
import random
import urllib.parse
from pprint import pprint
from typing import List, Iterator, Optional
import chess
import chess.svg
import chess.engine
def getEmptySquares(board: chess.Board) -> Iterator[chess.Square]:
return list(filter(lambda s: board.piece_at(s) is None, chess.SQUARES))
def getRandomEmptySquare(board: chess.Board) -> chess.Square:
return random.choice(getEmptySquares(board))
def findValidSquare(board: chess.Board, piece: chess.Piece) -> Optional[chess.Square]:
valid_squares = []
for square in getEmptySquares(board):
board_copy = board.copy()
board_copy.set_piece_at(square, piece)
if board_copy.is_valid():
valid_squares.append(square)
if valid_squares:
return random.choice(valid_squares)
else:
return None
def generateBoard() -> chess.Board:
pieces = [chess.QUEEN] + [chess.BISHOP] * 2 + [chess.KNIGHT] * 2 + [chess.ROOK] * 2 + [chess.PAWN] * 8
white_pieces = list(map(lambda p: chess.Piece(p, True), pieces))
black_pieces = list(map(lambda p: chess.Piece(p, False), pieces))
all_pieces = white_pieces + black_pieces
random.shuffle(all_pieces)
board = chess.Board(None)
board.set_piece_at(getRandomEmptySquare(board), chess.Piece(chess.KING, True))
black_king = chess.Piece(chess.KING, False)
board.set_piece_at(findValidSquare(board, black_king), black_king)
for i in range(len(all_pieces)):
square = findValidSquare(board, all_pieces[i])
if square:
board.set_piece_at(square, all_pieces[i])
if i > 25 and random.random() < 0.1:
break
return board
def getLichessUrl(board: chess.Board):
return f"https://lichess.org/analysis/{urllib.parse.quote(board.fen())}"
def main():
lowest_score = 99999
lowest_board = None
engine = chess.engine.SimpleEngine.popen_uci(
"D:/Downloads/stockfish-10-win/stockfish-10-win/Windows/stockfish_10_x64.exe")
engine.configure({"Threads": 7})
for i in range(100):
random_board = generateBoard()
if not random_board.is_game_over():
score = engine.analyse(random_board, chess.engine.Limit(time=1))['score'].white()
print("score:", score, "board:", getLichessUrl(random_board))
abs_score = abs(score.score(mate_score=100000))
if abs_score < lowest_score and abs_score != 0:
lowest_score = abs_score
lowest_board = random_board
print("new lowest!")
engine.quit()
print("lowest overall:", lowest_score)
print(getLichessUrl(lowest_board))
with open('out.svg', 'w') as o:
o.write(chess.svg.board(lowest_board))
if __name__ == '__main__':
main()
"V:\venvs\chess but good\Scripts\python.exe" "V:/Dropbox/PyCharm/chess but good/chessbutgood.py"
score: #-2 board: https://lichess.org/analysis/1r1kBn2/1b2RQr1/n6b/P1p1Pp2/pPPB3P/pp1PP2q/1Pp1Np1p/3NR2K%20w%20-%20-%200%201
new lowest!
score: +1228 board: https://lichess.org/analysis/4B2R/P2r2n1/1p1RpPPP/p1NnK1Q1/1Ppqppp1/pP1P3b/N4k1r/3b4%20w%20-%20-%200%201
new lowest!
score: #-3 board: https://lichess.org/analysis/1R4r1/1PP1Pp1B/1ppp4/1BP1p3/p3pkn1/1b1PNbr1/1PpP1KPR/1N2Q1nq%20w%20-%20-%200%201
score: +1484 board: https://lichess.org/analysis/3r4/p4nN1/1PBq1rp1/bp3Ppp/3bN1P1/RpQ1PpPp/1P1BPPk1/2KR4%20w%20-%20-%200%201
score: +1445 board: https://lichess.org/analysis/4r3/1ppPp1P1/4bpPn/1QPbR1PB/2BP1k1p/rN1pnpqp/KP2PNR1/8%20w%20-%20-%200%201
score: -915 board: https://lichess.org/analysis/3NB3/2qp2Pp/pBRp1bpP/P2PP1Nk/p1P1QP2/pK1n1b2/Pr3npr/4R3%20w%20-%20-%200%201
new lowest!
score: +68 board: https://lichess.org/analysis/1q1b3K/1Qr2pp1/1P2RB2/pb1RP1NP/1rP1p1n1/pn1PPpPp/BP1p4/4N1k1%20w%20-%20-%200%201
new lowest!
score: +1757 board: https://lichess.org/analysis/1K2rB1b/P2PPQ1p/pp2nPpP/1B2P2p/4P2b/R1rpN1RP/pp3Nq1/1k2n3%20w%20-%20-%200%201
score: #-2 board: https://lichess.org/analysis/2NQB3/r2pn2P/3PPP2/2p1Pk1p/Rr1nq1Np/1p2RPp1/1pK1PP1b/4b3%20w%20-%20-%200%201
score: #+8 board: https://lichess.org/analysis/1N6/qP1n4/1pPp1Ppb/PK1BP1np/N2k3r/1p1p1PpP/4b1R1/1BQ1R3%20w%20-%20-%200%201
score: #+7 board: https://lichess.org/analysis/4N3/PpP1RBn1/kB3pN1/n1ppppP1/1K6/2R2pPr/Pb1PPQp1/4q3%20w%20-%20-%200%201
score: -836 board: https://lichess.org/analysis/2Bb3b/1p2p1PR/n2rNP2/rPp1P1p1/np2ppRB/kPP1QKp1/Pq4P1/2N5%20w%20-%20-%200%201
score: #-3 board: https://lichess.org/analysis/1r5R/1B3PP1/Nk1pp1P1/pPq2p1b/R1nP2p1/pb2P1Pn/Qp1Pp3/1Nr2K1B%20w%20-%20-%200%201
score: -1220 board: https://lichess.org/analysis/8/2Pn3P/pPNRbPN1/PBppBPbQ/1rq1Pn2/2R2Kpp/krp2ppP/8%20w%20-%20-%200%201
score: +3032 board: https://lichess.org/analysis/2R5/1PP3rp/P1ppP3/1Rp1pK1N/4BbP1/Q1P1bnp1/1q1p1P1p/3k1B2%20w%20-%20-%200%201
score: #+2 board: https://lichess.org/analysis/1K2b1B1/PP1p4/P1PP2pn/pBqr1QN1/ppPR1b2/2RppP1N/n1r1kPp1/8%20w%20-%20-%200%201
score: +1060 board: https://lichess.org/analysis/5b1R/1P1rnQ2/2k1B1P1/P2nrp1K/1PP1p3/pNppR1qp/1PPPpN1p/3B2b1%20w%20-%20-%200%201
score: #+1 board: https://lichess.org/analysis/1r6/PP1PP1PB/Kp3Qb1/p1k1N2q/p1p2R2/1Pp1npBr/Nb1P1ppP/3R1n2%20w%20-%20-%200%201
score: #-4 board: https://lichess.org/analysis/8/1PRBPNrp/2QPn1kP/p2r4/pB2PnP1/3K1ppP/PpbNpqpb/3R4%20w%20-%20-%200%201
score: -129 board: https://lichess.org/analysis/3n4/pP2rpkp/P2p1b2/PPPRp1nP/N5p1/pNqp1b1P/1P2BrK1/2R2Q2%20w%20-%20-%200%201
score: #-8 board: https://lichess.org/analysis/4R3/Pn1pp1N1/PQPK3q/N3bp1p/1p3ppp/PPrb1P1k/3P2rP/1RBn2B1%20w%20-%20-%200%201
score: -717 board: https://lichess.org/analysis/2r3Q1/bP4P1/ppP1BP2/1RpPBpPR/2K2P2/pppN1k1q/pP2bnN1/1n5r%20w%20-%20-%200%201
score: +2094 board: https://lichess.org/analysis/2K2rB1/PBPpP1b1/bk1ppp1P/pPp2Q1p/nPr4P/6P1/1N1R1pRq/3N1n2%20w%20-%20-%200%201
score: +813 board: https://lichess.org/analysis/2Q3B1/1pqp1ppP/RKN5/1pPbP1b1/p4PPp/2npn1RP/1rNPP2k/1r5B%20w%20-%20-%200%201
score: #-5 board: https://lichess.org/analysis/2nNBR2/rp6/1P1bP3/1N1p1q1p/1PPp1rpP/p2K1n1B/PP2pp1R/3kb3%20w%20-%20-%200%201
score: +3351 board: https://lichess.org/analysis/3b3R/2PNP1bQ/2n1pp1P/1rpNqB2/1P2PPn1/1pKP1Pkp/1B3p2/2R5%20w%20-%20-%200%201
score: #+13 board: https://lichess.org/analysis/3k4/1RbPpPnP/2P1p3/1p1n2b1/ppP1Np1r/qp1N1pPB/PKR1QP1r/7B%20w%20-%20-%200%201
score: +1269 board: https://lichess.org/analysis/7B/2Ppkp1P/2pRqp1P/2P1K1pn/NrPbQ1RP/1Ppp2r1/n3P1Bp/1N5b%20w%20-%20-%200%201
score: #-2 board: https://lichess.org/analysis/4R2B/P3n2b/PrpP1PKP/P1BbPP2/n4pqk/rpp1Q1pp/1p5R/8%20w%20-%20-%200%201
score: #-3 board: https://lichess.org/analysis/2n3b1/pQ1p4/p1pPN2k/2p2R1b/PBPBnPpr/1P1PpP1P/1Rp1rNK1/5q2%20w%20-%20-%200%201
score: +2133 board: https://lichess.org/analysis/k7/1nP2B2/ppKp2pP/NPn1bpBP/3Rr1RP/pp1Pb1N1/2QP1P1p/5q2%20w%20-%20-%200%201
score: #+6 board: https://lichess.org/analysis/2Q5/1PR2Pkp/p1Kpp1P1/qB1bn2p/2p2P1P/p1PRB1P1/P1Np4/1r1brN2%20w%20-%20-%200%201
score: +397 board: https://lichess.org/analysis/1rbK4/Pp1p1P2/1B1PpP2/R1RN1r1q/n1PB1pQ1/Pp1P1p1n/1pp2bNP/1k6%20w%20-%20-%200%201
score: -1946 board: https://lichess.org/analysis/1Q1BK1q1/P1n1p2p/1P1pP3/p2b1RPP/3BnNbp/pN1PPkPr/3p2p1/1R2r3%20w%20-%20-%200%201
score: -3014 board: https://lichess.org/analysis/8/1P1K1r1N/2pPppb1/P2nPRP1/BPrp1pq1/P4pk1/N1p1P1np/2R1b1B1%20w%20-%20-%200%201
score: -2001 board: https://lichess.org/analysis/6r1/nP2BPpP/3pp1PP/QN1k1P2/pr2nR1p/pP2B3/p2bp1P1/2bKNRq1%20w%20-%20-%200%201
score: #-1 board: https://lichess.org/analysis/N1R1R2B/4PP1r/2PPpBb1/2bpn2p/kpnpPQpN/P3p1PK/1rP3p1/5q2%20w%20-%20-%200%201
score: +1949 board: https://lichess.org/analysis/8/1n1BNKpp/1p3P2/p1Q2ppP/1P2P1qn/1p2NPPp/2bRPP1r/kr6%20w%20-%20-%200%201
score: +2405 board: https://lichess.org/analysis/3Q4/pK2P2k/2P1pp2/P2Pp1nP/RrPP2q1/pbpNrR2/N2PBBp1/5n2%20w%20-%20-%200%201
score: #+3 board: https://lichess.org/analysis/Bbkr2b1/q1r1P2P/P3P1pK/n1pp4/1PpPNNpR/P1p1B1p1/5p1P/2QR1n2%20w%20-%20-%200%201
score: -1666 board: https://lichess.org/analysis/2q1B3/RnNR1B2/2p1pkbp/3P1r1K/Q3P1P1/ppnpbp1P/1P4P1/7N%20w%20-%20-%200%201
score: #-2 board: https://lichess.org/analysis/1RQ1q3/1P1PPk1B/1PPr1b2/1B1Pp1Pp/pp4p1/N1r3nR/pPpKb1p1/1n4N1%20w%20-%20-%200%201
score: #-3 board: https://lichess.org/analysis/3b2rN/2n1npk1/1PQ1Pb2/2pBp1PB/RP1PPpPP/RN1p2K1/p2rppq1/8%20w%20-%20-%200%201
score: #+8 board: https://lichess.org/analysis/4n3/B2R2P1/N1pp1pPp/Bk4KP/4pprP/PN4Qp/PP1pP1b1/1b1n1r2%20w%20-%20-%200%201
score: #+7 board: https://lichess.org/analysis/B4n2/RKP1kq1p/p1p2p1P/1N6/ppnP1p1P/3BP2P/rQ1P1NpP/6rR%20w%20-%20-%200%201
score: -826 board: https://lichess.org/analysis/3r4/2PPN3/bNPpb3/n1R1KpP1/1P1QppBP/P2p2q1/pppr2kP/5nB1%20w%20-%20-%200%201
score: #-3 board: https://lichess.org/analysis/r4N2/2b1rPPp/RRB2p1Q/1p1B1P2/1Pp1bPPP/1pKpk3/Pn2p3/6q1%20w%20-%20-%200%201
score: -630 board: https://lichess.org/analysis/bBn3NR/pPr1pPn1/2PK3B/3PpQNP/1bk2PqR/P1p3pp/1Pp1p3/8%20w%20-%20-%200%201
score: #+9 board: https://lichess.org/analysis/2B3n1/q1P1Nk1P/P2p4/1b1r3p/bn2PrP1/pQp1Bpp1/p2p1PPP/4R1K1%20w%20-%20-%200%201
score: #+2 board: https://lichess.org/analysis/Rb5K/P2pPP2/rp2nNP1/P3ppr1/3BQp2/PkP1ppPb/1Nnq2p1/1B3R2%20w%20-%20-%200%201
score: #+8 board: https://lichess.org/analysis/1k3bqn/p1pP1r1P/p2QBr1p/2PPPP2/2bPNpKN/n5P1/p1p3Rp/4B1R1%20w%20-%20-%200%201
score: +665 board: https://lichess.org/analysis/n7/1QqPp2B/p1b1ppnP/P1p1PP2/2kBp1P1/1R1NKpP1/b3p1RP/2r2rN1%20w%20-%20-%200%201
score: #+2 board: https://lichess.org/analysis/2N2kr1/1PPQ1Pn1/3pqpRp/1Bp1P2P/4RP1p/b1b1p2r/PpN1P1K1/6nB%20w%20-%20-%200%201
score: -1768 board: https://lichess.org/analysis/3b2B1/4NPpp/RrRp1rnQ/PPKp1Pb1/n1ppqPN1/1p1PP2P/1B2k2p/8%20w%20-%20-%200%201
score: #-1 board: https://lichess.org/analysis/2n5/pPP1rPBP/4qpp1/RQ2PrBp/N1KPb3/kbPp1R1N/4pppP/3n4%20w%20-%20-%200%201
score: +896 board: https://lichess.org/analysis/3q1Bn1/K2pQ2P/PppP4/1N1PPp1r/1rPpkp1b/2ppP1Pb/4B2n/1R5N%20w%20-%20-%200%201
score: #-14 board: https://lichess.org/analysis/2n1r1n1/q2rN1PN/1P1P1pb1/3Kb1pQ/1pPp1pRP/1Bk3P1/P1p1pp1P/3R3B%20w%20-%20-%200%201
score: #+1 board: https://lichess.org/analysis/Q3R3/1B3KpB/P1P2p1b/p1pp1b1P/1P1p2PP/rP1nNP2/n1pkq1Np/2R4r%20w%20-%20-%200%201
score: +1641 board: https://lichess.org/analysis/1n3q2/4PPKP/1P3Npr/p1Q1RB2/P2p1n1p/Pprp1Pb1/k1bBP2p/4N3%20w%20-%20-%200%201
score: +1249 board: https://lichess.org/analysis/5nr1/bppp4/1npP2q1/PPPB1PPb/Rp1Npk1P/2Q2r1p/K1P1p3/1R5N%20w%20-%20-%200%201
score: +1477 board: https://lichess.org/analysis/n2QR3/R2p2pP/KP1B1p2/1pppP3/P3pbr1/P2NrkPP/PN1n3p/2B2b1q%20w%20-%20-%200%201
score: +75 board: https://lichess.org/analysis/6N1/B1nQbpP1/RPPp1K2/2BpprPP/1n1N1pP1/1Pk1p3/pRP3p1/2b1rq2%20w%20-%20-%200%201
score: #-3 board: https://lichess.org/analysis/3Q4/1P2NP2/2p1pPRp/Pp3pnP/k4Bp1/PNr1Kb1b/2pp1PBq/2n1r3%20w%20-%20-%200%201
score: #-2 board: https://lichess.org/analysis/6r1/PP1n3N/nP1kBPpp/q2P3P/5bpp/b2pRP1B/pp1NP1pQ/2Kr2R1%20w%20-%20-%200%201
score: +1134 board: https://lichess.org/analysis/2n5/kpNnbPBK/2QPP1pP/3prPp1/Nq3B2/1rPp1b1P/1pPpR2p/4R3%20w%20-%20-%200%201
score: +1646 board: https://lichess.org/analysis/2b1KQ2/3PpB1R/PPpp1Nn1/1PbqpP2/1P1P1p2/k1p2rp1/2RnBPpN/7r%20w%20-%20-%200%201
score: #+1 board: https://lichess.org/analysis/4r2B/1K2p2k/p2pPP2/N2rP3/pPR4p/ppPn2Qb/p1RPP1NP/4nbBq%20w%20-%20-%200%201
score: -334 board: https://lichess.org/analysis/b7/nPrR2pP/NPbR2rK/1p1pNPnB/1p1pP2P/1p4p1/4PPkp/1B3q2%20w%20-%20-%200%201
score: +3073 board: https://lichess.org/analysis/B1n1Q2R/4b2P/1p2p1nP/Ppp1b3/p2PNPk1/2PpBr1p/1PNpRKP1/4r3%20w%20-%20-%200%201
score: 0 board: https://lichess.org/analysis/B5Q1/P1PR1ppn/1b1pK1Pk/BqrPp3/1p1b1p1P/pn2P3/p3r1N1/1R4N1%20w%20-%20-%200%201
score: +4379 board: https://lichess.org/analysis/3k4/PB3q1p/pR2p1p1/r2rNPpp/1PPR1p2/KBPP1P1n/1Q1pb2b/7N%20w%20-%20-%200%201
score: #-3 board: https://lichess.org/analysis/5k2/R2ppNpp/pN3Prn/3PP2P/PPbqRp1P/b2r1K2/3P2p1/2Q1n1B1%20w%20-%20-%200%201
score: +2594 board: https://lichess.org/analysis/5rn1/P2pPbR1/KRp4n/pPPbPpP1/p1B2kqP/3p2N1/pNB1pP2/3r1Q2%20w%20-%20-%200%201
score: +1570 board: https://lichess.org/analysis/1q6/pKB5/p2pN1QR/2nPPPp1/Nb2p1bp/Pp4r1/kp1PPPrP/1n1R3B%20w%20-%20-%200%201
score: +600 board: https://lichess.org/analysis/1n2KN2/2rpPP2/P3pRpp/N2Q4/bp3P1q/p2pP1Pp/1nPB1k1P/1r3B1R%20w%20-%20-%200%201
score: -306 board: https://lichess.org/analysis/3B4/P1P1RKn1/2PNrpPQ/3b1pb1/rpP1n1pp/R1Pp2p1/P1pP1q2/2NkB3%20w%20-%20-%200%201
score: -2638 board: https://lichess.org/analysis/2qr4/p1PpPBpn/bP1PnNp1/N1p2p1R/2Kb1PpP/1Q4Pr/3kPpB1/6R1%20w%20-%20-%200%201
score: -927 board: https://lichess.org/analysis/Qr2K3/PP1pp1nB/1qp2b2/P1RP2pp/2bP4/n1prP2P/1P1RpNpN/5Bk1%20w%20-%20-%200%201
score: -1258 board: https://lichess.org/analysis/BR3B2/1prp1bpn/p1PPp1nP/rP1P2Pq/2bN2k1/2PpP3/1RNp2p1/1K2Q3%20w%20-%20-%200%201
score: -835 board: https://lichess.org/analysis/b2bBRN1/P1pn2p1/Kp1P1N1P/B1rP1PP1/1p1p3p/2pRr3/QpP3nP/2k3q1%20w%20-%20-%200%201
score: +637 board: https://lichess.org/analysis/1n6/nNBppb2/r1PPKp2/1p3P2/1Q1PPb2/2PqpNB1/R1RP1pp1/5k2%20w%20-%20-%200%201
score: +550 board: https://lichess.org/analysis/3n2K1/p1pp1bN1/1k1B1pPp/pP1R2PP/1B1n2b1/1rPqPr2/3P1QP1/3N4%20w%20-%20-%200%201
score: +791 board: https://lichess.org/analysis/n5k1/p1PPn1r1/PpR3N1/pp1p1pB1/rpPP2bP/1bK2B2/PNQ2RP1/6q1%20w%20-%20-%200%201
score: #-5 board: https://lichess.org/analysis/1r1R4/1BPpP1Pn/prp1P1P1/Nn2bBpp/2pQ2P1/K1Pp1q1R/2p1Pb2/kN6%20w%20-%20-%200%201
score: +84 board: https://lichess.org/analysis/3Q4/1bp1PN1K/2pNR2n/1B1nbP1r/R1B1rPP1/ppqP2pp/1p1P1PPp/4k3%20w%20-%20-%200%201
score: +1342 board: https://lichess.org/analysis/4rRQ1/1bN1p1q1/1npP1ppP/1PB1r3/npp1PP1N/2kP1P2/PRbpB2p/7K%20w%20-%20-%200%201
score: #-2 board: https://lichess.org/analysis/5bR1/Pp1bPPPp/2RnNP2/r7/2p2Nk1/QBp2pP1/1PpPrKpB/3qn3%20w%20-%20-%200%201
score: #+6 board: https://lichess.org/analysis/5N2/PpPP4/Bp1pq1bP/2Ppr3/kpnrp3/p2p1b1R/KPP1P1Qn/1NB5%20w%20-%20-%200%201
score: +1378 board: https://lichess.org/analysis/1K4n1/RPpp1Nkp/Bp4Pr/PPP1b3/P1pqp1P1/1P1r2p1/1pRBn2Q/6b1%20w%20-%20-%200%201
score: #-2 board: https://lichess.org/analysis/4n1Q1/bPrPknR1/3NB3/P1PpB3/pNP1pPp1/p1PpK3/P1p1q1p1/3rb2R%20w%20-%20-%200%201
score: +1 board: https://lichess.org/analysis/2nR4/qp2rpP1/2p2pP1/pPP4K/b1NpPPQ1/2k1PNpP/2B1rnp1/5B1b%20w%20-%20-%200%201
new lowest!
score: +3524 board: https://lichess.org/analysis/B7/1PR1bpP1/p1Pq1R1n/2Pb1pPp/pNp1P1k1/1pn2rPr/1Bp1P3/3NQ1K1%20w%20-%20-%200%201
score: -1765 board: https://lichess.org/analysis/5q2/k4pnp/P3pPN1/1r1R1PbP/1KNpPnpQ/BPpPPR2/1p1p3r/2B1b3%20w%20-%20-%200%201
score: +1125 board: https://lichess.org/analysis/nBN1K3/1pQ1Pnqp/r1p3bP/Pk4p1/5NP1/pP1pPP1P/Rp1rp1R1/4B2b%20w%20-%20-%200%201
score: #+8 board: https://lichess.org/analysis/6b1/1pRN3P/1QP1p3/1rRp2PP/1P4Np/3BK2p/kBpPPpr1/1b5n%20w%20-%20-%200%201
lowest overall: 1
https://lichess.org/analysis/2nR4/qp2rpP1/2p2pP1/pPP4K/b1NpPPQ1/2k1PNpP/2B1rnp1/5B1b%20w%20-%20-%200%201
Process finished with exit code 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment