forked from LiewKuanYung/webale-chess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameWebale.java
150 lines (128 loc) · 3.95 KB
/
GameWebale.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
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
/**
* @author Liew Kuan Yung
*/
import java.util.*;
//By Liew Kuan Yung
public class GameWebale {
private Player[] players = new Player[2];
private Player currentTurn;
private GameBoard board;
private GameStatus status;
private ArrayList<GameMove> gameMovesPlayed = new ArrayList<GameMove>();
GameWebale(GameBoard board, Player p1, Player p2){
this.board = board;
initialize(p1, p2);
}
private void initialize(Player p1, Player p2)
{
players[0] = p1;
players[1] = p2;
board.resetBoard();
if (p1.isColorRed()) { //Red Start First
System.out.println("Initialize current Player to p1 Red");
this.currentTurn = p1;
}
else {
System.out.println("Initialize current Player to p2 Blue");
this.currentTurn = p2;
}
gameMovesPlayed.clear();
status = GameStatus.ACTIVE;
}
public GameStatus getStatus() {
return status;
}
public void setStatus(GameStatus status) {
this.status = status;
}
public boolean playerMove(Player player, int startX, int startY,
int endX, int endY)
{
GameBoardSpot startBox = null;
GameBoardSpot endBox = null;
try {
//check game status
if( status != GameStatus.ACTIVE) {
return false;
}
System.out.println("Game status is not ACTIVE");
//initialize startBox and endBox
startBox = board.getSpot(startX, startY);
System.out.println("playerMove: start xy "+ startX + " "+ startY);
if(startBox.getPiece() == null) {
System.out.println("Empty");
} else {
System.out.println(startBox.getPiece().getPieceInfo());
}
endBox = board.getSpot(endX, endY);
System.out.println("playerMove: end xy " + endX + " " + endY);
if(endBox.getPiece() == null) {
System.out.println("Empty");
} else {
System.out.println(endBox.getPiece().getPieceInfo());
}
} catch (Exception e) {
System.out.println("Exception: GameWebale");
}
GameMove move = new GameMove(player, startBox, endBox);
System.out.println("playerMove GameMove Saved");
return this.makeMove(move, player);
}
private boolean makeMove(GameMove move, Player player)
{
System.out.println("Check Point: inside make move");
Piece startPiece = move.getStart().getPiece();
if (startPiece == null) {
System.out.println("invalid null start piece");
return false;
}
System.out.println("Check Point 1 pass startPiece not null test");
// check valid player?
if (startPiece.getColor() != player.getColor()) {
System.out.println("invalid current player (color)");
return false;
}
System.out.println("Check Point 2 pass color and player test");
// check valid move?
if (!startPiece.isValidMove(board, move.getStart(), move.getEnd())) {
System.out.println("invalid move");
return false;
}
System.out.println("Check Point 3 pass isValidMove test");
// check any capture?
Piece endPiece = move.getEnd().getPiece();
if (endPiece != null) {
endPiece.setCaptured(true);
move.setPieceCaptured(endPiece);
}
System.out.println("Check Point 4 checked captured and stored in GameMove");
// store the move
gameMovesPlayed.add(move);
if (endPiece != null && endPiece instanceof Sun) {
System.out.println(endPiece.getPieceInfo());
if (player.isColorRed()) {
System.out.println("BLUE WIN");
this.setStatus(GameStatus.BLUE_WIN);
}
else {
System.out.println("RED WIN");
this.setStatus(GameStatus.RED_WIN);
}
}
// move piece from the start box to end box
move.getEnd().setPiece(move.getStart().getPiece());
move.getStart().setPiece(null);
System.out.println("Check Point 5 reset pieces in GameMove");
// set the current turn to the other player
if (this.currentTurn == players[0]) {
System.out.println("Change current player to player[1]");
this.currentTurn = players[1];
}
else {
System.out.println("Change current player to player[0]");
this.currentTurn = players[0];
}
System.out.println("!!!Successful Move!!!");
return true;
}
}