Skip to content

Commit

Permalink
Merge pull request #117 from ohahohah/ohahohah
Browse files Browse the repository at this point in the history
[WIP] exercise_자동차 경주게임 실습_ohahohah
  • Loading branch information
ohahohah authored Jun 19, 2020
2 parents 44453ee + cc88f85 commit 706646f
Show file tree
Hide file tree
Showing 10 changed files with 527 additions and 127 deletions.
45 changes: 45 additions & 0 deletions tddExercise/src/racingCar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package racingCar;

/**
* 자동차 경주에 참여하는 자동차
*/
public class Car {

private String name;
private int currentCoord;

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

public Car(int currentCoord) {
this.currentCoord = currentCoord;
}

String getName() {
return name;
}

int getCurrentCoord() {
return currentCoord;
}


public void goCar() {
this.currentCoord += calForwardCoord(getRandomVal());
}

void goCar(int randomVal) {
this.currentCoord += calForwardCoord(randomVal);
}

private int getRandomVal() {
return (int) (Math.random() * 8 + 1);
}

int calForwardCoord(int randomVal) {

return randomVal >= 4 ? 1 : 0;
}
}
5 changes: 5 additions & 0 deletions tddExercise/src/racingCar/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package racingCar;

public class Race {

}
4 changes: 0 additions & 4 deletions tddExercise/src/racingCar/RaceCar.java

This file was deleted.

110 changes: 110 additions & 0 deletions tddExercise/src/racingCar/RacingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package racingCar;

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

/**
* 자동차 경주 게임
*/
public class RacingGame {

List<Car> carList;
private int turn;
private List<String> winnersName; // TODO - car 객체가 더 나은지, 아니면 이름만?

public RacingGame(String carsName, int turn) {

this.turn = turn;

carList = new ArrayList<>();
for (String carName : splitCarsName(carsName)) {
carList.add(new Car(carName));
}
}

int getTurn() {
return turn;
}

public void doGame() {

String carNamee = scanCarNames();
int turn = scanTurn();
// 경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).
// 시도할 회수는 몇회인가요?

for (int i = 0; i < turn; i++) {
doTurnGame();
}

this.winnersName = calWinner(); //TODO - Is it better to use calWinner Void type?

printResult();
}

private int scanTurn() {
return 0;
}

private String scanCarNames() {
Scanner scan = new Scanner(System.in); // 문자 입력을 인자로 Scanner 생성
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).");

System.out.println("메시지를 입력하세요:");

return scan.nextLine();
}

void doTurnGame() {
for (int i = 0; i < carList.size(); i++) {
carList.get(i).goCar();
}

printTurnResult();
}

List<String> calWinner() {

int maxCoord = 0;
List<String> winnerNames = new ArrayList<>();

for (Car car : carList) {
if (car.getCurrentCoord() > maxCoord) {
maxCoord = car.getCurrentCoord();

winnerNames.clear(); // TODO test- vs new ArrayList() Is element size same compare to before ?
winnerNames.add(car.getName());

} else if (car.getCurrentCoord() == maxCoord) {
winnerNames.add(car.getName());
}
}

return winnerNames;
}

private void printResult() {
System.out.println(String.join(", ", winnersName) + "가 최종우승했습니다.");
}

private void printTurnResult() {
for (int i = 0; i < carList.size(); i++) {
System.out.print(carList.get(i).getName() + " : ");
printCurrentCoordLine(carList.get(i).getCurrentCoord());
System.out.println();
}
System.out.println(System.lineSeparator());
}

private void printCurrentCoordLine(int currentCoord) {
for (int i = 0; i < currentCoord; i++) {
System.out.print("-");
}
}

