potatoeggy / connectfour

A re-implementation of the popular Connect 4 game
GNU General Public License v3.0
2 stars 0 forks source link

The AI is stupid #29

Closed vincentjguo closed 4 years ago

vincentjguo commented 4 years ago

helphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelphelp I need to raise the AI's IQ but how

potatoeggy commented 4 years ago

increase its search depth

vincentjguo commented 4 years ago

that just makes it slower at being stupid

potatoeggy commented 4 years ago

verify your algorithm? is it the correct algorithm?

vincentjguo commented 4 years ago

probably not, i just realized today that i flipped the AI and player so the AI chose the worst possible option

vincentjguo commented 4 years ago

this is what happens when you code 200 lines without looking back

potatoeggy commented 4 years ago

isn't there a way to invert the scores?

vincentjguo commented 4 years ago

multiply by -1 but i fixed it, it's fine now. But there are still some problems with it

potatoeggy commented 4 years ago

Is the AI functional right now? Can I call a function that gives me a decision?

vincentjguo commented 4 years ago

you still need to process it, it will return an arraylist with a list of scores for each position. I can create a function that can process it.

potatoeggy commented 4 years ago

image

Hmmmmm This appears for both Normal and Hard (depths of 3 and 4)

It's really slow. There's very noticeable lag and I have a fairly powerful machine (Ryzen 5 4500U).

potatoeggy commented 4 years ago

(I'm red)

vincentjguo commented 4 years ago

is this on master?

vincentjguo commented 4 years ago

oh by the way the AI has to go first, the player parameter must always be 1

potatoeggy commented 4 years ago

It's in the ai-gui testing branch

On Jun 1 2020, at 7:56 pm, Vincent Guo notifications@github.com wrote:

is this on master? — You are receiving this because you commented. Reply to this email directly, view it on GitHub (https://github.com/potatoeggy/connectfour/issues/29#issuecomment-637187113), or unsubscribe (https://github.com/notifications/unsubscribe-auth/AGADGXV3DHJLNRV6X5KN44LRUQ54LANCNFSM4NQFEJVA).It's

vincentjguo commented 4 years ago

ok

potatoeggy commented 4 years ago

right now the identifiers used for players are 1 for player 1, and -1 for player 2

On Jun 1 2020, at 7:58 pm, Vincent Guo notifications@github.com wrote:

ok — You are receiving this because you commented. Reply to this email directly, view it on GitHub (https://github.com/potatoeggy/connectfour/issues/29#issuecomment-637187528), or unsubscribe (https://github.com/notifications/unsubscribe-auth/AGADGXTHKBBT7LKH4JH3I6TRUQ6CDANCNFSM4NQFEJVA).

vincentjguo commented 4 years ago

I have it as player1/AI is 1 and player 2 is 2

vincentjguo commented 4 years ago

Damn it we should have coordinated this better

potatoeggy commented 4 years ago

What's the player parameter used for in AI ?

On Jun 1 2020, at 8:01 pm, Vincent Guo notifications@github.com wrote:

Damn it we should have coordinated this better — You are receiving this because you commented. Reply to this email directly, view it on GitHub (https://github.com/potatoeggy/connectfour/issues/29#issuecomment-637188368), or unsubscribe (https://github.com/notifications/unsubscribe-auth/AGADGXXQUH6WIKWVVG75HFDRUQ6NHANCNFSM4NQFEJVA).

vincentjguo commented 4 years ago

So basically every time a score is generated, it could either be negative or positive or 0. Negative means P2 has an advantage, positive means the AI has an advantage and 0 is a tie. The AI uses a minimizer and maximizer to figure out what is the best possible move for each side, hence the name minimax as it tries to minimize its losses and maximize its wins. The player parameter basically tells the AI whose turn it is and use the corresponding min/max.

vincentjguo commented 4 years ago

If you want the AI to go second, call the function after the player moves

potatoeggy commented 4 years ago

image

potatoeggy commented 4 years ago

AI is still stupid

potatoeggy commented 4 years ago

image

potatoeggy commented 4 years ago

Fixed in 661f221c7042fe30b738282bf45d7855ab7dba75

potatoeggy commented 4 years ago

image ???

Normal difficulty (depth 4)

vincentjguo commented 4 years ago

is the ai red?

potatoeggy commented 4 years ago

They're both AIs

Also, the AI tries to make groups of threes even if a group of four would go outside the board

vincentjguo commented 4 years ago

that... shouldn't happen

vincentjguo commented 4 years ago

Can you send the code you used to make the AI fight each other? It would help a lot with debugging

vincentjguo commented 4 years ago

One more question, what's the issue with this?

potatoeggy commented 4 years ago

I just set both dropdowns to computer

the issue is that the ai is dumb and kinda slow. It seems to take exponentially longer after every turn even against humans.

potatoeggy commented 4 years ago

here's the relevant code chunk in MainWindow:

while (win.gameWindow.isVisible() && !win.gameWindow.isGameOver()) { // do not run timer when game is not ongoing
    if (win.gameWindow.getLock()) { // if event thread is waiting for ai calculation
        win.moveTimerInternal = win.moveTimerFull;
        win.gameWindow.setTimer(win.moveTimerInternal);
        int col = AI.bestColumn(win.gameWindow.getBoard(), win.gameWindow.getDifficulty()); // calculate in different thread so we don't hang the main event thread
        win.gameWindow.toggleLock(); // disable lock
        win.gameWindow.toggleAllButtons();
        win.gameWindow.sendClick(col); // send click
        responseTimer = 0;
vincentjguo commented 4 years ago

i assume that's because of the win checking thing method I used. I kinda just said screw time constraints and looped through every single token to find a win. Really bad method but it works

potatoeggy commented 4 years ago

Yeah, even in normal difficulty it starts to take tens of seconds after just 20 or so chips.

vincentjguo commented 4 years ago

ya AB pruning would help, but I should also find a way to optimize the win checking thing

potatoeggy commented 4 years ago

try optimising by only checking if the last piece placed gives a win

vincentjguo commented 4 years ago

Yes I tried doing that but the algo is made in a way where only at depth 0 could scoreGen/winchecking be called. So if at any other depth there is a win, it won't find it

vincentjguo commented 4 years ago

Im probably going to make a visited array, and for every 4 tokens in every direction call winchecking again. This would reduce the time to check for a win by 4.

vincentjguo commented 4 years ago

AI is less stupid as of 66958ad

potatoeggy commented 4 years ago

"Stupid" is subjective, anyway, and there isn't any concrete issue that we can determine we've fixed or not. Smaller, more clear issues can be used in the future. Closing due to vagueness.

vincentjguo commented 4 years ago

Yep this was just an issue for me to rage about my stupidity lul.