-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[3, 4단계 - 체스] 커찬(이충안) 미션 제출합니다. #21
base: leegwichan
Are you sure you want to change the base?
Changes from all commits
91a3e25
607649f
8ac528f
9abcbf8
edd54fa
e05d029
3625575
eff640d
e20e78c
2057f00
e7f9c78
23716a6
cbc4b37
a0db668
2d395f8
e955a9e
ba2012b
710bf81
3cf4dc0
8c7e15b
8fd21fe
56f5451
4e47860
e5e006c
da3bbf1
db1b6c3
f7cf3ff
e50ea69
2df6c31
bcf5d26
2317cc3
4564d64
7d6b083
bffe03f
c3bbcf1
b241845
00e8d61
a2f0d0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,4 @@ out/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
db/ | ||
|
||
docker-compose.yml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
version: "3.9" | ||
services: | ||
db: | ||
image: mysql:8.0.28 | ||
platform: linux/x86_64 | ||
restart: always | ||
ports: | ||
- "13306:3306" | ||
environment: | ||
MYSQL_ROOT_PASSWORD: root | ||
MYSQL_DATABASE: chess | ||
MYSQL_USER: user | ||
MYSQL_PASSWORD: password | ||
TZ: Asia/Seoul | ||
volumes: | ||
- ./db/mysql/data:/var/lib/mysql | ||
- ./db/mysql/config:/etc/mysql/conf.d | ||
- ./db/mysql/init:/docker-entrypoint-initdb.d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
CREATE DATABASE IF NOT EXISTS chess; | ||
|
||
USE chess; | ||
|
||
CREATE TABLE chess_game ( | ||
turn VARCHAR(10) CHECK (turn IN ('BLACK', 'WHITE')) not null, | ||
PRIMARY KEY (turn) | ||
); | ||
|
||
CREATE TABLE pieces ( | ||
board_file VARCHAR(2) CHECK (board_file IN ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H')) not null, | ||
board_rank VARCHAR(2) CHECK (board_rank IN ('1', '2', '3', '4', '5', '6', '7', '8')) not null, | ||
type VARCHAR(10) CHECK (type IN ('KING', 'QUEEN', 'ROOK', 'BISHOP', 'KNIGHT', 'PAWN', 'EMPTY')) not null, | ||
team VARCHAR(10) CHECK (team IN ('BLACK', 'WHITE', 'EMPTY')) not null, | ||
PRIMARY KEY (board_rank, board_file) | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,35 @@ | ||
package chess; | ||
|
||
import chess.dao.ChessDao; | ||
import chess.dao.ChessGameRepository; | ||
import chess.dao.ConnectionManager; | ||
import chess.dao.MysqlChessGameRepository; | ||
import chess.dao.MysqlPieceRepository; | ||
import chess.dao.PieceRepository; | ||
import chess.view.InputView; | ||
import chess.view.OutputView; | ||
|
||
public class ChessApplication { | ||
|
||
private static final InputView INPUT_VIEW = new InputView(); | ||
private static final OutputView OUTPUT_VIEW = new OutputView(); | ||
|
||
public static void main(String[] args) { | ||
Comment on lines
+14
to
17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. view 주입을 방식을 변경하게 된 이유가 궁금해요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메서드 줄 수 줄일려고 했는데, 통일성이 없어서 수정했습니다. public static void main(String[] args) {
ConnectionManager connectionManager = new ConnectionManager();
ChessService chessService = createChessService(connectionManager);
InputView inputView = new InputView();
OutputView outputView = new OutputView();
ChessController chessController = new ChessController(chessService, inputView, outputView);
try {
chessController.run();
} finally {
connectionManager.closeConnection();
}
}
private static ChessService createChessService(ConnectionManager connectionManager) {
ChessGameRepository chessGameRepository = new MysqlChessGameRepository(connectionManager);
PieceRepository pieceRepository = new MysqlPieceRepository(connectionManager);
ChessDao chessDao = new ChessDao(chessGameRepository, pieceRepository);
return new ChessService(chessDao);
} |
||
InputView inputView = new InputView(); | ||
OutputView outputView = new OutputView(); | ||
ChessGame chessGame = new ChessGame(inputView, outputView); | ||
ConnectionManager connectionManager = new ConnectionManager(); | ||
ChessGameRepository chessGameRepository = new MysqlChessGameRepository(connectionManager); | ||
PieceRepository pieceRepository = new MysqlPieceRepository(connectionManager); | ||
ChessDao chessDao = new ChessDao(chessGameRepository, pieceRepository); | ||
ChessService chessService = new ChessService(chessDao); | ||
|
||
ChessGame chessGame = new ChessGame(INPUT_VIEW, OUTPUT_VIEW, chessService); | ||
run(chessGame); | ||
} | ||
|
||
private static void run(ChessGame chessGame) { | ||
try { | ||
chessGame.start(); | ||
chessGame.run(); | ||
} catch (IllegalArgumentException exception) { | ||
outputView.printExceptionMessage(exception); | ||
OUTPUT_VIEW.printExceptionMessage(exception); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,97 @@ | ||
package chess; | ||
|
||
import chess.domain.Board; | ||
import chess.domain.BoardFactory; | ||
import chess.domain.piece.Piece; | ||
import chess.domain.Team; | ||
import chess.domain.position.Position; | ||
import chess.dto.PieceDto; | ||
import chess.dto.ProgressStatus; | ||
import chess.view.GameCommand; | ||
import chess.view.InputView; | ||
import chess.view.OutputView; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class ChessGame { | ||
|
||
private final InputView inputView; | ||
private final OutputView outputView; | ||
private final ChessService chessService; | ||
|
||
public ChessGame(InputView inputView, OutputView outputView) { | ||
public ChessGame(InputView inputView, OutputView outputView, ChessService chessService) { | ||
this.inputView = inputView; | ||
this.outputView = outputView; | ||
this.chessService = chessService; | ||
} | ||
|
||
public void start() { | ||
public void run() { | ||
startGame(); | ||
play(); | ||
} | ||
|
||
private void startGame() { | ||
outputView.printStartGame(); | ||
GameCommand command = inputView.readCommand(); | ||
if (command.isStart()) { | ||
Board board = BoardFactory.createInitBoard(); | ||
showBoard(board); | ||
play(board); | ||
} | ||
if (command.isMove()) { | ||
throw new IllegalArgumentException("아직 게임을 시작하지 않았습니다."); | ||
initBoard(); | ||
showCurrentTeam(); | ||
showBoard(); | ||
return; | ||
} | ||
throw new IllegalArgumentException("아직 게임을 시작하지 않았습니다."); | ||
} | ||
|
||
private void play(Board board) { | ||
while (true) { | ||
processTurn(board); | ||
} | ||
private void initBoard() { | ||
chessService.init(); | ||
} | ||
|
||
private void showCurrentTeam() { | ||
Team turn = chessService.findCurrentTurn(); | ||
outputView.printCurrentTurn(turn); | ||
} | ||
|
||
private void processTurn(Board board) { | ||
private void play() { | ||
ProgressStatus status; | ||
do { | ||
status = processTurn(); | ||
} while (status.isContinue()); | ||
showResult(status); | ||
} | ||
|
||
private ProgressStatus processTurn() { | ||
GameCommand command = inputView.readCommand(); | ||
if (command.isStart()) { | ||
throw new IllegalArgumentException("이미 게임을 시작했습니다."); | ||
} | ||
if (command.isEnd()) { | ||
System.exit(0); | ||
Comment on lines
-50
to
-51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. System.exit(0) 를 사용하지 않은 건 좋군요!👍 |
||
if (command.isMove()) { | ||
return executeMove(); | ||
} | ||
if (command.isStatus()) { | ||
return executeStatus(); | ||
} | ||
executeMove(board); | ||
Comment on lines
+58
to
-53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if문으로 분기하니, 의도가 명확하게 보여서 좋은 것 같아요 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 커멘드가 5~6개로 늘어난다면, 다른 대안을 생각해볼 것 같아. 지금과 같이 생각한 이유는 '특정 커멘드에 따라 행동하는 것은 하나의 파일로 같이 있어야 하지 않을까?'라는 생각이 있었어. 그런데 커멘드가 5~6개로 늘어난다면 지금과 같이 한 파일로 보는 것이 더 힘들것 같네... |
||
return ProgressStatus.END_GAME; | ||
} | ||
|
||
private void executeMove(Board board) { | ||
private ProgressStatus executeMove() { | ||
Position start = inputView.readPosition(); | ||
Position end = inputView.readPosition(); | ||
board.move(start, end); | ||
showBoard(board); | ||
ProgressStatus status = chessService.moveTo(start, end); | ||
showBoard(); | ||
return status; | ||
} | ||
|
||
private void showBoard(Board board) { | ||
List<Position> positions = Position.ALL_POSITIONS; | ||
Map<Position, PieceDto> boardDto = new HashMap<>(); | ||
positions.forEach(position -> addPiece(board, position, boardDto)); | ||
outputView.printBoard(boardDto); | ||
private ProgressStatus executeStatus() { | ||
Map<Team, Double> statusDto = chessService.calculatePiecePoints(); | ||
outputView.printStatus(statusDto); | ||
return ProgressStatus.PROGRESS; | ||
} | ||
|
||
private void addPiece(Board board, Position position, Map<Position, PieceDto> boardDto) { | ||
Optional<Piece> optionalPiece = board.find(position); | ||
|
||
if (optionalPiece.isEmpty()) { | ||
private void showResult(ProgressStatus status) { | ||
if (status.isInputEndCommand()) { | ||
return; | ||
} | ||
outputView.printWinnerMessage(status); | ||
} | ||
|
||
Piece piece = optionalPiece.get(); | ||
PieceDto pieceDto = PieceDto.from(piece); | ||
boardDto.put(position, pieceDto); | ||
private void showBoard() { | ||
Map<Position, PieceDto> boardDto = chessService.findTotalBoard(); | ||
outputView.printBoard(boardDto); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
테이블 명이나 컬럼명은 소문자로 하고 예약어는 대문자 컨벤션을 유지한거 같은데 not null은 소문자인 이유 궁금하군요!👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
어 그러게요 ㅎ;
대문자로 통일하는게 좋겠네요 ㅎㅎ