테스트케이스
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));
}
}
}