private String[] splitCarsName(String carsName) {
String nameDelim = ",";
return carsName.split(nameDelim);
}
}
73 changes: 73 additions & 0 deletions tddExercise/test/racingCar/CarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package racingCar;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class CarTest {

private Car rc = null;

private int fowarddRandomVal = 5;
private int stopRandomVal = 2;
private int goOnceVal = 1;

@Before
public void setUp() {
rc = new Car("testCar");
}

@After
public void tearDown() {
rc = null;
}

@Test
public void 전진조건값이_4이상일때_전진값이_맞게_주어지는지_확인() {
int randomNum = 4;
int expectedGoCarVal = 1;

assertEquals(expectedGoCarVal, rc.calForwardCoord(randomNum));

}

@Test
public void 전진조건값이_4미만일때_전진값이_맞게_주어지는지_확인() {
int randomNum = 3;
int expectedGoCarVal = 0;

assertEquals(expectedGoCarVal, rc.calForwardCoord(randomNum));
}

@Test
public void 전진값이_주어졌을떄_자동차의_현재_위치값이_1만큼_변경됨() {
int initialCoord = rc.getCurrentCoord();

rc.goCar(fowarddRandomVal);
int firstCurrentCoord = rc.getCurrentCoord();

assertEquals(initialCoord + goOnceVal, firstCurrentCoord);

rc.goCar(fowarddRandomVal);
int secondCurrentCoord = rc.getCurrentCoord();

assertEquals(firstCurrentCoord + goOnceVal, secondCurrentCoord);
}

@Test
public void 멈춤값이_주어졌을떄_자동차의_현재_위치값이_변경되지_않음() {
int initialCoord = rc.getCurrentCoord();

rc.goCar(stopRandomVal);
int firstCurrentCoord = rc.getCurrentCoord();

assertEquals(initialCoord + 0, firstCurrentCoord);

rc.goCar(stopRandomVal);
int secondCurrentCoord = rc.getCurrentCoord();

assertEquals(firstCurrentCoord + 0, secondCurrentCoord);
}
}
15 changes: 0 additions & 15 deletions tddExercise/test/racingCar/RaceCarTest.java

This file was deleted.

125 changes: 125 additions & 0 deletions tddExercise/test/racingCar/RacingGameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package racingCar;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class RacingGameTest {

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"car01,car02,car03", 3}});
}

private RacingGame racingGame = null;

private String carsName;
private int turn;

public RacingGameTest(String carsName, int turn) {
this.carsName = carsName;
this.turn = turn;

}

@Before
public void setUp() {
racingGame = new RacingGame(carsName, turn);
}

@After
public void tearDown() {
}


@Test
public void 입력받은_자동차_이름_수만큼_car_객체를_생성하는지_확인() {

assertEquals(carsName.split(",").length, racingGame.carList.size());
}

@Test
public void 입력받은_차_이름으로_car_객체를_생성하는지_확인() {

String[] carNames = carsName.split(",");

for (int i = 0; i < carNames.length; i++) {
assertEquals(carNames[i], racingGame.carList.get(i).getName());
}
}

@Test
public void 입력한_횟수만큼_게임진행회차정보를_가지는지_확인() {

assertEquals(turn, racingGame.getTurn());
}


@Test
@Ignore
public void testDoGame() {

racingGame.doGame();
}

@Test
@Ignore
public void testDoSingleGame() {

racingGame.doTurnGame();
}

@Test
public void 우승자_정보를_맞게_가져오는지_확인_우승자_두명() {
AddCarsCoord(3, 2, 3);
List<String> winners = Stream.of(racingGame.carList.get(0).getName(), racingGame.carList.get(2).getName()).collect(Collectors.toList());

assertEquals(winners, racingGame.calWinner());
}

@Test
public void 우승자_정보를_맞게_가져오는지_확인_우승자_한명() {
AddCarsCoord(1, 2, 3);
List<String> winners = Stream.of(racingGame.carList.get(2).getName()).collect(Collectors.toList());

assertEquals(winners, racingGame.calWinner());
}

@Test
public void 우승자_정보를_맞게_가져오는지_확인_우승자_세명() {
AddCarsCoord(3, 3, 3);
List<String> winners = Stream.of(racingGame.carList.get(0).getName(), racingGame.carList.get(1).getName(), racingGame.carList.get(2).getName()).collect(Collectors.toList());

assertEquals(winners, racingGame.calWinner());
}

private void AddCarsCoord(int firstCarCoord, int secondCarCoord, int thirdCarCoord) {

int randomValForward = 5;

for (int i = 0; i < firstCarCoord; i++) {
racingGame.carList.get(0).goCar(randomValForward);
}

for (int i = 0; i < secondCarCoord; i++) {
racingGame.carList.get(1).goCar(randomValForward);
}

for (int i = 0; i < thirdCarCoord; i++) {
racingGame.carList.get(2).goCar(randomValForward);
}
}
}
Loading

0 comments on commit 706646f

Please sign in to comment.