1

I'm creating a textbased noughts and crosses game in java and want to determine winning situations. I have used an array for the board such as String [] board :-

[0][1][2]

[3][4][5]

[6][7][8]

I have a whole list of possibilities that checks if 3 values in a row are the same in if statements.

This is an example of what i have:

if ((board [0] != "" && board [0] == board [1] && board [1] == board [2])) {
return true
}

Is there a way that does the same thing with a less amount of code?

Thanks

2
  • if it is string you should use equals method for comparison instead of board [0] != "" Commented Nov 9, 2011 at 22:28
  • 4
    First step. Know what == on strings really means in Java.... Commented Nov 9, 2011 at 22:28

2 Answers 2

2

Your idea can be made sound either by

  • Using .equals() instead of == if you are really storing strings, or
  • Store characters instead of strings, in which case == is safe.

Now if you are really making a noughts and crosses game, you have 8 different winning conditions, which, if you grow your current style of coding will have the form (here I am assuming characters, not strings):

winner = 
    (b[0] != ' ' && b[0] == b[1] && b[1] == b[2]) || 
    (b[3] != ' ' && b[3] == b[4] && b[4] == b[5]) ||
    ...
    (b[0] != ' ' && b[0] == b[4] && b[4] == b[8]);

There are other ways to do this; I'm sure a google search for tic-tac-toe or naughts and crosses implementations will show you quite a few.

If you would like to get fancy, there is a well-known technique of "labeling" each cell with a power of two. Then by adding up the scores (i.e., looking at the player's bit vector) and doing a binary AND on the set of eight winning conditions you can determine a winning position in one shot.

Here is a comment block for a tic-tac-toe game that illustrates the technique. (You didn't ask for actual code, so I'm withholding that from the answer):

/*
 * To determine a win condition, each square is "tagged"
 * from left to right, top to bottom, with successive
 * powers of 2.  Each cell thus represents an individual
 * bit in a 9-bit string, and a player's squares at any
 * given time can be represented as a unique 9-bit value.
 * A winner can thus be easily determined by checking
 * whether the player's current 9 bits have covered any
 * of the eight "three-in-a-row" combinations.
 *
 *     273                 84
 *        \               /
 *          1 |   2 |   4  = 7
 *       -----+-----+-----
 *          8 |  16 |  32  = 56
 *       -----+-----+-----
 *         64 | 128 | 256  = 448
 *       =================
 *         73   146   292
 *
 */
var wins = [7, 56, 448, 73, 146, 292, 273, 84];

This was JavaScript. For Java, use

private static final int[] WINS = new int[]{7, 56, 448, 73, 146, 292, 273, 84};

Apologies if the binary logic approach here is not what you want; I thought it would be a good place to show it off though, in case others land on this page.

Sign up to request clarification or add additional context in comments.

2 Comments

+1, it may not be what the OP was expecting, but it does do the same thing with less code
Thanks for the answer... does help. i know i didnt as for the code, but having seen this im curious to see the code. any chance you can share that? Thanks
0

How are you storing your data? As Strings? "X" and "O"? or as numbers? 0, 1?

Like others said if you use Strings (an object) the == operator will not work!. You'd want to use board[0].equals(....) for == and !board.equals(...) for !=.

Having said that, using Strings is really not flexable for testing scenarios. I did tic-tac-toe in highschool so I know your pain. Using copy & paste it really doesn't take that long to write a large if-statement to check all rows/columns/diagnals. Five or ten minutes tops.

If you're more interested in compact code you might want to consider using numbers since you can use math to check conditions...

if (row is not empty) and
((sum of indexes in row == 3) player 1 wins) or
(sum of indexes in row == 0) player 2 wins
)

Get the idea?

Also, one tip I would suggest is using a two dimensional array (String[][]) so you can check on rows/columns easier!

3 Comments

Or better use equalsIgnoreCase() if by any chance "X" and "O" are being stored as "x" and "o"...Greater flexibility...
because of my beginner compentency in java, im using single arrays just to get an idea on it. when you say (sum of indexes in row == 3), how would you code the fact that 'sum of indexes in row' as there are 8 possible winning situations available? Thanks
@uncleB Sorry... when I talk about the math/integer based approach I was just giving an example off the top of my head. There are alternatives posted around I'm sure you can find if you google "tic-tac-toe code". My code example is in regards to taking the 3 squares in a row (say array elements, 0, 1, 2 for the top row) and then adding together their contents. (board[0]+board[1]+board[2]) and checking the value. If it is 3 then the "1" player won, if its 0 the "0" player won. If its either 1 or 2 then the row has some other combination that isn't a win...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.