U E D R , A S I H C RSS

JTD Study/첫번째과제/원명

~cpp code
import javax.swing.JOptionPane;
public class BaseBall {	
	private int correctNumber;
	public static void main(String[] args) {
		BaseBall oneGame = new BaseBall();
		oneGame.setCorrectNumber();
		int guess = 0, result = 0;
		do
		{
			guess = oneGame.inputGuess();
			result = oneGame.compare(guess);
			if (result != 30)
				JOptionPane.showMessageDialog(null, (result / 10) + " Strike, " 
						+ (result % 10) + " Ball");
		}while (result != 30);
		oneGame.displayResult();
	}
	public void setCorrectNumber()
	{
		int setNumber = 0;
		do
		{
			setNumber = (int) (Math.random() * 10);
		}while (setNumber == 0);
		correctNumber = setNumber;
		do
		{
			setNumber = (int) (Math.random() * 10);
		}while (correctNumber == setNumber);
		correctNumber = correctNumber * 10 + setNumber;
		do
		{
			setNumber = (int) (Math.random() * 10);
		}while (((int)(correctNumber / 10)) == setNumber 
				|| ((int)(correctNumber % 10)) == setNumber);
		correctNumber = correctNumber * 10 + setNumber;
	}
	public int inputGuess()
	{
		String input =  JOptionPane.showInputDialog("Enter three different number\n");
		int guess = Integer.parseInt(input);
		return guess;
	}
	public int compare(int aGuess)
	{
		int compareResult = 0, cCorrect = correctNumber;
		int firstGuess = (int) (aGuess / 100);
		int secondGuess = ((int) (aGuess / 10)) % 10;
		int thirdGuess = aGuess % 10;
		if (((int)(cCorrect / 100) == firstGuess))
				compareResult ++;
		cCorrect = cCorrect % 100;
		if (((int)(cCorrect / 10) == secondGuess))
				compareResult ++;
		cCorrect = cCorrect % 10;
		if (cCorrect == thirdGuess)
				compareResult ++;
		compareResult *= 10;
		cCorrect = correctNumber;
		if (((int)(cCorrect / 100)) == secondGuess 
				|| ((int)(cCorrect / (100))) == thirdGuess)
			compareResult ++;
		cCorrect = cCorrect % 100;
		if (((int)(cCorrect / 10)) == firstGuess 
				|| ((int)(cCorrect / (10))) == thirdGuess)
			compareResult ++;
		cCorrect = cCorrect % 10;
		if ((cCorrect == firstGuess) || (cCorrect == secondGuess))
			compareResult ++;
		return compareResult;
	}
	public void displayResult()
	{
		JOptionPane.showMessageDialog(null, "You are right!\n Answer is " + correctNumber);
	}
}

  • int 3 -0-;; -

. . , . 행학 JUnit , http://junit.org .

... ;;
--NeoCoin
  • Test-Driven Development , Java ㅠㅠ. TDD . ㅎㅎ -
    • 트 합. TDD . TDD . Refactoring Regression Test JUnit .--NeoCoin
      • , TDD 히 하 . TDD , . ㅋ -

팅하 .
~java
// ..
	void setCorrectNumber(int in) {
		correctNumber = in;
	}

	@Test
	public void test2() {
		BaseBall oneGame = new BaseBall();
		oneGame.setCorrectNumber(123);
		assertEquals(30, oneGame.compare(123));
		assertEquals(10, oneGame.compare(100, 123));
		assertEquals(2, oneGame.compare(310, 123));
	}
//..

setCorrectNumber 하 compare ,
~java
	@Test
	public void test2() {
		BaseBall oneGame = new BaseBall();
		assertEquals(30, oneGame.compare(123, 123));
		assertEquals(10, oneGame.compare(100, 123));
		assertEquals(2, oneGame.compare(310, 123));
	}

	public int compare(int aGuess) {
		return compare(aGuess, correctNumber);
	}
	public int compare(int aGuess, int answer) {
		correctNumber = answer;

		int compareResult = 0, cCorrect = correctNumber;
//
	}

