Skip to content

[체스 게임 1단계] 리뷰 요청 드립니다. #9

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

Open
wants to merge 42 commits into
base: chunghyeon-kim
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
0856ae2
test: Generate King test
eunii Aug 15, 2021
cb08987
test: King creation test Done
eunii Aug 15, 2021
5a3143d
refactor: File, Rank enum 추가
eunii Aug 15, 2021
8cbba8c
feat: Create Piece class(ancestor class)
eunii Aug 15, 2021
467fe4e
feat: Generate All Piece classes
eunii Aug 15, 2021
b4d5d32
refactor: Extract rank by team
eunii Aug 15, 2021
75e36a4
test: Generate ChessPlateTest
eunii Aug 15, 2021
74509aa
feat: Generate printPlate
eunii Aug 15, 2021
61afd40
feat: Generate comparator
eunii Aug 15, 2021
74a6f6f
feat: Print initial chess plate
chunghyeon-kimm Aug 16, 2021
824c514
feat: Arrange rank when print chess plate
eunii Aug 17, 2021
7a90d5c
feat: get chess command
eunii Aug 17, 2021
80c0735
feat: make start/end feature
eunii Aug 17, 2021
be7a84d
feat: Get move source target position
eunii Aug 17, 2021
2d334fc
feat: Get target sourcepiece
eunii Aug 17, 2021
78a791e
feat: add move & movable abstract methods
eunii Aug 17, 2021
e836564
feat: Make king movable method and test
eunii Aug 17, 2021
18ad96d
refactor: Delete move method of piece
eunii Aug 17, 2021
584394a
feat: Make Bishop movable method and test
eunii Aug 17, 2021
f75d822
feat: Make Rook movable method and test
eunii Aug 17, 2021
7a9a66d
feat: Make Queen movable method and test
eunii Aug 18, 2021
4d17108
feat: Make Pawn movable method
eunii Aug 18, 2021
5a09eb1
refactor: pawn remove duplicated function
eunii Aug 18, 2021
f3bf8d8
feat: Make Knight movable method
eunii Aug 18, 2021
dc73e31
feat: Make havePieceOnStraightPath method
eunii Aug 18, 2021
8e3acb6
refactor: remove Neutral Team
eunii Aug 19, 2021
98d725b
feat: Make havePieceoOnDiagnalPath check method
eunii Aug 19, 2021
5446c4a
feat: chessPlate move method ongoing
eunii Aug 19, 2021
66d4e37
feat: make move method except pawn
eunii Aug 19, 2021
9b86fd3
feat: Make pawn diagonal move rule
eunii Aug 19, 2021
ba9d18c
feat: Make controller and service logic
eunii Aug 19, 2021
a22ce55
feat: Add score to pieces
eunii Aug 19, 2021
11d31f7
feat: Make print score method
eunii Aug 19, 2021
e693b9c
feat: make calculateDuplicatedPawn Method
eunii Aug 20, 2021
83119aa
refactor: Modify getScore Method
eunii Aug 20, 2021
a4076d6
refactor: Fix havePieceOnStraight bug
eunii Aug 20, 2021
90167da
doc: Modify readme
eunii Aug 20, 2021
acd33e4
refactor: Modify variable names
chunghyeon-kimm Aug 20, 2021
bc5ed40
feat: status 입력 시 점수와 승자 출력
chunghyeon-kimm Aug 20, 2021
f1da948
feat: King이 죽었는지 확인하는 기능 구현
chunghyeon-kimm Aug 20, 2021
907901d
feat: Print message and exit game when king is dead
chunghyeon-kimm Aug 20, 2021
58b2161
refactor: Modify ChessServiceTest, straight condition
chunghyeon-kimm Sep 1, 2021
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
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
# java-chess 게임

- [x] 체스판
- [x]rank(세로) : 8 :1~8
- [x]file(가로) : 8 :a~h
- [x] 말이 없는 칸은 .
- [x]초기화
- [x] 기물 이동기능

- [x] 아웃풋 뷰
- [x] 체스판 출력 기능
- [x] 'start' 입력 시 체스판 초기화 하고 출력
- [x] 게임 진행시 마다 체스판을 출력해준다
- [x] 게임 시작 설명 출력

-[x] 인풋 뷰
- [x] 게임 명령어 인풋 받기
- [x] start/end/move로 시작하지 않으면 계속 묻기


- [x] 기물
- [x] 말을 넘어갈 수 없음(나이트 제외)
- [x] king
- [x] 상하좌우 중 1칸 이동범위 체크
- [x] queen
- [x] 상하좌우 대각선 n칸 이동범위 체크
- [x] bishop
- [x] 대각선으로 n칸 이동범위 체크
- [x] knight
- [x] 사방중 한방향으로 한칸 & 양 대각선 중 한 방향으로 한칸 으로 이동범위 체크
- [x] 상대 말 넘어감
- [x] rook
- [x] 상하좌우 n칸 이동
- [x] pawn
- [x] 첫회 한정: 2칸 상 이동 || 1칸 상 이동
- [x] 1칸 이동
- [x] 공격은 대각선으로만

