-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessGameLogic.java
More file actions
181 lines (163 loc) · 6.35 KB
/
Copy pathChessGameLogic.java
File metadata and controls
181 lines (163 loc) · 6.35 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* ChessGameLogic.java
*
* @author Deep Patel
*
* REMARKS: This class implements the ChessController interface and
* and implements the methods of that interface to manage the game logic
*/
public class ChessGameLogic implements ChessController {
//Instance Variables
private GameDisplay display;
private Board board;
private boolean isHumanTurn;
private boolean gameOver;
private ChessPlayer player;
//Constructor
public ChessGameLogic(GameDisplay display) {
this.display = display;
this.board = new Board();
this.isHumanTurn = true;
this.gameOver = false;
}
/**
*
*
* Parameters: movePiece is passed a desired move for the human player. It should test whether the move is
* valid, and make the move (updating the status of the board) if it is allowed
* Move move - The method takes in the move that is to be made
*
* Returns whether the move was made or not
*/
public boolean movePiece(Move move) {
Piece piece = (board.getBox(move.getFromRow() - 1, move.getFromCol() - 1)).getPiece();
Piece destPiece = (board.getBox(move.getToRow() - 1, move.getToCol() - 1)).getPiece();
// Invalid move: either no piece at source or it's opponent's piece
if (piece == null || (isHumanTurn && !piece.getWhosePiece())
|| (!isHumanTurn && piece.getWhosePiece())
|| (destPiece != null && !isHumanTurn && !destPiece.getWhosePiece())
|| (destPiece != null && isHumanTurn && destPiece.getWhosePiece())) {
display.summarizeMove(new Move(move.getFromRow(), move.getFromCol(), move.getToRow(), move.getToCol(), false, false), board);
display.displayBoard(board);
return false;
}
boolean isValidMove;
if (piece instanceof Pawn) {
((Pawn) piece).setTurn(isHumanTurn);
}
isValidMove = piece.isValidMove(board, move);
if (isValidMove) {
move.setPrevPiece(destPiece);
if (piece instanceof Pawn) {
int choice = -1;
//Promotion of pawn
if (isHumanTurn && move.getToRow() == 1) {
choice = ((Pawn) piece).promotePawn();
} else if (!isHumanTurn && move.getToRow() == 8) {
Piece p = move.getPiece();
if (p instanceof Bishop) {
choice = 1;
} else if (p instanceof Rook) {
choice = 2;
} else if (p instanceof Knight) {
choice = 3;
} else {
choice = 0;
}
}
if (choice == 0) {
(board.getBox(move.getToRow() - 1, move.getToCol() - 1)).setPiece(new Queen(isHumanTurn));
} else if (choice == 1) {
(board.getBox(move.getToRow() - 1, move.getToCol() - 1)).setPiece(new Bishop(isHumanTurn));
} else if (choice == 2) {
(board.getBox(move.getToRow() - 1, move.getToCol() - 1)).setPiece(new Rook(isHumanTurn));
} else if (choice == 3) {
(board.getBox(move.getToRow() - 1, move.getToCol() - 1)).setPiece(new Knight(isHumanTurn));
} else {
(board.getBox(move.getToRow() - 1, move.getToCol() - 1)).setPiece(piece);
}
} else {
(board.getBox(move.getToRow() - 1, move.getToCol() - 1)).setPiece(piece);
}
(board.getBox(move.getFromRow() - 1, move.getFromCol() - 1)).setPiece(null);
if (destPiece instanceof King) {
gameOver = true;
}
isHumanTurn = !isHumanTurn;
move.setPiece((board.getBox(move.getToRow() - 1, move.getToCol() - 1)).getPiece());
}
move.setValid(isValidMove);
display.summarizeMove(move, board);
display.displayBoard(board);
return isValidMove;
}
//This method will reset the game and will be called
//when a player requests a new game so will set the turn
//to the players turn to start
public void reset() {
board.resetBoard();
isHumanTurn = true;
}
/**
* playGame will drive the game. It calls methods from the display to prompt the human user for
* input, summarize moves, and display the updated board. It also controls whose turn it is, tests
* whether the game is over, prompts the human on whether they want to play again
*
*/
public void playGame() {
gameOver = false;
int difficulty = display.promptForOpponentDifficulty(1);
//Choose AI difficulty
if(difficulty == 0){
player = new EasyAI();
}else if(difficulty == 1){
player = new HarderAI();
}
display.displayBoard(board);
Move move = null;
while (true) {
//Player turn
if (isHumanTurn) {
move = display.promptForMove();
//If forfeit is made
if (move.getForfeit()) {
display.gameOver(3);
if (display.promptPlayAgain()) {
reset();
playGame();
break;
} else {
System.out.println("Thank you for playing");
break;
}
}
boolean validMove = movePiece(move);
if (!validMove) {
continue;
}
//AI turn
} else {
Move x = player.makeMove(move, board);
boolean validMove = movePiece(x);
if (!validMove) {
continue;
}
}
//Check for whether the game is over and who the winner is
if (gameOver) {
if (isHumanTurn) {
display.gameOver(1);
} else {
display.gameOver(0);
}
if (display.promptPlayAgain()) {
reset();
playGame();
} else {
System.out.println("Thank you for playing");
break;
}
}
}
}
}