Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's a bit verbose, but I think this is probably the most efficient way to do it (unless someone can come up with a clever way to check both diagonals at once).</p> <pre><code>public class TicTacToe { char[][] ticTacToe = {{'X',' ','O'}, {'O','X','O'}, {'X',' ','X'},}; private Character winner = null; public Character getWinner() { return this.winner; } public boolean isSolved() { this.checkSolved(); return this.winner != null; } private void checkSolved() { for(int i = 0; i &lt; ticTacToe.length; i++) { Character win = checkRow(i); if(win != null || (win = checkColumn(i)) != null) { this.winner = win; return; } } //Check diagonal top left to bottom right if(this.ticTacToe[0][0] != ' ') { if(this.ticTacToe[0][0] == this.ticTacToe[1][1] &amp;&amp; this.ticTacToe[1][1] == this.ticTacToe[2][2]) { this.winner = this.ticTacToe[0][0]; } } //Check diagonal top right to bottom left else if(this.ticTacToe[0][2] != ' ') { if(this.ticTacToe[0][2] == this.ticTacToe[1][1] &amp;&amp; this.ticTacToe[1][1] == this.ticTacToe[2][0]) { this.winner = this.ticTacToe[0][2]; } } } private Character checkRow(int row) { if(this.ticTacToe[row][0] == ' ') { return null; } if(this.ticTacToe[row][0] == this.ticTacToe[row][1] &amp;&amp; this.ticTacToe[row][1] == this.ticTacToe[row][2]) { return this.ticTacToe[row][0]; } return null; } private Character checkColumn(int column) { if(this.ticTacToe[0][column] == ' ') { return null; } if(this.ticTacToe[0][column] == this.ticTacToe[1][column] &amp;&amp; this.ticTacToe[1][column] == this.ticTacToe[2][column]) { return this.ticTacToe[column][0]; } return null; } public static void main(String[] args) { TicTacToe ttt = new TicTacToe(); if(ttt.isSolved()) { System.out.println(ttt.getWinner()); // X } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload