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());
	}

}

작성후기

  • 테스트를 작성하며 느낀거지만 이건아니라는 생각이 자꾸만 드는 이유는 멀까요? ㅋㅋ 테스트를 이렇게 작성해도 돼는건지 모르겟네요... 프로그램도 좀 이상한거 같고... 괸히 삽질만 많이한거같은 생각이.... 흠 객체지향 개념을 다시한번 살펴봐야 겠다는 생각이 자꾸만 드네요... -장길-
    • 객체지향 개념은 어느정도 된거 같은데?^^; 음... 디자인 패턴이라는 것을 보면 조금 생각이 바뀔지도... - 상욱

Retrieved from http://wiki.zeropage.org/wiki.php/JTDStudy/첫번째과제/장길
last modified 2021-02-07 05:23:29