Comparison of Java ArrayList-TicTacToe

I am trying to make a very simple Tic-Tac-Toe game. I saved the values ​​of "X" and "O" in 0-8 ArrayList (essentially a 3x3 square). I can do the following for each example of a winning situation:

if ((newBoard().get(0)).equals("X") &&
    (newBoard().get(1)).equals("X") && 
    (newBoard().get(2)).equals("X")){
System.out.println("Player-X has won!");
return true;

However, it will take the TONE code! I was thinking of creating new ArrayLists that contain situations in which “X” won (3-in-roe), then copy and paste, replace “X” with “O”, and then compare them with the current ArrayList board, which user "interacts with". This is all good, but I don’t know how to compare them. I looked at the API, but I could not find anything that could do what I want to compare with ArrayLists, but only for the indicated indices.

All that is connected with the fact that this situation is a little less, according to the code, will be greatly appreciated. Thank!

+3
source share
5 answers

It's not that the rows, columns and diagonals are equal to X and O, but they are all equal to each other, whether they are X or O, so instead of using your row:

if ((newBoard().get(0)).equals(newBoard().get(1)) && newBoard().get(1).equals(newBoard().get(2))){
    System.out.println("Player-" + newBoard().get(0)  +" has won!");
return true;

This means the same thing, but it works for the X and About . The next step is to create a function that simply compares the three location indexes:

private boolean checkForWin(int x, int y, int z){
     return newBoard().get(x).equals(newBoard().get(y)) && newBoard().get(y).equals(newBoard().get(z)); 
}

Then you can simply pass this function to all possible winning location indices.

+1
source

Well, one option is not to think about all the winning boards, but about all the winning places. For instance:

private static final int[][] LINES_OF_THREE = {
  { 0, 1, 2 }, // Horizontals
  { 3, 4, 5 },
  { 6, 7, 8 },
  { 0, 3, 6 }, // Verticals
  { 1, 4, 7 },
  { 2, 5, 8 },
  { 0, 4, 8 }, // Diagonals
  { 6, 4, 2 }
};

Then something like:

for (int[] line : LINES_OF_THREE) {
  boolean won = true;
  for (int place : line) {
    // player = "O" or "X"
    if (!newBoard.get(place).equals(player)) { 
      won = false;
      break;
    }
  }
  if (won) {
    // Yippee!
  }
}
+7
+1

, , , TicTacToe:

Code Golf: Tic Tac Toe

0

tic tac 3 . ,

Here with a square array [3,3] (adapt if necessary), and then use iteration over columns and rows. Then you can check two diagonals.

I found this to be the easiest way to do this.

boolean HasWon(PLAYER p)
{
    for (int i = 0; i < 3; i++)
    {
        // Columns
        if (board[i, 0] == p)
            if (board[i,1] == p && board[i,2] == p)
                return true;
        // Rows
        if (board[0, i] == p)
            if (board[1, i] == p && board[2, i] == p)
                return true;
    }
    //Check diagonals
    if (board[0,0] == p && board[1,1] == p && board[2,2] == p) return true;
    if (board[2,0] == p && board[1,1] == p && board[0,2] == p) return true;
    return false;
}
0
source

Source: https://habr.com/ru/post/1771762/


All Articles