[Community Puzzle] Chess

https://www.codingame.com/multiplayer/bot-programming/chess

Send your feedback or ask for help here!

Created by @reCurse,validated by @jacek1,@field3 and @Illedan.
If you have any issues, feel free to ping them.

2 Likes

I love the useful information in the settings menu!

1 Like

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

2 Likes

Also according to lichess its checkmate Analysis board • lichess.org

Very frustrating to see the message “eliminated: Timeout” while having built a very fast (less than a half of second I think) and good chess program after one week of hard work ! … @reCurse, @jacek1

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 ! … :disappointed_relieved:

I wrote about 3000 lines of code (with line breaks). There are more than 80 functions.

Here are the classes I’m using :

  • Player
  • Situation
  • Move
  • Square
  • ChessPiece
  • King
  • Queen
  • Rook
  • Bishop
  • Knight
  • Pawn

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[0]:
  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 [8][8], 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[0]
    color = inputs[1]
    castling = inputs[2]
    en_passant = inputs[3]
    half_move_clock = int(inputs[4])
    full_move = int(inputs[5])
    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")
1 Like