首页 > 其他 > 详细

BullsAndCows猜数字游戏

时间:2020-03-29 18:15:25      阅读:48      评论:0      收藏:0      [点我收藏+]

BullsAndCows猜数字游戏

  1. 方法类

/**
 * 游戏规则:
        电脑出数字,人猜。出数字的电脑要预先生成一个没有重复数字的4位数 。 猜的人就可以开始猜。
        每猜一个数字,电脑就要根据这个数字给出几A几B,
        其中A前面的数字表示位置正确的数的个数,而B前的数字表示数字正确而位置不对的数的个数。
        如正确答案为 5234,而猜的人猜 5346,则是 1A2B,其中有一个5的位置对了,记为1A,
        而3和4这两个数字对了,而位置没对,因此记为 2B,合起来就是 1A2B。
        接着猜的人再根据出题者的几A几B继续猜,直到猜中(即 4A0B)为止。
        
       本次考试中,需要实现三个方法:
   1.无参数的构造方法:生成没有重复数字的4位数,首位不为0
   2.compare方法
   3.playGame方法:模拟玩家的猜测过程,按照下面的格式输出,第一行为Game Start,第二行是10个-
            后面是玩家每一次的猜测,并给出每次猜测的结果,不同猜测用10个-分割,最后输出一行胜利祝贺,
            使用步数和10个-。playGame方法需要使用compare方法来比较。
        要求:
   1.不可超过10000轮,不可少于1轮(至少得猜一次),不要求玩家采用最优策略;
   2.最后一次猜测的结果必须是4A0B,即猜对;
   3.只要玩家最后猜对即可,但是玩家不能猜测有重复数字的四位数,不能猜首位为0;
   4.最后一行没有换行符!!!!!!!!!!!!

        输出格式例1:
   Game Start
   ----------
   Guess:5346
   Result:1A2B
   ----------
   Guess:5324
   Result:4A0B
   ----------
   You win!You took 2 steps.
   ----------
   
        输出格式例2:
   Game Start
   ----------
   Guess:1234
   Result:0A0B
   ----------
   Guess:5607
   Result:1A3B
   ----------
   Guess:7650
   Result:4A0B
   ----------
   You win!You took 3 steps.
   ----------
	
 */
public class BullsAndCows {
	private int secret;//要猜的那个数
	
	/**
	 * 有参数的构造方法,将secret设置为传入的参数
	 * @param a
	 */
	public BullsAndCows(int a){
		this.secret = a;
	}
	
	/**
	 * 无参数的构造方法,随机生成四位数secret,注意这个四位数没有重复数字,首位不能是0
	 */
	public BullsAndCows(){
/*		while(true){
			secret = (int) (Math.random()*10000);
			int tmp = secret;
			if(tmp<=1000||tmp>=9999) continue;
			HashSet<Integer> set  = new HashSet<Integer>();
			int flag = 0;
			while(tmp>0){
				if(!set.contains(tmp%10)){
					set.add(tmp%10);
				}else{
					flag = 1;
					break;
				}
				tmp = tmp/10;
			}
			if(flag==1) continue;
			if(flag==0) break;
		}
		System.out.println(""+secret);*/
		this.secret =  random();
	}
	public int random(){
		int res;
		while(true){
			 int tmp = (int) (Math.random()*10000);
			 res = tmp;
			//System.out.println(tmp+"");
			if(tmp<=1000||tmp>=9999) continue;
			HashSet<Integer> set  = new HashSet<Integer>();
			int flag = 0;
			while(tmp>0){
				if(!set.contains(tmp%10)){
					set.add(tmp%10);
				}else{
					flag = 1;
					break;
				}
				tmp = tmp/10;
			}
			if(flag==1) continue;
			if(flag==0) break;
		}
		return res;
	}
	/**
	 * 比较传入的a与要猜的secret,返回几A几B
	 * @param a
	 * @return String
	 */
	public String compare(int a){
		int countA = 0;
		int countB = 0;
		int tmp1 = secret;
		int tmp2 = a;
		HashSet<Integer> set = new HashSet<Integer>();
		while(tmp1>0&&tmp2>0){
			if(tmp1%10==tmp2%10) countA++;
			set.add(tmp1%10);
			tmp1/=10;
			tmp2/=10;
		}
		while(a>0){
			int t = a%10;
			if(set.contains(t)){
				countB++;
			}
			a/=10;
		}
		countB -= countA;
		return countA+"A"+countB+"B";//such as return "1A2B";
	}
	
