Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1b6e5a4
feat: ๊ฐ ํ”Œ๋ ˆ์ด์–ด ์ง„ํ˜• ์ž…๋ ฅ ๊ธฐ๋Šฅ ๊ตฌํ˜„
win9-tech Mar 25, 2026
7e578e4
test: Board ์ดˆ๊ธฐํ™” ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๊ตฌํ˜„
win9-tech Mar 26, 2026
903920f
test: Position ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๊ตฌํ˜„
win9-tech Mar 26, 2026
9be4c87
feat: Board ์ดˆ๊ธฐํ™” ๊ธฐ๋Šฅ ๊ตฌํ˜„
win9-tech Mar 26, 2026
d96f899
feat: ์žฅ๊ธฐํŒ ์ƒํƒœ ์ถœ๋ ฅ ๊ธฐ๋Šฅ ๊ตฌํ˜„
win9-tech Mar 26, 2026
a9010a4
refactor: Board ์ดˆ๊ธฐ ๋ฐฐ์น˜ ๋กœ์ง ์ค‘๋ณต ์ œ๊ฑฐ
win9-tech Mar 26, 2026
08e53e3
feat: ์›€์ง์ผ ๊ธฐ๋ฌผ ์ขŒํ‘œ ์ž…๋ ฅ ๊ธฐ๋Šฅ ๊ตฌํ˜„
win9-tech Mar 26, 2026
7b91ab8
feat: ๋ชฉ์ ์ง€ ์ขŒํ‘œ ์ž…๋ ฅ ๊ธฐ๋Šฅ ๊ตฌํ˜„
win9-tech Mar 26, 2026
6ca130e
feat: ํ„ด์ œ ์‹œ์Šคํ…œ ๋ฐ ์žฅ๊ธฐ ์ง„ํ–‰์ƒํƒœ ์ถœ๋ ฅ ๊ธฐ๋Šฅ ๊ตฌํ˜„
win9-tech Mar 26, 2026
1f698a8
test: ๊ธฐ๋ฌผ ์ด๋™ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๊ตฌํ˜„
win9-tech Mar 27, 2026
0b0a46c
feat: ๊ธฐ๋ฌผ ์ด๋™ ๊ธฐ๋Šฅ ๊ตฌํ˜„
win9-tech Mar 27, 2026
668aced
feat: ํ„ด ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ Turn ๋„๋ฉ”์ธ ๊ฐ์ฒด ์ถ”๊ฐ€
win9-tech Mar 29, 2026
8542552
fix: KingTest์—์„œ Horse๋กœ ์ƒ์„ฑํ•˜๋˜ ๋ฒ„๊ทธ ์ˆ˜์ •
win9-tech Mar 29, 2026
4c158e8
refactor: ๊ธฐ๋ฌผ ๋ฐฐ์น˜ ๋กœ์ง์˜ ํ•˜๋“œ์ฝ”๋”ฉ์„ ์ƒ์ˆ˜ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐœ์„ 
win9-tech Mar 29, 2026
4895748
refactor: movePiece ๋‚ด๋ถ€ ๋กœ์ง์„ ๋ฉ”์„œ๋“œ๋กœ ๋ถ„๋ฆฌ
win9-tech Mar 29, 2026
50f7d45
refactor: MovementStrategy ํŒจํ„ด ์ ์šฉ ๋ฐ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์ƒ์ˆ˜ํ™”
win9-tech Mar 29, 2026
b797150
test: ๊ธฐ๋ฌผ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ˆ˜์ •
win9-tech Mar 29, 2026
46319b0
test: ๊ธฐ๋ฌผ ์ด๋™ ํ…Œ์ŠคํŠธ if ๋ถ„๊ธฐ ์ œ๊ฑฐ ๋ฐ ์œ ํšจ/๋ฌดํšจ ์ผ€์ด์Šค ๋ถ„๋ฆฌ
win9-tech Mar 29, 2026
ccf486f
refactor: ๊ธฐ๋ฌผ ์ด๋™ ์ „๋žต ๋ถ„๋ฆฌ ๋ฐ ๊ฒฝ๋กœ ๊ณ„์‚ฐ ๊ตฌ์กฐ ๊ฐœ์„ 
win9-tech Mar 29, 2026
3fa6172
docs: README.md ์—…๋ฐ์ดํŠธ
win9-tech Mar 29, 2026
d9dd2fa
refactor: Position ์บ์‹ฑ ์ ์šฉ
win9-tech Mar 29, 2026
6b196a3
test: Position canMove ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๊ตฌํ˜„
win9-tech Mar 29, 2026
cfe69c0
refactor: ERROR ๋ฉ”์‹œ์ง€ ์ƒ์ˆ˜์ฒ˜๋ฆฌ
win9-tech Mar 29, 2026
88cfd05
refactor: findRoute ๋ฉ”์„œ๋“œ movementStrategy.findRoute()๋กœ ๋ณ€๊ฒฝ
yeo-li Mar 29, 2026
909b7c5
fix: findRoute() targetPosition์ด List<Position>์˜ ๋งˆ์ง€๋ง‰์ธ์ง€ ํ™•์ธํ•˜๋„๋ก ๋ณ€๊ฒฝ
yeo-li Apr 1, 2026
cb5d39d
feat(input): ์ขŒํ‘œ ์ž…๋ ฅ ํŒŒ์‹ฑ ๋กœ์ง ์ถ”๊ฐ€ ๋ฐ ์œ ํšจ์„ฑ ๊ฒ€์ฆ ๊ฐ•ํ™”
yeo-li Apr 1, 2026
6694397
fix: Piece.checkRoute() ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ์ˆ˜์ •
yeo-li Apr 1, 2026
e4bb045
refactor: Board ๊ฒŒ์ž„ ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง ์ œ๊ฑฐ ๋ฐ Game์œผ๋กœ ์ด๋™
yeo-li Apr 2, 2026
bd6c89a
refactor: BoardFactory ๋„์ž… ๋ฐ Game/Board ํ…Œ์ŠคํŠธ ์ƒ์„ฑ ๋ฐฉ์‹ ์ •๋ฆฌ
yeo-li Apr 2, 2026
bfc79bc
refactor: Elephant ์ƒ์„ฑ์ž ์˜์กด์„ฑ ์ฃผ์ž…์œผ๋กœ ํ†ต์ผ
yeo-li Apr 2, 2026
9b17899
refactor: BoardSpec ๋„์ž…์œผ๋กœ ๋ณด๋“œ ๋ฒ”์œ„์™€ ์‹œ์ž‘ ํ„ด ํ•˜๋“œ์ฝ”๋”ฉ ์ œ๊ฑฐ
yeo-li Apr 2, 2026
0cf6e2e
refactor: Position CACHE key-value ๊ตฌ์กฐ ๋ณ€๊ฒฝ
yeo-li Apr 2, 2026
3f4be1b
refactor: Side.NONE ์ œ๊ฑฐ ๋ฐ ๊ฒŒ์ž„ ์ข…๋ฃŒ ์กฐ๊ฑด(isGameEnd) ๋„์ž…
yeo-li Apr 2, 2026
8569773
style: ๊ณต๋ฐฑ ๋ฐ ๋ฉ”์„œ๋“œ ์ˆœ์„œ ์ •๋ฆฌ
yeo-li Apr 2, 2026
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
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,73 @@
# java-janggi

