Repository for the diana chess competition. AI Lecture 21/22

Overview

Notes for Assignment 8 (Chess AI)

  • We recommend using an IDE (like Pycharm) for working on this assignment.

  • IMPORTANT: Please make sure you use python>=3.7 for this assignment.

  • First, you need to install python pygame package by executing the following command in terminal:

    • pip install pygame

    If you are using Pycharm, you can also add the package from: File-> Settings-> Project:dianachess-> Project Interpreter. Click the '+' sign to add new packages. Search pygame and install the package.

  • The game gui can be started by running the script 'ChessMain.py' the following way:

    • python ChessMain.py --agent1 [*] --agent2 [*] --verbose --time_control [how many seconds either player gets per move] --use_gui

    For [*] you can put either 'MrRandom', 'Agent1', 'Agent2', 'Human', or a path to your agent file. 'MrRandom' will play completely random moves (valid moves), 'Agent1' will use the class Agent from 'student_agents/template.py' while 'Agent2' will use the class Agent from 'student_agents/template2.py'. By entering 'Human' you act as the agent and can play yourself (if the gui is activated). WINDOWS USERS: Here you need to use 'Agent', it does not work to use the file path.

  • By choosing in between 1 and 2 in 'Settings.json' you can choose whichever board you like.

  • Please note that for the evaluation, --time_control=20 will be used (pending further tests, a higher value may instead be used to account for the server running the games being slightly slower, however your agent should be prepared for being given less time than expected by registering preliminary moves)

  • If your agent is still running after the time limit has passed, your agent will lose unless you have registered a preliminary move with update_move. See the template student_agents/template.py for details.

  • WHEN YOU WANT TO RUN ChessMain.py with '--agentx Agent1' you need to keep the file name 'template.py'.

  • FOR THE SUBMISSION you must rename the file studentagent.py to the last name of both team members (e.g. rahim_schroeder.py} for Rahim and Schröder). Keep the class name as 'Agent'. Your agent's functionality should be similar to the class MrRandom in 'agents/random.py'. You are not allowed to modify any other file in the program, so please make sure your agent works with the base version of the game as distributed. Please do not split your implementation across multiple files.

    Important: please include some documentation for your agent as a separate document. Document which algorithm you are using, what the idea behind your heuristic/evaluation function is, etc.

  • If you find any inefficiency in the code, e.g in ChessMain.py, ChessEngine.py, (or worse, bugs) and you can suggest any improvement, please send an email to [email protected].

  • You are allowed to use any basic package in python that helps in your implementation. Basic includes anything included in python3.7, numpy, and what might be discussed in the forum.

  • Your agent should be single-threaded. A multi-threaded agent will not get any marks for the assignment and will be disqualified from the tournament.

  • One agent is included in this framework to allow you to test your agent:

    • MrRandom: A very primitive agent that selects its moves randomly from the list of legal moves. Basically any agent should be able to beat this.
