• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Card Game AI

F

foxroar1

Guest
Hi everyone,

I need some help/suggestions for how to tackle AI for a card game.

I'm using a single card object that gets it's image_index (which is used to find it's card number and suit) from a list.

This card object can be moved to 4 different areas in the room.

I believe I want to use an AI object that can compare cards variables and then move it to the next appropriate area.

When a card is created I have its id stored in the array p2picksort[i,0] so I can access everything from the ai object.

But the problem I'm running into is sorting info for comparisons but still knowing which card id each variable belongs to.

Here's an example of my train of thought:

Code:
pick=ds_list_create()

if (p2spellpower==0) //if 0 spell power, choose highest spell power card
{
     for (i=1; i<=p2pickcards; i+=1) //usually three p2pickcards but not always
     {
     if (p2picksort[i,0].type==2) {ds_list_add(pick, p2picksort[i,0].cardnum)} //add each card # to list if it's type is spell power. p2picksort[i,0] is the id of each card
     }
ds_list_sort(pick,false) //sort in descending order
}
All I've done is add a cards variable cardnum to a list and sorted it in descending order so that first one in the list is the highest cardnum. But then how on earth would I know which card id is associated with that variable cardnum? It's like I've detached important info about the cards but don't know how to associate it back with the correct id.

I just need some ideas or help with maybe a better way to do this.

Thanks!
 

CloseRange

Member
is p2picksort[i,0].cardnum all unique id's? if so I don't see the problem:
Code:
var n = ds_list_find_index(pick, p2picksort[i, 0].cardnum);
that will show the index of that card, even after it gets sorted. So if n was 0 then that card has the highest spell power
 
F

foxroar1

Guest
Yes, p2picksort[i,0] holds all unique ids. I'll keep working through it, maybe I'm doing a better job than I thought.
 
F

foxroar1

Guest
I'm baffled as to why this for loop only does a half transfer.

If deck2 is 18, it will only transfer 9 of its' positions into deck1.

Code:
n=ds_list_size(deck2) //set n to size of deck2
for(i=0;i<n;i+=1)
{
ds_list_add(deck,ds_list_find_value(deck2,i)) //Add cards to deck list
ds_list_delete(deck2,i)
}
repeat(4) ds_list_shuffle(deck); //Shuffle it
 
H

Homunculus

Guest
Well that loop is simply wrong. You are iterating N number of times, where n is the INITIAL size of deck2, and at every iteration you DELETE an item from the very list you are iterating over, reducing its size. After the first iteration, you no longer have have 18 cards, but your loop will still try to access position 0 to 17 regardless.

When deleting an item from a ds_list, all the next items are shifted back. You can't have "holes".

The solution is fairly simple, clear everything from deck2 after the loop using ds_list_clear(). Or use a repeat(n) instead and check every time at position 0.

Moreover, shuffling 4 times the deck does absolutely nothing more than shuffling it once, why would you even do that?
 
F

foxroar1

Guest
I solved it by doing the list delete in it's own For loop and approaching the parameters of the loop differently. I still don't totally understand it, but something about deleting it from bottom up wasn't working.

Code:
for(i=0;i<ds_list_size(deck2);i+=1)
{
ds_list_add(deck,ds_list_find_value(deck2,i)) //transfer deck2 to deck
}
for (i=ds_list_size(deck2); i>=0; i-=1) //delete all of deck2
{
ds_list_delete(deck2,i)
}
 
F

foxroar1

Guest
Oh okay, didn't even know ds_list_clear was a thing, thanks :)

And for the shuffle 4 times, good point. That was just something I grabbed from some card tutorial a long, long time ago. You're right, it makes no sense.
 
Top