U E D R , A S I H C RSS

JTD Study/첫번째과제/정현

김정현

심심해서 해봤음;; 난 JTD랑은 상관이.. ㅋㅋ
TDD로 진행했습니다.

소스 : java


테스트케이스
public class BaseBallTest extends TestCase{
	Beholder beholder;
	Extractor extractor;
	BaseBall baseBall;
	public void setUp() {
		beholder= new Beholder();
		extractor= new Extractor();	
		baseBall= new BaseBall(beholder, extractor);
	}
	public void testStrikeCount() {
		beholder.setAnswer("111");
		assertEquals(0, beholder.getStrikeCount("000"));
		assertEquals(1, beholder.getStrikeCount("001"));
		assertEquals(2, beholder.getStrikeCount("110"));
	}
	public void testBallCount() {
		beholder.setAnswer("123");
		assertEquals(0, beholder.getBallCount("000"));
		assertEquals(1, beholder.getBallCount("102"));
		assertEquals(2, beholder.getBallCount("321"));
		assertEquals(3, beholder.getBallCount("231"));
	}
	
	public void testNumberCreation() {		
		for(int i=0;i<100;i++) {
			String number= extractor.getRandomBall();
			assertEquals(3, number.length());
			assertFalse(baseBall.duplicated(number));
		}
	}
	
	public void testDuplicated() {
		assertTrue(baseBall.duplicated("101"));
		assertTrue(baseBall.duplicated("122"));
		assertFalse(baseBall.duplicated("123"));
	}
	
	public void testGamePlay() {
		BaseBall game= new BaseBall(beholder, extractor);
		beholder.setAnswer("123");
		game.inputNumber("152");
		assertFalse(game.isGameOver());
		assertEquals(1, game.getStrike());
		assertEquals(1, game.getBall());
		game.inputNumber("123");
		assertTrue(game.isGameOver());
	}
}

게임 진행 메인
public class GameMain {

	public static void main(String[] args) {
		BaseBall baseBall= new BaseBall(new Beholder(), new Extractor());
		Scanner input= new Scanner(System.in);
		System.out.println("baseball game");
		
		while(!baseBall.isGameOver()) {
			String number= input.nextLine();
			if(baseBall.isProper(number)) {
				baseBall.inputNumber(number);
			} else {
				System.out.println("what are you doing?");
				System.exit(0);
			}
			System.out.print(baseBall.getStrike() + " strike, ");
			System.out.println(baseBall.getBall() + "ball");
		}
		System.out.println("good");
	}
}

야구게임몸통
public class BaseBall {
	String playerInput;
	private Beholder beholder;
	private Extractor extractor;
	private String number;
	
	public BaseBall(Beholder beholder, Extractor extractor) {
		number=	"000";
		this.beholder= beholder;
		this.extractor= extractor;
		beholder.setAnswer(this.extractor.getRandomBall());
	}
	public boolean isGameOver() {
		return getStrike()==3;
	}
	public void inputNumber(String string) {
		number= string;
	}
	public int getStrike() {
		return beholder.getStrikeCount(number);
	}
	public int getBall() {
		return beholder.getBallCount(number);
	}
	public boolean isProper(String number) {
		try {
			Integer.parseInt(number);
		} catch(Exception e) {
			return false;
		}
		return number.length()==3 && !duplicated(number);
	}
	public boolean duplicated(String number) {
		char[] chars= number.toCharArray();
		return chars[0]== chars[1] || chars[0]== chars[2] || chars[1]== chars[2];
	}
}


진행상황판단
public class Beholder {
	char[] numbers;
	
	public void setAnswer(String string) {
		numbers= string.toCharArray();
	}

	public int getStrikeCount(String string) {
		char[] inputChars= string.toCharArray();
		int count= 0;
		for(int i=0;i<numbers.length;i++) {
			count += numbers[i]==inputChars[i]? 1:0;
		}
		return count;
	}

	public int getBallCount(String string) {
		int count= 0;
		for(char c : numbers) {
			if(string.contains(String.valueOf(c))) 
				count++;
		}
		return count - getStrikeCount(string);
	}
}

번호 추출기
public class Extractor {
	Vector<String> numbers= new Vector<String>();
	
	public String getRandomBall() {
		initNumbers();
		String number= "";
		for(int i=0;i<3;i++) {
			int index= (int)(Math.random()*numbers.size());
			number+= numbers.elementAt(index);	
			numbers.removeElementAt(index);
		}
		return number;
	}
	
	private void initNumbers() {
		numbers.clear();
		for(int i=0;i<10;i++) {
			numbers.add(String.valueOf(i));
		}
	}	
}

리펙토링

추출기 - 매직 넘버와 공갯수 조절 가능하게
public class Extractor {
	Set<String> numbers= new LinkedHashSet<String>();	
	
	public String getRandomBall(int nBall) {
		while(numbers.size()<ballLimit(nBall)) {
			numbers.add(getRandom(ballLimit(nBall)));
		}
		return getOneWord(numbers);
	}
	private int ballLimit(int nBall) {
		return nBall>numberLimit()? numberLimit() : nBall;
	}
	public int numberLimit() {
		return 10;
	}	
	private String getOneWord(Set<String> set) {
		String result= "";
		for(String num : set) {
			result += num;
		}
		return result;
	}
	private String getRandom(int range) {
		return String.valueOf((int)(Math.random()*range));
	}
}

잡담

if문과 이중for 루프를 돌지 않으려고 발버둥쳤다;
벡터를 이용해 번호를 뽑는 것은 효율적인것 같진 않지만, 새로운 시도라서 신선했다

----

JTDStudy JTDStudy/첫번째과제

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:29
Processing time 0.0191 sec