Skip to content
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

장재훈 - 자동차 게임 구현 #2

Open
wants to merge 30 commits into
base: Jaehooni
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1613bb7
docs: update manual.md
Jaehooni Mar 14, 2023
494a07d
feat: move function
Jaehooni Mar 14, 2023
71039f0
docs: add new manual
Jaehooni Mar 14, 2023
0c861ab
feat: add game method
Jaehooni Mar 14, 2023
64e0e9f
feat: add make method
Jaehooni Mar 14, 2023
dfe3b19
docs: delete manual item
Jaehooni Mar 14, 2023
79e996e
fix: typo
Jaehooni Mar 14, 2023
e10a9cc
feat: add new func to show output
Jaehooni Mar 15, 2023
9b3f2c8
feat: add new error find features
Jaehooni Mar 15, 2023
4178bf0
docs: simpify fix sentence
Jaehooni Mar 15, 2023
f10bbe7
docs: simplify fix sentence
Jaehooni Mar 15, 2023
7635771
refactor: move input feature to InputCheck.java
Jaehooni Mar 15, 2023
78a914e
feat: make execute code
Jaehooni Mar 15, 2023
7a8542e
refactor: make project convention style same as naver java convention
Jaehooni Mar 15, 2023
7254c16
refactor(replace declaration type):
Jaehooni Mar 21, 2023
f73573a
refactor: Separate print func from Car.java
Jaehooni Mar 21, 2023
6330ace
refactor: Separate func from Car.java
Jaehooni Mar 21, 2023
6e6c23b
refactor: just fix some codes
Jaehooni Mar 21, 2023
1ba30dd
feat: make new instance parameter
Jaehooni Mar 21, 2023
88db2a1
style: change parameter name
Jaehooni Mar 21, 2023
bfa1c4e
style: change parameter name
Jaehooni Mar 24, 2023
d90073c
refactor: separate sort function from findMaxValue(...)
Jaehooni Mar 24, 2023
5a31b44
refactor: add new function to start()
Jaehooni Mar 24, 2023
aa47529
refactor: simple fix
Jaehooni Mar 24, 2023
f5c5154
refactor: move files to utility directory
Jaehooni Mar 25, 2023
b00e2b0
refactor: move files to utility directory
Jaehooni Mar 25, 2023
3de28a2
refactor: move sort func to Sort.java
Jaehooni Mar 25, 2023
4a7544c
feat: overriding toString()
Jaehooni Mar 25, 2023
582d57f
refactor: move sort func to Sort.java
Jaehooni Mar 25, 2023
7909af2
feat: white space
Jaehooni Apr 9, 2023
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
7 changes: 4 additions & 3 deletions src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package racingcar;

public class Application {
public static void main(String[] args) {
// TODO 구현 진행
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}
32 changes: 26 additions & 6 deletions src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
package racingcar;

import camp.nextstep.edu.missionutils.Randoms;

public class Car {
private final String name;
private int position = 0;
private final String name;
private int position = 0;

public Car(String name) {
this.name = name;
}

public int getPosition() {
return this.position;
}

public String getName() {
return this.name;
}

public Car(String name) {
this.name = name;
}
public String toString() {
String distance = new String(new char[this.getPosition()]).replace("\0", "-");
return this.getName() + " : " + distance;
}

// 추가 기능 구현
// 추가 기능 구현
void move() {
if (Randoms.pickNumberInRange(0, 9) >= 4) {
this.position += 1;
}
}
}
47 changes: 47 additions & 0 deletions src/main/java/racingcar/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package racingcar;

import java.util.ArrayList;
import java.util.List;

import utility.InputCheck;
import utility.Output;
import utility.Sort;

public class Game {
private List<Car> cars = new ArrayList<>();

private void make() {
List<String> names = InputCheck.inputCarNames();
for (String name : names) {
this.cars.add(new Car(name));
}
}

private void run() {
int num = InputCheck.inputRound();
for (int i = 0; i < num; i++) {
this.cars.forEach(Car::move);
Output.printProcedure(this.cars);
}
}

public List<String> findWinners() {
List<String> winner = new ArrayList<>();
List<Car> sortedCars = Sort.sortByPosition(this.cars);
int winnerScore = sortedCars.get(0).getPosition();
for (Car car : sortedCars) {
if (car.getPosition() < winnerScore) {
break;
}
winner.add(car.getName());
}

return winner;
}

public void start() {
make();
run();
Output.printResult(findWinners());
}
}
10 changes: 10 additions & 0 deletions src/main/java/racingcar/manual.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
자동차 경주 게임 기능 구현
======================

🗒️기능 목록
--------
### 1. 자동차가 주어진 Random 값에 따라 전진 혹은 멈추는 기능
### 2. 입력받은 이동 횟수만큼 1의 함수를 실행시키는 기능
### 3. 입력이 잘못되었을때 에러를 발생시키고 다시 입력받게 하는 기능
### 4. 입력으로 주어진 이름을 기반으로 Car Object를 생성하는 기능
### 5. 결과를 출력하는 기능
49 changes: 49 additions & 0 deletions src/main/java/utility/InputCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package utility;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

import camp.nextstep.edu.missionutils.Console;

public final class InputCheck {

public static List<String> inputCarNames() {
while (true) {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉽표(,) 기준으로 구분)");
List<String> names = Arrays.asList(Console.readLine().split(","));
try {
names.forEach(InputCheck::checkName);
return names;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}

private static void checkName(String name) {
if (name.trim().length() == 0 || name.trim().length() > 5) {
throw new IllegalArgumentException("[ERROR] 이름은 1~5자 사이로 작성하라.");
}
}

public static int inputRound() {
while (true) {
System.out.println("시도할 회수는 몇회인가요?");
try {
String input = Console.readLine();
checkRound(input);
return Integer.parseInt(input);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}

private static void checkRound(String input) {
String pattern = "^[0-9]+$";
if (!Pattern.matches(pattern, input)) {
throw new IllegalArgumentException("[ERROR] 시도 횟수는 자연수인 숫자여야 한다.");
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/utility/Output.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package utility;

import java.util.List;

import racingcar.Car;

public class Output {
public static void printProcedure(List<Car> cars) {
System.out.println("실행 결과");
cars.forEach(car -> System.out.println(car.toString()));
System.out.println();
}

public static void printResult(List<String> winners) {
System.out.println("최종 우승자" + " : " + String.join(", ", winners));
}

}
12 changes: 12 additions & 0 deletions src/main/java/utility/Sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package utility;

import java.util.List;

import racingcar.Car;

public class Sort {
public static List<Car> sortByPosition(List<Car> cars) {
cars.sort((x, y) -> y.getPosition() - x.getPosition());
return cars;
}
}