Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
48 changes: 48 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

# Created by .ignore support plugin (hsz.mobi)
### Java template
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

.DS_Store
.gradle
/build/
!gradle/wrapper/gradle-wrapper.jar
/out/
/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
9 changes: 9 additions & 0 deletions 8주차과제/RacingCar/java/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import utils.GameUtils;

public class Application {
public static void main(String[] args) {
//final Scanner scanner = new Scanner(System.in);
Comment thread
dustndus8 marked this conversation as resolved.
Outdated
GameUtils gameUtils = new GameUtils();
gameUtils.run();
}
}
21 changes: 21 additions & 0 deletions 8주차과제/RacingCar/java/inpututils/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package inpututils;

import java.util.Scanner;

public class InputView {
private static final Scanner SCANNER = new Scanner(System.in);

public String getCarName() {
System.out.print("경주 할 자동차 이름 : ");
return SCANNER.nextLine();
}

public int getTryNumber() {
System.out.print("시도할 횟수 : ");
try {
return SCANNER.nextInt();
} catch (NumberFormatException e) {
throw new NumberFormatException("[ERROR] 시도 횟수는 숫자여야 한다.");
}
}
}
28 changes: 28 additions & 0 deletions 8주차과제/RacingCar/java/outpututils/OutputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package outpututils;

import racingcar.Car;

import java.util.Vector;

public class OutputView {
Comment thread
dustndus8 marked this conversation as resolved.
public static void printCarName(String name){
System.out.print(name+" : ");
}

public static void printMove(){
System.out.print("-");
}

public static void printWinner(Vector<String> winner,int count){
Comment thread
dustndus8 marked this conversation as resolved.
Outdated

for (int i = 0; i < winner.size(); i++) {
System.out.print(winner.get(i));
if(i!=count-1)
Comment thread
dustndus8 marked this conversation as resolved.
Outdated
{
System.out.print(", ");
}
}

}

}
Comment on lines 17 to 22
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.

마지막에 ,이 나오지 않도록 수정 필요

25 changes: 25 additions & 0 deletions 8주차과제/RacingCar/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package racingcar;

import utils.GameUtils;

public class Car {
private final String name;
private int position = 0;
Copy link
Copy Markdown
Member

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.

프로그래밍 요구사항-Car객체에서 해당 부분이 고정인 것 같아 그대로썼는데, 생성시점에 초기화 해주는 방법이라는 것이 어떤 것인지 잘 이해가 가지 않습니다!


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

public String getName(){
return name;
}

public int plusPosition(){
return position++;
}

public int getPosition() {
return position;
}

}
100 changes: 100 additions & 0 deletions 8주차과제/RacingCar/java/utils/GameUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package utils;

import inpututils.InputView;
import outpututils.OutputView;
import racingcar.Car;

import java.util.Vector;

public class GameUtils {
Comment thread
dustndus8 marked this conversation as resolved.
private static final int START_NUMBER = 0;
private static final int END_NUMBER = 9;

private static InputView inputView = new InputView();
private static SplitString splitString = new SplitString();
private static GameUtils gameUtils = new GameUtils();
private static OutputView outputView = new OutputView();
Comment thread
dustndus8 marked this conversation as resolved.
Outdated

private static Car[] carList;
private static Vector<String> winner;

public static boolean move(int randomNumber) {
if (randomNumber >= 4) {
Comment thread
dustndus8 marked this conversation as resolved.
Outdated
return true;
}
return false;
}

public static void makeCarList(String[] splitResult) {
carList = new Car[splitResult.length];
for (int i = 0; i < splitResult.length; i++) {
carList[i] = new Car(splitResult[i]);
}
}

public static void playGame(Car[] carList) {
for (int j = 0; j < carList.length; j++) {
Boolean isMove = gameUtils.move(RandomUtils.nextInt(START_NUMBER, END_NUMBER));
outputView.printCarName(carList[j].getName());

if (isMove) {
carList[j].plusPosition();
}
for (int k = 0; k < carList[j].getPosition(); k++) {
outputView.printMove();
}
System.out.println();
}
}

public static int getMax(Car[] carList) {
int max = carList[0].getPosition();
for (int i = 1; i < carList.length; i++) {
if (max < carList[i].getPosition()) {
max = carList[i].getPosition();
}
}
return max;
}

public static int makeWinnerCount(int max, Car[] carList) {
int count = 0;
for (int i = 0; i < carList.length; i++) {
if (max == carList[i].getPosition()) {
count++;
}
}
return count;
}

public static void setWinnerVector(int max, Car[] carList, int count) {
winner = new Vector<String>(count);
for (int i = 0; i < carList.length; i++) {
if (max == carList[i].getPosition()) {
winner.add(carList[i].getName());
}
}
}

public static Vector<String> getWinnerVector() {
return winner;
}

public static void run() {

String inputName = inputView.getCarName();
int inputCount = inputView.getTryNumber();
String[] splitResult = splitString.splitString(inputName);

makeCarList(splitResult);

for (int i = 0; i < inputCount; i++) {
playGame(carList);
System.out.println();
}

setWinnerVector(getMax(carList), carList, makeWinnerCount(getMax(carList), carList));
outputView.printWinner(getWinnerVector(), makeWinnerCount(getMax(carList), carList));

}
}
24 changes: 24 additions & 0 deletions 8주차과제/RacingCar/java/utils/RandomUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils;

import java.util.Random;

public class RandomUtils {
private static final Random RANDOM = new Random();

private RandomUtils() {
}

public static int nextInt(final int startInclusive, final int endInclusive) {
if (startInclusive > endInclusive) {
throw new IllegalArgumentException();
}
if (startInclusive < 0) {
throw new IllegalArgumentException();
}

if (startInclusive == endInclusive) {
return startInclusive;
}
return RANDOM.nextInt(endInclusive - startInclusive + 1) + startInclusive;
}
}
7 changes: 7 additions & 0 deletions 8주차과제/RacingCar/java/utils/SplitString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package utils;

public class SplitString {
Comment thread
dustndus8 marked this conversation as resolved.
public static String[] splitString(String input) {
return input.split(",");
}
}
Loading