-
Notifications
You must be signed in to change notification settings - Fork 178
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
[무늬] 체스 스프링 4단계 미션 제출합니다. #186
Merged
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
42d3810
[스티치] 체스 스프링 실습 1단계 미션 제출입니다 (#37)
lxxjn0 4834662
[스티치] 체스 스프링 실습 2단계 미션 제출입니다 (#87)
lxxjn0 0ee87df
feat : Spring Data JDBC 적용
lxxjn0 8edb79f
feat : GameHistory, GameRoom Entity 구현
lxxjn0 f63dbb0
refactor : 게임방 생성과 새로운 방을 생성할 수 있도록 index.hbs 수정
lxxjn0 97e2920
refactor : 종료된 게임 표시 및 왕이 잡히면 게임이 종료 되도록 수정
lxxjn0 9c6e690
Merge branch 'step4-temp' of https://github.com/lxxjn0/jwp-chess into…
JinJuMoon a5938ab
refactor : ConsoleApplication 관련 패키지 및 클래스 삭제
JinJuMoon 17ec337
refactor : Service 흐름에 따라 메서드 순서 변경
JinJuMoon 06ac56e
feat : DB에 GameRoom이 존재하지 않는 경우 예외 발생 기능 구현
JinJuMoon 51f78a0
feat: MockMvc를 활용한 컨트롤러 테스트
JinJuMoon 82ae630
chore : 불필요한 todo 주석 제거
JinJuMoon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package wooteco.chess; | ||
|
||
import wooteco.chess.controller.ConsoleChessController; | ||
import wooteco.chess.domain.chessBoard.ChessBoard; | ||
import wooteco.chess.domain.chessBoard.ChessBoardInitializer; | ||
import wooteco.chess.domain.chessGame.ChessCommand; | ||
import wooteco.chess.domain.chessGame.ChessGame; | ||
import wooteco.chess.util.StringUtil; | ||
import wooteco.chess.view.ConsoleInputView; | ||
import wooteco.chess.view.ConsoleOutputView; | ||
|
||
import java.util.List; | ||
|
||
public class ConsoleChessApplication { | ||
|
||
public static void main(String[] args) { | ||
ChessBoard chessBoard = new ChessBoard(ChessBoardInitializer.create()); | ||
ChessGame chessGame = ChessGame.from(chessBoard); | ||
ConsoleChessController consoleChessController = new ConsoleChessController(chessGame); | ||
|
||
ConsoleOutputView.printChessStart(); | ||
if (isStartChessCommand()) { | ||
consoleChessController.run(); | ||
} | ||
ConsoleOutputView.printChessEnd(); | ||
} | ||
|
||
private static boolean isStartChessCommand() { | ||
List<String> commandArguments = StringUtil.splitChessCommand(ConsoleInputView.inputChessCommand()); | ||
|
||
if (!ChessCommand.of(commandArguments).isStartChessCommand()) { | ||
throw new IllegalArgumentException("게임을 시작해야 입력 가능한 명령어입니다."); | ||
} | ||
return true; | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/wooteco/chess/controller/ConsoleChessController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package wooteco.chess.controller; | ||
|
||
import wooteco.chess.domain.chessGame.ChessCommand; | ||
import wooteco.chess.domain.chessGame.ChessGame; | ||
import wooteco.chess.util.ChessBoardRenderer; | ||
|
||
import java.util.Objects; | ||
|
||
import static wooteco.chess.util.StringUtil.splitChessCommand; | ||
import static wooteco.chess.view.ConsoleInputView.inputChessCommand; | ||
import static wooteco.chess.view.ConsoleOutputView.*; | ||
|
||
public class ConsoleChessController { | ||
|
||
private final ChessGame chessGame; | ||
|
||
public ConsoleChessController(ChessGame chessGame) { | ||
Objects.requireNonNull(chessGame, "체스 게임이 null입니다."); | ||
this.chessGame = chessGame; | ||
} | ||
|
||
public void run() { | ||
do { | ||
printChessBoard(ChessBoardRenderer.render(chessGame.getChessBoard())); | ||
|
||
ChessCommand chessCommand = receiveChessCommand(); | ||
playChessGameBy(chessCommand); | ||
} while (!isEndState()); | ||
} | ||
|
||
private ChessCommand receiveChessCommand() { | ||
ChessCommand chessCommand = ChessCommand.of(splitChessCommand(inputChessCommand())); | ||
|
||
if (chessCommand.isStartChessCommand()) { | ||
throw new IllegalArgumentException("start 명령어은 최초 시작 때만 사용 가능합니다."); | ||
} | ||
return chessCommand; | ||
} | ||
|
||
private void playChessGameBy(ChessCommand chessCommand) { | ||
if (chessCommand.isMoveChessCommand()) { | ||
chessGame.move(chessCommand); | ||
} | ||
if (chessCommand.isStatusChessCommand()) { | ||
double score = chessGame.status(chessCommand); | ||
printStatus(chessCommand.getStatusPieceColor(), score); | ||
} | ||
if (chessCommand.isEndChessCommand()) { | ||
chessGame.end(); | ||
} | ||
} | ||
|
||
private boolean isEndState() { | ||
if (!chessGame.isEndState()) { | ||
return false; | ||
} | ||
return checkKingCaught(); | ||
} | ||
|
||
private boolean checkKingCaught() { | ||
if (chessGame.isKingCaught()) { | ||
printKingCaught(chessGame.getCurrentPieceColor()); | ||
} | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,17 +23,18 @@ private ChessGameDto(final Long id, final ChessBoardDto chessBoardDto, final Pie | |
this.isKingCaught = isKingCaught; | ||
} | ||
|
||
public static ChessGameDto of(final Long id, final ChessGame chessGame) { | ||
Objects.requireNonNull(chessGame, "체스 게임이 null입니다."); | ||
|
||
final ChessBoardDto chessBoardDto = ChessBoardDto.of(chessGame.getChessBoard()); | ||
final PieceColorDto pieceColorDto = PieceColorDto.of(chessGame.getCurrentPieceColor()); | ||
final ChessStatusDtos chessStatusDtos = ChessStatusDtos.of(chessGame.getChessGameStatus()); | ||
final boolean isEndStatus = chessGame.isEndState(); | ||
final boolean isKingCaught = chessGame.isKingCaught(); | ||
|
||
return new ChessGameDto(id, chessBoardDto, pieceColorDto, chessStatusDtos, isEndStatus, isKingCaught); | ||
} | ||
// NOTE: 2020/04/28 DTO가 관리해야하는 필드의 종류 | ||
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. 무슨 의미의 주석이신가요? 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 ChessGameDto of(final Long id, final ChessGame chessGame) { | ||
Objects.requireNonNull(chessGame, "체스 게임이 null입니다."); | ||
|
||
final ChessBoardDto chessBoardDto = ChessBoardDto.of(chessGame.getChessBoard()); | ||
final PieceColorDto pieceColorDto = PieceColorDto.of(chessGame.getCurrentPieceColor()); | ||
final ChessStatusDtos chessStatusDtos = ChessStatusDtos.of(chessGame.getChessGameStatus()); | ||
final boolean isEndStatus = chessGame.isEndState(); | ||
final boolean isKingCaught = chessGame.isKingCaught(); | ||
|
||
return new ChessGameDto(id, chessBoardDto, pieceColorDto, chessStatusDtos, isEndStatus, isKingCaught); | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package wooteco.chess.service.dto; | ||
|
||
import wooteco.chess.entity.GameRoom; | ||
|
||
import java.util.Objects; | ||
|
||
public class GameRoomDto { | ||
|
||
private Long id; | ||
private String name; | ||
private Boolean state; | ||
|
||
private GameRoomDto(final Long id, final String name, final Boolean state) { | ||
this.id = id; | ||
this.name = name; | ||
this.state = state; | ||
} | ||
|
||
public static GameRoomDto of(final GameRoom gameRoom) { | ||
Objects.requireNonNull(gameRoom, "게임이 null입니다."); | ||
return new GameRoomDto(gameRoom.getId(), gameRoom.getName(), gameRoom.getState()); | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Boolean getState() { | ||
return state; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package wooteco.chess.util; | ||
|
||
import wooteco.chess.domain.chessBoard.ChessBoard; | ||
import wooteco.chess.domain.position.ChessFile; | ||
import wooteco.chess.domain.position.ChessRank; | ||
import wooteco.chess.domain.position.Position; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import static java.util.stream.Collectors.*; | ||
|
||
public class ChessBoardRenderer { | ||
|
||
private static final String EMPTY_SPACE = ""; | ||
private static final String DELIMITER = " "; | ||
private static final String RANK_APPEND_FORMAT = "%s %s"; | ||
private static final String EMPTY_POSITION = "."; | ||
|
||
public static List<String> render(final ChessBoard chessBoard) { | ||
Objects.requireNonNull(chessBoard, "체스 보드가 null입니다."); | ||
final List<String> renderedChessBoard = renderEachChessRankFrom(chessBoard); | ||
|
||
Collections.reverse(renderedChessBoard); | ||
appendChessFileName(renderedChessBoard); | ||
return renderedChessBoard; | ||
} | ||
|
||
private static List<String> renderEachChessRankFrom(final ChessBoard chessBoard) { | ||
return Arrays.stream(ChessRank.values()) | ||
.map(chessRank -> renderChessRankFrom(chessBoard, chessRank)) | ||
.collect(toList()); | ||
} | ||
|
||
private static String renderChessRankFrom(final ChessBoard chessBoard, final ChessRank chessRank) { | ||
return Arrays.stream(ChessFile.values()) | ||
.map(chessFile -> renderChessPieceFrom(chessBoard, chessRank, chessFile)) | ||
.collect(collectingAndThen(joining(DELIMITER), | ||
renderedRank -> String.format(RANK_APPEND_FORMAT, renderedRank, chessRank))); | ||
} | ||
|
||
private static String renderChessPieceFrom(final ChessBoard chessBoard, final ChessRank chessRank, | ||
final ChessFile chessFile) { | ||
final Position renderingPosition = Position.of(chessFile, chessRank); | ||
|
||
if (chessBoard.isChessPieceOn(renderingPosition)) { | ||
return chessBoard.getChessPieceNameOn(renderingPosition); | ||
} | ||
return EMPTY_POSITION; | ||
} | ||
|
||
private static void appendChessFileName(final List<String> renderedChessBoard) { | ||
renderedChessBoard.add(EMPTY_SPACE); | ||
renderedChessBoard.add(Arrays.stream(ChessFile.values()) | ||
.map(ChessFile::toString) | ||
.collect(joining(DELIMITER))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package wooteco.chess.util; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
public class StringUtil { | ||
|
||
private static final String DELIMITER = " "; | ||
|
||
public static List<String> splitChessCommand(final String chessCommand) { | ||
Objects.requireNonNull(chessCommand, "분리할 명령어가 null입니다."); | ||
|
||
return Arrays.stream(chessCommand.split(DELIMITER)) | ||
.map(String::trim) | ||
.collect(toList()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package wooteco.chess.view; | ||
|
||
import java.util.Scanner; | ||
|
||
public class ConsoleInputView { | ||
|
||
private static final Scanner SCANNER = new Scanner(System.in); | ||
|
||
public static String inputChessCommand() { | ||
System.out.print(ConsoleOutputView.PROMPT); | ||
return SCANNER.nextLine(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package wooteco.chess.view; | ||
|
||
import wooteco.chess.domain.chessPiece.pieceType.PieceColor; | ||
|
||
import java.util.List; | ||
|
||
public class ConsoleOutputView { | ||
|
||
static final String PROMPT = "> "; | ||
|
||
private static void printNewLine() { | ||
System.out.println(); | ||
} | ||
|
||
public static void printChessStart() { | ||
System.out.println(PROMPT + "체스 게임을 시작합니다."); | ||
System.out.println(PROMPT + "게임 시작 : start"); | ||
System.out.println(PROMPT + "게임 이동 : move source위치 target위치 - 예. move b2 b3"); | ||
System.out.println(PROMPT + "게임 점수 : status 체스 색상 - 예. status white, status black"); | ||
System.out.println(PROMPT + "게임 종료 : end"); | ||
} | ||
|
||
public static void printChessBoard(List<String> renderedChessBoard) { | ||
printNewLine(); | ||
renderedChessBoard.forEach(System.out::println); | ||
} | ||
|
||
public static void printStatus(PieceColor statusPieceColor, double score) { | ||
System.out.println(String.format("%s 점수 : %.1f", statusPieceColor.getColor(), score)); | ||
} | ||
|
||
public static void printKingCaught(PieceColor catchingPieceColor) { | ||
System.out.println(String.format("%s가 킹을 잡았습니다.", catchingPieceColor.getColor())); | ||
} | ||
|
||
public static void printChessEnd() { | ||
System.out.println(PROMPT + "게임 종료!"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
엥 갑자기 분위기 콘솔인가요??
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.
엇! 브랜치를 병합하는 과정에서 다시 추가된 것 같습니다. 콘솔프로그램 관련 클래스 모두 삭제했습니다~ 😄