์žฅ๊ธฐ ๋ฏธ์…˜ ์ €์žฅ์†Œ

# ๊ธฐ๋Šฅ

## 1-1 ์š”๊ตฌ์‚ฌํ•ญ
- [x] ๊ฐ ํ”Œ๋ ˆ์ด์–ด ์ง„ํ˜• ์ž…๋ ฅ ๊ธฐ๋Šฅ
- [x] ์žฅ๊ธฐํŒ ์ดˆ๊ธฐํ™” ๊ธฐ๋Šฅ
- [x] ์žฅ๊ธฐํŒ ์ƒํƒœ ์ถœ๋ ฅ ๊ธฐ๋Šฅ

## 1-2 ์š”๊ตฌ์‚ฌํ•ญ
- [x] ์ด๋™ํ•  ๊ธฐ๋ฌผ ์œ„์น˜ ์ขŒํ‘œ ์ž…๋ ฅ ๊ธฐ๋Šฅ
- [x] ์ด๋™ํ•  ๋ชฉ์ ์ง€ ์ขŒํ‘œ ์ž…๋ ฅ ๊ธฐ๋Šฅ
- [x] ํ„ด์ œ ์‹œ์Šคํ…œ ๊ธฐ๋Šฅ
- [x] ์žฅ๊ธฐ ์ง„ํ–‰ ์ƒํƒœ ์ถœ๋ ฅ ๊ธฐ๋Šฅ
- [x] ๊ธฐ๋ฌผ ์ด๋™ ๊ธฐ๋Šฅ