- [x] 진영
- [x] 기물 2 세트 생성 (1개는 대문자, 소문자)

- [] 체스 게임 룰
- [x] start : 체스판 초기화 및 진영 세팅
- [x] end: 게임 종료
- [] 킹을 잡으면 게임이 종료된다
9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testCompile "org.assertj:assertj-core:3.14.0"
//lombok
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
testCompileOnly 'org.projectlombok:lombok:1.18.20'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
}

test {
useJUnitPlatform()
}
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.controller.ChessController;
import chess.domain.plate.ChessPlate;
import chess.view.OutputView;

public class ChessApplication {

public static void main(String[] args) {
ChessController chessController = new ChessController();
chessController.play();
}
}

77 changes: 77 additions & 0 deletions src/main/java/chess/controller/ChessController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package chess.controller;

import chess.domain.piece.Team;
import chess.domain.plate.ChessPlate;
import chess.service.ChessService;
import chess.view.InputView;
import chess.view.OutputView;

public class ChessController {
private static final OutputView outputView = new OutputView();
private static final InputView inputView = new InputView();
public static final String START = "start";
public static final String END = "end";
public static final String MOVE = "move";
public static final String STATUS = "status";

public void play() {
ChessService chessService = new ChessService();
outputView.initPrint();
String command = START;
while (!command.equals(END)){
command = getUserCommand();
checkCommadMessage(chessService, command);
gameOverIfKingDead(chessService);
}
}

private void checkCommadMessage(ChessService chessService, String command) {
if(command.equals(START)){
ChessPlate chessplate = chessService.start();
outputView.printChessPlate(chessplate);
}

if(command.startsWith(MOVE)) {
String[] position = command.split(" ");
boolean isMoved = chessService.move(position[1],position[2]);
if(isMoved) {
outputView.printChessPlate(chessService.getChessPlate());
}else {
outputView.printCannotMoveMessage();
}
}

if(command.equals(STATUS)) {
printResult(chessService);
}

}

private static String getUserCommand() {
String command = inputView.getUserCommand();
while(!(command.startsWith(START) || command.equals(END) || command.startsWith(MOVE) || command.equals(STATUS))){
outputView.printInCorrectCommandMessage();
command = inputView.getUserCommand();
}
return command;
}

private void printResult(ChessService chessService) {
double blackScore = chessService.getGameScore(Team.BLACK);
double whiteScore = chessService.getGameScore(Team.WHITE);

outputView.printScoresOfTwoTeams(blackScore, whiteScore);
outputView.printWinner(blackScore, whiteScore);
}

private void gameOverIfKingDead(ChessService chessService) {
if(chessService.isKingDead(Team.BLACK)) {
outputView.printBlackKingDeadMessage();
System.exit(0);
}
if(chessService.isKingDead(Team.WHITE)) {
outputView.printWhiteKingDeadMessage();
System.exit(0);
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/chess/domain/PicesPositionComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package chess.domain;

import chess.domain.piece.PiecePosition;
import chess.domain.piece.Rank;

import java.util.Comparator;

public class PicesPositionComparator implements Comparator {

@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof PiecePosition && o2 instanceof PiecePosition) {
PiecePosition p1 = (PiecePosition) o1;
PiecePosition p2 = (PiecePosition) o2;

int pvalue1 = p1.getRank().getRankPosition();
int pvalue2 = p2.getRank().getRankPosition();

if(pvalue1 == pvalue2) {
return p1.getFile().getFilePosition() - p2.getFile().getFilePosition();
}
return pvalue1 - pvalue2;
}
return 0;
}
}
19 changes: 19 additions & 0 deletions src/main/java/chess/domain/RankComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package chess.domain;

import chess.domain.piece.Rank;

import java.util.Comparator;

public class RankComparator implements Comparator {

@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Rank && o2 instanceof Rank) {
Rank r1 = (Rank) o1;
Rank r2 = (Rank) o2;

return r2.getRankPosition() - r1.getRankPosition();
}
return 0;
}
}
21 changes: 21 additions & 0 deletions src/main/java/chess/domain/piece/Bishop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package chess.domain.piece;

import lombok.Getter;

@Getter
public class Bishop extends Piece {
public Bishop(Team team, PiecePosition piecePosition) {
super("B", team, piecePosition,3);
}

@Override
public boolean movable(PiecePosition targetPosition) {
int originFile = this.piecePosition.getFile().getFilePosition();
int originRank = this.piecePosition.getRank().getRankPosition();
int targetFile = targetPosition.getFile().getFilePosition();
int targetRank = targetPosition.getRank().getRankPosition();
int fileGap = Math.abs(originFile - targetFile);
int rankGap = Math.abs(originRank - targetRank);
return (fileGap == rankGap);
}
}
39 changes: 39 additions & 0 deletions src/main/java/chess/domain/piece/File.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package chess.domain.piece;