readability typecasting (Java Language Specification . 해 typecasting .)
~java
	public int compare(int aGuess, int answer) {
		correctNumber = answer;

		int compareResult = 0, cCorrect = correctNumber;
		int firstGuess = aGuess / 100 % 10;
		int secondGuess = aGuess / 10 % 10;
		int thirdGuess = aGuess % 10;



		if (cCorrect / 100 == firstGuess)
			compareResult++;
		cCorrect = cCorrect % 100;
		if (cCorrect / 10 == secondGuess)
			compareResult++;
		cCorrect = cCorrect % 10;
		if (cCorrect == thirdGuess)
			compareResult++;
		compareResult *= 10;



		cCorrect = correctNumber;
		if ((cCorrect / 100) == secondGuess || (cCorrect / 100) == thirdGuess)
			compareResult++;
		cCorrect = cCorrect % 100;
		if ((cCorrect / 10) == firstGuess || (cCorrect / 10) == thirdGuess)
			compareResult++;
		cCorrect = cCorrect % 10;
		if ((cCorrect == firstGuess) || (cCorrect == secondGuess))
			compareResult++;
		return compareResult;
	}
,
~java
	int positionNum(int value, int pos) {
		if (pos < 1)
			throw new IndexOutOfBoundsException("pos 1 .");
		int a = value % (int) Math.pow(10, pos);
		return a / (int) Math.pow(10, pos - 1);
	}

	@Test
	public void getPositionNum() {
		assertEquals(3, positionNum(123, 1));
		assertEquals(2, positionNum(123, 2));
		assertEquals(1, positionNum(123, 3));
	}

, Test .

this.correctNumber
~java
public int compare(int aGuess, int cCorrect) {
		int firstGuess = positionNum(aGuess,3);
		int secondGuess = positionNum(aGuess,2);
		int thirdGuess = positionNum(aGuess,1);


		int strike=0;
		if (positionNum(cCorrect,3) == firstGuess)
			strike++;
		if (positionNum(cCorrect,2) == secondGuess)
			strike++;
		if (positionNum(cCorrect,1) == thirdGuess)
			strike++;


		int ball=0;
		if (positionNum(cCorrect,3) == secondGuess || positionNum(cCorrect,3) == thirdGuess)
			ball++;
		if (positionNum(cCorrect,2) == firstGuess || positionNum(cCorrect,2) == thirdGuess)
			ball++;
		if ((positionNum(cCorrect,1) == firstGuess) || (positionNum(cCorrect,1) == secondGuess))
			ball++;
		return ball + strike*10;
	}
. inline . xxxGuess inline
~java
public int compare(int aGuess, int answer) {
		int cCorrect = answer;
		int strike=0;
		if (positionNum(cCorrect,3) == positionNum(aGuess,3))
			strike++;
		if (positionNum(cCorrect,2) == positionNum(aGuess,2))
			strike++;
		if (positionNum(cCorrect,1) == positionNum(aGuess,1))
			strike++;


		int ball=0;
		if (positionNum(cCorrect,3) == positionNum(aGuess,2) || positionNum(cCorrect,3) == positionNum(aGuess,1))
			ball++;
		if (positionNum(cCorrect,2) == positionNum(aGuess,3) || positionNum(cCorrect,2) == positionNum(aGuess,1))
			ball++;
		if ((positionNum(cCorrect,1) == positionNum(aGuess,3)) || (positionNum(cCorrect,1) == positionNum(aGuess,2)))
			ball++;
		return ball + strike*10;
	}

, magic number
~java
	public int compare(int aGuess, int cCorrect) {
		final int START_POS = 1,END_POS = 3;

		int strike = 0;
		for (int pos = END_POS; pos >=START_POS; pos--) {
			if (positionNum(cCorrect, pos) == positionNum(aGuess, pos))
				strike++;
		}

		int ball = 0;
		for (int corPos = END_POS; corPos >=START_POS; corPos--) {
			for (int guePos = END_POS; guePos > 0; guePos--) {
				if (corPos != guePos && positionNum(cCorrect, corPos) == positionNum(aGuess,guePos))
					ball++;
			}
		}
		return ball + strike * 10;
	}

