-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.java
More file actions
executable file
·136 lines (122 loc) · 4.3 KB
/
Game.java
File metadata and controls
executable file
·136 lines (122 loc) · 4.3 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
package v1;
import v1.piece.Pawn;
import v1.piece.Piece;
import v1.piece.PieceId;
import v1.piece.Queen;
import static v1.Common.*;
public class Game {
private final Board board;
private final Renderer renderer;
private int checkTeamId = -1;
private int winnerTeamId = -1;
Game() {
this.board = new Board();
this.renderer = new Renderer();
this.renderBoard();
}
// 보드 인스턴스 반환
public Board getBoard() {
return this.board;
}
// 체크메이트 팀 반환
public int getIsCheckmate() {
return this.winnerTeamId + 1;
}
// 왕 위치 반환
private int[] getKingPosition(int teamId) {
for (int y = 0; y <= HEIGHT; y++) {
for (int x = 0; x <= WIDTH; x++) {
Piece piece = this.board.getPiece(x, y);
if (piece == null) {
continue;
}
if (piece.getTeamId() == teamId && piece.getId() == PieceId.KING.get()) {
return new int[]{x, y};
}
}
}
return new int[]{-1, -1};
}
// 체크 여부 반환
private boolean getIsCheck(int teamId) {
int[] targetKingPosition = this.getKingPosition(getOtherTeamId(teamId));
int targetKingX = targetKingPosition[0];
int targetKingY = targetKingPosition[1];
if (targetKingX == -1 && targetKingY == -1) {
return true;
}
for (int y = 0; y <= HEIGHT; y++) {
for (int x = 0; x <= WIDTH; x++) {
Piece piece = this.board.getPiece(x, y);
if ((x == targetKingX && y == targetKingY) || piece == null) {
continue;
} else if (piece.getTeamId() != teamId) {
continue;
}
boolean canMove = piece.getCanMove(this.board, x, y, targetKingX, targetKingY, 2);
if (canMove) {
return true;
}
}
}
return false;
}
// 체크 이벤트 제어
private void handleCheck(int teamId) {
System.out.println("시스템: 체크, " + (getOtherTeamId(teamId) == BLACK_ID ? "블랙 팀은" : "화이트 팀은") + " 대응하세요!");
}
// 프로모션 여부 반환
private boolean getIsPromotion(int y, int teamId) {
return (teamId == BLACK_ID && y == 7) || (teamId == WHITE_ID && y == 0);
}
// 프로모션 이벤트 제어
private void handlePromotion(int x, int y, int teamId) {
this.board.setPiece(x, y, new Queen(teamId));
}
// 착수 이벤트 제어
private void handlePut(int x, int y, int teamId) {
Piece piece = this.board.getPiece(x, y);
if (piece.getId() == PieceId.PAWN.get()) {
((Pawn) piece).setIsFirst(false);
boolean isPromotion = this.getIsPromotion(y, piece.getTeamId());
if (isPromotion) {
this.handlePromotion(x, y, piece.getTeamId());
}
}
boolean isCheck = this.getIsCheck(teamId);
if (isCheck) {
if (teamId == this.checkTeamId) {
this.winnerTeamId = teamId;
} else {
this.checkTeamId = teamId;
this.handleCheck(teamId);
}
} else {
this.checkTeamId = -1;
}
boolean isCheckmate = this.getIsCheck(getOtherTeamId(teamId));
if (isCheckmate) {
this.winnerTeamId = getOtherTeamId(teamId);
}
}
// 보드 렌더링
public void renderBoard() {
this.renderer.draw(this.board);
}
// 착수
public boolean put(int nowX, int nowY, int moveX, int moveY) {
Piece piece = board.getPiece(nowX, nowY);
if (piece != null) {
Piece targetPiece = board.getPiece(moveX, moveY);
int targetStatus = targetPiece == null ? 0 : (piece.getTeamId() == targetPiece.getTeamId() ? 1 : 2);
boolean canMove = piece.getCanMove(this.board, nowX, nowY, moveX, moveY, targetStatus);
if (canMove) {
board.movePiece(nowX, nowY, moveX, moveY);
this.handlePut(moveX, moveY, piece.getTeamId());
this.renderBoard();
return true;
}
}
return false;
}
}