-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.java
41 lines (33 loc) · 1.46 KB
/
Player.java
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
/*
Akhilesh Yarabarla- CS 2336.001
The abstract player class can be inherited by either a computer player or a human player. It has a method move() that takes a coordinate and check to see if it is a valid move. If so, it applies the move to the board and flips any intermediate squares as well. The abstract method makeMove() is used by subclasses to implement how they choose their moves.
*/
import java.util.Arrays;
public abstract class Player {
public String color;
public Player(){};
public Player(String color) {
this.color = color;
}
protected boolean move(Integer[] coordinates, Board board) {
boolean moveValid = false;
Integer[][] legalMoves = board.getLegalMoves(this.color);
for (Integer[] move : legalMoves) {
boolean isLegalMove = move[0] == coordinates[0] && move[1] == coordinates[1];
if (isLegalMove) {
board.changePosition(coordinates, this.color);
System.out.println("\nSuccess: " + this.color + " move at " + Arrays.toString(coordinates) + "\n");
Integer[][] flips = board.getFlips(coordinates);
for (Integer[] flip : flips) {
board.changePosition(flip, this.color);
}
moveValid = true;
}
}
if (!moveValid) {
System.out.println("Invalid move. ");
}
return moveValid;
}
public abstract void makeMove(Board board);
}