Comments
  • Own night takes own rook [getValidMoves returns invalid move]

    Own night takes own rook [getValidMoves returns invalid move]

    Description

    I run my agent and suddenly I saw this: image

    On the right bottom corner was my rook and now my own knight is there. That alone is defently not a valid move. Furthermore you can see the last position of my knight as red dot. So there is also an unvalid move from there as knight.

    Maybe there are some bugs in the Castle Right moves...

    You can test that behavior with that code:

    Code

            gs = GameState()
    
            gs.board = ['bR', 'bB', 'bK', 'bN', '--', 'bR',
                        'bp', 'bp', '--', '--', '--', 'bp',
                        '--', '--', '--', 'bp', '--', 'wp',
                        '--', '--', '--', '--', '--', '--',
                        'bB', 'wp', 'wp', 'wp', '--', '--',
                        'wR', 'wB', 'wK', 'wN', '--', 'wR']
    
            gs.whiteToMove = True
    
            moves = gs.getValidMoves()
    
    opened by coolusaHD 6
  • BUG: getValidMoves generates IndexError

    BUG: getValidMoves generates IndexError

    Description

    Sometimes the function getKingsideCastleMoves get called with index=35 and with

    getKingsideCastleMoves in ChessEngine:732 if self.board[rc + 1] == "--":

    it ends in an IndexError

    Error Output:

      File "C:\Projekte\chess_ai\student_agents\template.py", line 313, in alphaBetaMin
        validMoves = gs.getValidMoves()
      File "C:\Projekte\chess_ai\ChessEngine.py", line 306, in getValidMoves
        self.getCastleMoves(kingRow, kingCol, moves)
      File "C:\Projekte\chess_ai\ChessEngine.py", line 708, in getCastleMoves
        self.getKingsideCastleMoves(r, c, moves)
      File "C:\Projekte\chess_ai\ChessEngine.py", line 732, in getKingsideCastleMoves
        if self.board[rc + 1] == "--":
    IndexError: list index out of range
    
    opened by coolusaHD 4
  • Move with no figure is possible

    Move with no figure is possible

    Description

    Sometimes when I run my agent, I got an error that on after checking if the move is a capture the moving figure doesnt match the figure names. After further investiagation I found out that when my Quiesce function do recursivly moves without checking if the game is over, you can create a move with '--' as playing figure.

    Code Changes:

    With the addition in ChessEngine.py:800 if(self.pieceMoved == "--"): raise ValueError('moving a piece that is not on the board')

    Error Output

    I got the following console output:

    Process Process-1:
    Traceback (most recent call last):
      File "C:\Python39\lib\multiprocessing\process.py", line 315, in _bootstrap
        self.run()
      File "C:\Python39\lib\multiprocessing\process.py", line 108, in run
        self._target(*self._args, **self._kwargs)
      File "C:\Projekte\chess_ai\student_agents\template.py", line 107, in findBestMove
        score = self.alphaBeta(copyGS, self.currentDepth, -float('inf'), float('inf'), True)
      File "C:\Projekte\chess_ai\student_agents\template.py", line 284, in alphaBeta
        score = self.alphaBeta(copyGS, depth - 1, alpha, beta, False)
      File "C:\Projekte\chess_ai\student_agents\template.py", line 319, in alphaBeta
        score = self.alphaBeta(copyGS, depth - 1, alpha, beta, True)
      File "C:\Projekte\chess_ai\student_agents\template.py", line 264, in alphaBeta
        return self.Quiesce(gs,alpha,beta,depth)
      File "C:\Projekte\chess_ai\student_agents\template.py", line 388, in Quiesce
        score = -self.Quiesce(gs, -beta, -alpha ,depth)
      File "C:\Projekte\chess_ai\student_agents\template.py", line 388, in Quiesce
        score = -self.Quiesce(gs, -beta, -alpha ,depth)
      File "C:\Projekte\chess_ai\student_agents\template.py", line 388, in Quiesce
        score = -self.Quiesce(gs, -beta, -alpha ,depth)
      [Previous line repeated 1 more time]
      File "C:\Projekte\chess_ai\student_agents\template.py", line 384, in Quiesce
        validMoves = gs.getValidMoves()
      File "C:\Projekte\chess_ai\ChessEngine.py", line 300, in getValidMoves
        self.getKingMoves(kingRow, kingCol, moves)
      File "C:\Projekte\chess_ai\ChessEngine.py", line 688, in getKingMoves
        moves.append(Move((r, c), (endRow, endCol), self.board))
      File "C:\Projekte\chess_ai\ChessEngine.py", line 800, in __init__
        raise ValueError('moving a piece that is not on the board')
    ValueError: moving a piece that is not on the board
    

    There you can see that when you execute gs.getValidMoves() on a Board without a king it returns a move where '--' is the moving Figure

    opened by coolusaHD 4
  • Get KeyError on undoMove()

    Get KeyError on undoMove()

    Description

    it dont happen very often but when it happen I cannot found any problem in my code. I'm using undo move like this in my code

    gs.makeMove(move)
    score = RekursiveFunktion(x,y)
    gs.undoMove()
    

    Error Output

    Process Process-6:
    Traceback (most recent call last):
      File "C:\Projekte\chess_ai\ChessEngine.py", line 177, in undoMove
        self.game_log[tuple(self.board)] -= 1
    KeyError: ('--', 'bR', 'bN', 'bK', 'bB', 'bR', '--', 'bp', 'bp', '--', 'bp', 'bp', '--', 'bB', '--', 'bp', '--', '--', '--', '--', '--', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', '--', '--', 'wR', 'wR', 'wB', 'wN', 'wK', '--', 'wR')
    
    opened by coolusaHD 2
  • No Checkmate detection on pawn promotion

    No Checkmate detection on pawn promotion

    Checkmates are not detected if they happen within a pawn promotion move.

    Situation:

    Board:

    -- bK -- -- -- --
    -- -- -- wR wp --
    -- -- -- -- -- --
    -- -- -- -- -- --
    -- -- -- -- -- --
    -- -- wK -- -- --
    

    White's turn. Valid moves: ['Ra5', 'Rd2', 'Rc5', 'Kc2', 'e6', 'Rd1', 'Kd2', 'Rb5', 'Rd4', 'Kb1', 'Kd1', 'Rd3', 'Kb2', 'Rd6'] White makes move: 'e6'

    Board:

    -- bK -- -- wR --
    -- -- -- wR -- --
    -- -- -- -- -- --
    -- -- -- -- -- --
    -- -- -- -- -- --
    -- -- wK -- -- --
    

    Expected Behaviour:

    Pawn gets promoted to Rook. Black King is in Checkmate (gameState.checkMate evaluates to True)

    Actual Behaviour:

    Pawn gets promoted to Rook. Black King is not in Checkmate (gameState.checkMate evaluates to False)

    opened by Skyfighter64 6
Owner
Cognitive Systems Research Group
Autonomous Mobile Robots; Bioinformatics; Chemo- and Geoinformatics; Evolutionary Algorithms; Machine Learning
Cognitive Systems Research Group
Chesston (Chess+Python) is a two-player chess game with graphical user interface written in PyQt5

♟️ Chesston (Chess+Python) is a two-player chess game with graphical user interface written in PyQt5. ?? Dependencies This program uses Py

null 6 May 26, 2022
Chess GUI

Lucas Chess Lucas Chess is a GUI of chess: To learn to play chess. To play chess against engines. Dependencies Python 2.7 PyQt4 PyAudio psutil Python

Lucas 322 Dec 20, 2022
PyChess - a chess client for Linux/Windows

PyChess - a free chess client for Linux/Windows The mission of PyChess is to create a free, pleasant, PyGObject based chess game for the Linux desktop

null 559 Dec 28, 2022
A base chess engine that makes moves on an instance of board.

A base chess engine that makes moves on an instance of board.

null 0 Feb 11, 2022
This a Chess PGN saver which allows you to save your game pgns, in a .pgn file

PGN Saver This a Chess PGN saver which allows you to save your game pgns, in a .pgn file This can be a very useful tool for the people using chessbase

null 3 Jan 6, 2022
In the works, creating a new Chess Board and way to Play...

sWJz4KingsChess date started on github.com 11-13-2021 In the works, creating a new Chess Board and way to Play... starting to write this in Pygame, an

Shawn 2 Nov 18, 2021
Running Chess Night results tabulation

Running Chess Night results tabulation

Mitch LeBlanc 2 Nov 20, 2021
A playable version of Chess – classic two-player, various AI levels, and the crazyhouse variant! Written in Python 3

A playable version of Chess – classic two-player, various AI levels, and the crazyhouse variant! Written in Python 3. Requires the installation of PIL/Pillow and Requests

null 1 Dec 24, 2021
SuperChess is a GUI application for playing chess.

About SuperChess is a GUI application for playing chess. It is written in Python 3.10 programming language, uses PySide6 GUI library, python-chess lib

Boštjan Mejak 1 Oct 16, 2022
Chess - A python gui application

Chess Python version 3.10 or greater is required to play. Note This is a gui application, and as such will not run inside WSL.

Jonxslays 1 Dec 16, 2021
Visualizing and learning from games on chess.com

Better Your Chess What for? This project aims to help you learn from all the chess games you've played online, starting with a simple way to download

Luc d'Hauthuille 0 Apr 17, 2022
j-chess implementation in python

j-chess-client-python This repository aims to be a starting point for implementing a chess ai for the j-chess-server in python. To start, you can copy

Jonas 1 Dec 25, 2021
A didactic GUI chess game made in Python3 using pygame.

Chess A didactic GUI chess game made in Python3 using pygame. At the moment, there is no AI. The only way you can test the game is by playing against

Leonardo Delfino 1 Dec 22, 2021
Python code that gives the fastest path from point a to point b of a chess horse

PERSONAL-PROJECTS CARLOS MAGALLANES-ARANDA'S PERSONAL PROJECTS kchess.py is the code. its input is the start and the end. EXMPLE - a1 d5 its output is

Carlos Magallanes-Aranda 1 Dec 26, 2021
A Neural Network based chess engine and GUI made with Python and Tensorflow/Keras.

Haxaw-Chess Haxaw: Haxaw is the Neural Network based chess engine made with Python and Tensorflow/Keras. Also uses the python-chess library. (WIP: Imp

Sarthak Bharadwaj 8 Dec 10, 2022
Chess turnament organizer (short construct concept)

Turnament Organizer Chess turnament organizer (short construct concept). It is my hobby app I want to write to support lightweight tool for smart roun

kkuba91 3 Dec 16, 2022
Chess Game using Python

Chess Game is a single-player game where the objective is same as the original chess game. You just need to place your chess piece in a correct position. The purpose of the system is to provide some past time with your friends.

Yogesh Selvarajan 1 Aug 15, 2022
A chess engine with basic AI capabilities (search for best move using MinMax algorithm)

A chess engine with basic AI capabilities (search for best move using MinMax algorithm)

Ken Wu 1 Feb 2, 2022
Unknown Horizons official code repository

Unknown-Horizons based on Fifengine is no longer in development. We are porting it to Godot Engine. Please dont report any new bugs. Only bugfixes wil

Unknown Horizons 1.3k Dec 30, 2022