-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameLogic.java
More file actions
126 lines (101 loc) · 3.58 KB
/
Copy pathGameLogic.java
File metadata and controls
126 lines (101 loc) · 3.58 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
/**
* Game Logic
*
* @author Faiza Salami, 7941056
* <p>
* REMARKS: implementing the logic of the game which is the back end that manages the game logic
*/
public class GameLogic implements ChessController {
private GameDisplay game;
private Board gameBoard;
private ChessPlayer AI;
private int player;
public GameLogic(GameDisplay game) {
this.game = game;
gameBoard = new Board();
player = 1;
}
/**
* test whether the move is valid
*
* @param move the move the player made
* @return boolean-returns true if the move made by the player is valid and false otherwise
*/
public boolean movePiece(Move move) {
Piece piece;
boolean output = false;
if (move.getRow() >= 0 && move.getRow() <= 7 && move.getCol() >= 0 && move.getCol() <= 7 && move.getNewRow() >= 0 && move.getNewRow() <= 7 && move.getNewCol() >= 0 && move.getNewCol() <= 7) {
piece = gameBoard.getPiece(move.getRow(), move.getCol());
if (piece != null && piece.isPlayerPiece()) {
if (piece.validMove(move, gameBoard)) {
output = true;
} else {
output = false;
}
}
}
return output;
}
/**
* resets the game
*/
public void reset() {
if (game.promptPlayAgain()) {
gameBoard = new Board();
playGame();
} else {
System.exit(0);
}
}
/**
* this method drives the game,it determines whos turn it is, checks if the king has been captured,
* summarizes the moves and displays the board
*/
public void playGame() {
Move move = null;
int difficulty = game.promptForOpponentDifficulty(1);
//determines which AI is going to be played against
if (difficulty == 0) {
AI = new AI1();
} else {
AI = new AI2();
}
game.displayBoard(gameBoard);
//keeps playing the game until the king has been captured
while (!gameBoard.capturedKing()) {
if (player == 1) {
move = game.promptForMove();
} else if (player == 2) {
if (difficulty != 0 && gameBoard.getPiece(move.getNewRow(), move.getNewCol()).getCaptured() != null) {
((AI2) AI).removeFromList(gameBoard.getPiece(move.getNewRow(), move.getNewCol()).getCaptured(), move.getNewRow(), move.getNewCol());
}
move = AI.makeMove(move, gameBoard);
}
if (move == null) {
reset();
}
if ((movePiece(move) && player == 1) || player == 2) {
if (((move.getNewRow() == 0 && player == 1) || (move.getNewRow() == 7 && player == 2)) && !gameBoard.capturedKing()) {
game.promotion(move, gameBoard, AI);
}
gameBoard.updateBoard(move);
game.summarizeMove(move, gameBoard);
game.displayBoard(gameBoard);
//change whichever player's turn it is
if (player == 1) {
player = 2;
} else {
player = 1;
}
} else {
System.out.println("The move you have entered is invalid. Please enter another move.");
}
}
//get the winner
if (gameBoard.getPiece(move.getNewRow(), move.getNewCol()).isAIPiece()) {
game.gameOver(2);
} else {
game.gameOver(1);
}
}
}