-
Notifications
You must be signed in to change notification settings - Fork 416
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단계 - 체스] 우르(김현우) 미션 제출합니다. #539
Changes from 43 commits
f7efb30
ddeac08
7ce52f3
5137cb1
b20331a
d126e10
0a921db
9e5b907
9ec979a
01503f1
89fc213
4d495b9
ae10041
2a9f93e
da8bd1b
0ed9430
0bdb5cc
60cce6a
cdb8d98
7d3c0b1
fa4f2c7
b729273
89fefb9
af353d3
616db10
77f907e
4922e2d
3689f24
eb726bd
17b7bbf
1d4fe09
e510493
7ef3dff
d1ac9af
70ab27a
3953e8d
f004e29
e250c1c
2f96883
995d7fe
b8b9bf0
fbc9a63
02ad7bf
10c8eba
8630166
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
version: "3.9" | ||
services: | ||
db: | ||
image: mysql:8.0.28 | ||
platform: linux/amd64 # mac m1 | ||
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,14 @@ | ||
package chess; | ||
|
||
import chess.config.ChessConfigure; | ||
import chess.controller.ChessController; | ||
|
||
public class ChessApplication { | ||
public static void main(String[] args) { | ||
|
||
final ChessConfigure chessConfigure = new ChessConfigure(); | ||
|
||
new ChessController(chessConfigure.boardQueryService(), | ||
chessConfigure.boardCommandService()).run(); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package chess.common; | ||
|
||
public enum IndexCommand { | ||
|
||
START_COMMAND_INDEX(0), | ||
SOURCE_POSITION(1), | ||
TARGET_POSITION(2), | ||
POSITION_COLUMN(0), | ||
POSITION_ROW(1), | ||
POSITION_INDEX(1), | ||
PIECE_INDEX(0); | ||
|
||
private final int value; | ||
|
||
IndexCommand(final int value) { | ||
this.value = value; | ||
} | ||
|
||
public int value() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package chess.config; | ||
|
||
import chess.dao.BoardDao; | ||
import chess.domain.board.service.BoardCommandService; | ||
import chess.domain.board.service.BoardQueryService; | ||
import chess.domain.board.service.mapper.BoardMapper; | ||
|
||
public class ChessConfigure { | ||
|
||
public BoardDao boardDao() { | ||
return new BoardDao(); | ||
} | ||
|
||
public BoardMapper boardMapper() { | ||
return new BoardMapper(); | ||
} | ||
|
||
public BoardQueryService boardQueryService() { | ||
return new BoardQueryService(boardDao(), boardMapper()); | ||
} | ||
|
||
public BoardCommandService boardCommandService() { | ||
return new BoardCommandService(boardDao(), boardMapper()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,151 @@ | ||
package chess.controller; | ||
|
||
import chess.domain.board.Board; | ||
import chess.domain.board.BoardFactory; | ||
import chess.domain.board.Turn; | ||
import chess.domain.board.factory.BoardFactory; | ||
import chess.domain.board.position.Position; | ||
import chess.domain.board.score.BoardScore; | ||
import chess.domain.board.score.Score; | ||
import chess.domain.board.search.BoardSearch; | ||
import chess.domain.board.service.BoardCommandService; | ||
import chess.domain.board.service.BoardQueryService; | ||
import chess.domain.piece.Color; | ||
import chess.view.Command; | ||
import chess.view.InputView; | ||
import chess.view.OutputView; | ||
|
||
import java.util.List; | ||
|
||
import static chess.common.IndexCommand.POSITION_COLUMN; | ||
import static chess.common.IndexCommand.POSITION_ROW; | ||
import static chess.common.IndexCommand.SOURCE_POSITION; | ||
import static chess.common.IndexCommand.START_COMMAND_INDEX; | ||
import static chess.common.IndexCommand.TARGET_POSITION; | ||
|
||
public class ChessController { | ||
|
||
private static final int START_COMMAND_INDEX = 0; | ||
private static final int SOURCE_POSITION = 1; | ||
private static final int TARGET_POSITION = 2; | ||
private static final int POSITION_COLUMN = 0; | ||
private static final int POSITION_ROW = 1; | ||
private final BoardQueryService boardQueryService; | ||
private final BoardCommandService boardCommandService; | ||
|
||
public ChessController(final BoardQueryService boardQueryService, | ||
final BoardCommandService boardCommandService) { | ||
this.boardQueryService = boardQueryService; | ||
this.boardCommandService = boardCommandService; | ||
} | ||
|
||
public void run() { | ||
final BoardFactory boardFactory = new BoardFactory(); | ||
final Board board = new Board(boardFactory); | ||
System.out.println("새로운 게임을 시작하려면 1, 게임을 불러오려면 2를 누르세요."); | ||
|
||
final String newGameCommand = InputView.readNewGameCommand(); | ||
final Board board = loadChessBoard(newGameCommand); | ||
|
||
final String command = InputView.readStartCommand(); | ||
|
||
if (Command.isNotStart(command)) { | ||
return; | ||
} | ||
|
||
final Color turn = Color.WHITE; | ||
startGame(board, turn); | ||
startGame(board); | ||
} | ||
|
||
private Board loadChessBoard(final String newGameCommand) { | ||
|
||
if (Command.isNewGame(newGameCommand)) { | ||
|
||
final Board board = Board.makeNewGame(new BoardFactory()); | ||
final Long savedId = boardCommandService.registerBoard(board); | ||
|
||
board.assignId(savedId); | ||
|
||
return board; | ||
} | ||
|
||
if (Command.isExistedGame(newGameCommand)) { | ||
System.out.println("현재 진행하고 있는 게임 번호들입니다."); | ||
|
||
final List<Long> boardIds = boardQueryService.searchAllBoards(); | ||
|
||
OutputView.printGameCandidates(boardIds); | ||
|
||
System.out.println("불러올 게임의 번호를 입력해주세요."); | ||
|
||
final long loadGameId = InputView.readLoadGameCommand(); | ||
|
||
return boardQueryService.searchBoard(loadGameId); | ||
} | ||
|
||
throw new IllegalArgumentException("올바르지 않은 command 입니다."); | ||
} | ||
|
||
private void startGame(final Board board, Color turn) { | ||
private void startGame(final Board board) { | ||
|
||
while (true) { | ||
OutputView.printBoard(board.chessBoard()); | ||
|
||
final List<String> moveCommand = InputView.readMoveCommand(); | ||
|
||
final String startCommand = moveCommand.get(START_COMMAND_INDEX); | ||
final List<String> moveCommands = InputView.readMoveCommand(); | ||
final String startCommand = moveCommands.get(START_COMMAND_INDEX.value()); | ||
|
||
if (Command.isEnd(startCommand)) { | ||
break; | ||
end(board); | ||
return; | ||
} | ||
|
||
turn = movePiece(board, turn, moveCommand, startCommand); | ||
if (Command.isStatus(startCommand)) { | ||
status(board); | ||
return; | ||
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. status 를 봐도 게임이 꺼지고 있습니다. |
||
} | ||
|
||
final Turn beforeTurn = board.turn(); | ||
|
||
if (Command.isMove(startCommand)) { | ||
move(board, moveCommands); | ||
return; | ||
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. return 때문에 게임 진행이 안되고 있네요. 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. 으악,, 죄송합니다 !! 바로 수정하겠습니다 |
||
} | ||
|
||
final BoardSearch boardSearch = BoardSearch.countPiecePerClassTypeFrom(board.chessBoard()); | ||
|
||
if (boardSearch.isKingDead()) { | ||
final BoardScore boardScore = BoardScore.flatByColumnFrom(board.chessBoard()); | ||
OutputView.printWinner(beforeTurn.color(), boardScore.calculateBoardScoreBy(beforeTurn.color())); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
private Color movePiece(final Board board, Color turn, final List<String> moveCommands, final String startCommand) { | ||
if (Command.isMove(startCommand)) { | ||
final Position fromPosition = convertPositionFrom(moveCommands.get(SOURCE_POSITION)); | ||
final Position toPosition = convertPositionFrom(moveCommands.get(TARGET_POSITION)); | ||
private void end(final Board board) { | ||
boardCommandService.modifyBoard(board); | ||
} | ||
|
||
private void status(final Board board) { | ||
final BoardScore boardScore = BoardScore.flatByColumnFrom(board.chessBoard()); | ||
|
||
final Score blackScore = boardScore.calculateBoardScoreBy(Color.BLACK); | ||
final Score whiteScore = boardScore.calculateBoardScoreBy(Color.WHITE); | ||
|
||
OutputView.printWinner(whoIsWinner(blackScore, whiteScore), whiteScore, blackScore); | ||
boardCommandService.modifyBoard(board); | ||
} | ||
|
||
board.move(fromPosition, toPosition, turn); | ||
private void move(final Board board, final List<String> moveCommands) { | ||
final Position fromPosition = convertPositionFrom(moveCommands.get(SOURCE_POSITION.value())); | ||
final Position toPosition = convertPositionFrom(moveCommands.get(TARGET_POSITION.value())); | ||
|
||
board.move(fromPosition, toPosition); | ||
} | ||
|
||
private Color whoIsWinner(final Score blackScore, final Score whiteScore) { | ||
if (blackScore.isGreaterThan(whiteScore)) { | ||
return Color.BLACK; | ||
} | ||
|
||
turn = turn.opposite(); | ||
if (blackScore.equals(whiteScore)) { | ||
return Color.NONE; | ||
} | ||
|
||
return turn; | ||
return Color.WHITE; | ||
} | ||
|
||
private Position convertPositionFrom(String moveCommand) { | ||
return new Position(moveCommand.charAt(POSITION_COLUMN), moveCommand.charAt(POSITION_ROW)); | ||
return new Position(moveCommand.charAt(POSITION_COLUMN.value()), moveCommand.charAt(POSITION_ROW.value())); | ||
} | ||
} |
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.
안그래도 ddl 올려달라고 말하는걸 까먹고 못말했는데 덕분에 편하게 했습니다 👍