## โš ๏ธ ๊ธฐ๋Šฅ ์˜ˆ์™ธ ์‚ฌํ•ญ

### ์ž…๋ ฅ ๊ฒ€์ฆ
- ํฌ์ง„ ์„ ํƒ: 1~4 ๋ฒ”์œ„ ์™ธ ์ž…๋ ฅ ์‹œ ์˜ˆ์™ธ
- ์ขŒํ‘œ ๋ฒ”์œ„: x(1~9), y(1~10) ๋ฒ”์œ„ ์™ธ ์ž…๋ ฅ ์‹œ ์˜ˆ์™ธ

### ๊ธฐ๋ฌผ ์„ ํƒ ๊ฒ€์ฆ
- ์„ ํƒํ•œ ์œ„์น˜์— ๊ธฐ๋ฌผ์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์˜ˆ์™ธ
- ์„ ํƒํ•œ ๊ธฐ๋ฌผ์ด ์•„๊ตฐ ๊ธฐ๋ฌผ์ด ์•„๋‹ ๊ฒฝ์šฐ ์˜ˆ์™ธ

### ์ด๋™ ๊ทœ์น™ ๊ฒ€์ฆ
- ๋ชฉ์ ์ง€๊ฐ€ ๊ธฐ๋ฌผ์˜ ์ด๋™ ๊ทœ์น™์— ๋งž์ง€ ์•Š์„ ๊ฒฝ์šฐ ์˜ˆ์™ธ
- ๋ชฉ์ ์ง€์— ์•„๊ตฐ ๊ธฐ๋ฌผ์ด ์กด์žฌํ•  ๊ฒฝ์šฐ ์˜ˆ์™ธ
- ์ด๋™ ๊ฒฝ๋กœ์— ๊ธฐ๋ฌผ์ด ์กด์žฌํ•  ๊ฒฝ์šฐ ์˜ˆ์™ธ (ํฌ ์ œ์™ธ)

### ํฌ ๊ทœ์น™ ๊ฒ€์ฆ
- ์ด๋™ ๊ฒฝ๋กœ์— ํฌ๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ ์˜ˆ์™ธ
- ์ด๋™ ๊ฒฝ๋กœ์— ๊ธฐ๋ฌผ์ด ์ •ํ™•ํžˆ 1๊ฐœ๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ ์˜ˆ์™ธ
- ํฌ๋กœ ํฌ๋ฅผ ์žก์œผ๋ ค ํ•  ๊ฒฝ์šฐ ์˜ˆ์™ธ

---

## ๐Ÿ“ ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

```
src/main/java
โ”œโ”€โ”€ Main.java
โ”œโ”€โ”€ controller
โ”‚ โ””โ”€โ”€ JanggiController.java
โ”œโ”€โ”€ domain
โ”‚ โ”œโ”€โ”€ Board.java
โ”‚ โ”œโ”€โ”€ Direction.java
โ”‚ โ”œโ”€โ”€ Formation.java
โ”‚ โ”œโ”€โ”€ Position.java
โ”‚ โ”œโ”€โ”€ Side.java
โ”‚ โ”œโ”€โ”€ Turn.java
โ”‚ โ”œโ”€โ”€ piece
โ”‚ โ”‚ โ”œโ”€โ”€ Piece.java
โ”‚ โ”‚ โ”œโ”€โ”€ PieceType.java
โ”‚ โ”‚ โ”œโ”€โ”€ King.java
โ”‚ โ”‚ โ”œโ”€โ”€ Guard.java
โ”‚ โ”‚ โ”œโ”€โ”€ Horse.java
โ”‚ โ”‚ โ”œโ”€โ”€ Elephant.java
โ”‚ โ”‚ โ”œโ”€โ”€ Chariot.java
โ”‚ โ”‚ โ”œโ”€โ”€ Cannon.java
โ”‚ โ”‚ โ”œโ”€โ”€ Soldier.java
โ”‚ โ”‚ โ””โ”€โ”€ Empty.java
โ”‚ โ””โ”€โ”€ strategy
โ”‚ โ”œโ”€โ”€ MovementStrategy.java
โ”‚ โ”œโ”€โ”€ PathMovement.java
โ”‚ โ””โ”€โ”€ LinearMovement.java
โ””โ”€โ”€ view
โ”œโ”€โ”€ InputView.java
โ””โ”€โ”€ OutputView.java
```
Empty file removed src/main/java/.gitkeep
Empty file.
10 changes: 10 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import controller.JanggiController;
import view.InputView;
import view.OutputView;

