-
Notifications
You must be signed in to change notification settings - Fork 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
car game 구현 #9
Open
splguyjr
wants to merge
4
commits into
Dcom-KHU:splguyjr
Choose a base branch
from
splguyjr:splguyjr
base: splguyjr
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
car game 구현 #9
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(","); | ||
Car[] cars = new Car[car_name.length]; | ||
|
||
|
||
|
||
int attempt =0; | ||
boolean validInput = false; | ||
|
||
// | ||
while(!validInput) { | ||
try { | ||
System.out.println("시도할 회수는 몇회인가요?"); | ||
attempt = scanner.nextInt(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
|
||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
시도 횟수를 입력 받을 때 예외 처리를 잘 해주셨는데, 여기서 자동차 이름들을 입력받을 때도 조건이었던 5글자인지 확인하고 예외 처리하는 부분도 추가하면 더 좋을 것 같습니다!