-
Notifications
You must be signed in to change notification settings - Fork 467
[1,2,3단계 - 체스] 포츈(정윤성) 미션 제출합니다. #197
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
Changes from all commits
7dde154
18cb973
668d201
931ea4c
07ef8a7
46db8cf
4acb1be
d2d3d48
3e93f5d
47bd99f
ae22a87
e42c849
eb93d2c
4d11549
37e9d26
50cc856
d4740c3
65f6251
db0b509
82b89c9
2500577
38b9fde
dafdb18
9332cc7
47babd3
b3ebe19
2044845
d29f309
a4973a2
1b90dd9
09cf32b
c5d6323
7058cd9
5187175
bd7ca3b
b0162b5
db367e7
2a1c23c
b49ec90
9001be0
6485e0e
abc4b19
caa4d1b
816d7c5
60393f0
ed5c057
7ea33b6
b697952
cd19b86
dba3ca6
a02b562
06c837d
8341be6
d6931b3
f09e8ea
4ff7dc3
1a3b663
28bd722
abab70e
786175c
cdc3c35
9e850ac
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 |
---|---|---|
|
@@ -18,4 +18,5 @@ bin/ | |
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
*.ipr | ||
/src/main/java/docs/TODO.md |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package chess; | ||
|
||
import chess.controller.ChessGameController; | ||
|
||
public class App { | ||
public static void main(String[] args) { | ||
ChessGameController chessGameController = new ChessGameController(); | ||
chessGameController.start(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package chess; | ||
|
||
import chess.domain.Team; | ||
import chess.domain.board.Board; | ||
import chess.domain.board.BoardFactory; | ||
import chess.domain.position.Position; | ||
|
||
public final class ChessGame { | ||
private final Board board; | ||
private boolean isPlaying = true; | ||
private Team currentTurnTeam; | ||
|
||
public ChessGame() { | ||
this(new BoardFactory().getBoard(), Team.WHITE); | ||
} | ||
|
||
public ChessGame(final Board board, final Team team) { | ||
this.board = board; | ||
currentTurnTeam = team; | ||
} | ||
|
||
public void end() { | ||
isPlaying = false; | ||
} | ||
|
||
public boolean isPlaying() { | ||
return isPlaying; | ||
} | ||
|
||
public Board getBoard() { | ||
return board; | ||
} | ||
|
||
public final void move(final Position startPoint, final Position endPoint) { | ||
board.move(startPoint, endPoint, currentTurnTeam); | ||
if (board.isEnemyKingDie(currentTurnTeam)) { | ||
end(); | ||
} | ||
currentTurnTeam = Team.getAnotherTeam(currentTurnTeam); | ||
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. 원래 controller가 하고있던 턴 교체를 ChessGame이 수행하게 바꾸었어요 |
||
} | ||
|
||
public double getScoreByTeam(final Team team) { | ||
return board.scoreByTeam(team); | ||
} | ||
|
||
public boolean isKingDieEnd() { | ||
return board.isEnemyKingDie(currentTurnTeam) || board.isEnemyKingDie(Team.getAnotherTeam(currentTurnTeam)); | ||
} | ||
|
||
public Team winner() { | ||
if (board.isEnemyKingDie(currentTurnTeam)) { | ||
return currentTurnTeam; | ||
} | ||
return Team.getAnotherTeam(currentTurnTeam); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package chess.controller; | ||
|
||
import chess.ChessGame; | ||
import chess.domain.Team; | ||
import chess.domain.position.Position; | ||
import chess.domain.util.ColumnConverter; | ||
import chess.domain.util.RowConverter; | ||
import chess.view.Command; | ||
import chess.view.InputView; | ||
import chess.view.OutputView; | ||
|
||
import java.util.Objects; | ||
|
||
public final class ChessGameController { | ||
private ChessGame chessGame; | ||
|
||
public void start() { | ||
OutputView.printStartMessage(); | ||
run(); | ||
} | ||
|
||
private void run() { | ||
do { | ||
turnExecute(); | ||
} while (Objects.nonNull(chessGame) && chessGame.isPlaying()); | ||
|
||
if (Objects.nonNull(chessGame) && chessGame.isKingDieEnd()) { | ||
OutputView.printWinner(chessGame.winner(), chessGame.getScoreByTeam(Team.BLACK), chessGame.getScoreByTeam(Team.WHITE)); | ||
} | ||
} | ||
|
||
private void turnExecute() { | ||
try { | ||
Command command = Command.valueOf(InputView.getCommand()); | ||
commandExecute(command); | ||
} catch (IllegalArgumentException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
|
||
private void commandExecute(final Command command) { | ||
if (Objects.isNull(chessGame) && command.equals(Command.MOVE)) { | ||
OutputView.printNoStartMessage(); | ||
turnExecute(); | ||
return; | ||
} | ||
interactiveCommand(command); | ||
unInteractiveCommand(command); | ||
printCurrentBoard(command); | ||
} | ||
|
||
private void unInteractiveCommand(final Command command) { | ||
if (command.equals(Command.START)) { | ||
chessGame = new ChessGame(); | ||
} | ||
if (command.equals(Command.END) && Objects.nonNull(chessGame)) { | ||
chessGame.end(); | ||
} | ||
} | ||
|
||
private void interactiveCommand(final Command command) { | ||
if (command.equals(Command.MOVE)) { | ||
move(); | ||
} | ||
if (command.equals(Command.STATUS)) { | ||
OutputView.printEachTeamScore(chessGame.getScoreByTeam(Team.BLACK), chessGame.getScoreByTeam(Team.WHITE)); | ||
} | ||
} | ||
|
||
private void printCurrentBoard(final Command command) { | ||
if (!command.isPrintCommand()) { | ||
return; | ||
} | ||
printBoard(); | ||
} | ||
|
||
private void move() { | ||
String startPoint = InputView.getPoint(); | ||
String endPoint = InputView.getPoint(); | ||
|
||
Position startPosition = position(startPoint); | ||
Position endPosition = position(endPoint); | ||
chessGame.move(startPosition, endPosition); | ||
} | ||
|
||
|
||
private Position position(final String point) { | ||
return new Position( | ||
RowConverter.getLocation(String.valueOf(point.charAt(1))), | ||
ColumnConverter.getLocation(String.valueOf(point.charAt(0))) | ||
); | ||
} | ||
|
||
private void printBoard() { | ||
OutputView.printBoard(chessGame.getBoard()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package chess.domain; | ||
|
||
import chess.domain.util.RowConverter; | ||
|
||
public enum Team { | ||
BLACK(RowConverter.getLocation("7")), | ||
WHITE(RowConverter.getLocation("2")); | ||
|
||
private final int pawnInitRow; | ||
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. 전체적으로 만들어둔 enum 등을 사용하는 것이 아닌 원시값으로 가지고 있으신데 이유가 있을까요? 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. ui 에 보이는것과 동일한 값을 넣어주는것을 의도해서 저렇게 해두었었는데, 지금 보니 저 도메인은 플레이어가 접근할 가능성이 없으니 |
||
|
||
Team(final int pawnInitRow) { | ||
this.pawnInitRow = pawnInitRow; | ||
} | ||
|
||
public static Team getAnotherTeam(final Team userTeam) { | ||
if (Team.BLACK.equals(userTeam)) { | ||
return WHITE; | ||
} | ||
return BLACK; | ||
} | ||
|
||
public boolean isInitPawn(final int row) { | ||
return pawnInitRow == row; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package chess.domain.board; | ||
|
||
import chess.domain.Team; | ||
import chess.domain.pieces.Piece; | ||
import chess.domain.pieces.Pieces; | ||
import chess.domain.position.Position; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public final class Board { | ||
public static final int RANGE_MIN_PIVOT = 0; | ||
public static final int RANGE_MAX_PIVOT = 7; | ||
|
||
private final Map<Team, Pieces> board; | ||
|
||
public Board(final Map<Team, Pieces> board) { | ||
this.board = new HashMap<>(board); | ||
} | ||
|
||
public Map<Team, Pieces> toMap() { | ||
return new HashMap<>(board); | ||
} | ||
|
||
public void move(final Position startPoint, final Position endPoint, final Team team) { | ||
Pieces pieces = board.get(team); | ||
Piece startPointPiece = pieces.getPieceByPosition(startPoint); | ||
startPointPiece.move(this, endPoint); | ||
} | ||
|
||
public final Pieces piecesByTeam(final Team team) { | ||
return board.get(team); | ||
} | ||
|
||
public boolean validateRange(final int row, final int col) { | ||
return !(row < RANGE_MIN_PIVOT || row > RANGE_MAX_PIVOT || col < RANGE_MIN_PIVOT || col > RANGE_MAX_PIVOT); | ||
} | ||
|
||
public boolean isEnemyKingDie(final Team team) { | ||
Pieces enemyPieces = board.get(Team.getAnotherTeam(team)); | ||
return !enemyPieces.kingAlive(); | ||
} | ||
|
||
public double scoreByTeam(final Team team) { | ||
Pieces pieces = board.get(team); | ||
return pieces.calculateScore(RANGE_MIN_PIVOT, RANGE_MAX_PIVOT); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package chess.domain.board; | ||
|
||
import chess.domain.Team; | ||
import chess.domain.pieces.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public final class BoardFactory { | ||
private final Map<Team, Pieces> board = new HashMap<>(); | ||
|
||
public BoardFactory() { | ||
initSetting(); | ||
} | ||
|
||
public Board getBoard() { | ||
return new Board(board); | ||
} | ||
|
||
private void initSetting() { | ||
List<Piece> black = new ArrayList<>(); | ||
List<Piece> white = new ArrayList<>(); | ||
|
||
pawnInitSetting(black, white); | ||
rookInitSetting(black, white); | ||
knightInitSetting(black, white); | ||
bishopInitSetting(black, white); | ||
queenInitSetting(black, white); | ||
kingInitSetting(black, white); | ||
|
||
makeBoard(black, white); | ||
} | ||
|
||
private void pawnInitSetting(final List<Piece> black, final List<Piece> white) { | ||
black.addAll(Pawn.getInitPawns(Team.BLACK)); | ||
white.addAll(Pawn.getInitPawns(Team.WHITE)); | ||
} | ||
|
||
private void rookInitSetting(final List<Piece> black, final List<Piece> white) { | ||
black.addAll(Rook.getInitRooks(Team.BLACK)); | ||
white.addAll(Rook.getInitRooks(Team.WHITE)); | ||
} | ||
|
||
private void knightInitSetting(final List<Piece> black, final List<Piece> white) { | ||
black.addAll(Knight.getInitKnights(Team.BLACK)); | ||
white.addAll(Knight.getInitKnights(Team.WHITE)); | ||
} | ||
|
||
private void bishopInitSetting(final List<Piece> black, final List<Piece> white) { | ||
black.addAll(Bishop.getInitBishop(Team.BLACK)); | ||
white.addAll(Bishop.getInitBishop(Team.WHITE)); | ||
} | ||
|
||
private void queenInitSetting(final List<Piece> black, final List<Piece> white) { | ||
black.addAll(Queen.getInitQueen(Team.BLACK)); | ||
white.addAll(Queen.getInitQueen(Team.WHITE)); | ||
} | ||
|
||
private void kingInitSetting(final List<Piece> black, final List<Piece> white) { | ||
black.addAll(King.getInitKing(Team.BLACK)); | ||
white.addAll(King.getInitKing(Team.WHITE)); | ||
} | ||
|
||
private void makeBoard(final List<Piece> black, final List<Piece> white) { | ||
board.put(Team.BLACK, new Pieces(black)); | ||
board.put(Team.WHITE, new Pieces(white)); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package chess.domain.pieces; | ||
|
||
import chess.domain.Team; | ||
import chess.domain.board.Board; | ||
import chess.domain.pieces.Movable.MultiMove; | ||
import chess.domain.position.Position; | ||
import chess.domain.util.ColumnConverter; | ||
import chess.domain.util.RowConverter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public final class Bishop extends NoKingPieces implements MultiMove { | ||
private static final String BLACK_TEAM_ROW = "8"; | ||
private static final String WHITE_TEAM_ROW = "1"; | ||
private static final double SCORE = 3.0; | ||
private static final int LEFT_SIDE_INIT_COL = 2; | ||
private static final int RIGHT_SIDE_INIT_COL = 5; | ||
|
||
private Bishop(final Team team, final Position position) { | ||
super(position, "B", team, SCORE); | ||
} | ||
|
||
private Bishop(final Position position, final String initial, final Team team, final double score) { | ||
super(position, initial, team, score); | ||
} | ||
|
||
public static Bishop of(final Team team, final Position position) { | ||
return new Bishop(position, "B", team, SCORE); | ||
} | ||
|
||
public static Bishop of(final Team team, final int col) { | ||
if (col != LEFT_SIDE_INIT_COL && col != RIGHT_SIDE_INIT_COL) { | ||
throw new IllegalArgumentException("잘못된 초기 위치입니다."); | ||
} | ||
return new Bishop(team, getInitPosition(team, col)); | ||
} | ||
|
||
private static Position getInitPosition(final Team team, final int col) { | ||
if (team.equals(Team.BLACK)) { | ||
return new Position(RowConverter.getLocation(BLACK_TEAM_ROW), col); | ||
} | ||
return new Position(RowConverter.getLocation(WHITE_TEAM_ROW), col); | ||
} | ||
|
||
public static List<Bishop> getInitBishop(final Team team) { | ||
List<Bishop> bishops = new ArrayList<>(); | ||
ColumnConverter.getBishopInitCols().forEach((col) -> bishops.add(Bishop.of(team, col))); | ||
return bishops; | ||
} | ||
|
||
@Override | ||
public List<Position> getMovablePositions(final Board board) { | ||
int[] rowDir = {-1, 1, -1, 1}; | ||
int[] colDir = {-1, 1, 1, -1}; | ||
return getMovablePositionsByDir(board, rowDir, colDir); | ||
} | ||
|
||
@Override | ||
public boolean isMoveAble(final List<Position> movablePositions, final Board board, final int nextRow, final int nextCol) { | ||
return isMoveAbleDir(movablePositions, board, nextRow, nextCol, getTeam()); | ||
} | ||
} |
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.
더이상 상속을 통해 기능확장을 기대하지 않는 클래스에 대해서는 메소드가 아닌 클래스에 final을 붙이게 했어요!