class TestStockfish(unittest.TestCase): def setUp(self): self.stockfish = Stockfish() def test_get_best_move(self): best_move = self.stockfish.get_best_move() self.assertIn(best_move, ('e2e4', 'g1f3',)) self.stockfish.set_position(['e2e4', 'e7e6']) best_move = self.stockfish.get_best_move() self.assertIn(best_move, ('d2d4', 'g1f3',)) # mate self.stockfish.set_position(['f2f3', 'e7e5', 'g2g4', 'd8h4']) self.assertFalse(self.stockfish.get_best_move()) def test_is_move_correct(self): self.assertFalse(self.stockfish.is_move_correct('e2e1')) self.assertTrue(self.stockfish.is_move_correct('a2a3')) self.stockfish.set_position(['e2e4', 'e7e6']) self.assertFalse(self.stockfish.is_move_correct('e2e1')) self.assertTrue(self.stockfish.is_move_correct('a2a3'))
""" Before running do pip install stockfish to get the stockfish python interface """ from stockfish import Stockfish stockfish = Stockfish() stockfish.set_position(['e2e4', 'e7e6']) stockfish.depth = 20 print(stockfish.get_best_move()) print(stockfish.is_move_correct('a2a3'))
from stockfish import Stockfish import chess board = chess.Board() stockfish = Stockfish() moves = [] print chess.SQUARES print chess.SQUARE_NAMES print board.piece_type_at(9) while True: moves.append(raw_input("Next Move:")) stockfish.set_position(moves) board.push(chess.Move.from_uci(moves[len(moves) - 1])) moves.append(stockfish.get_best_move()) stockfish.set_position(moves) board.push(chess.Move.from_uci(moves[len(moves) - 1])) print "computer made move:", moves[len(moves) - 1] print board
clientsMove = '' cv2.waitKey(4000) turnImg = screenshot(region=(1335, 665, 10, 16)) turnImg = cv2.cvtColor(np.array(turnImg), cv2.COLOR_RGB2BGR) while (checkTurn(turnImg) == False): turnImg = screenshot(region=(1335, 665, 10, 16)) turnImg = cv2.cvtColor(np.array(turnImg), cv2.COLOR_RGB2BGR) cv2.waitKey(1) cv2.waitKey(250) board = screenshot(region=(575, 164, 735, 735)) board = cv2.cvtColor(np.array(board), cv2.COLOR_RGB2BGR) opponentMove = getLastMove(board, playerColor == 'w') while opponentMove == '': board = screenshot(region=(575, 164, 735, 735)) board = cv2.cvtColor(np.array(board), cv2.COLOR_RGB2BGR) opponentMove = getLastMove(board, playerColor == 'w') print('Opponent move: ' + opponentMove[0] + opponentMove[1]) gameMoveSet.append(opponentMove[0] + opponentMove[1]) print(gameMoveSet) stockfish.set_position(gameMoveSet) nextBestMove = stockfish.get_best_move() print('Best next move: ' + nextBestMove) drawLiveOnBoard(nextBestMove, playerColor) pygame.display.update() startAsBlackFlag = False cv2.waitKey(500)
elif start_json['speed'] == 'blitz' and i == 1: stockfish.set_depth(15) stockfish.set_skill_level(25) elif start_json['speed'] == 'rapid' and i == 1: stockfish.set_depth(19) stockfish.set_skill_level(30) elif start_json['speed'] == 'classical' and i == 1: stockfish.set_depth(20) stockfish.set_skill_level(30) elif start_json['speed'] == 'correspondence' and i == 1: stockfish.set_depth(20) stockfish.set_skill_level(30) if white and i == 1: position.clear() stockfish.set_position() best_move = stockfish.get_best_move() requests.post( bot_move_url + best_move, params={}, headers={'Authorization': f'Bearer {api_key}'}) best_move = str(best_move) position.append(best_move) stockfish.set_position(position) if not white and i == 1: print('I am waiting to move.') if not white and i == 2: black_position.clear() current_move = start_json["moves"]