コード例 #1
0
 /**
  * This method reads computer input it takes TicTacData class as parameter.
  *
  * @param tdata
  */
 public void gameInput(TicTacData tdata) {
   if (tdata.getCOUNT() == ONE) {
     System.out.println(tdata.getCOUNT());
     array = tdata.getArray();
     random = new Random();
     array[random.nextInt(ROW - 1)][random.nextInt(COL - 1)] = C;
     tdata.setArray(array);
   } else {
     array = tdata.getArray();
     boolean check = false;
     check = rowCheck(tdata, array);
     if (check == false) {
       check = colCheck(tdata, array);
     }
     if (check == false) {
       check = diagonalsCheck(tdata, array);
     }
     if (check == false) {
       check = rowCheckAgain(tdata, array);
     }
     if (check == false) {
       check = colCheckAgain(tdata, array);
     }
     if (check == false) {
       check = diagonalsCheckAgain(tdata, array);
     }
   }
 }
コード例 #2
0
  private boolean diagonalsCheck(TicTacData tdata, char[][] array) {

    boolean flag = false;
    int i = 0, j = 0;
    if (array[i][j] == C && array[i + 1][j + 1] == C && array[i + 2][j + 2] == X) {
      array[i + 2][j + 2] = C;
      flag = true;
    } else if (array[i][j] == C && array[i + 1][j + 1] == X && array[i + 2][j + 2] == C) {
      array[i + 1][j + 1] = C;
      flag = true;
      tdata.setArray(array);
    } else if (array[i][j] == X && array[i + 1][j + 1] == C && array[i + 2][j + 2] == C) {
      array[i][j] = C;
      flag = true;
      tdata.setArray(array);
    } else if (array[i][j + 2] == C && array[i + 1][j + 1] == C && array[i + 2][j] == X) {
      array[i + 2][j] = C;
      flag = true;
      tdata.setArray(array);
    } else if (array[i][j + 2] == C && array[i + 1][j + 1] == X && array[i + 2][j] == C) {
      array[i + 1][j + 1] = C;
      flag = true;
      tdata.setArray(array);
    } else if (array[i][j + 2] == X && array[i + 1][j + 1] == C && array[i + 2][j] == C) {
      array[i][j + 2] = C;
      flag = true;
      tdata.setArray(array);
    }
    return flag;
  }
コード例 #3
0
 private boolean rowCheckAgain(TicTacData tdata, char[][] array) {
   int j = 0;
   boolean flag = false;
   for (int i = 0; i < ROW; i++) {
     if (array[i][j] == O && array[i][j + 1] == X && array[i][j + 2] == O) {
       array[i][j + 1] = C;
       flag = true;
       break;
     } else if (array[i][j] == O && array[i][j + 1] == O && array[i][j + 2] == X) {
       array[i][j + 2] = C;
       flag = true;
       break;
     } else if (array[j][j] == X && array[i][j + 1] == O && array[i][j + 2] == O) {
       array[i][j] = C;
       flag = true;
       break;
     } else if (array[i][j] == C && array[i][j + 1] == X && array[i][j + 2] == X) {
       array[i][j + 2] = C;
       flag = true;
       break;
     } else if (array[i][j] == X && array[i][j + 1] == X && array[i][j + 2] == C) {
       array[i][j] = C;
       flag = true;
       break;
     } else if (array[i][j] == X && array[i][j + 1] == C && array[i][j + 2] == X) {
       array[i][j + 2] = C;
       flag = true;
       break;
     } else if (array[i][j] == O && array[i][j + 1] == X && array[i][j + 2] == X) {
       array[i][j + 2] = C;
       flag = true;
       break;
     } else if (array[i][j] == X && array[i][j + 1] == X && array[i][j + 2] == O) {
       array[i][j] = C;
       flag = true;
       break;
     } else if (array[i][j] == O && array[i][j + 1] == X && array[i][j + 2] == C) {
       array[i][j + 1] = C;
       flag = true;
       break;
     } else if (array[i][j] == C && array[i][j + 1] == X && array[i][j + 2] == O) {
       array[i][j + 1] = C;
       flag = true;
       break;
     }
   }
   tdata.setArray(array);
   return flag;
 }
コード例 #4
0
  /**
   * this method reads user input from console It takes TicTacData class parameter.
   *
   * @param tdata
   */
  public void userInput(TicTacData tdata) {
    array = tdata.getArray();
    Scanner scan = new Scanner(System.in);
    correct:
    for (int row = 0; row < ROW; row++) {
      String c = scan.next();
      for (int col = 0; col < COL; col++) {

        if (c.charAt(col) == O || c.charAt(col) == X || c.charAt(col) == C) {
          if (array[row][col] == C) {
            array[row][col] = C;
          } else if (array[row][col] == O) {
            array[row][col] = O;
          } else if (array[row][col] == X) {
            array[row][col] = c.charAt(col);
          }
        } else {
          System.out.println("WRONG INPUT");
          continue correct;
        }
      }
    }
    tdata.setArray(array);
  }