Results 1 to 2 of 2
  1. #1
    Joni777 is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default Problem with minmax on X O(tic tac toe) android game eclipse

    Hello,

    I created as class project an app of X O(tic tac toe) for player vs player.
    I wanted to upgrade it to player vs unbeatable PC so i have searched for it online and found the min max algorithm and translated it to java android.
    Its currently working as player begin and always X.
    I'm having few bugs that i didn't mange to fix and i hope someone here may be AWESOME and help me please with the problems.

    Currently two problems I encounter with are:
    First one is that when i click X on a corner the PC replay and than i do on the corner of the other side Like this
    |X| | |
    | |O| |
    | | |X
    Its somehow complete all the board with X and O.

    Second Problem is when i play and the PC got to a 2 in a row and almost win and than i cannot click any button.
    If i click any button nothing happen

    Please someone help me! I don't understand what is the problem :(:(

    here is the code+ if someone wants i can send him rar with the whole project. Open it with Eclipse its built with Android 4.1.2

    +Sorry for bad English lol

    Java Code:

    Java Code:
    public class TicTacToeCom extends Activity implements OnClickListener {
    
    	private Button[] bt;
    	private TextView tvResults,tvTurn;
    	private String turn="X";
    	private int WinX=0,WinO=0;
    	private int played=0;
    	private Button ClearBoardB,ClearResultsB,BackToMenuB;
    	private Intent back;
    	private int[][] winCombos = new int[][] //Win Combonation Squares
    		    {
    		        {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
    		        {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //virticle wins
    		        {0, 4, 8}, {2, 4, 6}             //diagonal wins
    		    };
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_tic_tac_toe_com);
    		back=new Intent(this,ChooseAGame.class);
            BackToMenuB=(Button)findViewById(R.id.BackToMenuB);
            BackToMenuB.setOnClickListener(this);
            ClearBoardB=(Button)findViewById(R.id.ClearBoardB);
    		ClearBoardB.setOnClickListener(this);
    		ClearResultsB=(Button)findViewById(R.id.ClearResultsB);
    		ClearResultsB.setOnClickListener(this);
    		tvTurn=(TextView)findViewById(R.id.tvTurn);
    		tvResults=(TextView)findViewById(R.id.tvResults);
    		bt=new Button[9];
    		bt[0]=(Button)findViewById(R.id.Button01);
    		bt[1]=(Button)findViewById(R.id.Button02);
    		bt[2]=(Button)findViewById(R.id.Button03);
    		bt[3]=(Button)findViewById(R.id.Button04);
    		bt[4]=(Button)findViewById(R.id.Button05);
    		bt[5]=(Button)findViewById(R.id.Button06);
    		bt[6]=(Button)findViewById(R.id.Button07);
    		bt[7]=(Button)findViewById(R.id.Button08);
    		bt[8]=(Button)findViewById(R.id.Button09);
    		for(int i=0;i<9;i++)
    		{
    			bt[i].setOnClickListener(this);
    		}
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.activity_tic_tac_toe_com, menu);
    		return true;
    	}
    
    	@Override
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		
    		if(v==BackToMenuB)
    			startActivity(back);
    		else
    		{
    			if(isWin())
    			{
    				for(int k=0;k<9;k++)
    				{
    						bt[k].setClickable(false);
    				}
    				if(turn.toString()=="X")
    				{
    					Toast.makeText(this,("O won"), Toast.LENGTH_LONG).show();
    					WinO++;
    				}
    				else
    				{
    					Toast.makeText(this,("X won"), Toast.LENGTH_LONG).show();
    					WinX++;
    				}
    				tvResults.setText(WinX + " : " + WinO);
    			}
    			else
    			{
    				if((played==9)&&(!isWin()))
    					Toast.makeText(this,("Tie"), Toast.LENGTH_LONG).show();
    				else
    				{
    					if(v==ClearBoardB)
    					{
    						for(int i=0;i<9;i++)
    						{
    								bt[i].setText("");
    								bt[i].setTextColor(Color.BLACK);
    								bt[i].setClickable(true);
    								played=0;
    						}
    					}
    					else
    					{
    						if(v==ClearResultsB)
    						{
    							WinX=0;
    							WinO=0;
    							tvResults.setText(WinX + " : " + WinO);
    						}
    						else
    						{
    							if(turn=="X")
    							{
    								for(int i=0;i<9;i++)
    								{
    									if(v==bt[i])
    									{
    										
    											bt[i].setText(turn);
    											bt[i].setClickable(false);
    											played++;
    											turn="O";
    											tvTurn.setText("O Turn!");
    										
    									}
    								}
    								if(played<9)
    								{
    									Toast.makeText(this,("unbeatableAI"), Toast.LENGTH_SHORT).show();
    									unbeatableAI();
    									return;
    								}
    								return;
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    	public Boolean isWin()
    	{
    		if ((bt[0].getText()==bt[4].getText())&&(bt[4].getText()==bt[8].getText())&&(bt[0].getText()!=""))
    		{
    			bt[0].setTextColor(Color.YELLOW);
    			bt[4].setTextColor(Color.YELLOW);
    			bt[8].setTextColor(Color.YELLOW);
    			return true;		
    		}
    		if ((bt[6].getText()==bt[4].getText())&&(bt[4].getText()==bt[2].getText())&&(bt[6].getText()!=""))
    		{
    			bt[6].setTextColor(Color.YELLOW);
    			bt[4].setTextColor(Color.YELLOW);
    			bt[2].setTextColor(Color.YELLOW);
    			return true;		
    		}
    		for (int i=0;i<7;i=i+3)
    			if ((bt[i].getText()==bt[i+1].getText())&&(bt[i+1].getText()==bt[i+2].getText())&&(bt[i+0].getText()!=""))
    			{
    				bt[i].setTextColor(Color.YELLOW);
    				bt[i+1].setTextColor(Color.YELLOW);
    				bt[i+2].setTextColor(Color.YELLOW);
    				return true;		
    			}
    		for (int i=0;i<3;i++)
    			if ((bt[i].getText()==bt[i+3].getText())&&(bt[i+3].getText()==bt[i+6].getText())&&(bt[i].getText()!="")){
    				bt[i].setTextColor(Color.YELLOW);
    				bt[i+3].setTextColor(Color.YELLOW);
    				bt[i+6].setTextColor(Color.YELLOW);
    				return true;		
    			}
    		return false;
    	}
    	public void unbeatableAI()
        {
            Button temp[] = cloneButtons(bt);
            
            //Increment Count
            played++;
            
            //Checks to see if all of the spots are gone
            if (played >= 9)
                return;        //End the Turn
            else
    	    {
    	        if (played == 2)
    	        {
    	        	Toast.makeText(this,("firstMove()"), Toast.LENGTH_SHORT).show();
    	            firstMove();
    	            turn="X";
    	            return;
    	        }
    	        else
    	        {
    		        if (possibleWin("O",bt) != -1)
    		        {
    		            int i = possibleWin("O", bt);
    		            bt[i].setText("O");
    		            bt[i].setClickable(false);
    		            return;
    		        }
    		        else
    		        {
    			        if (possibleWin("X",bt) != -1)
    			        {
    			            int i = possibleWin("X", bt);
    			            bt[i].setText("O");
    			            bt[i].setClickable(false);
    			            return;
    			        }
    			        else
    			        {
    				        int max = -1;
    				        int best = 0;
    				        for (int x = 0; x < 9; x++)
    				        {
    				            if (temp[x].isClickable() && minimax(temp, "O") >= max)
    				            {
    				                max = minimax(temp, "O");
    				                System.out.println(max);
    				                best = x;
    				            }
    				        }
    				        bt[best].setText("O");
    				        bt[best].setClickable(false);
    				        turn="X";
    				        return;
    			        }
    		        }
    	        }
    	   }
        }
    	public void firstMove()
        {
            if( bt[0].getText()==("X") || bt[2].getText()==("X") || bt[6].getText()==("X") || bt[8].getText()==("X"))
            {
            	bt[4].setText("O");
            	bt[4].setClickable(false);
                return;
            }
            if( bt[1].getText()==("X"))
            {
            	bt[0].setText("O");
            	bt[0].setClickable(false);
                return;
            }
            if(  bt[3].getText()==("X"))
            {
            	bt[0].setText("O");
            	bt[0].setClickable(false);
                return;
            }
            if(  bt[5].getText()==("X"))
            {
            	bt[4].setText("O");
            	bt[4].setClickable(false);
                return;
            }
            if( bt[7].getText()==("X"))
            {
            	bt[6].setText("O");
            	bt[6].setClickable(false);
                return;
            }
            if(bt[4].getText()==("X"))
            {
            	bt[0].setText("O");
            	bt[0].setClickable(false);
                return;
            }
        }
    	public int possibleWin(String player, Button[] temp)
        {
            for(int x = 0; x < 8; x++)
            {
                //Check Place 1
                if( temp[winCombos[x][0]].getText()==(temp[winCombos[x][1]].getText()) && 
                    temp[winCombos[x][0]].getText()==(player) && temp[winCombos[x][2]].getText()==(""))
                    return winCombos[x][2];
                        
                //Check Place 2
                if( temp[winCombos[x][1]].getText()==(temp[winCombos[x][2]].getText()) && 
                    temp[winCombos[x][1]].getText()==(player) && temp[winCombos[x][0]].getText()==(""))
                    return winCombos[x][0];
                    
                //Check Place 3
                if( temp[winCombos[x][0]].getText()==(temp[winCombos[x][2]].getText()) && 
                    temp[winCombos[x][0]].getText()==(player) && temp[winCombos[x][1]].getText()==(""))
                    return winCombos[x][1];
            }
            return -1;
        }
    	public Button[] cloneButtons(Button[] bt) 
    	{
            Button[] clones = new Button[bt.length];
            for(int i=0; i<bt.length; i++)
            {
            	clones[i]=bt[i];
                clones[i].setText(bt[i].getText());
                clones[i].setClickable(bt[i].isEnabled());
            }
            return clones;
        }
    	public int minimax(Button[] temp, String turn)
        {
            int min,max,val=0;
            min = 1;
            max = -1;
            //Check Wins Or Ties
            if(isWin())
    		{
    			if(turn.toString()=="X")
    				return 1;
    			else
    				return -1;
    		}
            else
            {
    			if((played==9)&&(!isWin()))
    				return 0;
    			else
    			{
    		        //Check Max
    				if(turn=="O")
    				{
    					for (int x = 0; x < 9; x++)
    					{
    			            if (possibleWin("O",temp) != -1)
    			            {
    			                int i = possibleWin("O", temp);
    			                temp[i].setText("O");
    			                temp[i].setClickable(false);
    			                return 1;
    			            }
    			            else 
    			            {
    			            	if (possibleWin("X", temp) != -1)
    			            	{
    			                    int i = possibleWin("X", temp);
    			                    temp[i].setText("O");
    			                    temp[i].setClickable(false);
    			                    val = minimax(temp, "X");
    			                    if (val > max)
    			                        val = max;
    			                }
    			            	else 
    			            	{
    			            		if (temp[x].getText()==(""))
    			            		{
    			                        temp[x].setText("O");
    			                        temp[x].setClickable(false);
    			                        val = minimax(temp, "X");
    			                        if (val > max)
    			                            val = max;
    			                    }
    			            	}
    			            }
    					}
    				}
    				else
    				{
    					for (int y = 0; y < 9; y++)
    					{
    		                if (possibleWin("X",temp) != -1)
    		                {
    		                    int i = possibleWin("X", temp);
    		                    temp[i].setText("X");
    		                    temp[i].setClickable(false);
    		                    return -1;
    		                }
    		                else 
    		                {
    		                	if (possibleWin("O", temp) != -1)
    		                	{
    		                        int i = possibleWin("O", temp);
    		                        temp[i].setText("X");
    		                        temp[i].setClickable(false);
    		                        val = minimax(temp, "O");
    		                        if (val < min)
    		                            val = min;
    		                    }
    		                	else 
    		                	{
    		                		if (temp[y].getText()==(""))
    		                		{
    		                            temp[y].setText("X");
    		                            temp[y].setClickable(false);
    		                            val = minimax(temp, "O");
    		                            if (val < min)
    		                                val = min;
    		                        }
    		                	}
    		                }
    					}
    				}
    			}
    			
    		}
    		
            return val;
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Problem with minmax on X O(tic tac toe) android game eclipse

    Also StackOverflow

    Please, Joni777, if you start a discussion in multiple places it is polite to post a link at each to the others. That way everyone taking part in a discussion knows what else is being said. be aware that some people will not reply to a cross post for fear of wasting their time addressing something that has been dealt with elsewhere.

    If your program is not doing what you expect you need to debug it. Toast or log messages to the console so that you can see which lines of code are being executed as you reproduce the problem. Check that the expressions in the code have the value you expect them to have. In short, narrow down the problem.

    Consider using short methods, and document what they are intended to do. That makes it easier for others here to understand your code, but it also makes the process of debugging your code more straightforward. Each comment is something specific to test.

    ---

    All that said, you appear to be comparing strings with ==.

    Java Code:
    if(turn.toString()=="X")
    Don't do that. To see if two strings are equal - ie consist of the same characters in the same order - use the equals() method because that is what it is defined to do. == does something else.

    Java Code:
    if(turn.toString().equals("X"))

Similar Threads

  1. My very first game for Android – Range Racer – TOP OR FLOP ???
    By Stan Stone in forum Reviews / Advertising
    Replies: 0
    Last Post: 07-15-2012, 10:06 AM
  2. Eclipse vs Netbeans for android game
    By beast in forum New To Java
    Replies: 5
    Last Post: 12-30-2011, 09:48 PM
  3. Simple Android Game Design
    By fresh83 in forum New To Java
    Replies: 1
    Last Post: 05-16-2011, 06:40 AM
  4. Android on Eclipse
    By cselic in forum Eclipse
    Replies: 2
    Last Post: 05-20-2010, 02:40 PM
  5. minmax with Alpha - Beta Pruning
    By Zosden in forum Advanced Java
    Replies: 6
    Last Post: 05-02-2008, 09:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •