Discuss Scratch
- BabyChewie
-
91 posts
Chess AI
Alrighty, I'm working on a chess game. I've gotten a lot of work done in a day, but I just can't get the artificial intelligence opponent working. I've tried my own code as well as looked up how to make it, but I can't figure it out. I need some help.
First off, you'll want to know how the game works.
I have two lists to represent the board: board and board-colors. They look like this:
The algorithm that it seems most people want is the minimax algorithm, which is recursive, but I can't seem to implement it to scratch due to not being able to have local variables.
Can anyone help me?
First off, you'll want to know how the game works.
I have two lists to represent the board: board and board-colors. They look like this:
boardThe board-colors list simply holds whether a piece is white or black. Board holds which piece it is: R for rook, N for knight, B for bishop, Q for queen, K for king, and P for pawn. I have some code that allows me to move a piece from one place to another like this: g5 to g6. But the AI is hosted in a separate sprite, so I have two variables and a broadcast to do the work.
rnbqkbnr
ppppppp
0000000
0000000
0000000
0000000
ppppppp
rnbqkbnr
board-colors
wwwwwwww
wwwwwwww
0000000
0000000
0000000
0000000
bbbbbbbb
bbbbbbbb
The algorithm that it seems most people want is the minimax algorithm, which is recursive, but I can't seem to implement it to scratch due to not being able to have local variables.
Can anyone help me?
- jokebookservice1
-
1000+ posts
Chess AI
If all you need is local variables, here is a possible solution:
Using a list, you could store the variables.
In your recursive block, place the index in the list of the block, e.g.
EDIT: You can either create multiple lists or allocate more slots in the list to emulate more local variables
Using a list, you could store the variables.
In your recursive block, place the index in the list of the block, e.g.
I have not much knowledge on the minmax algorithm though, but thatis how you might want to do the local variables?
EDIT: You can either create multiple lists or allocate more slots in the list to emulate more local variables
Last edited by jokebookservice1 (May 9, 2016 17:53:11)
- BabyChewie
-
91 posts
Chess AI
If all you need is local variables, here is a possible solution:
Using a list, you could store the variables.
In your recursive block, place the index in the list of the block, e.g.I have not much knowledge on the minmax algorithm though, but thatis how you might want to do the local variables?
EDIT: You can either create multiple lists or allocate more slots in the list to emulate more local variables
I don't have much knowledge either. I still don't think I could create a good Chess AI with those things. I need something that analyzes the few best moves immediately, cly 3.
Last edited by BabyChewie (May 9, 2016 17:57:38)
- jokebookservice1
-
1000+ posts
Chess AI
What does cly 3 mean?If all you need is local variables, here is a possible solution:
Using a list, you could store the variables.
In your recursive block, place the index in the list of the block, e.g.I have not much knowledge on the minmax algorithm though, but thatis how you might want to do the local variables?
EDIT: You can either create multiple lists or allocate more slots in the list to emulate more local variables
I don't have much knowledge either. I still don't think I could create a good Chess AI with those things. I need something that analyzes the few best moves immediately, cly 3.
- Tymewalk
-
1000+ posts
Chess AI
For local variables can't you just use regular variables that get emptied or reset every time a loop is run? Just prefix them with "[LOCAL]" and then don't use them for anything else.If all you need is local variables, here is a possible solution:
Using a list, you could store the variables.
In your recursive block, place the index in the list of the block, e.g.I have not much knowledge on the minmax algorithm though, but thatis how you might want to do the local variables?
EDIT: You can either create multiple lists or allocate more slots in the list to emulate more local variables
I don't have much knowledge either. I still don't think I could create a good Chess AI with those things. I need something that analyzes the few best moves immediately, cly 3.
Unless I'm misunderstanding something
Last edited by Tymewalk (May 9, 2016 17:59:52)
- jokebookservice1
-
1000+ posts
Chess AI
The variables need to have multiple instances since they are using recursionFor local variables can't you just use regular variables that get emptied or reset every time a loop is run? Just prefix them with "[LOCAL]" and then don't use them for anything else.If all you need is local variables, here is a possible solution:
Using a list, you could store the variables.
In your recursive block, place the index in the list of the block, e.g.I have not much knowledge on the minmax algorithm though, but thatis how you might want to do the local variables?
EDIT: You can either create multiple lists or allocate more slots in the list to emulate more local variables
I don't have much knowledge either. I still don't think I could create a good Chess AI with those things. I need something that analyzes the few best moves immediately, cly 3.
Unless I'm misunderstanding something
- BabyChewie
-
91 posts
Chess AI
The way the minimax algorithm works, I don't think so. BoltBait pulled it off, so I'll take a look at his and see how it was done.For local variables can't you just use regular variables that get emptied or reset every time a loop is run? Just prefix them with "[LOCAL]" and then don't use them for anything else.If all you need is local variables, here is a possible solution:
Using a list, you could store the variables.
In your recursive block, place the index in the list of the block, e.g.I have not much knowledge on the minmax algorithm though, but thatis how you might want to do the local variables?
EDIT: You can either create multiple lists or allocate more slots in the list to emulate more local variables
I don't have much knowledge either. I still don't think I could create a good Chess AI with those things. I need something that analyzes the few best moves immediately, cly 3.
Unless I'm misunderstanding something
- BabyChewie
-
91 posts
Chess AI
It looks like Bolt used ply 3 and checked for which ways had the most points, which seems simple, but I don't know how I can record which one has the most points, due to recursion in scratch being so complicated and all.
- jokebookservice1
-
1000+ posts
Chess AI
Can I simplify making the local variables, I'll show you an example. It will use two variables, and call itself recursively:
That is my example implementation of local variables using a list
Last edited by jokebookservice1 (May 9, 2016 18:14:14)
- BabyChewie
-
91 posts
Chess AI
I'm going to add a download and a link to the phosphorus so you guys can see what you can do.
Download: http://darpion.github.io/downloads/Chess.sb2
Phosphorus: http://phosphorus.github.io/app.html?id=108697481&turbo=true&full-screen=true
Download: http://darpion.github.io/downloads/Chess.sb2
Phosphorus: http://phosphorus.github.io/app.html?id=108697481&turbo=true&full-screen=true
- __init__
-
1000+ posts
Chess AI
Your AI is a very bad player. I'm going to add a download and a link to the phosphorus so you guys can see what you can do.
Download: http://darpion.github.io/downloads/Chess.sb2
Phosphorus: http://phosphorus.github.io/app.html?id=108697481&turbo=true&full-screen=true

