Method to make the computer play 1 move. @return boolean to specify if the game is finished.
()
| 683 | * @return boolean to specify if the game is finished. |
| 684 | */ |
| 685 | public boolean autoOne(){ |
| 686 | if (is_finished()) return true; |
| 687 | |
| 688 | //Reset the toggle if the user touched any cell before. |
| 689 | toggle = false; |
| 690 | for (int i = 0; i < board.length; ++i) |
| 691 | for (int j = 0; j < board.length; ++j) |
| 692 | if (board[i][j].getBackground() == Color.YELLOW) board[i][j].setBackground(Color.BLACK); |
| 693 | |
| 694 | Random random = new Random(); |
| 695 | boolean flag = false; |
| 696 | int row, col, direction; |
| 697 | while (!flag){ |
| 698 | //Selecting random numbers for the move. |
| 699 | row = random.nextInt(board.length); |
| 700 | col = random.nextInt(board[0].length); |
| 701 | if (boardType != 6) direction = random.nextInt(4); |
| 702 | else direction = random.nextInt(6); |
| 703 | |
| 704 | //Trying to move until selected row, column, and direction values are valid. |
| 705 | if (board[row][col].getBackground() != Color.BLACK) continue; |
| 706 | if (direction == 0){ |
| 707 | //UPWARDS |
| 708 | if (is_valid_up(row, col, row-2, col)){ |
| 709 | if (board[row-2][col].getBackground() != Color.GRAY) continue; |
| 710 | board[row][col].setBackground(Color.GRAY); |
| 711 | board[row-1][col].setBackground(Color.GRAY); |
| 712 | board[row-2][col].setBackground(Color.BLACK); |
| 713 | flag = true; |
| 714 | moves.add(row*100+col*10+0); |
| 715 | } |
| 716 | } |
| 717 | else if (direction == 1){ |
| 718 | //DOWNWARDS |
| 719 | if (is_valid_down(row, col, row+2, col)){ |
| 720 | if (board[row+2][col].getBackground() != Color.GRAY) continue; |
| 721 | board[row][col].setBackground(Color.GRAY); |
| 722 | board[row+1][col].setBackground(Color.GRAY); |
| 723 | board[row+2][col].setBackground(Color.BLACK); |
| 724 | flag = true; |
| 725 | moves.add(row*100+col*10+1); |
| 726 | } |
| 727 | } |
| 728 | else if (direction == 2){ |
| 729 | //RIGHT |
| 730 | if (is_valid_right(row, col, row, col+2)){ |
| 731 | if (board[row][col+2].getBackground() != Color.GRAY) continue; |
| 732 | board[row][col].setBackground(Color.GRAY); |
| 733 | board[row][col+1].setBackground(Color.GRAY); |
| 734 | board[row][col+2].setBackground(Color.BLACK); |
| 735 | flag = true; |
| 736 | moves.add(row*100+col*10+2); |
| 737 | } |
| 738 | } |
| 739 | else if (direction == 3){ |
| 740 | //LEFT |
| 741 | if (is_valid_left(row, col, row, col-2)){ |
| 742 | if (board[row][col-2].getBackground() != Color.GRAY) continue; |
nothing calls this directly
no test coverage detected