- ๐ Hi, Iโm @jsun1590
- ๐ซ Reach me at [email protected]
jsun1590 / chess.com-bot Goto Github PK
View Code? Open in Web Editor NEWSimple Chess.com bot written in Python.
License: MIT License
Simple Chess.com bot written in Python.
License: MIT License
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
Cheating is illegal
The bot currently cannot handle a pawn promotion move.
behaviours:
result.move
returns a 5 character string for pawn promotions.result.move
possible fix:
Inputs color
, castle_w
, castle_b
, en_passant
, half_move
, and full_move
are not check if valid in src/main.py
.
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
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!
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.
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"}
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'
cmd logs
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:
Expected behavior
Wrong FEN gets printed and bot fails
Desktop:
Describe the bug
A clear and concise description of what the bug is.
To Reproduce
Steps to reproduce the behavior:
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):
Smartphone (please complete the following information):
Additional context
Add any other context about the problem here.
Need help with formatting. Also welcoming additional information and content, such as better installation and startup instructions
why it doesn't work with real players?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.