Skip to content
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

Merged
merged 45 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
f7efb30
refactor : index 접근 상수 enum으로 만들기
java-saeng Mar 22, 2023
ddeac08
fix : move a22 a44 로 입력할 경우 예외 처리
java-saeng Mar 22, 2023
7ce52f3
feat : 값 객체 Score 추가
java-saeng Mar 22, 2023
5137cb1
feat : Score에 곱셈 추가
java-saeng Mar 22, 2023
b20331a
feat : 피스들의 합을 구하는 기능 구현
java-saeng Mar 22, 2023
d126e10
feat : 색깔에 따라 모든 보드 위의 기물들의 합을 구하는 기능 구현
java-saeng Mar 22, 2023
0a921db
feat : Status Command 추가
java-saeng Mar 22, 2023
9e5b907
feat : status 입력 시 점수 출력 및 이긴 팀 출력
java-saeng Mar 22, 2023
9ec979a
feat : king 이 죽으면 게임 종료 구현
java-saeng Mar 22, 2023
01503f1
refactor : 패키지 위치 변경
java-saeng Mar 22, 2023
89fc213
feat : Board CRUD 구현
java-saeng Mar 22, 2023
4d495b9
refactor : 데이터베이스 요청에 따라 Dao 추가
java-saeng Mar 23, 2023
ae10041
feat : Database의 Data Type과 객체의 Data Type 불일치를 해결하기 위해 BoardMapper 추가
java-saeng Mar 23, 2023
2a9f93e
feat : Board를 조회하는 service 생성
java-saeng Mar 23, 2023
da8bd1b
feat : Board 를 저장하는 Service 추가
java-saeng Mar 23, 2023
0ed9430
feat : Board 를 수정하는 Service 추가
java-saeng Mar 23, 2023
0bdb5cc
feat : Board 를 삭제하는 Service 추가
java-saeng Mar 23, 2023
60cce6a
refactor : 보드 저장 시 PK 반환
java-saeng Mar 23, 2023
cdb8d98
feat : 사용자가 참여하고 있는 보드를 모두 출력하는 기능 구현
java-saeng Mar 24, 2023
7d3c0b1
feat : 체스 게임 데이터베이스 저장
java-saeng Mar 24, 2023
fa4f2c7
refactor : 문자열, 매직 넘버 상수화
java-saeng Mar 24, 2023
b729273
feat : 피스 점수 enum
java-saeng Mar 24, 2023
89fefb9
refactor : if 중괄호 추가
java-saeng Mar 24, 2023
af353d3
refactor : 방어적 복사 사용
java-saeng Mar 24, 2023
616db10
refactor : 필요없는 괄호 삭제
java-saeng Mar 24, 2023
77f907e
refactor : 메서드 extract
java-saeng Mar 24, 2023
4922e2d
refactor : 스트림 사용하지 않고 바로 비교해서 return 하기
java-saeng Mar 26, 2023
3689f24
refactor : 개행 제거
java-saeng Mar 26, 2023
eb726bd
feat : Board 에 Turn 추가
java-saeng Mar 26, 2023
17b7bbf
feat : BoardDao 에서 데이터를 영속화
java-saeng Mar 26, 2023
1d4fe09
feat : BoardMapper 변경
java-saeng Mar 26, 2023
e510493
feat : BoardQueryService 메서드 반환형 변경
java-saeng Mar 27, 2023
7ef3dff
feat : BoardCommandService 메서드 반환형 변경
java-saeng Mar 27, 2023
d1ac9af
remove : 잘못 사용한 DAO 삭제, Repository 삭제
java-saeng Mar 27, 2023
70ab27a
refactor : 필요없는 메서드 삭제
java-saeng Mar 27, 2023
3953e8d
feat : Board 등록 시 Service에 Board 넘겨주기
java-saeng Mar 27, 2023
f004e29
feat : Board 수정 시 Service에 Board, BoardId 넘기기
java-saeng Mar 27, 2023
e250c1c
feat : Board 에서 turn 인스턴스 필드 갖기
java-saeng Mar 27, 2023
2f96883
feat : Board에 id 추가
java-saeng Mar 27, 2023
995d7fe
refactor : DB 길이 최소화 시키기
java-saeng Mar 27, 2023
b8b9bf0
refactor : controller method extract
java-saeng Mar 27, 2023
fbc9a63
style : pr 제출 전 reformatting
java-saeng Mar 27, 2023
02ad7bf
feat : db 초기화 셋팅
java-saeng Mar 27, 2023
10c8eba
refactor : move 시 return 삭제
java-saeng Mar 31, 2023
8630166
refactor : status, end 시 게임 저장 추가
java-saeng Mar 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions 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/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
Comment on lines +1 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안그래도 ddl 올려달라고 말하는걸 까먹고 못말했는데 덕분에 편하게 했습니다 👍

14 changes: 14 additions & 0 deletions src/main/java/chess/ChessApplication.java
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();
}
}
9 changes: 0 additions & 9 deletions src/main/java/chess/application/ChessApplication.java

This file was deleted.

22 changes: 22 additions & 0 deletions src/main/java/chess/common/IndexCommand.java
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;
}
}
25 changes: 25 additions & 0 deletions src/main/java/chess/config/ChessConfigure.java
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());
}
}
131 changes: 107 additions & 24 deletions src/main/java/chess/controller/ChessController.java
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;
Copy link
Member

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return 때문에 게임 진행이 안되고 있네요.

Copy link
Author

Choose a reason for hiding this comment

The 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()));
}
}
Loading