Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Min-Max AI works only when depth = 1

1. ## Min-Max AI works only when depth = 1

Here is my code:
```public static int minimax(int[][] board, int depth, boolean maximizingPlayer) {
if(depth==0 || (!w && !s && !a && !d))
return ocena(board); // evaluation method
int[] r = wygenerujRuchy(board); // generate all possible moves
if(k2==0) k2 = r[0];
if(maximizingPlayer) {
najWynik = -10000;
for(int i=0; i<r.length; i++) {
for(int k=0; k<game2048.wielkosc; k++) { //copy board
for(int j=0; j<game2048.wielkosc; j++) {
planszaL[k][j] = board[k][j];
}
}
val = minimax(zrobRuch(planszaL,r[i]), depth - 1, false);
if(val>=najWynik) {
k2 = r[i]; // the best move
najWynik = val; // the best score
}
//System.out.println(najWynik);
}
//System.out.println(depth + "\t" + najWynik + "\t" + maximizingPlayer + "\t" + k2);
return najWynik; // return the best score
}
else {
najWynik = 10000; // score
val = minimax(dodajKlocek(board), depth - 1, true);
najWynik = Math.min(najWynik, val); // the worst score
//System.out.println(najWynik);
System.out.println(depth + "\t" + najWynik + "\t" + maximizingPlayer + "\t" + k2);
return najWynik; // return score
}
}```

I made a 2048 game solver and I want to implement mini-max alghoritm.
But it works only when I lunch with depth=1, this isn't good enough for me because the avarage score is 14000-15000 points and the best score is ~65000. I think my evaluation function is very good but I don't why it give random moves when depth>1.
Could you help me?

2. ## Re: Min-Max AI works only when depth = 1

What is a 2048 game solver?
Perhaps you would get a little bit more help if you give us more details about your problem.

3. ## Re: Min-Max AI works only when depth = 1

Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

We're at a bit of a disadvantage. Since there are no comments in English and the variable names are completely foreign, most of us can't understand what your code is supposed to do. I'm not sure we can do anything with what you've given us so far. Are there errors? If so, post them. But I suspect the results are just not as good as you'd like them to be, and I don't know how to help with that without understanding how your algorithm works.

4. ## Re: Min-Max AI works only when depth = 1

Cornix, 2048 this is the simple game. Here is a link to orginal version: 2048
I made my 2048 game i java and i'm trying to add the best artifacial intelligence, but min-max don't work well. This alghoritm should choose the best move in every situation.

5. ## Re: Min-Max AI works only when depth = 1

That's actually a really fun game! Just played the online version