Discuss Scratch

ArnoHu
Scratcher
1000+ posts

Scratch Chess Engine - Game of Kings

The original idea behind this project was to build a simple chess game for a local kids programming club, and to create a tutorial about chess programming. The Scratch community then picked it up and provided additional ideas and feature requests, so the project kept on expanding in scope.

Today Chess - Game of Kings has six difficulty levels, and displays the progress of the current move calculation. Its move evaluation is based on board material, piece position, mobility and special endgame handling. It allows to take back moves, record games in algebraic notation, and to export or import boards. Of course the program supports all common chess rules, including pawn promotion, castling and en-passant, and has a small opening book included. Game experience / usability was another particular focus point.

Internally it uses a minimax / alpha-beta-pruning approach (up to ply 4).

Maybe you want to give it a try at https://scratch.mit.edu/projects/148769358/. Feedback is very welcome, e.g. via project comments. Thank you!



Last edited by ArnoHu (Nov. 10, 2023 05:28:47)

Samueldora
Scratcher
100+ posts

Scratch Chess Engine - Game of Kings

This project really deserves your attention. There are very few real chess engines on Scratch, and ArnoHu's is a new and good one! I tested it a few times in the last weeks and saw it improve day by day.

I am a former club player and programmed a chess engine myself, and I recommend ArnoHu's Chess project to you: Give it a try!

Last edited by Samueldora (April 10, 2017 11:09:04)

Dojo2000
Scratcher
6 posts

Scratch Chess Engine - Game of Kings

Wouldn't it be cool to have like a competition between the best Scratch chess programs?

Only thing, I think most of them play the black side only. That might be a problem.

if <(mood) = [sad]> then
set (mood) to [happy]
end
Samueldora
Scratcher
100+ posts

Scratch Chess Engine - Game of Kings

Archimedes496 proposed such a tournament in his project Chess AI Contest and defined rules, e.g. for time control. With the new projects published recently this could be really interesting.
plumpudin
Scratcher
30 posts

Scratch Chess Engine - Game of Kings

Hey ArnoHu,

It's a nice project you have made.

