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

[WIP] exercise_자동차 경주게임 실습_ohahohah #117

Merged
merged 27 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
332589c
Add Tdd practice metting
kimsunoh Jun 27, 2018
c8cc738
Update 20180623 log
ohahohah Jun 27, 2018
16792d0
Update meeting_log.md
kimsunoh Jun 27, 2018
ffce7b8
Docs meeting_log INDEX 수정
kimsunoh Jun 28, 2018
587370c
Docs meeting_log modify sub title for indexing
kimsunoh Jun 28, 2018
821ede1
Docs 180623 log sub title modify
kimsunoh Jun 28, 2018
69a374d
Fix Toc link
ohahohah Jun 28, 2018
1bfc245
Merge Upstream
ohahohah Jul 11, 2018
fcbeddd
Rebase remote repository
ohahohah Sep 15, 2018
797a98f
Fix 인코딩
ohahohah Sep 15, 2018
a78d8ad
Add 자동차경주게임 테스트케이스 구현
ohahohah Sep 15, 2018
70c0b94
20190107 Morning TDD / Racing car
ohahohah Jan 7, 2019
d1b8abf
Tdd Racing game- 전진조건 확인 / 20190108 Morning TDD
ohahohah Jan 8, 2019
3d89927
Rename Class name for matching ClassDiagram
ohahohah Jan 8, 2019
5a9228f
새로운 요구사항 반영 / 20190109 Morning TDD
ohahohah Jan 8, 2019
6a068d4
Add 객체 설계
ohahohah Jan 11, 2019
a6fc13b
Update testcase 새로운 요구사항에 맞게 / 20190111 Morning TDD
ohahohah Jan 11, 2019
9542865
Update 추가 요구사항 / 20190114 Mornint TDD
ohahohah Jan 14, 2019
cd3feaf
Update 테스트케이스 / 20190115 Morning TDD
ohahohah Jan 14, 2019
5a534d8
Add 입력받은 자동차이름들만큼 자동차 객체 생성 확인 / 20190115 Morning TDD
ohahohah Jan 14, 2019
5077cbc
Add 생성자, Refactoring name / 20190115 Morning TDD
ohahohah Jan 14, 2019
512ce94
Add testcase / 20190116 Morning TDD
ohahohah Jan 15, 2019
75c2264
Update testcase / 20190121 Morning TDD
ohahohah Jan 21, 2019
585209d
Update testcase(ing) / 20190123 Morning TDD
ohahohah Jan 23, 2019
4770900
Update 게임진행,출력 구현 / 20190124 Morning TDD
ohahohah Jan 23, 2019
b3623b8
Add 자동차경주게임 테스트케이스 구현
ohahohah Jan 25, 2019
cc88f85
[WIP] Add racing car TDD
ohahohah Feb 16, 2019
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
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