Need good logic for TicTacToe

I am using a TicTacToe GUI application using Java Swing. Current win logic:

JButton[] button = new JButton[9]; boolean win = false; //logic for player win if (button[0].getText() == button[1].getText() && button[1].getText() == button[2].getText() && button[0].getText() != "") { Won = true; } else if (button[3].getText() == button[4].getText() && button[4].getText() == button[5].getText() && button[3].getText() != "") { Won = true; } else if (button[6].getText() == button[7].getText() && button[7].getText() == button[8].getText() && button[6].getText() != "") { Won = true; } else if (button[0].getText() == button[3].getText() && button[3].getText() == button[6].getText() && button[0].getText() != "") { Won = true; } else if (button[1].getText() == button[4].getText() && button[4].getText() == button[7].getText() && button[1].getText() != "") { Won = true; } else if (button[2].getText() == button[5].getText() && button[5].getText() == button[8].getText() && button[2].getText() != "") { Won = true; } else if (button[0].getText() == button[4].getText() && button[4].getText() == button[8].getText() && button[0].getText() != "") { Won = true; } else if (button[2].getText() == button[4].getText() && button[4].getText() == button[6].getText() && button[2].getText() != "") { Won = true; } 

It looks a little awkward. Is there a better logic for winning a player?

+2
source share
1 answer

So, you have a (essentially) 2D matrix (ok, this is virtual, but it makes it easier to think) ...

What you need is a tool with which you can search for three cells of this matrix, starting from a given row and column.

So, given the starting point, you also need to provide information about which direction you want to search (how sometimes you want to look back), maybe something like ...

 public boolean matrixWin(int row, int col, int rowDelta, int colDelta) { boolean win = false; String value = button[(row * 3) + col].getText(); if (!value.isEmpty()) { win = true; for (int count = 1; count < 3; count++) { row += rowDelta; col += colDelta; String test = button[(row * 3) + col].getText(); if (test.isEmpty() || !test.equals(value)) { win = false; break; } } } return win; } 

This basically gets the value of the first cell, if it is not empty, it starts moving around the matrix based on the delta values ​​and checks every other cell to see if it is empty or if it matches the value of the first cell.

Well, that’s cool, but I’m a way to be lazy to try to configure EVERY permutation that I could check, so instead I will do some helper methods ...

Basically, you want to check three rows for horizontal winning, three columns for vertical winning and left or right diagonals ...

 public boolean horizontalWin(int row) { return matrixWin(row, 0, 0, 1); } public boolean verticalWin(int col) { return matrixWin(0, col, 1, 0); } public boolean leftDiagonalWin() { return matrixWin(0, 0, 1, 1); } public boolean rightDiagonalWin() { return matrixWin(0, 2, 1, -1); } 

But even then, if I do not want to know which line / col / diagonal ACTUALLY won, I would make it even easier ...

 public boolean horizontalWins() { int row = 0; boolean win = false; do { win = horizontalWin(row); row++; } while (row < 3 && !win); return win; } public boolean verticalWins() { int col = 0; boolean win = false; do { win = verticalWin(col); col++; } while (col < 3 && !win); return win; } 

Then you can go to ...

 public boolean didWin() { return horizontalWins() || verticalWins() || leftDiagonalWin() || rightDiagonalWin(); } 

Yes, one method call, but you still have the ability to determine exactly how

+3
source

All Articles