-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasyAI.java
More file actions
61 lines (57 loc) · 1.94 KB
/
Copy pathEasyAI.java
File metadata and controls
61 lines (57 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* EasyAI.java
*
* @author Deep Patel
*
* REMARKS: This class implements the easy AI which just implements
* random valid chess moves
*/
import java.util.Random;
public class EasyAI implements ChessPlayer {
//Constructor
EasyAI(){}
/**
* This method is used to make a move by the easy ai which just make a
* random valid move on the board
*
* Parameters:
* Board board - The method take in the current state of the board to validate the move
* Move move - The method takes in the last move made by the player
*
* Returns the move to be made by the AI
*/
public Move makeMove(Move move, Board board) {
Move m = null;
boolean isVal = false;
//Generate moves until valid move is acquired
while (!isVal) {
int fromR = generateRandomInt();
int fromC = generateRandomInt();
Piece p = board.getBox(fromR, fromC).getPiece();
if(p != null && !p.getWhosePiece()){
int toR = generateRandomInt();
int toC = generateRandomInt();
if(board.getBox(toR, toC).getPiece() == null){
m = new Move(fromR+1, fromC+1, toR+1, toC+1, false, false);
if (p instanceof Pawn) {
((Pawn) p).setTurn(false);
}
isVal = p.isValidMove(board, m);
}else if(board.getBox(toR, toC).getPiece().getWhosePiece()){
m = new Move(fromR+1, fromC+1, toR+1, toC+1, false, false);
if (p instanceof Pawn) {
((Pawn) p).setTurn(false);
}
isVal = p.isValidMove(board, m);
}
}
}
m.setPiece(move.getPiece());
return m;
}
//Generates a random integer from 0 to 7
private int generateRandomInt(){
Random rand = new Random();
return rand.nextInt(8);
}
}