Discuss Scratch

BabyChewie
Scratcher
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:
board
rnbqkbnr
ppppppp
0000000
0000000
0000000
0000000
ppppppp
rnbqkbnr

board-colors
wwwwwwww
wwwwwwww
0000000
0000000
0000000
0000000
bbbbbbbb
bbbbbbbb
The 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.
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
Scratcher
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.

whenIreceiveAIdeleteallofvariable-stack You could have multiple list for multiple varsmyAIBlock1Call it like thisdefinemyAIBlockindexaddtovariable-stack Initialize var (FIRST THING YOU DO!)To set the local variablereplaceitemindexofvariable-stack withvalueTo access the local variableitemindexofvariable-stack To call recursivelymyAIBlocklengthofvariable-stack+1
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
Scratcher
91 posts

Chess AI

jokebookservice1 wrote:

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.

whenIreceiveAIdeleteallofvariable-stack You could have multiple list for multiple varsmyAIBlock1Call it like thisdefinemyAIBlockindexaddtovariable-stack Initialize var (FIRST THING YOU DO!)To set the local variablereplaceitemindexofvariable-stack withvalueTo access the local variableitemindexofvariable-stack To call recursivelymyAIBlocklengthofvariable-stack+1
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
Scratcher
1000+ posts

Chess AI

BabyChewie wrote:

jokebookservice1 wrote:

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.

whenIreceiveAIdeleteallofvariable-stack You could have multiple list for multiple varsmyAIBlock1Call it like thisdefinemyAIBlockindexaddtovariable-stack Initialize var (FIRST THING YOU DO!)To set the local variablereplaceitemindexofvariable-stack withvalueTo access the local variableitemindexofvariable-stack To call recursivelymyAIBlocklengthofvariable-stack+1
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.
What does cly 3 mean?
Tymewalk
Scratcher
1000+ posts

Chess AI

BabyChewie wrote:

jokebookservice1 wrote:

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.

whenIreceiveAIdeleteallofvariable-stack You could have multiple list for multiple varsmyAIBlock1Call it like thisdefinemyAIBlockindexaddtovariable-stack Initialize var (FIRST THING YOU DO!)To set the local variablereplaceitemindexofvariable-stack withvalueTo access the local variableitemindexofvariable-stack To call recursivelymyAIBlocklengthofvariable-stack+1
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.
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.

Unless I'm misunderstanding something

Last edited by Tymewalk (May 9, 2016 17:59:52)

jokebookservice1
Scratcher
1000+ posts

Chess AI

Tymewalk wrote:

BabyChewie wrote:

jokebookservice1 wrote:

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.

whenIreceiveAIdeleteallofvariable-stack You could have multiple list for multiple varsmyAIBlock1Call it like thisdefinemyAIBlockindexaddtovariable-stack Initialize var (FIRST THING YOU DO!)To set the local variablereplaceitemindexofvariable-stack withvalueTo access the local variableitemindexofvariable-stack To call recursivelymyAIBlocklengthofvariable-stack+1
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.
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.

Unless I'm misunderstanding something
The variables need to have multiple instances since they are using recursion
BabyChewie
Scratcher
91 posts

Chess AI

Tymewalk wrote:

BabyChewie wrote:

jokebookservice1 wrote:

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.

whenIreceiveAIdeleteallofvariable-stack You could have multiple list for multiple varsmyAIBlock1Call it like thisdefinemyAIBlockindexaddtovariable-stack Initialize var (FIRST THING YOU DO!)To set the local variablereplaceitemindexofvariable-stack withvalueTo access the local variableitemindexofvariable-stack To call recursivelymyAIBlocklengthofvariable-stack+1
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.
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.

Unless I'm misunderstanding something
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.
BabyChewie
Scratcher
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
Scratcher
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:
whenIreceivestartRecursiondeleteallofstack Do this at the top of the treeAI1must be 1Note: No need to delete the stack if instead of putting "1" as the arg, you put (len(list)+1) insteaddefineAIiaddvar1Valuetostack addvar2Valuetostack replaceitemiofstack withvar1Changedreplaceitemi+1ofstack withvar2Changedsayitemiofstack for2secsvar1Changedsayitemi+1ofstack for2secsvar2ChangedAIlengthofstack+1Recursive call
That is my example implementation of local variables using a list

Last edited by jokebookservice1 (May 9, 2016 18:14:14)

BabyChewie
Scratcher
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
__init__
Scratcher
1000+ posts

Chess AI

BabyChewie wrote:

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
Your AI is a very bad player.

I put the king in check, he left it in check, and then I captured it
jokebookservice1
Scratcher
1000+ posts

Chess AI

__init__ wrote:

BabyChewie wrote:

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
Your AI is a very bad player.

I put the king in check, he left it in check, and then I captured it
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)

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
Scratcher
1000+ posts

Chess AI

Use an auxiliary custom block:
definemyblocknmyblockhelperninitial value for xdefinemyblockhelpernx x is lexically scoped. . . do stuff
(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
Scratcher
91 posts

Chess AI

__init__ wrote:

BabyChewie wrote:

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
Your AI is a very bad player.

I put the king in check, he left it in check, and then I captured it
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).

Jonathan50 wrote:

Use an auxiliary custom block:
definemyblocknmyblockhelperninitial value for xdefinemyblockhelpernx x is lexically scoped. . . do stuff
(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!
I need local lists is the problem though, and I need it to be able to return the list.
jokebookservice1
Scratcher
1000+ posts

Chess AI

BabyChewie wrote:

__init__ wrote:

BabyChewie wrote:

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
Your AI is a very bad player.

I put the king in check, he left it in check, and then I captured it
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).

Jonathan50 wrote:

Use an auxiliary custom block:
definemyblocknmyblockhelperninitial value for xdefinemyblockhelpernx x is lexically scoped. . . do stuff
(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!
I need local lists is the problem though, and I need it to be able to return the list.
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])

Last edited by jokebookservice1 (May 10, 2016 15:53:49)

kvackkvack
Scratcher
500+ posts

Chess AI

jokebookservice1 wrote:

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! [impossible in Scratch])?
He could implement his own first-class linked lists system, then it'd work like 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
Scratcher
1000+ posts

Chess AI

kvackkvack wrote:

jokebookservice1 wrote:

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! [impossible in Scratch])?
He could implement his own first-class linked lists system, then it'd work like 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.
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.
kvackkvack
Scratcher
500+ posts

Chess AI

jokebookservice1 wrote:

kvackkvack wrote:

jokebookservice1 wrote:

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! [impossible in Scratch])?
He could implement his own first-class linked lists system, then it'd work like 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.
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.
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.
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
Scratcher
1000+ posts

Chess AI

kvackkvack wrote:

jokebookservice1 wrote:

kvackkvack wrote:

jokebookservice1 wrote:

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! [impossible in Scratch])?
He could implement his own first-class linked lists system, then it'd work like 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.
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.
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.
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!
I think I get it
BabyChewie
Scratcher
91 posts

Chess AI

jokebookservice1 wrote:

-snip-
I need it to return the entire list, separated by a character, which can be a comma.

Powered by DjangoBB