-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComputerPlayer_qwnz51.java
119 lines (116 loc) · 4.36 KB
/
ComputerPlayer_qwnz51.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.awt.Point;
/**
* This class represents a computer AI within the game of Hex and the methods that a computer AI should make
*/
public class ComputerPlayer_qwnz51 implements PlayerInterface
{
private Piece colour = Piece.UNSET;
private GameState gameState = GameState.INCOMPLETE;
/**
* Ask the computer to make a move.
*
* @param boardView the current state of the board
* @return a Move object representing the desired place to place a piece
*
* @throws NoValidMovesException Indicates that no valid moves are possible - e.g. all cells on the
* board are already occupied by a piece.
*/
public MoveInterface makeMove(Piece[][] boardView) throws NoValidMovesException
{
//Check if their are any more possible moves
boolean unset = false;
for(int i = 0; i < boardView.length; i++){
for(int j = 0; j < boardView[0].length; j++){
if(boardView[i][j].equals(Piece.UNSET)){
unset = true;
}
}
}
if(!unset){
throw new NoValidMovesException();
}
//Make a move
else{
System.out.println();
//Displays who's turn it is
if(colour.equals(Piece.RED)){
System.out.println("Red's turn");
}
else if(colour.equals(Piece.BLUE)){
System.out.println("Blue's turn");
}
MoveInterface move = new Move();
//Check move.setPosition doesn't fail (i.e. doesn't return false)
boolean methodSuccess = false;
while(!methodSuccess){
ComputerAI_qwnz51 ai = new ComputerAI_qwnz51();
Point movePos = ai.calculateMove(boardView, colour);
if(movePos == null){
methodSuccess = move.setConceded();
if(colour.equals(Piece.RED)){
DisplayBoard_qwnz51.winner(Piece.BLUE);
}
else if(colour.equals(Piece.BLUE)){
DisplayBoard_qwnz51.winner(Piece.RED);
}
}
else{
int x = (int)movePos.getX();
int y = (int)movePos.getY();
try{
methodSuccess = move.setPosition(x, y);
}
catch(InvalidPositionException e){
e.getStackTrace();
}
}
//if move.setPosition or move.setConceeded returned false, display the error
if(!methodSuccess){
System.out.print("UNKNOWN ERROR: Move.setPosition() or Move.setConceded()");
}
}
return move;
}
}
/**
* Set the colour that this player will be
*
* @param colour A Piece (RED/BLUE) that this player will be
* @return true indicating that the method succeeded
*
* @throws InvalidColourException A colour other than RED/BLUE was provided
* @throws ColourAlreadySetException The colour has already been set for this player.
*/
public boolean setColour(Piece colour) throws InvalidColourException, ColourAlreadySetException
{
if(!this.colour.equals(Piece.UNSET)){
throw new ColourAlreadySetException();
}
else if(colour.equals(Piece.RED) == colour.equals(Piece.BLUE)){
throw new InvalidColourException();
}
else if(this.colour.equals(Piece.UNSET) && !(colour.equals(Piece.RED) == colour.equals(Piece.BLUE))){
this.colour = colour;
return true;
}
System.out.println("UNKOWN ERROR: Computer.setColour()");
return false;
}
/**
* Informs the player of the final game state. Player has Won, lost.
*
* @param state either WON or LOST
* @return true indicating method has compleated successfully.
*
*/
public boolean finalGameState(GameState state)
{
//Checks game state hasn't already been set
if(gameState.equals(GameState.INCOMPLETE)){
gameState = state;
return true;
}
System.out.println("UNKOWN ERROR: Computer.finalGameState()");
return false;
}
}