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

car game 구현 #9

Open
wants to merge 4 commits into
base: splguyjr
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
84 changes: 83 additions & 1 deletion src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,89 @@
package racingcar;

import java.util.InputMismatchException;
import java.util.Scanner;
import camp.nextstep.edu.missionutils.Console;

public class Application {
public static void main(String[] args) {
// TODO 구현 진행
//getInput();
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
Scanner scanner = new Scanner(System.in);

String input = Console.readLine();

String[] car_name = input.split(",");

Choose a reason for hiding this comment

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

시도 횟수를 입력 받을 때 예외 처리를 잘 해주셨는데, 여기서 자동차 이름들을 입력받을 때도 조건이었던 5글자인지 확인하고 예외 처리하는 부분도 추가하면 더 좋을 것 같습니다!

Car[] cars = new Car[car_name.length];



int attempt =0;
boolean validInput = false;

//
while(!validInput) {
try {
System.out.println("시도할 회수는 몇회인가요?");
attempt = scanner.nextInt();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Scanner 대신 console.readLine() -> Integer.parseInt 라는 함수로 파싱을 해서 사용하실 수 있습니다!

validInput = true;
} catch (InputMismatchException e) {
System.out.println("[ERROR] 시도 횟수는 숫자여야 한다.");
Console.readLine();
}
}

System.out.println("\n실행 결과");

//makeCarInstance
for(int i=0; i< car_name.length; i++) {
String carName = car_name[i];
cars[i] = new Car(carName);
}

int[] distance_arr = new int[car_name.length];

for(int j=0; j<attempt; j++) {
for(int i=0; i< car_name.length; i++) {
int check = cars[i].GenerateRandomNumber();
if (cars[i].CheckAdvance(check)) {
cars[i].Advance();
}
cars[i].show_result();
if(j == attempt-1) {
distance_arr[i] = cars[i].getDistance();
}
}
System.out.println();
}

//checkMax
int max = 0;

for(int i=0; i< car_name.length; i++) {
if(distance_arr[i]> max) {
max = distance_arr[i];
}
}
int cnt = 0;
for(int i=0; i< car_name.length; i++) {
if(distance_arr[i] == max) {
cnt++;
}
}

//printWinner carArray
System.out.print("최초 우승자 : ");
for(int i=0; i< car_name.length; i++) {
if (distance_arr[i] == max) {
if (cnt > 1) {
System.out.print(cars[i].getName() + ", ");
cnt--;
}
else {
System.out.println(cars[i].getName());
}
}
}
}

}
41 changes: 39 additions & 2 deletions src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
package racingcar;
import camp.nextstep.edu.missionutils.Randoms;

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

private int distance = 0;
public Car(String name) {
this.name = name;
}

// 추가 기능 구현
public int GenerateRandomNumber() {
int randomNumber = Randoms.pickNumberInRange(0, 9);
return randomNumber;
}

public int getDistance() {
return distance;
}

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

public boolean CheckAdvance(int num) {
if(num>=4) {
return true; }
return false;
}

public void Advance() {
this.distance++;
}

public void show_result() {
System.out.print(this.name + " : ");
for(int i=0; i<this.distance; i++) {
System.out.print("-");
}
System.out.println();
}

public void show_winner() {
System.out.print("최종 우승자 : " + this.name);

}


}