I put the king in check, he left it in check, and then I captured it

- jokebookservice1
-
1000+ posts
Chess AI
Yeah, if you are using minimax, you have to go in at least 1 layer/branch/(?) ! (you can see I did a bit of research)Your AI is a very bad player. I'm going to add a download and a link to the phosphorus so you guys can see what you can do.
Download: http://darpion.github.io/downloads/Chess.sb2
Phosphorus: http://phosphorus.github.io/app.html?id=108697481&turbo=true&full-screen=true
I put the king in check, he left it in check, and then I captured it
If the local variable issue is still bothering you, check out….https://scratch.mit.edu/projects/108867331/
I whipped it together just now!
But yeah, it should try all possible outcomes in a non-refresh custom block.
You could go just 1 layer in for now
Last edited by jokebookservice1 (May 9, 2016 21:06:49)
- Jonathan50
-
1000+ posts
Chess AI
Use an auxiliary custom block:
Hope that helps!
(Of course you can't change x but you can pass a new x in the recursive call, and it's usually more elegant that way IMO)
Hope that helps!
Last edited by Jonathan50 (May 9, 2016 23:22:11)
- BabyChewie
-
91 posts
Chess AI
Heehee ^-^ it's not done. I need to add the AI, fix pawn moving bugs, add en passant, castling, promotion, check (which is pretty easy) and checkmate (which still isn't hard).Your AI is a very bad player. I'm going to add a download and a link to the phosphorus so you guys can see what you can do.
Download: http://darpion.github.io/downloads/Chess.sb2
Phosphorus: http://phosphorus.github.io/app.html?id=108697481&turbo=true&full-screen=true
I put the king in check, he left it in check, and then I captured it
I need local lists is the problem though, and I need it to be able to return the list. Use an auxiliary custom block:(Of course you can't change x but you can pass a new x in the recursive call, and it's usually more elegant that way IMO)
Hope that helps!
- jokebookservice1
-
1000+ posts
Chess AI
I am working on local lists, in the same project as loca vars. What do you mean by return the “list” (i.e. an item from the list, or the concatenation of all the items with spaces between, or something like in Snap! [impossible in Scratch])Heehee ^-^ it's not done. I need to add the AI, fix pawn moving bugs, add en passant, castling, promotion, check (which is pretty easy) and checkmate (which still isn't hard).Your AI is a very bad player. I'm going to add a download and a link to the phosphorus so you guys can see what you can do.
Download: http://darpion.github.io/downloads/Chess.sb2
Phosphorus: http://phosphorus.github.io/app.html?id=108697481&turbo=true&full-screen=true
I put the king in check, he left it in check, and then I captured itI need local lists is the problem though, and I need it to be able to return the list. Use an auxiliary custom block:(Of course you can't change x but you can pass a new x in the recursive call, and it's usually more elegant that way IMO)
Hope that helps!
Last edited by jokebookservice1 (May 10, 2016 15:53:49)
- kvackkvack
-
500+ posts
Chess AI
! [impossible in Scratch])?He could implement his own first-class linked lists system, then it'd work like Snap I am working on local lists, in the same project as local vars. What do you mean by returning the “list” (i.e. an item from the list, or the concatenation of all the items with spaces between, or something like in Snap!'s one. I'd recommend checking out Jonathan50's project for it, but perhaps a whole linked list implementation is a bit overkill for the project.
- jokebookservice1
-
1000+ posts
Chess AI
What is a linked list? I am working on storing multiple lists in a single list, by using an extra list to remember where one list starts and the other ends.! [impossible in Scratch])?He could implement his own first-class linked lists system, then it'd work like Snap I am working on local lists, in the same project as local vars. What do you mean by returning the “list” (i.e. an item from the list, or the concatenation of all the items with spaces between, or something like in Snap!'s one. I'd recommend checking out Jonathan50's project for it, but perhaps a whole linked list implementation is a bit overkill for the project.
- kvackkvack
-
500+ posts
Chess AI
A linked list in Scratch is kept in a normal list, but it consists of pairs of items - the first one is the real value of an item, the second one is the index of the next pair.What is a linked list? I am working on storing multiple lists in a single list, by using an extra list to remember where one list starts and the other ends.! [impossible in Scratch])?He could implement his own first-class linked lists system, then it'd work like Snap I am working on local lists, in the same project as local vars. What do you mean by returning the “list” (i.e. an item from the list, or the concatenation of all the items with spaces between, or something like in Snap!'s one. I'd recommend checking out Jonathan50's project for it, but perhaps a whole linked list implementation is a bit overkill for the project.
An example list in JS-syntax would look something like this:
[
“”, // Null, used for the last items in lists
“item 3”,
1,
“item 2”,
2,
“item 1”,
4
]
The nice things about these are that you can keep multiple of these linked lists in one Scratch list, that it's easy to make custom blocks replicating the function of normal Scratch lists and that you can pass them around in functions - wherever you would pass an anonymous list, instead pass the index of the first item in your linked list!
- jokebookservice1
-
1000+ posts
Chess AI
I think I get itA linked list in Scratch is kept in a normal list, but it consists of pairs of items - the first one is the real value of an item, the second one is the index of the next pair.What is a linked list? I am working on storing multiple lists in a single list, by using an extra list to remember where one list starts and the other ends.! [impossible in Scratch])?He could implement his own first-class linked lists system, then it'd work like Snap I am working on local lists, in the same project as local vars. What do you mean by returning the “list” (i.e. an item from the list, or the concatenation of all the items with spaces between, or something like in Snap!'s one. I'd recommend checking out Jonathan50's project for it, but perhaps a whole linked list implementation is a bit overkill for the project.
An example list in JS-syntax would look something like this:
[
“”, // Null, used for the last items in lists
“item 3”,
1,
“item 2”,
2,
“item 1”,
4
]
The nice things about these are that you can keep multiple of these linked lists in one Scratch list, that it's easy to make custom blocks replicating the function of normal Scratch lists and that you can pass them around in functions - wherever you would pass an anonymous list, instead pass the index of the first item in your linked list!
- BabyChewie
-
91 posts
Chess AI
I need it to return the entire list, separated by a character, which can be a comma. -snip-