확한 , 합
~java
	public int compare(int aGuess, int cCorrect) {
		final int START_POS = 1, END_POS = 3;

		int strike = 0, ball = 0;

		for (int corPos = END_POS; corPos >= START_POS; corPos--) {
			for (int guePos = END_POS; guePos >= START_POS; guePos--) {
				if (positionNum(cCorrect, corPos) == positionNum(aGuess, guePos)) {
					if (corPos == guePos)
						strike++;
					else
						ball++;
				}
			}
		}
		return ball + strike * 10;
	}

result 10 1
~java
	public int compare(int aGuess, int cCorrect) {
		final int START_POS = 1, END_POS = 3, STRIKE_DELTA = 10, BALL_DELTA = 1;

		int result = 0;  //10 strike, 1 ball 
		for (int corPos = END_POS; corPos >= START_POS; corPos--)
			for (int guePos = END_POS; guePos >= START_POS; guePos--)
				if (positionNum(cCorrect, corPos) == positionNum(aGuess, guePos))
					result += (corPos == guePos) ? STRIKE_DELTA : BALL_DELTA;
		return result;
	}

overloading 한 compare . setCorrectNumber .
..
~java
package test;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import javax.swing.JOptionPane;

public class BaseBall {
	private int correctNumber;

	public static void main(String[] args) {
		final int END_GAME_NUM = 30;

		BaseBall oneGame = new BaseBall();
		while (true) {
			int result = oneGame.runOneTurn();
			if (result == END_GAME_NUM)
				break;
			else
				JOptionPane.showMessageDialog(null,
					String.format("%d Strike, %d Ball", result / 10, result % 10));
		}
		oneGame.displayResult();
	}

	public int runOneTurn() {
		return compare(this.correctNumber, inputGuess());
	}

	public BaseBall() {
		int setNumber = 0;
		do {
			setNumber = (int) (Math.random() * 10);
		} while (setNumber == 0);
		correctNumber = setNumber;
		do {
			setNumber = (int) (Math.random() * 10);
		} while (correctNumber == setNumber);
		correctNumber = correctNumber * 10 + setNumber;
		do {
			setNumber = (int) (Math.random() * 10);
		} while (correctNumber / 10 == setNumber
				|| correctNumber % 10 == setNumber);
		correctNumber = correctNumber * 10 + setNumber;
	}

	public int inputGuess() {
		String input = JOptionPane
				.showInputDialog("Enter three different number\n");
		int guess = Integer.parseInt(input);
		return guess;
	}

	int positionNum(int value, int pos) {
		if (pos < 1)
			throw new IndexOutOfBoundsException("pos 1");
		int a = value % (int) Math.pow(10, pos);
		return a / (int) Math.pow(10, pos - 1);
	}

	public int compare(int aGuess, int cCorrect) {
		final int START_POS = 1, END_POS = 3, STRIKE_DELTA = 10, BALL_DELTA = 1;

		int result = 0; // 10 strike, 1 ball 
		for (int corPos = END_POS; corPos >= START_POS; corPos--)
			for (int guePos = END_POS; guePos >= START_POS; guePos--)
				if (positionNum(cCorrect, corPos) == positionNum(aGuess, guePos))
					result += (corPos == guePos) ? STRIKE_DELTA : BALL_DELTA;
		return result;
	}

	public void displayResult() {
		JOptionPane.showMessageDialog(null, "You are right!\n Answer is "
				+ correctNumber);
	}

	@Test
	public void getPositionNum() {
		assertEquals(3, positionNum(123, 1));
		assertEquals(2, positionNum(123, 2));
		assertEquals(1, positionNum(123, 3));
	}

	@Test
	public void test2() {
		BaseBall oneGame = new BaseBall();
		assertEquals(30, oneGame.compare(123, 123));
		assertEquals(10, oneGame.compare(100, 123));
		assertEquals(2, oneGame.compare(310, 123));
	}
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:29
Processing time 0.0283 sec