	/**
	 * 模拟玩家的猜测过程,按要求输出
	 */
	public void playGame(){
		boolean flag = true;
		int step = 0;
		//Scanner s = new Scanner(System.in);
		System.out.println("Game Start");
		while(flag){
			System.out.println("----------");
			//int Guess = s.nextInt();
			int Guess = random();
			//System.out.println(Guess+"");
			step++;
			System.out.println("Guess:"+Guess);
			String res = compare(Guess);
			System.out.println("Result:"+res);
			if(Guess == secret){
				System.out.println("----------");
				System.out.println("You win!You took "+step+" steps.");
				System.out.print("----------");
				break;
			}
			if(step > 10000) break;
			
		}
	}
	
	/**
	 * 外部获取secret成员变量的方法,不可修改
	 * @return
	 */
	public int getSecret(){
		return secret;
	}
}

  1. 测试类
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;


public class BullsAndCowsTest {
	PrintStream console;
	ByteArrayOutputStream bytes;
	static String SEPARATOR = System.getProperty("line.separator");
	
	@org.junit.Before
	public void setUp() throws Exception{
		bytes = new ByteArrayOutputStream();
		console = System.out;
		System.setOut(new PrintStream(bytes));
	}
	
	@org.junit.After
	public void tearDown() throws Exception{
		System.setOut(console);
	}
	
	@org.junit.Test
	public void testBullsAndCows1(){
		BullsAndCows bac = new BullsAndCows();
		int secret = bac.getSecret();
		assertTrue(secret >= 1000 && secret <= 9999);
	}

	@org.junit.Test
	public void testCompare1(){
		BullsAndCows bac = new BullsAndCows(5234);
		assertEquals(bac.compare(5346),"1A2B");
	}
	
	@org.junit.Test
	public void testCompare2(){
		BullsAndCows bac = new BullsAndCows(5234);
		assertEquals(bac.compare(5234),"4A0B");
	}
	
	@org.junit.Test
	public void testCompare3(){
		BullsAndCows bac = new BullsAndCows(3210);
		assertEquals(bac.compare(1023),"0A4B");
	}
	
	@org.junit.Test
	public void testPlayGame1(){
		BullsAndCows bac = new BullsAndCows(5234);
		bac.playGame();
		String[] out = bytes.toString().split(SEPARATOR);
		assertTrue(out.length >= 7 && out.length <=30004);
		assertEquals(out[0],"Game Start");
		assertEquals(out[1],"----------");
		assertEquals(out[out.length - 2],"You win!You took "+ (out.length - 4) / 3 +" steps.");
		assertEquals(out[out.length - 1],"----------");
		
		for(int i = 0; i < (out.length - 4) / 3; i++){
			String[] guess = out[2 + i * 3].split(":");
			String[] result = out[3 + i * 3].split(":");
			String _ = out[4 + i * 3];
			assertEquals(_,"----------");
			assertTrue(guess.length == 2 && result.length == 2);
			assertEquals(guess[0], "Guess");
			assertEquals(result[0], "Result");
			assertEquals(result[1],bac.compare(Integer.parseInt(guess[1])));
			assertTrue(Integer.parseInt(guess[1]) >= 1000 && Integer.parseInt(guess[1]) <= 9999);
			for(int j = 0; j < 4; j++){
				for(int k = 3; k > j; k--){
					if(guess[1].charAt(j) == guess[1].charAt(k)) {
						assertTrue(false);
					}
				}
			}
		}
	}
	
	@org.junit.Test
	public void testPlayGame2(){
		BullsAndCows bac = new BullsAndCows(7890);
		bac.playGame();
		String[] out = bytes.toString().split(SEPARATOR);
		assertTrue(out.length >= 7 && out.length <=30004);
		assertEquals(out[0],"Game Start");
		assertEquals(out[1],"----------");
		assertEquals(out[out.length - 2],"You win!You took "+ (out.length - 4) / 3 +" steps.");
		assertEquals(out[out.length - 1],"----------");
		
		for(int i = 0; i < (out.length - 4) / 3; i++){
			String[] guess = out[2 + i * 3].split(":");
			String[] result = out[3 + i * 3].split(":");
			String _ = out[4 + i * 3];
			assertEquals(_,"----------");
			assertTrue(guess.length == 2 && result.length == 2);
			assertEquals(guess[0], "Guess");
			assertEquals(result[0], "Result");
			assertEquals(result[1],bac.compare(Integer.parseInt(guess[1])));
			assertTrue(Integer.parseInt(guess[1]) >= 1000 && Integer.parseInt(guess[1]) <= 9999);
			for(int j = 0; j < 4; j++){
				for(int k = 3; k > j; k--){
					if(guess[1].charAt(j) == guess[1].charAt(k)) {
						assertTrue(false);
					}
				}
			}
		}
	}

}

BullsAndCows猜数字游戏

原文:https://www.cnblogs.com/lvgj/p/12593444.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!