Week 2 -10 SimpleDotCom


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * GameHelper class from Head First Java: Second Edition, Authors: Kathy Sierra
 * and Bert Bates.
 *
 */
public class GameHelper
{
	private static final String alphabet = "abcdefg";
	private int gridLength = 7;
	private int gridSize = 49;
	private int[] grid = new int[gridSize];
	private int comCount = 0;

	public String getUserInput(String prompt)
	{
		String inputLine = null;
		System.out.println(prompt + " ");
		try
		{
			BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
			inputLine = is.readLine();
			if (inputLine.length() == 0)
				return null;
		} catch (IOException e)
		{
			System.out.println("IOException: " + e);
		}
		return inputLine;
	}

	public ArrayList<String> placeDotCom(int comSize)
	{
		ArrayList<String> alphaCells = new ArrayList<String>();
		String[] alphacoords = new String[comSize];
		String temp = null;
		int[] coords = new int[comSize];
		int attempts = 0;
		boolean success = false;
		int location = 0;

		comCount++;
		int incr = 1;
		if ((comCount % 2) == 1)
		{
			incr = gridLength;
		}
		while (!success & attempts++ < 200)
		{
			location = (int) (Math.random() * gridSize);
			int x = 0;
			success = true;
			while (success && x < comSize)
			{
				if (grid[location] == 0)
				{
					coords[x++] = location;
					location += incr;
					if (location >= gridSize)
					{
						success = false;
					}
					if (x > 0 && (location % gridLength == 0))
					{
						success = false;
					}
				} else
				{
					success = false;
				}
			}
		}
		int x = 0;
		int row = 0;
		int column = 0;
		while (x < comSize)
		{
			grid[coords[x]] = 1;
			row = (int) (coords[x] / gridLength);
			column = coords[x] % gridLength;
			temp = String.valueOf(alphabet.charAt(column));
			alphaCells.add(temp.concat(Integer.toString(row)));
			x++;
		}
		return alphaCells;
	}
}
public class SimpleDotCom
{
	private int locationCells[];
	private int numberOfHits = 0;

	public void setLocationCells(int loc[])
	{
		locationCells = loc;
	}

	public String checkYourself(String stringGuess)
	{
		String result = "miss";
		int x = Integer.parseInt(stringGuess);
		for (int i = 0; i < locationCells.length; i++)
		{
			if (locationCells[i] == x)
			{
				result = "hit";
				numberOfHits++;
				break;
			}
		}
		if (numberOfHits == locationCells.length)
		{
			result = "kill";
		}
		System.out.println(result);
		return result;
	}

	public static void main(String[] args)
	{
		int guessNum = 0;
		// 随机创建3个位置
		int start = (int) Math.random() * 5;
		int array[] =
		{ start, start + 1, start + 2 };
		// 创建一个名字为dot 的SimpleDotCom类
		SimpleDotCom dot = new SimpleDotCom();
		dot.setLocationCells(array);

		// 创建一个名字为helper 的类变量
		GameHelper helper = new GameHelper();
		// 设置一个类型为boolean类型的变量 isAlive
		// 设置一个while循环 只要没有被完全击中 就一直在循环里

		boolean isAlive = true;
		while (isAlive == true)
		{
			String guess = helper.getUserInput("Enter a number");
			String result = dot.checkYourself(guess);
			guessNum++;
			if (result == "kill")
			{
				isAlive = false;
			}
		}
		System.out.println("you hava tried " + guessNum + " times");

	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值