๐ ๊ตฌํ ๊ธฐ๋ฅ ๋ชฉ๋ก
- ์
๋ ฅ
- ์ฐธ์ฌ์ ์ด๋ฆ
- ์คํ ๊ฒฐ๊ณผ
- ์ต๋ ์ฌ๋ค๋ฆฌ ๋์ด
- ๊ฒฐ๊ณผ ๋ณด๊ณ ์ถ์ ์ฌ๋ ์ด๋ฆ (๊ฐ์ธ or all)
- ์ถ๋ ฅ
- ์ฌ๋ค๋ฆฌ ๊ฒฐ๊ณผ
- ํ๋์ ๊ฐ๋ก ๋ผ์ธ์ ๊ฐ์ง๊ณ ์๋ Line Class
- ํ๋์ ์ธ๋ก ๋ผ์ธ์ ๋น ๊ฐ์ด ์๋๋ก ๊ฐ๋ก ๋ผ์ธ์ ์์ฑํ๋ ๋ก์ง ์ถ๊ฐ
- ์ต๋ ์ฌ๋ค๋ฆฌ ๋์ด๋งํผ Line์ ๊ฐ์ง๊ณ ์๋ Ladder Class
- ์คํ ๊ฒฐ๊ณผ (๊ฐ์ธ or all)
- ๋ผ์ธ์ ๋ฐ๋ผ player์ ์์น๋ฅผ ์ด๋ ์์ผ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์์ฑํ๋ ๋ก์ง ์ถ๊ฐ
- ์ฌ๋ค๋ฆฌ ๊ฒฐ๊ณผ
๊ธฐ๋ฅ ์๊ตฌ ์ฌํญ
๐- ์ฌ๋ค๋ฆฌ ๊ฒ์์ ์ฐธ์ฌํ๋ ์ฌ๋์ ์ด๋ฆ์ ์ต๋5๊ธ์๊น์ง ๋ถ์ฌํ ์ ์๋ค. ์ฌ๋ค๋ฆฌ๋ฅผ ์ถ๋ ฅํ ๋ ์ฌ๋ ์ด๋ฆ๋ ๊ฐ์ด ์ถ๋ ฅํ๋ค.
- ์ฌ๋ ์ด๋ฆ์ ์ผํ(,)๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถํ๋ค.
- ์ฌ๋ ์ด๋ฆ์ 5์ ๊ธฐ์ค์ผ๋ก ์ถ๋ ฅํ๊ธฐ ๋๋ฌธ์ ์ฌ๋ค๋ฆฌ ํญ๋ ๋์ด์ ธ์ผ ํ๋ค.
- ์ฌ๋ค๋ฆฌ ํ๊ธฐ๊ฐ ์ ์์ ์ผ๋ก ๋์ํ๋ ค๋ฉด ๋ผ์ธ์ด ๊ฒน์น์ง ์๋๋ก ํด์ผ ํ๋ค.
- |-----|-----| ๋ชจ์๊ณผ ๊ฐ์ด ๊ฐ๋ก ๋ผ์ธ์ด ๊ฒน์น๋ ๊ฒฝ์ฐ ์ด๋ ๋ฐฉํฅ์ผ๋ก ์ด๋ํ ์ง ๊ฒฐ์ ํ ์ ์๋ค.
- ์ฌ๋ค๋ฆฌ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํด์ผ ํ๋ค.
- ๊ฐ์ธ๋ณ ์ด๋ฆ์ ์ ๋ ฅํ๋ฉด ๊ฐ์ธ๋ณ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๊ณ , "all"์ ์ ๋ ฅํ๋ฉด ์ ์ฒด ์ฐธ์ฌ์์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค.
๊ตฌํํด์ผ ํ ๊ธฐ๋ฅ
๐- ์ ์๊ตฌ์ฌํญ์ ๋ฐ๋ผ 4๋ช ์ ์ฌ๋์ ์ํ 5๊ฐ ๋์ด ์ฌ๋ค๋ฆฌ๋ฅผ ๋ง๋ค ๊ฒฝ์ฐ, ํ๋ก๊ทธ๋จ์ ์คํํ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ๋ค.
์ฐธ์ฌํ ์ฌ๋ ์ด๋ฆ์ ์
๋ ฅํ์ธ์. (์ด๋ฆ์ ์ผํ(,)๋ก ๊ตฌ๋ถํ์ธ์)
dowon, halim, geinee, dowon2
์คํ ๊ฒฐ๊ณผ๋ฅผ ์
๋ ฅํ์ธ์. (๊ฒฐ๊ณผ๋ ์ผํ(,)๋ก ๊ตฌ๋ถํ์ธ์)
๊ฝ,5000,๊ฝ,3000
์ต๋ ์ฌ๋ค๋ฆฌ ๋์ด๋ ๋ช ๊ฐ์ธ๊ฐ์?
5
์ฌ๋ค๋ฆฌ ๊ฒฐ๊ณผ
dowon halim geinee dowon2
|-----| |-----|
| |-----| |
|-----| | |
| |-----| |
|-----| |-----|
๊ฝ 5000 ๊ฝ 3000
๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ณ ์ถ์ ์ฌ๋์?
dowon
์คํ ๊ฒฐ๊ณผ
๊ฝ
๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ณ ์ถ์ ์ฌ๋์?
all
์คํ ๊ฒฐ๊ณผ
dowon : ๊ฝ
halim : 3000
geinee : ๊ฝ
dowon2 : 5000
###
- ๋ฉ์๋ ์ ์ธ ๊ท์น
1.
์์ฑ์
static ๋ฉ์๋
public ๋ฉ์๋
private ๋ฉ์๋
getter ๋ฉ์๋
2.
์์ฑ์
static ๋ฉ์๋
public ๋ฉ์๋
์ public๋ฉ์๋์์ ํธ์ถํ๋ private ๋ฉ์๋๋ค
public ๋ฉ์๋
์ public๋ฉ์๋์์ ํธ์ถํ๋ private ๋ฉ์๋๋ค
๋ฐ๋ณต..
getter ๋ฉ์๋
- Random ๊ฐ์ฒด nextBoolean()
- ๋ฐฐ์ด๋ณด๋ค๋ java Collection(List, Map, Set) ์ฌ์ฉ
- ์ซ์ ์์ ์ ์ธ
- stream ์ฌ์ฉ
- ํ ์คํธ ์ปค๋ฒ๋ฆฌ์ง ํ์ฉ
- for, if๋ฌธ ๋ฑ ํ์ค ์ค๊ดํธ ์๋ต x
main controller
public class LadderGame {
public static final String ALL = "all";
public void play() {
Players players = createPlayers();
int playerCount = players.getPlayerCount();
Prizes prizes = createPrizes(playerCount);
int height = inputHeight();
Ladder ladder = new Ladder(playerCount, height);
printLadder(players, ladder, prizes);
GameResult gameResult = createGameResult(players, ladder, prizes);
printGameResult(gameResult);
}
private Players createPlayers() {
String[] names;
Players players;
do {
names = inputPlayersNames();
players = new Players(names);
} while (!players.isValidNameLength(names) || !players.isValidPlayerCount(names));
return players;
}
private String[] inputPlayersNames() {
String names = InputView.inputPlayersNames();
return Convert.splitNames(names);
}
private Prizes createPrizes(int playerCount) {
String[] prize;
Prizes prizes;
do {
prize = inputPrizes();
prizes = new Prizes(prize);
} while (!prizes.isValidPrizeCount(prize, playerCount));
return prizes;
}
private String[] inputPrizes() {
String prizes = InputView.inputPrizes();
return Convert.splitNames(prizes);
}
private int inputHeight() {
return InputView.inputHeight();
}
private void printLadder(Players players, Ladder ladder, Prizes prizes) {
OutputView.printPlayersNames(players.getPlayers());
for (Line line : ladder.getLadder()) {
OutputView.printLine(line.getLine());
}
OutputView.printPrizes(prizes.getPrizes());
}
private GameResult createGameResult(Players players, Ladder ladder, Prizes prizes) {
GameResultCreator gameResultCreator = new GameResultCreator(players.getPlayers(), ladder.getLadder(), prizes.getPrizes());
return new GameResult(gameResultCreator.getGameResult());
}
private void printGameResult(GameResult gameResult) {
do {
String name = InputView.inputPeopleResultRequest();
if (name.equals(ALL)) {
break;
}
printPlayerResult(gameResult, name);
} while (true);
OutputView.printAllGameResult(gameResult.getGameResult());
}
private void printPlayerResult(GameResult gameResult, String name) {
Map<String, String> results = gameResult.getGameResult();
if (results.get(name) == null) {
System.out.println("ํด๋น ํ๋ ์ด์ด๋ ์กด์ฌํ์ง ์์ต๋๋ค. \n๋ค์ ์
๋ ฅ ํด ์ฃผ์ธ์.");
return;
}
OutputView.printOnePeopleGameResult(gameResult.getGameResult(), name);
}
}
model.GameResultCreator
public class GameResultCreator {
private final Map<String, String> gameResult;
public GameResultCreator(List<String> players, List<Line> lines, List<String> prizes) {
this.gameResult = createGameResult(players, lines, prizes);
}
private Map<String, String> createGameResult(List<String> players, List<Line> lines, List<String> prizes) {
Map<String, String> gameResult = new HashMap<>();
for (int i = 0; i < players.size(); i++) {
int finalPlayerLocationIdx = findFinalPlayerLocationIdx(lines, i);
String playerName = players.get(i);
String prize = prizes.get(finalPlayerLocationIdx);
gameResult.put(playerName, prize);
}
return gameResult;
}
private int findFinalPlayerLocationIdx(List<Line> lines, int playerStartIdx) {
int location = playerStartIdx;
int height = 0;
while (height < lines.size()) {
Line line = lines.get(height);
location = updateLocation(line, location);
height++;
}
return location;
}
private int updateLocation(Line line, int location) {
int lastIdx = line.getLine().size();
if (location == 0) {
location = updateRightDirection(line.getLine(), location);
return location;
}
if (location == lastIdx) {
location = updateLeftDirection(line.getLine(), location);
return location;
}
int newLocation = updateRightDirection(line.getLine(), location);
return newLocation != location ? newLocation : updateLeftDirection(line.getLine(), location);
}
private int updateRightDirection(List<Boolean> points, int location) {
boolean rightPoint = points.get(location);
return rightPoint ? location + 1 : location;
}
private int updateLeftDirection(List<Boolean> points, int location) {
boolean leftPoint = points.get(location - 1);
return leftPoint ? location - 1 : location;
}
public Map<String, String> getGameResult() {
return this.gameResult;
}
}
model.Line
public class Line {
private final List<Boolean> line;
public Line(int width) {
this.line = createLine(width);
}
private List<Boolean> createLine(int width) {
Random random = new Random();
List<Boolean> points = new ArrayList<>();
for (int i = 0; i < width; i++) {
points.add(random.nextBoolean());
}
return createValidLine(points);
}
private List<Boolean> createValidLine(List<Boolean> points) {
int width = points.size();
for (int i = 0; i < width; i++) {
modifyPoint(points, i);
}
return points;
}
private void modifyPoint(List<Boolean> points, int pointIdx) {
int width = points.size();
int lastIdx = width - 1;
if (pointIdx != lastIdx && isValidPoints(points, pointIdx)) {
points.set(pointIdx + 1, false);
}
}
private boolean isValidPoints(List<Boolean> points, int index) {
return points.get(index) && points.get(index + 1);
}
public List<Boolean> getLine() {
return this.line;
}
}
model.Lines
public class Lines {
private final List<Line> lines;
public Lines(int width, int height) {
this.lines = createValidLines(width, height);
}
private List<Line> createValidLines(int width, int height) {
List<Line> lines;
boolean replay;
do {
lines = createRandomLines(width, height);
List<Integer> count = calculateBridgesCounts(lines, width, height);
replay = count.stream()
.anyMatch(bridgeCount -> bridgeCount == 0);
} while (replay);
return lines;
}
private List<Line> createRandomLines(int width, int height) {
List<Line> lines = new ArrayList<>();
for (int i = 0; i < height; i++)
lines.add(new Line(width));
return lines;
}
private List<Integer> calculateBridgesCounts(List<Line> lines, int width, int height) {
List<Integer> bridgesCounts = new ArrayList<>();
for (int i = 0; i < width; i++) {
int bridgeCount = getBridgeCount(lines, i, height);
bridgesCounts.add(bridgeCount);
}
return bridgesCounts;
}
private int getBridgeCount(List<Line> lines, int index, int height) {
int count = 0;
for (int i = 0; i < height; i++) {
Line line = lines.get(i);
List<Boolean> points = line.getLine();
count = updateBridgeCount(points.get(index), count);
}
return count;
}
private int updateBridgeCount(boolean point, int count) {
return point ? count + 1 : count;
}
public List<Line> getLines() {
return this.lines;
}
}
'๐ป > Java CleanCode Study' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐๋ธ๋์ญ ๊ฒ์ # 2022.03.13~2022.04.09 (0) | 2022.06.12 |
---|---|
๐ฐ๋ก๋ ๊ฒ์ (MVC ํจํด) #2022.02.21~2022.03.05 (0) | 2022.06.12 |
Could not find method testCompile() for arguments [{group=junit, name=junit, version=4.12}] (0) | 2022.04.22 |
[ERROR] - Could not find or load main class worker.org.gradle.process.internal.worker.GradleWorkerMain (0) | 2022.02.26 |
[JAVA] test code ์์ฑ๋ฒ (0) | 2022.02.26 |