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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

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

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?
    Last edited by zagi; July 27th, 2014 at 01:02 PM.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default 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. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    GregBrannon, sorry I was added few comments in english.

    Thanks for yours reply

  5. #5
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Min-Max AI works only when depth = 1

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

    I am not quite sure what you are trying to do with the code - are you
    trying to re-write the game in Java or just a solution solver? If it is
    the latter is this going to be an algorithm that fits into the main
    game itself?

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  6. #6
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Min-Max AI works only when depth = 1

    I made my own version of this game and I want to implement min-max alghoritm, please check my code.

Similar Threads

  1. min and max value of int
    By Oldemor in forum Java Theory & Questions
    Replies: 13
    Last Post: October 22nd, 2013, 05:43 AM
  2. Min/Max of fields
    By jbraque in forum Object Oriented Programming
    Replies: 1
    Last Post: May 27th, 2013, 03:58 PM
  3. Replies: 9
    Last Post: December 5th, 2012, 05:03 PM
  4. Multi-D array. daily max/min/avg, weekly max/min/avg, sum total (99% done)
    By TheWhopper858 in forum Collections and Generics
    Replies: 1
    Last Post: November 6th, 2011, 08:50 PM
  5. Need help with C++ min and max heaps.
    By javapenguin in forum Other Programming Languages
    Replies: 6
    Last Post: October 19th, 2011, 11:08 AM