Some suggestions tho:
- The IA oppening miss peps… for 4-5 moves, IA does the SAME thing as me.
- When you construct your chessmate plan, IA seems doing weird and does non-sense moves (like moving the corner pawn, then the pawn next to it… which makes the chessmate even easier.

Good luck with your work (y)

If you like mnemonics devices, feel free to go there
ArnoHu
Scratcher
1000+ posts

Scratch Chess Engine - Game of Kings

Samueldora, plumpudin, thanks a lot for the endorsement and feedback!

Yes, a tournament sounds like a nice idea. I think I could provide a change of sides for the human player with reasonable effort.
plumpudin
Scratcher
30 posts

Scratch Chess Engine - Game of Kings

My pleasure !

Btw, I was wondering if your game was also considering the En passant move ?

If you like mnemonics devices, feel free to go there
ArnoHu
Scratcher
1000+ posts

Scratch Chess Engine - Game of Kings

Yes, en-passant is supported. I also improved think-time now on level “Difficult”.
Dojo2000
Scratcher
6 posts

Scratch Chess Engine - Game of Kings

Samueldora wrote:

Archimedes496 proposed such a tournament in his project Chess AI Contest and defined rules, e.g. for time control. With the new projects published recently this could be really interesting.

Exactly what I was looking for! But another prerequisite is to be able to play both black and white, or am I wrong?
Samueldora
Scratcher
100+ posts

Scratch Chess Engine - Game of Kings

Dojo2000 wrote:

Samueldora wrote:

Archimedes496 proposed such a tournament in his project Chess AI Contest and defined rules, e.g. for time control. With the new projects published recently this could be really interesting.

Exactly what I was looking for! But another prerequisite is to be able to play both black and white, or am I wrong?

Yes! Bonsai Chess can play both sides, Archimedes496s project as well. This might be sufficient for a knock-out tournament (as long as the more flexible system wins ), but not for a round robin.
Itharius
Scratcher
1000+ posts

Scratch Chess Engine - Game of Kings

e2e4
e7e5
Ng1f3
Nb8c6
Bf1c4
Ng8f6
0-0
Bf8b4
d2d3
h6h5 <—-here is the first thing strange i think it was h7h6
c2c3
Bb4c5
Nf3h3 <—-here another something wrong, i am sure it was Nf3h4
Nf6xe4
d3xe4
Qd8xh3 <—- So here it was Qd8xh4 of course
Qd1f3
f7f6
b2b4
Bc5b6
a2a4
a7a5
b4b5
Nc6e7 <—- it was here that the tower in A8 disappears. i


i do not post the rest of the play. hope this helps
ArnoHu
Scratcher
1000+ posts

Scratch Chess Engine - Game of Kings

Itharius wrote:

e2e4
e7e5
Ng1f3
Nb8c6
Bf1c4
Ng8f6
0-0
Bf8b4
d2d3
h6h5 <—-here is the first thing strange i think it was h7h6
c2c3
Bb4c5
Nf3h3 <—-here another something wrong, i am sure it was Nf3h4
Nf6xe4
d3xe4
Qd8xh3 <—- So here it was Qd8xh4 of course
Qd1f3
f7f6
b2b4
Bc5b6
a2a4
a7a5
b4b5
Nc6e7 <—- it was here that the tower in A8 disappears. i


i do not post the rest of the play. hope this helps

So you actually found two things.I fixed both of them - thank you!

Last edited by ArnoHu (April 14, 2017 15:20:04)

Samueldora
Scratcher
100+ posts

Scratch Chess Engine - Game of Kings

ArnoHu wrote:

Samueldora, plumpudin, thanks a lot for the endorsement and feedback!

Yes, a tournament sounds like a nice idea. I think I could provide a change of sides for the human player with reasonable effort.

I played a small tournament with four Scratch Chess machines, as they are today:

AI Tournament
Scratch, 2017.04.13 - 2017.04.14
Score B S C T
—————————————————————————————
1: Bonsai Chess (Samueldora) 3.0 / 3 X 1 1 1 (+3 -0 =0)
2: Scratch Chess (ArnoHu) 1.5 / 3 0 X 1 = (+1 -1 =1)
3: Chess (Archimedes496) 1.0 / 3 0 0 X 1 (+1 -2 =0)
4: The Turk (BoltBait) 0.5 / 3 0 = 0 X (+0 -2 =1)
—————————————————————————————-
6 games: +4 =1 -1

Remark: The draw between Scratch Chess and The Turk was the result of neither of them playing white. The Turk has the fine feature to be able to process FEN Codes, so I put in the start position with black to move to emulate white play. Unfortunately, after a few moves The Turk refuted to accept castling of Scratch Chess. Normally The Turk accepts this correctly, so I assume, it was due to the FEN work around. As I had found it unfair to disqualify The Turk under these conditions, I took the game as draw.

Here the decisive game between Bonsai Chess and Scratch Chess in PGN:

Event “AI Tournament”
Site “Scratch”
Date “2017.04.13”
Round “?”
White “Bonsai Chess (Samueldora)”
Black “Scratch Chess (ArnoHu)”
Result “1-0”
EventDate “2017.04.13”

1.d4 d5 2.c4 dxc4 3.e3 e5 4.Bxc4 Bb4+ 5.Bd2 Bxd2+ 6.Nxd2 exd4 7.exd4 Qxd4 8.Qh5 g6 9.Qd5 Qxd5 10.Bxd5 Nh6 11.Rc1 c6 12.Bf3 Kd8 13.Rd1 Nd7 14.Nb1 a5 15.Nc3 Re8+ 16.Kd2 Re4 17.Nxe4 Ne5 18.Kc1+ Ke7 19.Be2 Bg4 20.Nc3 Bxe2 21.Ngxe2 f5 22.f4 Nef7 23.h3 c5 24.Rhf1 Ng8 25.Nd5+ Kf8 26.Nc7 Rc8 27.Nb5 Re8 28.Nec3 Re3 29.Na4 c4 30.Nc5 Ngh6 31.Nxb7 a4 32.Nc5 g5 33.fxg5 Nxg5 34.Nxa4 Re5 35.Na3 Ra5 36.Nc3 Rc5 37.Na4 Re5 38.Nxc4 Re4 39.Nab6 f4 40.Nd6 Rb4 41.Nd7+ Ke7 42.Nc8+ Kf7 43.Nd6+ Ke7 44.Nc5 f3 45.gxf3 Rh4 46.Rfe1+ Kf6 47.Nd7+ Kg6 48.Rf1 Rxh3 49.f4 Ne6 50.Rg1+ Kh5 51.f5 Nxf5 52.Nf6+ Kh4 53.Nxf5# 1-0

PGNs of all games are put as comments into Archimedes496's chess AI studio.

Last edited by Samueldora (April 14, 2017 15:12:33)

ArnoHu
Scratcher
1000+ posts

Scratch Chess Engine - Game of Kings

Samueldora wrote:

ArnoHu wrote:

Samueldora, plumpudin, thanks a lot for the endorsement and feedback!

Yes, a tournament sounds like a nice idea. I think I could provide a change of sides for the human player with reasonable effort.

I played a small tournament with four Scratch Chess machines, as they are today:

AI Tournament
Scratch, 2017.04.13 - 2017.04.14
Score B S C T
—————————————————————————————
1: Bonsai Chess (Samueldora) 3.0 / 3 X 1 1 1 (+3 -0 =0)
2: Scratch Chess (ArnoHu) 1.5 / 3 0 X 1 = (+1 -1 =1)
3: Chess (Archimedes496) 1.0 / 3 0 0 X 1 (+1 -2 =0)
4: The Turk (BoltBait) 0.5 / 3 0 = 0 X (+0 -2 =1)
—————————————————————————————-
6 games: +4 =1 -1

Remark: The draw between Scratch Chess and The Turk was the result of neither of them playing white. The Turk has the fine feature to be able to process FEN Codes, so I put in the start position with black to move to emulate white play. Unfortunately, after a few moves The Turk refuted to accept castling of Scratch Chess. Normally The Turk accepts this correctly, so I assume, it was due to the FEN work around. As I had found it unfair to disqualify The Turk under these conditions, I took the game as draw.

Here the decisive game between Bonsai Chess and Scratch Chess in PGN:

Event “AI Tournament”
Site “Scratch”
Date “2017.04.13”
Round “?”
White “Bonsai Chess (Samueldora)”
Black “Scratch Chess (ArnoHu)”
Result “1-0”
EventDate “2017.04.13”

1.d4 d5 2.c4 dxc4 3.e3 e5 4.Bxc4 Bb4+ 5.Bd2 Bxd2+ 6.Nxd2 exd4 7.exd4 Qxd4 8.Qh5 g6 9.Qd5 Qxd5 10.Bxd5 Nh6 11.Rc1 c6 12.Bf3 Kd8 13.Rd1 Nd7 14.Nb1 a5 15.Nc3 Re8+ 16.Kd2 Re4 17.Nxe4 Ne5 18.Kc1+ Ke7 19.Be2 Bg4 20.Nc3 Bxe2 21.Ngxe2 f5 22.f4 Nef7 23.h3 c5 24.Rhf1 Ng8 25.Nd5+ Kf8 26.Nc7 Rc8 27.Nb5 Re8 28.Nec3 Re3 29.Na4 c4 30.Nc5 Ngh6 31.Nxb7 a4 32.Nc5 g5 33.fxg5 Nxg5 34.Nxa4 Re5 35.Na3 Ra5 36.Nc3 Rc5 37.Na4 Re5 38.Nxc4 Re4 39.Nab6 f4 40.Nd6 Rb4 41.Nd7+ Ke7 42.Nc8+ Kf7 43.Nd6+ Ke7 44.Nc5 f3 45.gxf3 Rh4 46.Rfe1+ Kf6 47.Nd7+ Kg6 48.Rf1 Rxh3 49.f4 Ne6 50.Rg1+ Kh5 51.f5 Nxf5 52.Nf6+ Kh4 53.Nxf5# 1-0

PGNs of all games are put as comments into Archimedes496's chess AI studio.

Nice, cool you really ran that! I will have a look at the details later.
ArnoHu
Scratcher
1000+ posts

Scratch Chess Engine - Game of Kings

Samueldora, congratulations to the tournament win! I am happy to be in second place within such strong a competition.

I might have a solution for the “only-plays-black” problem of certain projects. You can feed any board into “Chess - Game of Kings” (click “Import”). The starting board has this format:

500,310,330,900,20000,330,310,500,100,100,100,100,100,100,100,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-100,-100,-100,-100,-100,-100,-100,-100,-500,-310,-330,-900,-20000,-330,-310,-500,

(that's actually the value of the pieces, greather than zero is black)

Now you could switch the queens' positions and set a first “black” move on the board data. Then load this into Chess, and it will play the white part, (despite moving black pieces). I guess that's what you tried to achieve? I haven't tried, but it might work. I will run óne of those myself, but for your tournament and the sake of neutrality it's probably better you do it.

One other questions I have is: I remember our two programs had a draw before; “Chess - Game of Kings” is completely deterministic (something I am planning to change by introducing some randomness), what about Bonsai? If both would be deterministic, wouldn't they play the same game over and over again?

And finally, what about https://scratch.mit.edu/projects/569176/ - it seems to be the oldest and most commonly used chess game on Scratch.

Thanks once more!

Last edited by ArnoHu (April 14, 2017 18:25:56)

Samueldora
Scratcher
100+ posts

Scratch Chess Engine - Game of Kings

Use the link to see the PGN games of the tournament in a nice game viewer on chess.com. I posted them in the Scratch Chess Club which was founded there a couple of years ago.

Last edited by Samueldora (April 14, 2017 18:31:48)

Samueldora
Scratcher
100+ posts

Scratch Chess Engine - Game of Kings

ArnoHu wrote:

I might have a solution for the “only-plays-black” problem of certain projects. You can feed any board into “Chess - Game of Kings” (click “Import”). The starting board has this format:

500,310,330,900,20000,330,310,500,100,100,100,100,100,100,100,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-100,-100,-100,-100,-100,-100,-100,-100,-500,-310,-330,-900,-20000,-330,-310,-500,

(that's actually the value of the pieces, greather than zero is black)

Now you could switch the queens' positions and set a first “black” move on the board data. Then load this into Chess, and it will play the white part, (despite moving black pieces). I guess that's what you tried to achieve? I haven't tried, but it might work. I will run óne of those myself, but for your tournament and the sake of neutrality it's probably better you do it.

One other questions I have is: I remember our two programs had a draw before; “Chess - Game of Kings” is completely deterministic (something I am planning to change by introducing some randomness), what about Bonsai? If both would be deterministic, wouldn't they play the same game over and over again?

And finally, what about https://scratch.mit.edu/projects/569176/ - it seems to be the oldest and most commonly used chess game on Scratch.

Thanks once more!

Thank you, ArnoHu! I will try your solution, but probably not today, my time budget for Scratch is used up by far. As to neutrality: well, I am not really neutral, but I tried to run a neutral tournament. I think the game protocol may help to find bugs. There a some moves that mustn't happen within 4-ply-search.

Bonsai is deterministic in the main section, but the opening book gives several random choices and is still growing, so that you normally get a certain variety. And as long as we both are working on the projects I expect changes as well.

I very much appreciate Midecah's project. It was a great achievement in the early Scratch days, but when testing it recently I experienced correctness problems as well as very limited search depth, so I decided to not include it in this competition.
ArnoHu
Scratcher
1000+ posts

Scratch Chess Engine - Game of Kings

I tried the “virtual white” approach described above, technically it worked. “The Turk” and “Game of Kings” ended up in a draw, with “Game of Kings” having taken over the white part (which comes with several disadvantages like not being able to castle, and too early queen and king moves as it thought they had been moved already anyway).

So your tournament result was confirmed in that sense.

Last edited by ArnoHu (April 15, 2017 07:51:20)

ArnoHu
Scratcher
1000+ posts

Scratch Chess Engine - Game of Kings

Hey and thanks a lot for providing the PGN data - great insight. Wow, what happened there in move 17, a rook sacrifice? I loaded that board into Chess, and it moved to e5, not e4. Here it is for retry:

500,0,330,20000,500,0,0,0,0,100,0,310,0,100,0,100,0,0,100,0,0,0,100,310,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-310,0,0,-330,0,0,-100,-100,0,0,0,-100,-100,-100,0,0,0,-500,-20000,0,-310,-500,

Maybe a bug I have fixed since, who knows…

Last edited by ArnoHu (April 15, 2017 00:23:52)

Samueldora
Scratcher
100+ posts

Scratch Chess Engine - Game of Kings

ArnoHu wrote:

I tried the “virtual white” approach described above, technically it worked. “The Turk” and “Game of Kings” ended up in a draw, with “Game of Kings” having taken over the white part (which comes with several disadvantages like not being able to castle, and too early queen and king moves as it thought they had been moved already anyway).

So your tournament result was confirmed in that sense.

I managed to load your file into “Game of Kings” but didn't get Black to move then. But your idea to exchange queen positions was useful. I fed a FEN with switched KQ positions for White and Black (to avoid the castling problems) into “The Turk” and “Game of Kings” and let “The Turk” begin. “Game of Kings” finally won after a 15 move dolce-far-niente-period in the middle of the game (move 22 to 37), which taxed my patience . Here's the PGN (brackets removed):

Event “AI Match”
Site “Scratch”
Date “2017.04.15”
Round “?”
White “The Turk (BoltBait)”
Black “Scratch Chess (ArnoHu)”
Result “0-1”
ECO “D41”
PlyCount “34”
FEN “rnbkqbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBKQBNR w - - 0 1”

1.Nc3 Nf6 2.Nb5 a6 3.Nc3 d6 4.d3 h6 5.Bf4 e5 6.Bg3 Qc6 7.e4 Ke8 8.Be2 Qb6 9.Rb1 Be6 10.Nh3 Nc6 11.Kd2 Nd4 12.Bd1 Be7 13.Qe3 Nc6 14.Bxe5 Qxe3+ 15.fxe3 Nxe4+ 16.dxe4 Nxe5 17.Bh5 Nc4+ 18.Kd3 Ne5+ 19.Ke2 Bc4+ 20.Kd1 Kd8 21.Kc1 f6 22.Ra1 Bf8 23.Rb1 Kd7 24.Ra1 Rb8 25.Rb1 Kd8 26.Ra1 Rh7 27.Rb1 Ra8 28.Ra1 Kd7 29.Rb1 Rb8 30.Ra1 Rd8 31.Rb1 Rh8 32.Ra1 Rg8 33.Rb1 Ra8 34.Ra1 Rb8 35.Rb1 Kd8 36.Ra1 Ra8 37.Rb1 Ke7 38.Ra1 g6 39.Nd5+ Bxd5 40.Bxg6 Be6 41.Nf4 Nxg6 42.Nxe6 Kxe6 43.Rb1 d5 44.exd5+ Kxd5 45.Ra1 Bc5 46.Re1 b5 47.Rb1 b4 48.Ra1 Ne5 49.Re2 a5 50.Rb1 a4 51.Ra1 h5 52.Rb1 h4 53.Ra1 c6 54.Rb1 Nc4 55.Ra1 Bxe3+ 56.Kd1 Nxb2+ 57.Ke1 Bg1 58.Rb1 a3 59.Rd2+ Kc4 60.Ke2 Rxg2+ 61.Ke1 Re8+ 62.Re2 Rgxe2+ 63.Kf1 Bxh2 64.Rxb2 axb2 65.a3 b1=Q# 0-1

Last edited by Samueldora (April 15, 2017 10:26:31)

Powered by DjangoBB