public class Main {
public static void main(String[] args) {
JanggiController janggiController = new JanggiController(new InputView(), new OutputView());
janggiController.run();
}
}
13 changes: 13 additions & 0 deletions src/main/java/constant/BoardSpec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package constant;

import domain.Side;

public class BoardSpec {

public static final int MIN_X = 1;
public static final int MAX_X = 9;
public static final int MIN_Y = 1;
public static final int MAX_Y = 10;

public static final Side DEFAULT_STARTING_SIDE = Side.CHO;
}
98 changes: 98 additions & 0 deletions src/main/java/controller/JanggiController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package controller;

import domain.Board;
import domain.BoardFactory;
import domain.Formation;
import domain.Game;
import domain.Position;
import domain.Side;
import util.Parser;
import view.InputView;
import view.OutputView;

public class JanggiController {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  1. ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€ ์ผ๊ด€์„ฑ๊ณผ ์ž…๋ ฅ ์•ˆ์ •์„ฑ
    ๋„๋ฉ”์ธ ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ถœ๋ ฅ์„ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ๋ฉ”์‹œ์ง€์˜ ์˜๋ฏธ/์ •ํ™•์„ฑ์ด ์ถฉ๋ถ„ํ•œ์ง€์™€
    ์ž…๋ ฅ ํŒŒ์‹ฑ(nextInt) ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ View/Controller/Domain ์–ด๋””์—์„œ ๋‹ด๋‹นํ•˜๋Š” ๊ฒŒ ์ข‹์€์ง€ ์˜๊ฒฌ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค!

์„œ์—ฌ๋Š” ์•„๋งˆ๋„ ์ „์ฒด๋ฅผ ํ†ตํ‹€์–ด์„œ ์งˆ๋ฌธ์„ ํ•˜์‹  ๊ฒƒ ๊ฐ™์€๋ฐ์š”..!
์ƒํ™ฉ์— ๋”ฐ๋ผ ์กฐ๊ธˆ ๋‹ค๋ฅธ ๊ฒƒ ๊ฐ™์•„์š”.

๋‹จ์ˆœํžˆ ์ž…๋ ฅ๊ฐ’์—๋Œ€ํ•œ ์ž…๋ ฅ ํ˜•์‹ ๋ฌธ์ œ๋Š” View๋‚˜ Controller ์ชฝ์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฒŒ ์ž์—ฐ์Šค๋Ÿฝ์ง€๋งŒ
์กธ๋ณ‘์„ ๋‘ ์นธ ์ „์ง„์‹œ์ผฐ๋‹ค๊ฑฐ๋‚˜ํ•˜๋Š” ์ผ€์ด์Šค๋Š” ๋„๋ฉ”์ธ ๊ทœ์น™์— ์œ„๋ฐฐ๋˜๋Š” ๊ฒฝ์šฐ์ด๋ฏ€๋กœ ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” Domain์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์š”. ๐Ÿ˜ƒ

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

๋ฉ”์‹œ์ง€์˜ ์˜๋ฏธ/์ •ํ™•์„ฑ์€ ๊ผผ๊ผผํ•˜๊ฒŒ ๋‹ค ์‚ดํŽด๋ณด์ง€๋Š” ๋ชปํ–ˆ์ง€๋งŒ, ๋‚จ๊ธด ์ฝ”๋ฉ˜ํŠธ์ค‘์—์„œ ์ƒํ™ฉ์— ๋งž์ง€์•Š๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ฐ€ ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ์š”. ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€์˜ ์˜๋ฏธ/์ •ํ™•์„ฑ์ด๋ž€๊ฒŒ ๋”ฑ ์ •์˜๋œ ๊ฒƒ์€ ์—†์ง€๋งŒ, ๊ฒฐ๊ตญ ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€๋ฅผ ์™œ ๋‚จ๊ธฐ๋Š”์ง€?์— ๋Œ€ํ•ด์„œ ๊ณ ๋ฏผํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”.

๊ฒฐ๊ตญ์—๋Š” ์˜ˆ์™ธ ์ƒํ™ฉ์„ ํ‘œ์‹œํ•˜๊ณ  ์ ์ ˆํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ์œ„ํ•จ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ์š”.
๊ทธ๋ ‡๋‹ค๋ฉด ํ•ด๋‹น ์˜ˆ์™ธ ์ƒํ™ฉ์ด ์™œ ๋ฐœ์ƒํ–ˆ๋Š”์ง€๋ฅผ ์ž˜ ๋“œ๋Ÿฌ๋‚ด์•ผํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์š”.
๋˜ ๋” ๋‚˜์•„๊ฐ€์„œ ์‹ค์ œ ์šด์˜ํ™˜๊ฒฝ์—์„œ ๋ฒ„๊ทธ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์žฅ์• ๋Œ€์‘์„ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด๋ฉด ๊ฒฐ๊ตญ ๋””๋ฒ„๊น…์˜ ์‹œ์ž‘์ ์ด ์ด ์˜ˆ์™ธ๋ฉ”์‹œ์ง€๋ผ๊ณ  ์ƒ๊ฐํ•ด์š”.

์ด ์˜ˆ์™ธ๋ฉ”์‹œ์ง€๊ฐ€ ๋””๋ฒ„๊น…์˜ ์‹œ์ž‘ ํฌ์ธํŠธ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด....! ์ •๋ง ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ์ง€ ์•Š๋‚˜์š”? ๐Ÿ˜„

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

์˜ˆ์™ธ ๋ฉ”์„ธ์ง€์— ์‹ ๊ฒฝ์„ ์ž˜ ์“ฐ์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์—ˆ๋Š”๋ฐ, ์•ž์œผ๋กœ ๋” ๋ฉด๋ฐ€ํžˆ ์ž‘์„ฑํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹คโ˜บ๏ธ ๋‹ต๋ณ€ ์ •๋ง ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!


private final InputView inputView;
private final OutputView outputView;

public JanggiController(InputView inputView, OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}

public void run() {
Game game = initGame();
processMove(game);
}

private Game initGame() {
Formation choformation = readChoFormation();
Formation hanformation = readHanFormation();

Board board = BoardFactory.createBoard(choformation, hanformation);
Game game = new Game(board);
outputView.printBoardStatus(game.getBoard());
return game;
}

private void processMove(Game game) {
while (!game.isGameEnd()) {
Side currentTurn = game.getCurrentTurn();
outputView.printCurrentTurn(currentTurn);
Position sourcePosition = readSourcePosition();
Position targetPosition = readTargetPosition();
try {
game.move(sourcePosition, targetPosition);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
}
outputView.printBoardStatus(game.getBoard());
}
}

private Position readSourcePosition() {
while (true) {
try {
int x = Parser.parseInput(inputView.readSourceXPosition());
int y = Parser.parseInput(inputView.readSourceYPosition());
return Position.of(x, y);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
}
}
}

private Position readTargetPosition() {
while (true) {
try {
int x = Parser.parseInput(inputView.readTargetXPosition());
int y = Parser.parseInput(inputView.readTargetYPosition());
return Position.of(x, y);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
}
}
}

private Formation readChoFormation() {
while (true) {
try {
String choFormation = inputView.readChoFormation();
return Formation.from(choFormation);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
}
}
}

private Formation readHanFormation() {
while (true) {
try {
String hanFormation = inputView.readHanFormation();
return Formation.from(hanFormation);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
}
}
}
}
49 changes: 49 additions & 0 deletions src/main/java/domain/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package domain;

