-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathChessGame.java
More file actions
91 lines (78 loc) · 2.58 KB
/
ChessGame.java
File metadata and controls
91 lines (78 loc) · 2.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
package chess;
import chess.piece.King;
import chess.piece.Piece;
import chess.view.InputView;
import chess.view.OutputView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class ChessGame {
private final List<Piece> pieces;
public ChessGame(List<Piece> pieces) {
this.pieces = new ArrayList<>(pieces);
}
public ChessGame() {
InitiatePieces initiatePieces = new InitiatePieces();
this.pieces = new ArrayList<>(initiatePieces.pieces());
}
public boolean isSomeBodyKingDoesntExist(){
Map<Color,Boolean> colorKingDead = getKingSurviveStatus();
for(Color color : colorKingDead.keySet()){
if(colorKingDead.get(color)){
return true;
}
}
return false;
}
private Map<Color, Boolean> getKingSurviveStatus() {
Map<Color,Boolean> colorKingDead = new HashMap<>(Map.of(Color.BLACK,true,Color.WHITE,true));
for(Piece piece : pieces){
if(piece instanceof King){
colorKingDead.put(piece.getColor(),false);
}
}
return colorKingDead;
}
public Color getLoseColor(){
Map<Color,Boolean> colorKingDead = getKingSurviveStatus();
for(Color color : colorKingDead.keySet()){
if(colorKingDead.get(color)){
return color;
}
}
return Color.EMPTY;
}
public Optional<Piece> killPieceWhenExistSamePositionPiece(Piece movePiece) {
Optional<Piece> samePositionOtherPiece = findSamePositionPiece(movePiece);
if(samePositionOtherPiece.isEmpty()){
return samePositionOtherPiece;
}
Piece otherPiece = samePositionOtherPiece.get();
if(movePiece.isSamePosition(otherPiece) && movePiece.isOpposite(otherPiece)){
killPiece(otherPiece);
return Optional.of(otherPiece);
}
return Optional.empty();
}
private Optional<Piece> findSamePositionPiece(Piece movePiece) {
for(Piece piece : pieces){
if(piece.isSamePosition(movePiece) && movePiece.isOpposite(piece)){
return Optional.of(piece);
}
}
return Optional.empty();
}
private void killPiece(Piece deadPiece) {
for (int i = 0; i < pieces.size(); i++) {
Piece piece = pieces.get(i);
if(piece.equals(deadPiece)){
pieces.remove(piece);
}
}
}
public List<Piece> getPieces() {
return pieces;
}
}