Dealer ¶
~cpp public class Dealer { public int[]ball = new int[3]; public int count= 0; public int creatBall() { // TODO 자동 생성된 메소드 스텁 return (int) (Math.random()*1000); } public int getBall(int i) { // TODO 자동 생성된 메소드 스텁 return ball[i-1]; } public void setBall(int number) { // TODO 자동 생성된 메소드 스텁 for(int i = 1 ; i < 101 ; i*=10){ ball[count++]= ((int)number/(100/i)); number%= (100/i); } } }
Judgement ¶
~cpp public class Judgement { public int dBall[]= new int[3]; public int pBall[]= new int[3]; private int strike=0; private int ball= 0; private boolean win= true; public Judgement(int[] dealer, int[] player) { // TODO 자동 생성된 생성자 스텁 dBall= dealer; pBall= player; for(int i = 0; i < 3; i++){ for(int j= 0; j<3 ; j++){ if(dBall[i] == pBall[j] && i == j ) strike++; if(dBall[i] == pBall[j] && i != j ) ball++; } } if( strike == 3) win= false; } public int getStrikeConclusion() { // TODO 자동 생성된 메소드 스텁 return strike; } public int getBallConclusion() { // TODO 자동 생성된 메소드 스텁 return ball; } public boolean getConclusion() { // TODO 자동 생성된 메소드 스텁 return win; } }
mainBaseball ¶
~cpp import javax.swing.JOptionPane; public class mainBaseball { public static void main(String[] args) { Dealer dealer= new Dealer(); dealer.setBall(dealer.creatBall()); boolean judge= true; do{ Player player= new Player(); String input= JOptionPane.showInputDialog(null, "숫자입력"); int number= Integer.parseInt(input); player.setBall(number); Judgement judgement= new Judgement(dealer.ball, player.ball); JOptionPane.showMessageDialog(null, judgement.getStrikeConclusion() + " Strikes " + judgement.getBallConclusion() + " Balls "); judge= judgement.getConclusion(); }while( judge ); JOptionPane.showMessageDialog(null, "you win"); } }
Player ¶
~cpp public class Player { public String name; public int count= 0; public int ball[]=null;//={0,1,2,3,4}; public Player() { ball= new int[3]; count=0; } public int getBall(int i) { // TODO 자동 생성된 메소드 스텁 return ball[i-1]; } public void setBall(int number) { // TODO 자동 생성된 메소드 스텁 for(int i = 1 ; i < 101 ; i*=10){ ball[count++]= ((int)number/(100/i)); number%= (100/i); } } }
testcode ¶
~cpp import junit.framework.TestCase; public class testBaseball extends TestCase { // public void setUp(){ // } public void testDealer(){ Dealer dealer= new Dealer(); dealer.setBall(123); assertEquals(dealer.ball[0], dealer.getBall(1)); assertEquals(dealer.ball[1], dealer.getBall(2)); assertEquals(dealer.ball[2], dealer.getBall(3)); assertTrue(0 <= dealer.creatBall() && dealer.creatBall() < 1001); } public void testPlayer(){ Player player= new Player(); player.setBall(123); assertEquals(player.ball[0], player.getBall(1)); assertEquals(player.ball[1], player.getBall(2)); assertEquals(player.ball[2], player.getBall(3)); } public void testJudgement(){ Dealer dealer= new Dealer(); dealer.setBall(123); Player player= new Player(); player.setBall(123); Judgement judgement= new Judgement(dealer.ball, dealer.ball); assertEquals(3, judgement.getStrikeConclusion()); assertEquals(0, judgement.getBallConclusion()); } }