import domain.piece.Empty;
import domain.piece.King;
import domain.piece.Piece;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Board {

private final Map<Position, Piece> board;

public Board(Map<Position, Piece> board) {
this.board = board;
}

public void movePiece(Position sourcePosition, Position targetPosition) {
board.put(targetPosition, board.get(sourcePosition));
board.put(sourcePosition, new Empty());
}

public Piece getPiece(Position position) {
return board.get(position);
}

public List<Piece> findPiecesOnRoute(List<Position> route, Position targetPosition) {
List<Piece> pieces = new ArrayList<>();
for (Position position : route) {
if (!position.equals(targetPosition)) {
pieces.add(board.get(position));
}
}
return pieces;
}

public boolean hasKing(Side side) {
for (Piece piece : board.values()) {
if (piece instanceof King && piece.isSameSide(side)) {
return false;
}
}
return true;
}

public Map<Position, Piece> getBoard() {
return Map.copyOf(board);
}
}
73 changes: 73 additions & 0 deletions src/main/java/domain/BoardFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package domain;

import constant.BoardSpec;
import domain.piece.Empty;
import domain.piece.Piece;
import domain.piece.PieceType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BoardFactory {

private static final int BACK_Y = 0;
private static final int KING_Y = 1;
private static final int CANNON_Y = 2;
private static final int SOLDIER_Y = 3;

private static final List<Integer> CANNON_X = List.of(2, 8);
private static final List<Integer> CHARIOT_X = List.of(1, 9);
private static final List<Integer> GUARD_X = List.of(4, 6);
private static final List<Integer> KING_X = List.of(5);
private static final List<Integer> SOLIDER_X = List.of(1, 3, 5, 7, 9);
private static final List<Integer> FORMATION_X = List.of(2, 3, 7, 8);

public static Board createBoard(Formation choFormation, Formation hanFormation) {
Map<Position, Piece> board = new HashMap<>();
for (int x = BoardSpec.MIN_X; x <= BoardSpec.MAX_X; x++) {
for (int y = BoardSpec.MIN_Y; y <= BoardSpec.MAX_Y; y++) {
placePiece(board, Position.of(x, y), new Empty());
}
}
placePieces(board, choFormation, Side.CHO);
placePieces(board, hanFormation, Side.HAN);

return new Board(board);
}

private static void placePieces(Map<Position, Piece> board, Formation formation, Side side) {
List<Integer> rows = getRowForSide(side);
placePiece(board, CANNON_X, rows.get(CANNON_Y), PieceType.CANNON, side);
placePiece(board, CHARIOT_X, rows.get(BACK_Y), PieceType.CHARIOT, side);
placePiece(board, GUARD_X, rows.get(BACK_Y), PieceType.GUARD, side);
placePiece(board, KING_X, rows.get(KING_Y), PieceType.KING, side);
placePiece(board, SOLIDER_X, rows.get(SOLDIER_Y), PieceType.SOLDIER, side);
placeFormationPiece(board, formation, FORMATION_X, rows.get(BACK_Y), side);
}

private static List<Integer> getRowForSide(Side side) {
if (side == Side.HAN) {
return List.of(1, 2, 3, 4);
}
return List.of(10, 9, 8, 7);
}

private static void placePiece(Map<Position, Piece> board, Position position, Piece piece) {
board.put(position, piece);
}

private static void placePiece(Map<Position, Piece> board, List<Integer> xPositions, int y, PieceType pieceType,
Side side) {
for (int x : xPositions) {
board.put(Position.of(x, y), pieceType.create(side));
}
}

private static void placeFormationPiece(Map<Position, Piece> board, Formation formation, List<Integer> xPositions,
int y, Side side) {
List<PieceType> pieceTypes = formation.getPieceTypes();
for (int i = 0; i < pieceTypes.size(); i++) {
board.put(Position.of(xPositions.get(i), y), pieceTypes.get(i).create(side));
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/domain/Direction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package domain;

public enum Direction {
UP(0,-1),
DOWN(0, 1),
LEFT(-1, 0),
RIGHT(1, 0),
UP_LEFT(-1, -1),
UP_RIGHT(1, -1),
DOWN_LEFT(-1, 1),
DOWN_RIGHT(1, 1);

private final int x;
private final int y;

Direction(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}
38 changes: 38 additions & 0 deletions src/main/java/domain/Formation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package domain;

import domain.piece.PieceType;
import java.util.List;

public enum Formation {
์ƒ๋งˆ๋งˆ์ƒ("1", List.of(PieceType.ELEPHANT, PieceType.HORSE, PieceType.HORSE, PieceType.ELEPHANT)),
๋งˆ์ƒ์ƒ๋งˆ("2", List.of(PieceType.HORSE, PieceType.ELEPHANT, PieceType.ELEPHANT, PieceType.HORSE)),
์ƒ๋งˆ์ƒ๋งˆ("3", List.of(PieceType.ELEPHANT, PieceType.HORSE, PieceType.ELEPHANT, PieceType.HORSE)),
๋งˆ์ƒ๋งˆ์ƒ("4", List.of(PieceType.HORSE, PieceType.ELEPHANT, PieceType.HORSE, PieceType.ELEPHANT));

private static final String INVALID_OPTION_RANGE = "๋ฒˆํ˜ธ๋Š” 1~4 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.";

private final String option;
private final List<PieceType> pieceTypes;

Formation(String option, List<PieceType> pieceTypes) {
this.option = option;
this.pieceTypes = pieceTypes;
}

public static Formation from(String input) {
for (Formation formation : Formation.values()) {
if (formation.option.equals(input)) {
return formation;
}
}
throw new IllegalArgumentException(INVALID_OPTION_RANGE);
}

public List<PieceType> getPieceTypes() {
return pieceTypes;
}

public String toDisplayString() {
return option + ". " + name();
}
}
Loading