-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathChessGame.java
More file actions
65 lines (54 loc) · 2.14 KB
/
ChessGame.java
File metadata and controls
65 lines (54 loc) · 2.14 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
package chess;
import chess.piece.Piece;
import java.util.Scanner;
public class ChessGame {
private static final Scanner scanner = new Scanner(System.in);
private static final Color START_COLOR = Color.WHITE;
public static void main(String[] args) {
ChessGame chessGame = new ChessGame();
chessGame.play();
}
public void play() {
System.out.println("게임을 시작합니다.");
ChessBoard chessBoard = ChessBoard.initialize();
printChessBoard(chessBoard);
Color currentTurnColor = START_COLOR;
while (true) {
System.out.println(currentTurnColor + "의 차례입니다. 기물을 이동하세요 ex) a1 a2");
String[] commands = readLine().split(" ");
Position from = toPosition(commands[0]);
Position to = toPosition(commands[1]);
chessBoard.move(currentTurnColor, from, to);
printChessBoard(chessBoard);
currentTurnColor = currentTurnColor.opposite();
}
}
private void printChessBoard(ChessBoard chessBoard) {
StringBuilder sb = new StringBuilder();
for (int rowSymbol = 8; rowSymbol > 0; rowSymbol--) {
Row row = Row.fromSymbol(rowSymbol);
sb.append(row.getSymbol()).append(" ");
for (char col = 'A'; col <= 'H'; col++) {
Column column = Column.valueOf(col + "");
Position position = new Position(row, column);
Piece piece = chessBoard.findPiece(position);
if (piece == null) {
sb.append(". ");
continue;
}
sb.append(piece).append(" ");
}
sb.append("\n");
}
sb.append(" ").append("a b c d e f g h");
System.out.println(sb);
}
private String readLine() {
return scanner.nextLine().trim();
}
private Position toPosition(String command) {
Column column = Column.valueOf(Character.toString(command.charAt(0)).toUpperCase());
Row row = Row.fromSymbol(command.charAt(1) - '0');
return new Position(column, row);
}
}