Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java - Trying to simply compare three Jtextfields

Options
  • 25-03-2012 5:17pm
    #1
    Registered Users Posts: 266 ✭✭


    I'm trying to find the best way to compare three textfields for a Tic Tac Toe game.
    I have the textfields declared as an array inputText[9]

    At present I'm using the code below to check for three x's on the top horizontal row.
    This is the code:
    if((inputText[0].getText()).equalsIgnoreCase("x") && (inputText[1].getText()).equalsIgnoreCase("x") && (inputText[2].getText()).equalsIgnoreCase("x"))
    {xWin= true;}


    else if((inputText[0].getText()).equalsIgnoreCase("o") && (inputText[1].getText()).equalsIgnoreCase("o") && (inputText[2].getText()).equalsIgnoreCase("o"))
    {oWin= true;}


    I have this 16 times in totalto check for both x's and o's for a total of 8 times each. Then I have a method for this:

    if (xWins = true and oWins = false)
    then message = x wins;
    else if (oWins = true and xWins = false)
    then message = o wins;

    It works ok but if only insert two x's or o's it outputs a win message.
    Is there a simpler way of doing this?

    Any help with this would be gratefull recieved.
    Thanx
    Ger.


Comments

  • Registered Users Posts: 131 ✭✭CuAnnan


    Why are you binding the logic to the interface? It leads to messy code.

    The text fields should populate an array or object representing the game.
    The logic should then be performed on the array or object.
    Then you can loop through all of the possible win conditions in one loop.


  • Registered Users Posts: 266 ✭✭Gerb68


    CuAnnan wrote: »
    The text fields should populate an array...

    I'm not sure what you mean by this, although what you are saying is exactly the information I am seeking.

    Forgive my ignorance but I have the textfields declared in an array.
    JTextField[] inputText

    Are you saying somehow I should maybe declare another array(let say String[] letters) and try to fill it with inputText.getText()


  • Registered Users Posts: 131 ✭✭CuAnnan


    I'd use a two dimensional array.

    That way what you have on screen represents what you are modelling.
    I did the FÁS Software developper course, we had this exact problem on our course. What I ended up doing was implementing an object that could be populated independently of any interface.


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    Gerb68 wrote: »
    I'm trying to find the best way to compare three textfields for a Tic Tac Toe game.
    I have the textfields declared as an array inputText[9]

    At present I'm using the code below to check for three x's on the top horizontal row.
    This is the code:
    if((inputText[0].getText()).equalsIgnoreCase("x") && (inputText[1].getText()).equalsIgnoreCase("x") && (inputText[2].getText()).equalsIgnoreCase("x"))
    {xWin= true;}


    else if((inputText[0].getText()).equalsIgnoreCase("o") && (inputText[1].getText()).equalsIgnoreCase("o") && (inputText[2].getText()).equalsIgnoreCase("o"))
    {oWin= true;}


    I have this 16 times in totalto check for both x's and o's for a total of 8 times each. Then I have a method for this:

    if (xWins = true and oWins = false)
    then message = x wins;
    else if (oWins = true and xWins = false)
    then message = o wins;

    It works ok but if only insert two x's or o's it outputs a win message.
    Is there a simpler way of doing this?

    Any help with this would be gratefull recieved.
    Thanx
    Ger.

    Probably somewhere among all the if statements the booleans are being set to true, could be a missing && or something. Can you show all code so I can have a look?


  • Registered Users Posts: 266 ✭✭Gerb68


    This class is accompanied by a GUI class for to build GUI interface and a GameMessage class to output messages.

    public class GameLogic{
    static boolean xWin= false;
    static boolean oWin= false;

    public static void checkRowsAndColumns() throws StalemateException, PlayerOutOfTurnException, IncorrectEntryException
    {
    for(int i = 0; i < GUI.inputText.length; i++){
    if((GUI.inputText[0].getText()).equalsIgnoreCase("x") && (GUI.inputText[1].getText()).equalsIgnoreCase("x") && (GUI.inputText[2].getText()).equalsIgnoreCase("x")) //check top horizontal for 3 x's.
    {xWin= true;}

    else if((GUI.inputText[0].getText()).equalsIgnoreCase("o") && (GUI.inputText[1].getText()).equalsIgnoreCase("o") && (GUI.inputText[2].getText()).equalsIgnoreCase("o")) //check top horizontal for 3 o's.
    {oWin= true;}

    else if((GUI.inputText[3].getText()).equalsIgnoreCase("x") && (GUI.inputText[4].getText()).equalsIgnoreCase("x") && (GUI.inputText[5].getText()).equalsIgnoreCase("x")) //check middle horizontal for 3 x's.
    {xWin= true;}

    else if((GUI.inputText[3].getText()).equalsIgnoreCase("o") && (GUI.inputText[4].getText()).equalsIgnoreCase("o") && (GUI.inputText[5].getText()).equalsIgnoreCase("o")) //check middle horizontal for 3 o's.
    {oWin= true;}

    else if((GUI.inputText[6].getText()).equalsIgnoreCase("x") && (GUI.inputText[7].getText()).equalsIgnoreCase("x") && (GUI.inputText[8].getText()).equalsIgnoreCase("x")) //check bottom horizontal for 3 x's.
    {xWin= true;}

    else if((GUI.inputText[6].getText()).equalsIgnoreCase("o") && (GUI.inputText[6].getText()).equalsIgnoreCase("o") && (GUI.inputText[8].getText()).equalsIgnoreCase("o")) //check bottom horizontal for 3 o's.
    {oWin= true;}

    else if((GUI.inputText[0].getText()).equalsIgnoreCase("x") && (GUI.inputText[3].getText()).equalsIgnoreCase("x") && (GUI.inputText[6].getText()).equalsIgnoreCase("x")) //check left vertical for 3 x's.
    {xWin= true;}

    else if((GUI.inputText[0].getText()).equalsIgnoreCase("o") && (GUI.inputText[3].getText()).equalsIgnoreCase("o") && (GUI.inputText[6].getText()).equalsIgnoreCase("o")) //check left vertical for 3 o's.
    {oWin= true;}

    else if((GUI.inputText[1].getText()).equalsIgnoreCase("x") && (GUI.inputText[4].getText()).equalsIgnoreCase("x") && (GUI.inputText[7].getText()).equalsIgnoreCase("x")) //check centre vertical for 3 x's.
    {xWin= true;}

    else if((GUI.inputText[1].getText()).equalsIgnoreCase("o") && (GUI.inputText[4].getText()).equalsIgnoreCase("o") && (GUI.inputText[7].getText()).equalsIgnoreCase("o")) //check centre vertical for 3 o's.
    {oWin= true;}

    else if((GUI.inputText[2].getText()).equalsIgnoreCase("x") && (GUI.inputText[5].getText()).equalsIgnoreCase("x") && (GUI.inputText[8].getText()).equalsIgnoreCase("x")) //check right vertical for 3 x's.
    {xWin= true;}

    else if((GUI.inputText[2].getText()).equalsIgnoreCase("o") && (GUI.inputText[5].getText()).equalsIgnoreCase("o") && (GUI.inputText[8].getText()).equalsIgnoreCase("o")) //check right vertical for 3 o's.
    {oWin= true;}

    else if((GUI.inputText[0].getText()).equalsIgnoreCase("x") && (GUI.inputText[4].getText()).equalsIgnoreCase("x") && (GUI.inputText[8].getText()).equalsIgnoreCase("x")) //check left to right diagonal for 3 x's.
    {xWin= true;}

    else if((GUI.inputText[0].getText()).equalsIgnoreCase("o") && (GUI.inputText[4].getText()).equalsIgnoreCase("o") && (GUI.inputText[8].getText()).equalsIgnoreCase("o")) //check left to right diagonal for 3 o's.
    {oWin= true;}

    else if((GUI.inputText[2].getText()).equalsIgnoreCase("x") && (GUI.inputText[4].getText()).equalsIgnoreCase("x") && (GUI.inputText[6].getText()).equalsIgnoreCase("x")) //check right to left diagonal for 3 x's.
    {xWin= true;}

    else if((GUI.inputText[2].getText()).equalsIgnoreCase("o") && (GUI.inputText[4].getText()).equalsIgnoreCase("o") && (GUI.inputText[6].getText()).equalsIgnoreCase("o")) //check right to left diagonal for 3 o's.
    {oWin= true;}
    }//end of for loop

    if(xWin == true && oWin == false)
    {
    GameMessage.xWinsMessage();
    //return xWin = true;
    }

    else if(oWin == true && xWin == false)
    {
    GameMessage.oWinsMessage();
    }

    }//end of checkRowsAndColumns method
    }//end of class


  • Advertisement
  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    I'm a bit confused about the for loop? You are checking every combination manually, so all you would be doing is checking every combination 8 times?

    Do you need flexibility on board-size? Or will it always be a 3x3 board?

    For a 3x3 board, you could code it something like this:
    private bool isSamePiece(JTextField field1, JTextField field2)
    {
    	return field1.getText().equalsIgnoreCase(field2.getText());
    }
    
    private bool isWinningCombination(int a, int b, int c)
    {
    	if (!GUI.inputText[a].getText().equalsIgnoreCase("x") || !GUI.inputText[a].getText().equalsIgnoreCase("o"))
    	{
    		return false;
    	}
    	
    	return isSamePiece(GUI.inputText[a].getText(), GUI.inputText[b].getText()) && isSamePiece(GUI.inputText[a].getText(), GUI.inputText[c].getText());
    }
    
    public void checkForWin()
    {
    	if (isWinningCombination(0, 1, 2))
    	{
    		GameMessage.WinsMessage(GUI.inputText[0].getText());
    	}
    	else if (isWinningCombination(3, 4, 5))
    	{
    		GameMessage.WinsMessage(GUI.inputText[3].getText());
    	}
    	else if (isWinningCombination(6, 7, 8))
    	{
    		GameMessage.WinsMessage(GUI.inputText[6].getText());
    	}
    	else if (isWinningCombination(0, 3, 6))
    	{
    		GameMessage.WinsMessage(GUI.inputText[0].getText());
    	}
    	else if (isWinningCombination(1, 4, 7))
    	{
    		GameMessage.WinsMessage(GUI.inputText[1].getText());
    	}
    	else if (isWinningCombination(2, 5, 8))
    	{
    		GameMessage.WinsMessage(GUI.inputText[2].getText());
    	}
    	else if (isWinningCombination(0, 4, 8))
    	{
    		GameMessage.WinsMessage(GUI.inputText[0].getText());
    	}
    	else if (isWinningCombination(2, 4, 6))
    	{
    		GameMessage.WinsMessage(GUI.inputText[2].getText());
    	}
    }
    

    This runs on the assumption that you could change WinsMessage to take a string value on who won rather than an individual method for X and O.

    If you wanted to have the ability to do variable board sizes, you would need to check for wins in a loop.


  • Registered Users Posts: 266 ✭✭Gerb68


    Thanx for this help. Its the type of code I was after.
    The for loop was my mistake. At some point I may have thought I was dealing with GUI.inputText and yes it does need to be 3x3.

    The only problem I am having is with understanding this...

    return isSamePiece(GUI.inputText[a].getText(), GUI.inputText.getText()) && isSamePiece(GUI.inputText[a].getText(), GUI.inputText[c].getText());

    I see it as...
    The isWinningCombination() method recieves the three integers a, b, c representing 0, 1 and 2 (taking the first row) from the checkForWin() method then if textfield at index 0 does not equal x or o then return false else it seems to return 4 strings to the isSamePiece() method which seems to take in 2 textfields.


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    Gerb68 wrote: »
    The isWinningCombination() method recieves the three integers a, b, c representing 0, 1 and 2 (taking the first row) from the checkForWin() method then if textfield at index 0 does not equal x or o then return false else it seems to return 4 strings to the isSamePiece() method which seems to take in 2 textfields.

    You are correct about the first if. It checks that the first position to check is either X or O. If it's not, then we can't have 3 in a row, so we return false. It's important to do this check, cause otherwise the return statement will report three blank spaces in each position as a winning combination.

    The return statement can be summarised as:
    If position A's piece equals both position B and position C piece, then we have 3 in a row and we return true.

    The return line calls the method isSamePiece twice (which returns a bool).
    So the call isSamePiece(GUI.inputText[a].getText(), GUI.inputText.getText()) checks that position a == b. If that is true we go to the next the next isSamePiece call and check that position a piece is the same a position c. If both isSamePiece are true, the return expression evaluates as return true && true, which of course evaluates as return true. If one or both of the isSamePiece return false, then false will be returned.


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    Just in case all that didn't make sense, seperated out:
    bool aMatchesB = isSamePiece(GUI.inputText[a], GUI.inputText[b]);
    bool aMatchesC = isSamePiece(GUI.inputText[a], GUI.inputText[c]);
    return aMatchesB && aMatchesC;
    

    Note: I had an error in the previous version, as I was passing in getText() string rather than just the JTextField isSamePiece was expecting. I haven't tested the code.


Advertisement