import lombok.Getter;

import java.util.Arrays;

@Getter
public enum File {
A("a", 1),
B("b", 2),
C("c", 3),
D("d", 4),
E("e", 5),
F("f", 6),
G("g", 7),
H("h", 8);

private String fileMarker;
private int filePosition;

File(String fileMarker, int filePosition) {
this.fileMarker = fileMarker;
this.filePosition = filePosition;
}

public static File findBy(String fileMarker) {
return Arrays.stream(File.values())
.filter(v -> v.fileMarker.equals(fileMarker))
.findFirst()
.orElseThrow(() -> new IllegalAccessError("해당되는 위치가 없습니다."));
}

public static File findBy(int filePosition) {
return Arrays.stream(File.values())
.filter(v -> v.filePosition==filePosition)
.findFirst()
.orElseThrow(() -> new IllegalAccessError("해당되는 위치가 없습니다."));
}
}
19 changes: 19 additions & 0 deletions src/main/java/chess/domain/piece/King.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package chess.domain.piece;

public class King extends Piece {
public King(Team team, PiecePosition piecePosition) {
super("K", team, piecePosition, 0);
}

@Override
public boolean movable(PiecePosition targetPosition) {
int originFile = this.piecePosition.getFile().getFilePosition();
int originRank = this.piecePosition.getRank().getRankPosition();
int targetFile = targetPosition.getFile().getFilePosition();
int targetRank = targetPosition.getRank().getRankPosition();
//상하좌우 한칸씩
int fileGap = Math.abs(originFile - targetFile);
int rankGap = Math.abs(originRank - targetRank);
return fileGap + rankGap == 1;
}
}
22 changes: 22 additions & 0 deletions src/main/java/chess/domain/piece/Knight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package chess.domain.piece;

import lombok.Getter;

@Getter
public class Knight extends Piece {
public Knight(Team team, PiecePosition piecePosition) {
super("N", team, piecePosition, 2.5);
}

@Override
public boolean movable(PiecePosition targetPosition) {
int originFile = this.piecePosition.getFile().getFilePosition();
int originRank = this.piecePosition.getRank().getRankPosition();
int targetFile = targetPosition.getFile().getFilePosition();
int targetRank = targetPosition.getRank().getRankPosition();
//상하좌우 한칸씩
int fileGap = Math.abs(originFile - targetFile);
int rankGap = Math.abs(originRank - targetRank);
return (fileGap + rankGap == 3) && (fileGap == 1 || rankGap == 1);
}
}
39 changes: 39 additions & 0 deletions src/main/java/chess/domain/piece/Pawn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package chess.domain.piece;

import lombok.Getter;

@Getter
public class Pawn extends Piece {
public Pawn(Team team, PiecePosition piecePosition) {
super("P", team, piecePosition,1);
}

@Override
public boolean movable(PiecePosition targetPosition) {
int originFile = this.piecePosition.getFile().getFilePosition();
int originRank = this.piecePosition.getRank().getRankPosition();
int targetFile = targetPosition.getFile().getFilePosition();
int targetRank = targetPosition.getRank().getRankPosition();
//처음 두칸
if (checkFirstMove()) {
if (this.getTeam().equals(Team.BLACK)) {
return ((originFile == targetFile) && (originRank - targetRank <= 2))
|| canMoveDiagonalAndOneBlock(originFile, originRank, targetFile, targetRank);
}
if (this.getTeam().equals(Team.WHITE)) {
return (originFile == targetFile) && (originRank - targetRank >= -2)
|| canMoveDiagonalAndOneBlock(originFile, originRank, targetFile, targetRank);
}
}
//한칸
return canMoveDiagonalAndOneBlock(originFile, originRank, targetFile, targetRank);
}

private boolean canMoveDiagonalAndOneBlock(int originFile, int originRank, int targetFile, int targetRank) {
return (originRank - targetRank == 1*this.getTeam().getPawnDirection()) && Math.abs(originFile - targetFile) <= 1;
}

private boolean checkFirstMove() {
return this.getPiecePosition().getRank().equals(this.getTeam().getPawnInitRank());
}
}
27 changes: 27 additions & 0 deletions src/main/java/chess/domain/piece/Piece.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package chess.domain.piece;

import lombok.Getter;

@Getter
public abstract class Piece {
protected String name;
protected Team team;
protected PiecePosition piecePosition;
protected String displayName;
protected double score;

public Piece(String name, Team team, PiecePosition piecePosition, double score) {
this.name = name;
this.team = team;
this.displayName = team.displayName(name);
this.piecePosition = piecePosition;
this.score = score;
}

public abstract boolean movable(PiecePosition targetPosition);

public void move(PiecePosition targetPosition){
piecePosition = targetPosition;
}

}
Loading