diff --git a/build.gradle b/build.gradle index bc22cd5..24eb78d 100644 --- a/build.gradle +++ b/build.gradle @@ -12,5 +12,11 @@ repositories { } dependencies { - testCompile group: 'junit', name: 'junit', version: '4.12' + testCompile('org.junit.jupiter:junit-jupiter:5.4.2') + testCompile('org.assertj:assertj-core:3.11.1') + implementation 'junit:junit:4.12' +} + +test { + useJUnitPlatform() } diff --git a/src/main/java/gradle/wrapper/gradle-wrapper.jar b/src/main/java/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..87b738c Binary files /dev/null and b/src/main/java/gradle/wrapper/gradle-wrapper.jar differ diff --git a/src/main/java/gradle/wrapper/gradle-wrapper.properties b/src/main/java/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..44e7c4d --- /dev/null +++ b/src/main/java/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/src/main/java/gradlew b/src/main/java/gradlew new file mode 100644 index 0000000..af6708f --- /dev/null +++ b/src/main/java/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/src/main/java/gradlew.bat b/src/main/java/gradlew.bat new file mode 100644 index 0000000..6d57edc --- /dev/null +++ b/src/main/java/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/java/racingCar/Application.java b/src/main/java/racingCar/Application.java index b761a7a..d8e007f 100644 --- a/src/main/java/racingCar/Application.java +++ b/src/main/java/racingCar/Application.java @@ -3,8 +3,8 @@ import java.util.Scanner; public class Application { - public static void main(String[] args) { - final Scanner scanner = new Scanner(System.in); - // TODO 구현 진행 - } -} + public static void main(String[] args) { + RacingCarGame racingCarGame = new RacingCarGame(); + racingCarGame.play(); + } +} \ No newline at end of file diff --git a/src/main/java/racingCar/Car.java b/src/main/java/racingCar/Car.java index 685ffed..aa2e105 100644 --- a/src/main/java/racingCar/Car.java +++ b/src/main/java/racingCar/Car.java @@ -1,12 +1,53 @@ package racingCar; +import utils.RandomUtils; + public class Car { - private final String name; - private int position = 0; + private static final int START_INCLUSIVE = 0; + private static final int END_INCLUSIVE = 9; + private static final int MOVING_STANDARD = 4; + private final String name; + private int position = 0; + + public Car(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } + + private int getNumber() { + return RandomUtils.nextInt(START_INCLUSIVE, END_INCLUSIVE); + } + + public int getPosition() { + return this.position; + } + + public void moveOrStop(int number) { + if (number >= MOVING_STANDARD) + this.position++; + } + + public int getMoveCount(){ + int number = getNumber(); + moveOrStop(number); + + int moveCount = this.getPosition(); + + return moveCount; + } - public Car(String name) { - this.name = name; - } + public String printMove(int count) { + String move = ""; + char racePoint = '-'; - // 추가 기능 구현 -} + System.out.print(this.name + ":"); + for (int i = 0; i < count; i++) { + move += racePoint; + System.out.print("-"); + } + return move; + } +} \ No newline at end of file diff --git a/src/main/java/racingCar/RacingCarGame.java b/src/main/java/racingCar/RacingCarGame.java new file mode 100644 index 0000000..aff272a --- /dev/null +++ b/src/main/java/racingCar/RacingCarGame.java @@ -0,0 +1,152 @@ +package racingCar; + +import java.util.ArrayList; +import java.util.InputMismatchException; +import java.util.List; +import java.util.Scanner; + +class RacingCarGame { + private static final int NAME_LENGTH_STANDARD = 5; + private static final int NOT_A_NUMBER = -1; + final Scanner scanner = new Scanner(System.in); + + public void play() { + String[] names = inputNames(); + int tryNumber = inputTryNumber(); + List cars = createCars(names); + + System.out.println("실행 결과"); + for (int i = 0; i < tryNumber; i++) + printRaceResult(cars); + + System.out.print("최종 우승자: "); + List winners = findWinner(cars); + printWinner(winners); + } + + public int inputTryNumber() { + int tryNumber = NOT_A_NUMBER; + + while (!isNumber(tryNumber)) { + try { + System.out.println("시도할 횟수는 몇회인가요?"); + tryNumber = scanner.nextInt(); + } catch (InputMismatchException ime) { + System.out.println("[ERROR] 시도 횟수는 숫자여야 한다."); + tryNumber = NOT_A_NUMBER; + scanner.next(); + } + } + return tryNumber; + } + + public boolean isNumber(int tryNumber) { + return tryNumber != NOT_A_NUMBER; + } + + public String[] splitNames(String name) { + return name.split(","); + } + + public boolean checkNameLength(String[] names) { + for (String name : names) { + if (name.length() > NAME_LENGTH_STANDARD) { + System.out.println("[ERROR] 5자 이하의 이름으로 입력하세요."); + return false; + } + } + return true; + } + + private String[] inputNames() { + boolean check; + String[] names; + + System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)"); + do { + String name = scanner.next(); + names = splitNames(name); + check = checkNameLength(names); + } while (!check); + + return names; + } + + public List createCars(String[] names) { + List cars = new ArrayList<>(); + + for (String name : names) { + Car car = new Car(name); + cars.add(car); + } + return cars; + } + + private void printRaceResult(List cars) { + for (int i = 0; i < cars.size(); i++) { + Car car = cars.get(i); + int count = car.getMoveCount(); + car.printMove(count); + System.out.println(""); + } + System.out.println(""); + } + + public int getMaxPositionIdx(List cars, Car car, int maxPosition) { + int maxPositionIdx = 0; + + for (int i = 0; i < cars.size(); i++) { + car = cars.get(i); + int position = car.getPosition(); + + if (maxPosition < position) { + maxPosition = position; + maxPositionIdx = i; + } + } + + return maxPositionIdx; + } + + public List getCoWinnerIdx(List cars, int maxPositionIdx) { + List coWinnerIdx = new ArrayList(); + Car car = cars.get(maxPositionIdx); + int maxPosition = car.getPosition(); + + for (int i = maxPositionIdx + 1; i < cars.size(); i++) { + car = cars.get(i); + if (maxPosition == car.getPosition()) { + coWinnerIdx.add(i); + } + } + + return coWinnerIdx; + } + + private void addWinner(List cars, List winners, int maxPositionIdx) { + Car car = cars.get(maxPositionIdx); + String winnerName = car.getName(); + winners.add(winnerName); + } + + private List findWinner(List cars) { + List winners = new ArrayList<>(); + Car initialCar = cars.get(0); + int initialPosition = initialCar.getPosition(); + int maxPositionIdx = getMaxPositionIdx(cars, initialCar, initialPosition); + addWinner(cars, winners, maxPositionIdx); + + List coWinnerIndex = getCoWinnerIdx(cars, maxPositionIdx); + for (int coWinnerIdx : coWinnerIndex) + addWinner(cars, winners, coWinnerIdx); + + return winners; + } + + public String printWinner(List winners) { + String winner = String.join(", ", winners); + System.out.println(winner); + return winner; + } +} + diff --git a/src/main/java/racingCar/gradle/wrapper/gradle-wrapper.jar b/src/main/java/racingCar/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..87b738c Binary files /dev/null and b/src/main/java/racingCar/gradle/wrapper/gradle-wrapper.jar differ diff --git a/src/main/java/racingCar/gradle/wrapper/gradle-wrapper.properties b/src/main/java/racingCar/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..44e7c4d --- /dev/null +++ b/src/main/java/racingCar/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/src/main/java/racingCar/gradlew b/src/main/java/racingCar/gradlew new file mode 100644 index 0000000..af6708f --- /dev/null +++ b/src/main/java/racingCar/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/src/main/java/racingCar/gradlew.bat b/src/main/java/racingCar/gradlew.bat new file mode 100644 index 0000000..6d57edc --- /dev/null +++ b/src/main/java/racingCar/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/java/utils/RandomUtils.java b/src/main/java/utils/RandomUtils.java index 61372c0..63ba351 100644 --- a/src/main/java/utils/RandomUtils.java +++ b/src/main/java/utils/RandomUtils.java @@ -3,24 +3,24 @@ import java.util.Random; public class RandomUtils { - private static final Random RANDOM = new Random(); + private static final Random RANDOM = new Random(); - private RandomUtils() { - } + private RandomUtils() { + } - public static int nextInt(final int startInclusive, final int endInclusive) { - if (startInclusive > endInclusive) { - throw new IllegalArgumentException(); - } + public static int nextInt(final int startInclusive, final int endInclusive) { + if (startInclusive > endInclusive) { + throw new IllegalArgumentException(); + } - if (startInclusive < 0) { - throw new IllegalArgumentException(); - } + if (startInclusive < 0) { + throw new IllegalArgumentException(); + } - if (startInclusive == endInclusive) { - return startInclusive; - } + if (startInclusive == endInclusive) { + return startInclusive; + } - return startInclusive + RANDOM.nextInt(endInclusive - startInclusive + 1); - } + return startInclusive + RANDOM.nextInt(endInclusive - startInclusive + 1); + } } diff --git a/src/test/java/racingCar/ApplicationTest.java b/src/test/java/racingCar/ApplicationTest.java new file mode 100644 index 0000000..7a2c677 --- /dev/null +++ b/src/test/java/racingCar/ApplicationTest.java @@ -0,0 +1,12 @@ +package racingCar; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +public class ApplicationTest { + @Test + void test(){ + assertThat(1); + } +} diff --git a/src/test/java/racingCar/CarTest.java b/src/test/java/racingCar/CarTest.java new file mode 100644 index 0000000..fc1bac2 --- /dev/null +++ b/src/test/java/racingCar/CarTest.java @@ -0,0 +1,46 @@ +package racingCar; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +public class CarTest { + @Test + void 랜덤_값이_4보다_큰_경우(){ + //given + Car car = new Car("halim"); + int number = 8; + + //when + car.moveOrStop(number); + + //then + assertThat(car.getPosition()).isEqualTo(1); + } + + @Test + void 랜덤_값이_4보다_작은_경우() { + //given + Car car = new Car("halim"); + int number = 3; + + //when + car.moveOrStop(number); + + //then + assertThat(car.getPosition()).isEqualTo(0); + } + + @Test + void 경주_결과를_출력하는_경우(){ + //given + Car car = new Car("halim"); + int moveCount = 4; + + //when + String move = car.printMove(moveCount); + + //then + assertThat(move).isEqualTo("----"); + } +} diff --git a/src/test/java/racingCar/RacingGameTest.java b/src/test/java/racingCar/RacingGameTest.java new file mode 100644 index 0000000..8b45bb4 --- /dev/null +++ b/src/test/java/racingCar/RacingGameTest.java @@ -0,0 +1,132 @@ +package racingCar; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +public class RacingGameTest { + @Test + void 쉼표_기준으로_이름이_쪼개지는_경우(){ + //given + RacingCarGame game = new RacingCarGame(); + String name = "하림,진희,도원"; + + //when + String[] names = game.splitNames(name); + + //then + assertThat(names.length).isEqualTo(3); + assertThat(names[0]).isEqualTo("하림"); + assertThat(names[1]).isEqualTo("진희"); + assertThat(names[2]).isEqualTo("도원"); + } + + @Test + void 자동차_객체가_생성되는_경우(){ + //given + RacingCarGame game = new RacingCarGame(); + String[] names = {"하림", "진희" , "도원"}; + + //when + List car = game.createCars(names); + + //then + assertThat(car.size()).isEqualTo(3); + assertThat(car.get(0).getName()).isEqualTo("하림"); + assertThat(car.get(1).getName()).isEqualTo("진희"); + assertThat(car.get(2).getName()).isEqualTo("도원"); + } + + @Test + void 시도횟수가_숫자인_경우(){ + //given + RacingCarGame game = new RacingCarGame(); + int tryNumber = 4; + + //when + boolean check = game.isNumber(tryNumber); + + //then + assertThat(check).isEqualTo(true); + } + + @Test + void 시도횟수가_숫자가_아닌_경우(){ + //given + RacingCarGame game = new RacingCarGame(); + int tryNumber = -1; + + //when + boolean check = game.isNumber(tryNumber); + + //then + assertThat(check).isEqualTo(false); + } + + @Test + void 이름의_길이가_5자_이하일_경우(){ + //given + RacingCarGame game = new RacingCarGame(); + String[] names = {"하림", "진희" , "도원"}; + + //when + boolean check = game.checkNameLength(names); + + //then + assertThat(check).isEqualTo(true); + + } + + @Test + void 이름의_길이가_5자_이상일_경우(){ + //given + RacingCarGame game = new RacingCarGame(); + String[] names = {"Margaret", "halim" , "Elizabethe"}; + + //when + boolean check = game.checkNameLength(names); + + //then + assertThat(check).isEqualTo(false); + } + + @Test + void 우승자를_쉼표를_포함해_출력하는_꼉경우(){ + //given + RacingCarGame game = new RacingCarGame(); + List winners = new ArrayList<>(); + winners.add("하림"); + winners.add("진희"); + + //when + String winner = game.printWinner(winners); + + //then + assertThat(winner).isEqualTo("하림, 진희"); + } + + @Test + void 가장_큰_포지션의_인덱스를_구하는_경우(){ + //given + RacingCarGame game = new RacingCarGame(); + List cars = new ArrayList<>(); + Car car1 = new Car("하림"); + Car car2 = new Car("진희"); + Car car3 = new Car("도원"); + car1.moveOrStop(1); + car2.moveOrStop(8); + car3.moveOrStop(2); + cars.add(car1); + cars.add(car2); + cars.add(car3); + + //when + int maxPosition = game.getMaxPositionIdx(cars, car1, car1.getPosition()); + + //then + assertThat(maxPosition).isEqualTo(1); + } +} diff --git a/src/test/java/utils/RandomUtilsTest.java b/src/test/java/utils/RandomUtilsTest.java new file mode 100644 index 0000000..3d4f5c1 --- /dev/null +++ b/src/test/java/utils/RandomUtilsTest.java @@ -0,0 +1,58 @@ +package utils; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + + +public class RandomUtilsTest { + + @Test + void 랜덤_값이_0부터_9사이인_경우(){ + //given + int startInclusive = 0; + int endInclusive = 9; + + //when + int result = RandomUtils.nextInt(startInclusive, endInclusive); + + // then + assertThat(result).isBetween(startInclusive, endInclusive); + } + + @Test + void 시작값이_종료값보다_큰_경우() throws IllegalArgumentException{ + //given + int startInclusive = 5; + int endInclusive = 2; + + //when + Assertions.assertThrows(IllegalArgumentException.class, + () -> RandomUtils.nextInt(startInclusive, endInclusive)); + } + + @Test + void 시작값이_음수인_경우() { + //given + int startInclusive = -2; + int endInclusive = 9; + + //when + Assertions.assertThrows(IllegalArgumentException.class, + () -> RandomUtils.nextInt(startInclusive, endInclusive)); + } + + @Test + void 시작값과_종료값이_동일한_경우(){ + //given + int startInclusive = 5; + int endInclusive = 5; + + //when + int result = RandomUtils.nextInt(startInclusive, endInclusive); + + // then + assertThat(result).isEqualTo(startInclusive); + } +}