// Gets the AI's next move giving the current state of the board
  private void getAIMove(GameBoard board) {
    // Send the board to the AI for it to determine and return the move in an array {x,y}
    int[] move = ai.move(board, aiMark);
    TextView cell = null;
    // Determine the right cell to use by id first go to the right row then the right column
    switch (move[0]) {
      case 0:
        switch (move[1]) {
          case 0:
            cell = (TextView) findViewById(R.id.cell11);
            break;
          case 1:
            cell = (TextView) findViewById(R.id.cell12);
            break;
          case 2:
            cell = (TextView) findViewById(R.id.cell13);
            break;
        }
        break;
      case 1:
        switch (move[1]) {
          case 0:
            cell = (TextView) findViewById(R.id.cell21);
            break;
          case 1:
            cell = (TextView) findViewById(R.id.cell22);
            break;
          case 2:
            cell = (TextView) findViewById(R.id.cell23);
            break;
        }
        break;
      case 2:
        switch (move[1]) {
          case 0:
            cell = (TextView) findViewById(R.id.cell31);
            break;
          case 1:
            cell = (TextView) findViewById(R.id.cell32);
            break;
          case 2:
            cell = (TextView) findViewById(R.id.cell33);
            break;
        }
        break;
    }

    // Make sure there's nothing already in the cell
    //	then place the mark with the ai's Mark, increment move count
    //	and check to see if the game's over
    if (cell != null && cell.getText() == "") {
      board.placeMark(move[0], move[1], aiMark);
      cell.setText(aiMark);
      moveCount++;
      isOver = checkEnd(aiMark);
    }
  }
Exemple #2
0
    public void actionPerformed(ActionEvent e) {
      // initial for icon and mark
      if (countMove % 2 == 0) {
        mark = "X";
        markIcon = new ImageIcon(this.getClass().getResource("x.png"));
      } else {
        mark = "O";
        markIcon = new ImageIcon(this.getClass().getResource("o.png"));
      }
      // handle for move(set mark and icon for player's move)
      if (isLastMove()) { // check if ai already win
        // check if button already fill
      } else if (((e.getSource() == button1) && (button1Desc != null))
          || ((e.getSource() == button2) && (button2Desc != null))
          || ((e.getSource() == button3) && (button3Desc != null))
          || ((e.getSource() == button4) && (button4Desc != null))
          || ((e.getSource() == button5) && (button5Desc != null))
          || ((e.getSource() == button6) && (button6Desc != null))
          || ((e.getSource() == button7) && (button7Desc != null))
          || ((e.getSource() == button8) && (button8Desc != null))
          || ((e.getSource() == button9) && (button9Desc != null))) {

      } else { // make move
        if (e.getSource() == button1) {
          button1Desc = mark;
          button1.setIcon(markIcon);
        } else if (e.getSource() == button2) {
          button2Desc = mark;
          button2.setIcon(markIcon);
        } else if (e.getSource() == button3) {
          button3Desc = mark;
          button3.setIcon(markIcon);
        } else if (e.getSource() == button4) {
          button4Desc = mark;
          button4.setIcon(markIcon);
        } else if (e.getSource() == button5) {
          button5Desc = mark;
          button5.setIcon(markIcon);
        } else if (e.getSource() == button6) {
          button6Desc = mark;
          button6.setIcon(markIcon);
        } else if (e.getSource() == button7) {
          button7Desc = mark;
          button7.setIcon(markIcon);
        } else if (e.getSource() == button8) {
          button8Desc = mark;
          button8.setIcon(markIcon);
        } else if (e.getSource() == button9) {
          button9Desc = mark;
          button9.setIcon(markIcon);
        }
        countMove += 1;
        // set navigate text
        if (theChoice == 1) {
          if (isLastMove()) { // check if player already win
            navigateText.setText("   You Win!!!   ");
            navigateText.setFont(fontDone);
          } else if (countMove == 9) {
            navigateText.setText("   Draw!!!   ");
            navigateText.setFont(fontDone);
          } else {
            // make ai move
            AI aiMove = new AI();
            aiMove.move();
            if (isLastMove()) { // check ai win
              navigateText.setText("   You Lose!!!   ");
              navigateText.setFont(fontDone);
            }
          }
        } else {
          if (countMove % 2 == 0) {
            if (isLastMove()) {
              navigateText.setText("   O Win!!!   ");
              navigateText.setFont(fontDone);
            } else {
              navigateText.setText("   X Turn   ");
            }
          } else {
            if (isLastMove()) {
              navigateText.setText("   X Win!!!   ");
              navigateText.setFont(fontDone);
            } else if (countMove == 9) {
              navigateText.setText("   Draw!!!   ");
              navigateText.setFont(fontDone);
            } else {
              navigateText.setText("   O Turn   ");
            }
          }
        }
      }
    }