ive made a surprisingly functional chess port over the course of the last 2 days
i tried doing this in turing like half a year ago but couldn't get check detection down
this time it actually worked
it comes with 4 modes of difficulty:
0 - random
1 - captures when possible
2 - makes trades
3 - avoids checkmate
according to an elo tester, level 3 is rated around 1530
i don't trust this number, the test comprised of 10 questions and i ranked 1615 despite having a chess.com elo of 612 (:
the difficulty, plus "many" more things, can be changed in the pause menu
update 1.1:
update 1.2:
update 1.3:
update 1.4:
update 1.5:
update 1.6:
update 1.7:
Very well done, an impressive feat of programming capability!
August 2022
To anyone looking to play this, please see lower down this thread where I have uploaded a version with a number of bugfixes.
The original (above) works, but is unintentionally a little random in some of its moves.
July 2020
I played a game and am really quite impressed. On the occasions I have begun to program it, I normally get distracted by other projects before I implement any AI for chess (with, at time of original post, one exception which had AI but not a strong game).
Unfortunately, I had a runtime error occur after the computer player (v1.4) got in a tight spot.
Click show for error details.
Similarly (v1.5), in the hope that posting these helps you to track them down:
Update:
20/7/2020 Everything seems fixed by version 1.7
I have played a few more games without any crashes. The computer gives a varied game, and is quite pleasant to play against (which is to say that unlike competition level chess computers it doesn't crush me).
Minor tactical spoilers:
I think it's the first time I've had an en passant situation against SWEEZEchess.
There is a minor bug in the rules logic; it won't occur often, but the logic should allow a player to capture en passant to get out of check.
The following position is white to move after black plays pawn d7-d5+. White should be able to play cxd6 en passant (if white wanted to), but the computer does not recognise it.
White: remcode
Black: SWEEZEChess 1.7
(The above position was after a deliberately unusual opening for variety, with 1. b4 Nf6 2. c4 allowing some interesting play later after ... a5; Qa4 pinning the black a-pawn to the rook and the black d-pawn to the king, and e3 to allow both bishops to move to the queenside to target the black kingside.)
Non-authoritative reference:https://en.wikipedia.org/wiki/En_passant#Unusual_examples
"In the diagram, if Black plays 1...g5+, ... Black overlooks that White can counter this check with the en passant capture 2. fxg6e.p.#"
SWEEZEchess 1.7/4
SWEEZEchess 1.7/4 is primarily a bugfix (by remcode) for MEINsweeze's SWEEZEchess 1.7. In 2020 an additional AI was added providing two new levels. In 2022 this has now been updated to be faster, and so the number of levels has been increased again.
Change to levels (and other settings) is accessed through the pause menu.
The AI have been added in such a way that they can all be easily backed out should MEINsweeze want the bugfixes but not the additional AI.
AI 4 through 8
General overview of AI 4 through 8
These AI opponents were written to play with a level somewhere between SWEEZEchess's existing AI and Krystman's CheckMate AI. There was neither an intention to provide an AI comparable to Krystman's, nor (when originally written) to provide an AI much stronger than SWEEZEchess level 3.
At level 4 the AI should provide a game of about the same level as MEINsweeze's AI 3, but might be a little less aggressive. Levels 5 and 6 should should hopefully still be suitable in speed and strength for a fairly casual game. All the AI from level 2 through to level 8 use the same worth tables so should have some similarity to their gameplay.
An estimate of thinking time per move at different levels is:
- level 4: 40 seconds
- level 5: 1 minute 30 seconds
- level 6: 3 minutes
- level 7: 6 minutes
- level 8: 20 minutes
As positions become more complex or harder to judge, this will of course increase. Some rudimentary feedback is given on screen about their thinking process.
AI 3 will play faster game than the other AI, so if you're looking for a quick casual game, MEINsweeze's AI 3 is still likely the more suitable choice. AIs 4 through 8 will play slightly better in some given situations, but are ultimately limited by the shallow depth of their search and thus by not following exchanges through to their conclusion.
Technical overview of AIs 4 through 8
The extra AI opponents share a minimax algorithm which uses alpha-beta pruning and movelist optimisation based on good moves already found at the same depth of search.
They use this algorithm with different settings for standard depth of search, extended depth of search in lines with captures and/or checks, randomness, and amount they pay attention to activity in the scoring.
More on those settings - the standard depth of search is either two or three half moves. The extended depth of search gives a maximum depth from three to six half moves. The randomness is a random value from zero upto (depending on the settings) either about a third the worth of a pawn or the worth of a pawn, and this is added to the rating of a position so that near-best lines may get chosen instead of those the AI would have rated best without the randomness. The amount the AI pay attention to activity in the scoring is minimal in all cases.
For anyone thinking of programming a chess AI, hopefully ai_four (which is what all the AI from level 4 through level 8 are) can give you an idea of possible program structure (although it is by no means definitive). Minimax and negamax and alpha-beta pruning are well worth researching for a chess AI.
The 2022 update to ai_four is intended to make it faster, primarily through movelist optimisation and also by changing how not quite best moves can be selected as best. With the extra speed comes extra depth, and with the extra depth of search comes extra strength.
ai_four was written with clarity in mind - so as to provide anyone looking at the code a greater chance of understanding how it arrives at its decision. This is mainly to align with pico-8 encouraging new coders to look at code and learn from it. I hope the 2022 update to the 2020 release of ai_four is marginally clearer than it was.
Removing the additional AI
@MEINsweeze ai_four and upwards can be easily removed from SWEEZEchess - simply remove the last two tabs (7 and 8), change the ai_level in _init, and make the appropriate adjustments to your own functions deeper (tab 6) and cycle_level (tab 0). The added minimax AI was carefully added in such a way that if you should want to remove it, it would be easily possible to do so; this will allow you to keep the bugfixes without having someone else's AI in your program.
Notes
Fix summary
Detail of fixes and improvements
wow! timely or not, i'm happy to see any interaction with this!
i'm astonished my code was legible enough to reverse engineer and fix!
i was planning on getting back to this at some point to fix a few bugs, as there are a few that're still extant (primarily one which, considering you seem to be a better chess player than i, i'm not surprised you haven't come across: if your rook gets taken before your king has moved, you can still castle with it; the castling will take place as if the piece that took the rook was the rook and it'll basically necromancy it back into existence. i know how to fix this, just to check if the rook is actually alive for whether it's legal, but it's pretty silly i missed that.)
regardless of the still living bugs, thanks for making the fix!
maybe this'll be the kick i need to start working on it again lmao
re: legible - yes ... it took a while to work out enough of how it worked to fix some of these (a few local variables, and returns from functions might have made it easier for me) :-)
I didn't mention it above but I think while I was trying things out to fix the problem with white playing to lose, I may have swapped the 1 and -1 in the calls to analyze(1) and analyze(-1) at the tops of tabs 1 and 6 around. This is purely accidental - I have to admit I'm not entirely sure what difference it makes, or if it makes a difference depending which colour pieces the AI is playing. This might be a change you'll need to playtest to make sure I haven't made the gameplay worse. [Edit: I am sure now I put them back the right way round, and it makes a considerable difference.] :)
Good luck with any future fixes you make to it!
thank you! i'll try to fix it, and i'm gonna be honest, i didn't know about local variables and returns at all when i made this! the language i'm used to programming in, turing, is very similar to lua except locals aren't a thing; when i learned lua, i never bothered to google how to do local variables since i was completely unaware that they were even a thing. as a result, i use an abundance of variables, and that gets tedious, so i just name neatly every variable whatever mouth sound i make, maybe plus an x or y if it's revelant lmao
as for returns, that's more of my bad habits; i use so many variables that my first instinct is to just save the result of a function as yet another global variable
EDIT: oh god, turing has locals too.. i just never learned about them. :(
[Please log in to post a comment]