Send your feedback or ask for help here!
I love the useful information in the settings menu!
In this replay, I was checkmated, but the game continued and I timed out. I believe that it thought I could castle, but you can not castle out of check. Coding Games and Programming Challenges to Code Better
Also according to lichess its checkmate Analysis board • lichess.org
Note : I’m using Python 3.
The time limit is 50ms. Though I use more like 35ms, for some reason there is some big overhead in this game :\
Why are the best players all using C or C++ ? Is it because these languages are faster and then you can calculate more moves in advance ? For example, with the program I’ve created with Python (and using object oriented structure), in 50 ms, I can only calculate something like 4 or 5 various positions… So in fact, it’s pretty the same as playing randomly ! …
I wrote about 3000 lines of code (with line breaks). There are more than 80 functions.
Here are the classes I’m using :
After all this hard work, I’m so frustrated to not even be able to beat the AI !
How can I optimize my code ?
Python 3 is an interpreted language. Putting it simple each line of code you write in python will be read and executed by the interpreter when it gets there during the runtime of your program. Therefore, there is not much possibility for optimizations. C/C++ in the other hand are translated and optimized into machine code before execution.
There are ways to achieve high performance with python. However, I would expect that your code written in C++ will always outperform the python code.
5 positions per turn? It seems low even for python, I would think python would be capable of at least 1-ply search. Have you checked out Board Representation - Chessprogramming wiki especially bitboards? Or maybe other approaches are more suitable for python.
Thank you for your explanation.
Thank you for your answer. What do you mean by “1-ply search” ? I’ve read some articles on bitboards, but I don’t really understand how to use them with a chess game. I understand that you will use a single line of 0s and 1s to represent the whole chessboard, instead of some objects like Square or ChessPiece as I did. But then, I don’t see how to find relations between each pieces on the board, which squares they control, where they can move, and so on…
By 1-ply search I mean making all possible moves from current position:
bestMove = (-9999, None) moves = getAllPossibleMoves() foreach move in moves: board.makeMove(move) score = board.getScore() # score can be: player's material - opponent's material board.undoMove() if score > bestMove: bestMove = score, move
bestMove contains move with highest score
Bitboards may be confusing, especially in chess, because there are many ways to represent them. Easiest representation is 2d array , with elements representing chess pieces. But dunno how performant would that be in python.
And python is possible to beat default AI, after all there are plenty python bots in the higher league.
Thank you again. I’m using a minimax algorithm with alpha-beta pruning to get the best move. I don’t know if my code will be faster with 2d array representation rather than square and piece objects. Well, I guess I have to rewrite my code completly.
I have a question about en passant, if i do a capture en passant, should the move be “e5c6” or should i specify something for en passant like for pawn promotion
You print like any other move, from-to squares.
I am using python, and it says there is a moves variable. But I don’t see it and cannot use it either does anyone know why?
For this game you need to request for the moves with print(“fen moves”) and then parse them yourself.
Full starter code with moves:
import sys import math # Auto-generated code below aims at helping you parse # the standard input according to the problem statement. constants_count = int(input()) for i in range(constants_count): name, value = input().split() # Write an action using print # To debug: print("Debug messages...", file=sys.stderr, flush=True) print("fen moves") # game loop while True: inputs = input().split() board = inputs color = inputs castling = inputs en_passant = inputs half_move_clock = int(inputs) full_move = int(inputs) movesCount = int(input()) moves =  for i in range(movesCount): moves.append(input()) # Write an action using print # To debug: print("Debug messages...", file=sys.stderr, flush=True) print(str(moves), file=sys.stderr, flush=True) print("random")