You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
675 B

  1. package de.tims.tictactoe.ai;
  2. import de.tims.tictactoe.GameLogic;
  3. import java.util.Random;
  4. public class AIEasy implements TicTacToeAI {
  5. private static final char AI_CHAR = 'o';
  6. private static final char EMPTY_CHAR = '-';
  7. private Random rand;
  8. private GameLogic gl;
  9. private int boardSize;
  10. public AIEasy(GameLogic gl) {
  11. this.gl = gl;
  12. boardSize = gl.getBoard().length;
  13. rand = new Random();
  14. }
  15. @Override
  16. public void calculateNextMove() {
  17. char[][] board = gl.getBoard();
  18. int row;
  19. int col;
  20. do {
  21. row = rand.nextInt(boardSize);
  22. col = rand.nextInt(boardSize);
  23. } while (board[row][col] != EMPTY_CHAR);
  24. gl.setField(row, col, AI_CHAR);
  25. }
  26. }