Code Monkey home page Code Monkey logo

chess.com-bot's Introduction

chess.com-bot's People

Contributors

abdallah1097 avatar erdemipek avatar jsun1590 avatar k9ur avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

chess.com-bot's Issues

check_fen() function returns error if chrome window isn't open

check_fen() function searches for download button, however, if the window isn't open for any reason (One of them is the script requires input from the user, so when the user switches to the terminal the chrome window is closed)

This even prevents the bot to start and operate at all! Which causes the bot to fail totally even though the script is running

The error is as follows:

$ python main.py
Press any key to continue...
Traceback (most recent call last):
  File "main.py", line 52, in <module>
    board = chess.Board(check_fen())
  File "main.py", line 37, in check_fen
    download.click()
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=87.0.4280.88)

Exception ignored in: <module 'threading' from 'C:\\ProgramData\\Anaconda3\\lib\\threading.py'>
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\threading.py", line 1388, in _shutdown
    lock.acquire()
KeyboardInterrupt:

Edit: This happens if you start the script and minimize the chrome tab, it'll not start playing at all

Implement pawn promotion handling

The bot currently cannot handle a pawn promotion move.

behaviours:

  • result.move returns a 5 character string for pawn promotions.
  • code can only handle 4 lettered strings from result.move

possible fix:

  • check if the returned string is 5 letters and if so, parse the last letter separately.

Inputs not validated

Inputs color, castle_w, castle_b, en_passant, half_move, and full_move are not check if valid in src/main.py.

Add command line options

Allows for setting arguments and options for Stockfish and/or Selenium at run time.

Can be implemented with a third party library such as argparse

[Need Code Review; see PR #10] Implement turn detection

The bot currently handles execution by playing a turn every 3 seconds. A turn detection feature can be implemented by comparing the FEN values every 3 seconds instead and play a turn if the new FEN does not match the previous FEN.

Additional ideas for turn detection are welcome!

Use piece locations to generate FEN

Currently, we are getting the FEN from chess.com directly via the PGN download button.

This is a very slow process (takes around 6s from opening to closing popup modal) as decreasing the delay time will cause possible input overlaps with selenium, leading to errors.

We could possibly replace this system by locating the element of each piece and their respective position on the board with selenium or another library (such as pyautogui), then generating our own FEN from that.

error when starting match

getting this error when starting a match

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".layout-board.board"}

giving error after running main.py

C:\Users\kapse\Downloads\chess\chess.com-bot-main\src>python main.py
Traceback (most recent call last):
File "C:\Users\kapse\Downloads\chess\chess.com-bot-main\src\main.py", line 5, in
from pywinauto import application
File "C:\Users\kapse\AppData\Roaming\Python\Python310\site-packages\pywinauto_init_.py", line 89, in
from . import findwindows
File "C:\Users\kapse\AppData\Roaming\Python\Python310\site-packages\pywinauto\findwindows.py", line 42, in
from . import controls
File "C:\Users\kapse\AppData\Roaming\Python\Python310\site-packages\pywinauto\controls_init_.py", line 36, in
from . import uiawrapper # register "uia" back-end (at the end of uiawrapper module)
File "C:\Users\kapse\AppData\Roaming\Python\Python310\site-packages\pywinauto\controls\uiawrapper.py", line 47, in
from ..uia_defines import IUIA
File "C:\Users\kapse\AppData\Roaming\Python\Python310\site-packages\pywinauto\uia_defines.py", line 181, in
pattern_ids = _build_pattern_ids_dic()
File "C:\Users\kapse\AppData\Roaming\Python\Python310\site-packages\pywinauto\uia_defines.py", line 169, in _build_pattern_ids_dic
if hasattr(IUIA().ui_automation_client, cls_name):
File "C:\Users\kapse\AppData\Roaming\Python\Python310\site-packages\pywinauto\uia_defines.py", line 50, in call
cls._instances[cls] = super(_Singleton, cls).call(*args, **kwargs)
File "C:\Users\kapse\AppData\Roaming\Python\Python310\site-packages\pywinauto\uia_defines.py", line 63, in init
self.ui_automation_client.CUIAutomation().IPersist_GetClassID(),
AttributeError: module 'comtypes.gen.UIAutomationClient' has no attribute 'CUIAutomation'

Screenshot (87)

cmd logs

Incorrect FEN gets printed after clicking a new computer game

Describe the bug
Well I am kind of a python expert(1 year experience) so I know how things work. When I start my second bot match, things don't work right. The bot starts moving random pieces to incorrect locations(the piece cannot technically move there) and as a result, the bot fails to continue. When I check what the problem was, I was surprised! It was not the bot, but the page html that caused the issue. Basically in the 2nd bot match, instead of (class=piece wp square-22), some pieces are (class=piece square-22 wp). This is just an example of what happens to some pieces. So when you take (in get_fen.py) piece_name = piece.get_attribute("class").split()[1], the first word is not wp anymore, it is square! Further, piece_name[1].upper() the second letter here is q (s>q<uare), that's right, the piece is read as a queen instead of a pawn. I have attached a screenshot of this. This change of the position of the word doesn't appear to affect the chess pieces in chess.com, it appears to be correct. Its just that get_fen is reading it wrong. I actually also found out a working fix for this.

Replace This:
piece_name = piece.get_attribute("class").split()[1] (LINE 13)
With This:
if piece.get_attribute("class").split()[1][0] != "s":
piece_name = piece.get_attribute("class").split()[1]
else:
piece_name = piece.get_attribute("class").split()[2]
fen += piece_name[1].upper() if piece_name[0] == "w" else piece_name[1].lower()

This also doesn't cause any new bugs

To Reproduce
Steps to reproduce the behavior:

  1. Go to Vs Computer in Chess.com
  2. Complete first bot game
  3. Play new game (without refreshing browser)
  4. See error

Expected behavior
Wrong FEN gets printed and bot fails

Screenshots
image

Desktop:

  • OS: Windows 10 Home Single Language
  • Browser Microsoft Edge
  • Version 91.0.864.48

When I go to write the command, pip3 install -r requirements.txt --user, It tells me ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'.

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Expand/develop README

Need help with formatting. Also welcoming additional information and content, such as better installation and startup instructions

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.