Discuss Scratch
- 26averyj
-
100+ posts
Better Scratch Chess Engines
The subject of this post is completely theoretical, and has never been done before. I'm not even sure it would be possible to do. But anyway, as the title suggests, it is about chess computers on scratch. Currently, I believe the best chess engine on scratch is Chess Next Generation Prototype by Arnohu, which uses advanced techniques like alpha-beta pruning, futility pruning, aspiration windows, and late move pruning to calculate very far ahead, sometimes more than 10 ply. I would estimate that it could play at about 1900 Elo on Forkforus, but Stockfish, which is considered to be the best chess engine (I'll get back to that), plays at most than 3450 Elo. The only real hope to improve scratch chess engines any more would be to take a whole different approach: neural networks. Alphazero, Deepmind's neural network that was released in December 2017, played a set of games against stockfish, and won almost a third of the games, while most of the others were draws. Later, Gary Linscott and some of the other people who worked on Stockfish set out to recreate it, and created Leela Chess Zero, or Lc0. Pretty soon, Lc0 reached Stockfish's level, and it is unclear which is better. The thing that I was thinking about was how Stockfish on 1 ply can play at about 1200 Elo, and Lc0 on 1 ply can play at more than 2300. If we could create a chess neural network on scratch, even if it was only fast enough to get 1 ply, it could still potentially beat Arnohu's traditional chess engine. This would be a very interesting programming challenge to take on in scratch.
- 26averyj
-
100+ posts
Better Scratch Chess Engines
Update: I've done some research, and I've discovered that 1: yes, it is entirely possible to make a neural network in scratch, but more importantly, the weights used in Lc0 are available on the internet.
This means that it would be entirely possible to make a chess neural network in scratch, and that there would be no training necessary because we already have the weights.
This means that it would be entirely possible to make a chess neural network in scratch, and that there would be no training necessary because we already have the weights.
- PintOfMilk
-
1000+ posts
Better Scratch Chess Engines
It's great that you are interested in chess. I am currently making a chess engine in Javascript however it is only early stages. (1ply , 2ply for deep search and up to 6 ply for takes)
There is something called an opening book which traditionally was used to get a good advantage in the first part of the game.
In addition there is also something called piece table functions which are tables for each piece. For example bishops do prefer to be fianchettoed. This
Finally , although I haven't really researched neural networks one of my friends is making a neural network with artificial intelligence. Be warned that aritificial intelligence is typically year long postgraduate qualification in the UK at least, or integrated bachelors degree and typically requires high levels of maths, perhaps multi-variable calculus.
https://www.chessprogramming.org/Piece-Square_Tables
Note:Calculus tends to require reasonably high algebra skills.
There is something called an opening book which traditionally was used to get a good advantage in the first part of the game.
In addition there is also something called piece table functions which are tables for each piece. For example bishops do prefer to be fianchettoed. This
Finally , although I haven't really researched neural networks one of my friends is making a neural network with artificial intelligence. Be warned that aritificial intelligence is typically year long postgraduate qualification in the UK at least, or integrated bachelors degree and typically requires high levels of maths, perhaps multi-variable calculus.
https://www.chessprogramming.org/Piece-Square_Tables
Note:Calculus tends to require reasonably high algebra skills.
- Centuriasoft
-
46 posts
Better Scratch Chess Engines
Yes neural networks can be built on Scratch, however, and this is a big however, the average chess AI has thousands if not hundreds of thousands of neurons and scratch, being interpreted in Javascript which is slow rather than compiled which is fast, simply isn't capable of handling that many individual neurons, unless you wish to co-ordinate hundreds of lists and run it at a turn per century. However, a really smart algorithm can do fairly well against most players of chess and you can make it do mistakes if you want users to have a chance, otherwise, with a neural net based AI the user really has no chance
- 26averyj
-
100+ posts
Better Scratch Chess Engines
Actually, I think, if it's only looking ahead 1 ply, scratch could certainly handle a hundred thousand neurons in a matter of seconds. As it looks deeper and deeper, the think time would increase exponentially, but 1 or 2 ply would be perfectly reasonable. Yes neural networks can be built on Scratch, however, and this is a big however, the average chess AI has thousands if not hundreds of thousands of neurons and scratch, being interpreted in Javascript which is slow rather than compiled which is fast, simply isn't capable of handling that many individual neurons, unless you wish to co-ordinate hundreds of lists and run it at a turn per century. However, a really smart algorithm can do fairly well against most players of chess and you can make it do mistakes if you want users to have a chance, otherwise, with a neural net based AI the user really has no chance
- 26averyj
-
100+ posts
Better Scratch Chess Engines
Thanks for the advice! I am definitely going to have an opening book, but I don't think I will be using piece tables, because the idea of a neural network is that it doesn't need to be specifically instructed, and that it can figure it out on its own. It's great that you are interested in chess. I am currently making a chess engine in Javascript however it is only early stages. (1ply , 2ply for deep search and up to 6 ply for takes)
There is something called an opening book which traditionally was used to get a good advantage in the first part of the game.
In addition there is also something called piece table functions which are tables for each piece. For example bishops do prefer to be fianchettoed. This
Finally , although I haven't really researched neural networks one of my friends is making a neural network with artificial intelligence. Be warned that aritificial intelligence is typically year long postgraduate qualification in the UK at least, or integrated bachelors degree and typically requires high levels of maths, perhaps multi-variable calculus.
https://www.chessprogramming.org/Piece-Square_Tables
Note:Calculus tends to require reasonably high algebra skills.
Good luck on your chess engine.
- ArnoHu
-
1000+ posts
Better Scratch Chess Engines
Yes neural networks can be built on Scratch, however, and this is a big however, the average chess AI has thousands if not hundreds of thousands of neurons and scratch, being interpreted in Javascript which is slow rather than compiled which is fast, simply isn't capable of handling that many individual neurons, unless you wish to co-ordinate hundreds of lists and run it at a turn per century. However, a really smart algorithm can do fairly well against most players of chess and you can make it do mistakes if you want users to have a chance, otherwise, with a neural net based AI the user really has no chance
Agreed, while it is a nice thought, a chess neural network cannot be done in Scratch IMHO. What I mean is that yes, you certainly cannot train such a neural network on a limited platform like Scratch. And even re-using a reduced trained network, the approach you mentioned before, is unlikely to work due to its expected complexity. You may also want to have a look at https://www.chess.com/article/view/whats-inside-alphazeros-brain for some more details.
Last edited by ArnoHu (Oct. 19, 2019 21:07:34)
- ArnoHu
-
1000+ posts
Better Scratch Chess Engines
It is also worth noting that the modest Game of Kings Scratch Chess Engine drew a game against Leela Zero Chess more than a year ago: https://scratch.mit.edu/discuss/post/3099651/. Admitted, that was Leela Zero still in an early stage, and in normal playing mode.
- 26averyj
-
100+ posts
Better Scratch Chess Engines
You're probably right about that, but it's still worth a try. If we could simply recreate Lc0 in scratch, we wouldn't have to do any training because it's open source and the weights are available on the internet. Still, it probably wouldn't be able to get over 1 ply, and there are many other challenges due to the fact that C++ has many features that scratch doesn't. Also, you bring up a good point that GoK was able to draw against Lc0, and on 1 ply, it might not be better at all.Yes neural networks can be built on Scratch, however, and this is a big however, the average chess AI has thousands if not hundreds of thousands of neurons and scratch, being interpreted in Javascript which is slow rather than compiled which is fast, simply isn't capable of handling that many individual neurons, unless you wish to co-ordinate hundreds of lists and run it at a turn per century. However, a really smart algorithm can do fairly well against most players of chess and you can make it do mistakes if you want users to have a chance, otherwise, with a neural net based AI the user really has no chance
Agreed, while it is a nice thought, a chess neural network cannot be done in Scratch, not even remotely. You may want to have a look at https://www.chess.com/article/view/whats-inside-alphazeros-brain for details.
- ArnoHu
-
1000+ posts
Better Scratch Chess Engines
Yes neural networks can be built on Scratch, however, and this is a big however, the average chess AI has thousands if not hundreds of thousands of neurons and scratch, being interpreted in Javascript which is slow rather than compiled which is fast, simply isn't capable of handling that many individual neurons, unless you wish to co-ordinate hundreds of lists and run it at a turn per century. However, a really smart algorithm can do fairly well against most players of chess and you can make it do mistakes if you want users to have a chance, otherwise, with a neural net based AI the user really has no chance
Agreed, while it is a nice thought, a chess neural network cannot be done in Scratch IMHO. What I mean is that yes, you certainly cannot train such a neural network on a limited platform like Scratch. And even re-using a reduced trained network, the approach you mentioned before, is unlikely to work due to its expected complexity. You may also want to have a look at https://www.chess.com/article/view/whats-inside-alphazeros-brain for some more details.
I looked up distilled Leela Zero Chess networks on github, their NN weights take up dozens to hundreds of megabytes of memory.
Last edited by ArnoHu (Oct. 19, 2019 21:23:44)
- PintOfMilk
-
1000+ posts
Better Scratch Chess Engines
Perhaps you could train a neural network on scratch to play https://en.m.wikipedia.org/wiki/Hexapawn
But if you want to get into proper AI avoid scratch like the plague and look into python.
But if you want to get into proper AI avoid scratch like the plague and look into python.
- Centuriasoft
-
46 posts
Better Scratch Chess Engines
I stand by what I said about too many neurons, however, if you can make a more basic one with 1/2ply which may well be possible i'd love to see it, it's would be cool to see scratch's limits when it comes to enormous amounts of data access
- 26averyj
-
100+ posts
Better Scratch Chess Engines
https://en.m.wikipedia.org/wiki/HexapawnActually, I believe that Perhaps you could train a neural network on scratch to play already exists!
But if you want to get into proper AI avoid scratch like the plague and look into python.
- Nishpish
-
47 posts
Better Scratch Chess Engines
I believe this could be done in Scratch with enough effort. A LOT of data would be required, as well a lot of training time and computation power. For the task, reinforcement learning would almost definitely be used. What type, I'm not sure. I'm no expert in reinforcement learning, though I do have quite a bit of experience in standard neural networks. I do think it's possible.
- ArnoHu
-
1000+ posts
Better Scratch Chess Engines
I believe this could be done in Scratch with enough effort. A LOT of data would be required, as well a lot of training time and computation power. For the task, reinforcement learning would almost definitely be used. What type, I'm not sure. I'm no expert in reinforcement learning, though I do have quite a bit of experience in standard neural networks. I do think it's possible.
Thanks a lot, do you think we could represent the NN data by means of Scratch lists? There is a 200,000 item limit per list. And could Scratch be fast enough to process it (probably run it on TurboWarp)? I suppose it would be a limited-scale NN, and the question is which kind of playing strength we could expect from it. Also Scratch projects are limited in size for script + list + variable data (5MB AFAIK), e.g. I cannot save GoK chess if its transposition table is full, I always have to empty it before saving.
Disclaimer: Nishpish was kind enough to take a look and answer in this forum on my request (no necropost, or if it is, put it on me

- Nishpish
-
47 posts
Better Scratch Chess Engines
Hmm, lists probably would be too short, at least in some parts of the network. I didn’t know about the data limit for Scratch projects, that could be a potential problem. If we could actually load all necessary data, then it could be run on TurboWarp and given enough time, I think it could be trainable. (I’m actually working on a handwritten digit recognition network, and it trains surprisingly fast. It is however much simpler than a chess AI would be.)
- ArnoHu
-
1000+ posts
Better Scratch Chess Engines
Hmm, lists probably would be too short, at least in some parts of the network. I didn’t know about the data limit for Scratch projects, that could be a potential problem. If we could actually load all necessary data, then it could be run on TurboWarp and given enough time, I think it could be trainable. (I’m actually working on a handwritten digit recognition network, and it trains surprisingly fast. It is however much simpler than a chess AI would be.)
The maximum list size limit does not exist on TurboWarp (only on Scratch 3). So this might be a way to go.
- Inter-Technic
-
1 post
Better Scratch Chess Engines
Hmm, lists probably would be too short, at least in some parts of the network. I didn’t know about the data limit for Scratch projects, that could be a potential problem. If we could actually load all necessary data, then it could be run on TurboWarp and given enough time, I think it could be trainable. (I’m actually working on a handwritten digit recognition network, and it trains surprisingly fast. It is however much simpler than a chess AI would be.)
The maximum list size limit does not exist on TurboWarp (only on Scratch 3). So this might be a way to go.
I made a project were you can create neuronal networks and run them https://scratch.mit.edu/projects/443705762/. Scratchs list size limit is reached very early (200.000 weights), TurboWarp works fine with that size, but 5 layers with 10000 neurons crashes my browser “Error code: Out of Memory” (400.000.000 weights).

Ok, I did some more testing. A neuronal network with all neurons of one layer connected to all neurons of the next layer (I think there is word for that) and 9 layers:
1. layer: 256 neurons
2. layer: 1000 neurons
3. layer 2000 neurons
4. layer 3000 neurons
5. layer 5000 neurons
6. layer 3000 neurons
7. layer 2000 neurons
8. layer 1000 neurons
9. layer 256 neurons
has a total of 17.512 neurons, 46.512.000 weights and biases and needs 2 seconds to run on TurboWarp. I don't know if that is enough for a chess ai.
Networks with more than 110.000.000 weights and biaseses crash my browser.
Last edited by Inter-Technic (Nov. 1, 2020 12:19:08)
- ArnoHu
-
1000+ posts
Better Scratch Chess Engines
Hmm, lists probably would be too short, at least in some parts of the network. I didn’t know about the data limit for Scratch projects, that could be a potential problem. If we could actually load all necessary data, then it could be run on TurboWarp and given enough time, I think it could be trainable. (I’m actually working on a handwritten digit recognition network, and it trains surprisingly fast. It is however much simpler than a chess AI would be.)
The maximum list size limit does not exist on TurboWarp (only on Scratch 3). So this might be a way to go.
I made a project were you can create neuronal networks and run them https://scratch.mit.edu/projects/443705762/. Scratchs list size limit is reached very early (200.000 weights), TurboWarp works fine with that size, but 5 layers with 10000 neurons crashes my browser “Error code: Out of Memory” (400.000.000 weights).
Ok, I did some more testing. A neuronal network with all neurons of one layer connected to all neurons of the next layer (I think there is word for that) and 9 layers:
1. layer: 256 neurons
2. layer: 1000 neurons
3. layer 2000 neurons
4. layer 3000 neurons
5. layer 5000 neurons
6. layer 3000 neurons
7. layer 2000 neurons
8. layer 1000 neurons
9. layer 256 neurons
has a total of 17.512 neurons, 46.512.000 weights and biases and needs 2 seconds to run on TurboWarp. I don't know if that is enough for a chess ai.
Networks with more than 110.000.000 weights and biaseses crash my browser.
Really cool, thank you! Regarding to https://www.chess.com/article/view/whats-inside-alphazeros-brain it is estimated that AlphaZero has 80 layers and 100,000s of neurons.
- ArnoHu
-
1000+ posts
Better Scratch Chess Engines
First of all, sorry for necro-posting, but there is good reason. Back in 2019 / 2020 I had not heard of Efficiently Updatable Neural Networks (NNUEs) yet, and was thinking of Chess NNs in terms as of Leela Zero.
And even once I had learned about NNUEs, amazing as they were in native engines, I doubted we could make it function on Scratch (TurboWarp yes, Scratch no).
However, instead of speculating, two weeks ago I simply tried and did it. It only took me three evenings, as I ported an existing NNUE and replaced GoK's classic evaluation function with it.
Quick note on its neural network design:
768 => ( 2 x 256 ) => 1
Featuerset (input) size is 768 (for 12 piece types x 64 squares), with 2 layers of 256 neurons each. NN has “viewpoints” for black and white. For every simple move applied or reverted, 2,048 accumulator items must updated (more on captures, promotions, castling). for evaluation, 1,024 weights and accumulator items are combined. That is still a heavy workload esp. on the Scratch runtime.
Here is GoK NNUE:
https://scratch.mit.edu/projects/1033379869
TurboWarp version:
https://turbowarp.org/1033379869
You can find more details about GoK NNUE's implementation in this forum posting:
https://scratch.mit.edu/discuss/post/8014663/
I don't know how many of you are still on Scratch, but I would be happy if you'd give a try and tell me what you think.
Thank you!
And even once I had learned about NNUEs, amazing as they were in native engines, I doubted we could make it function on Scratch (TurboWarp yes, Scratch no).
However, instead of speculating, two weeks ago I simply tried and did it. It only took me three evenings, as I ported an existing NNUE and replaced GoK's classic evaluation function with it.
Quick note on its neural network design:
768 => ( 2 x 256 ) => 1
Featuerset (input) size is 768 (for 12 piece types x 64 squares), with 2 layers of 256 neurons each. NN has “viewpoints” for black and white. For every simple move applied or reverted, 2,048 accumulator items must updated (more on captures, promotions, castling). for evaluation, 1,024 weights and accumulator items are combined. That is still a heavy workload esp. on the Scratch runtime.
Here is GoK NNUE:
https://scratch.mit.edu/projects/1033379869
TurboWarp version:
https://turbowarp.org/1033379869
You can find more details about GoK NNUE's implementation in this forum posting:
https://scratch.mit.edu/discuss/post/8014663/
I don't know how many of you are still on Scratch, but I would be happy if you'd give a try and tell me what you think.
Thank you!
Last edited by ArnoHu (March 29, 2025 07:32:17)