游戏目标:以最少的猜测次数打掉计算机所安排的达康公司(Dot Com) 网站。计算机会根据你的表现来评分。
初始设置:程序启动后,计算机会在虚拟的7x7方格(从上到下为从A到G,从左到右为从0到6)上安排3个网站(每个网站占据3个格子)。安排完成后,游戏会要求你开始猜坐标。
进行游戏:计算机会提示你输入所猜测的位置(格子),你会输入“A3”或“C5”等。计算机会反馈给你命中“Hit"没中”Miss"或击沉“Sunk”等回应。当你清光所有网站时,游戏会列出你的分数并结束。
与简单版的不同
- DotCom类
- 增加名称变量,用来保存DotCom的名字,以便击沉它们时列出名字。
- DotComBust类(the game)
- 创建出3个DotCom
- 指定DotCom的名称
- 将DotCom放在方阵上
- 每次猜测要检查3个DotCom
- 击沉3个DotCom后才能结束游戏
- 脱离main()
代码
- DotComBust.java
package dotComGame;
import java.util.*;
public class DotComBust {
private GameHelper helper = new GameHelper();
private ArrayList<DotCom> dotComsList = new ArrayList<DotCom>();
private int numOfGuesses = 0;
private void setUpGame() {
// first make some dot coms and give them locations
DotCom one = new DotCom();
one.setName("Pets.com");
DotCom two = new DotCom();
two.setName("eToys.com");
DotCom three = new DotCom();
three.setName("Go2.com");
dotComsList.add(one);
dotComsList.add(two);
dotComsList.add(three);
System.out.println("Your goal is to sink three dot coms.");
System.out.println("Pets.com, eToys.com, Go2.com");
System.out.println("Try to sink them all in the fewest number of guesses");
for (DotCom dotComToSet : dotComsList) {
ArrayList<String> newLocation = helper.placeDotCom(3);
dotComToSet.setLocationCells(newLocation);
}
}
private void startPlaying() {
while(!dotComsList.isEmpty()) {
String userGuess = helper.getUserInput("Enter a guess");
checkUserGuess(userGuess);
}
finishGame();
}
private void checkUserGuess(String userGuess) {
numOfGuesses++;
String result = "miss";
for (DotCom dotComToTest : dotComsList) {
result = dotComToTest.checkYourself(userGuess);
if (result.equals("hit")) {
break;
}
if (result.equals("kill")) {
dotComsList.remove(dotComToTest);
break;
}
}
System.out.println(result);
}
private void finishGame() {
System.out.println("All Dot Coms are dead! Your stock is now worthless.");
if (numOfGuesses <= 18) {
System.out.println("It only took you " + numOfGuesses + " guesses");
System.out.println(" You got out before your options sank.");
} else {
System.out.println("Took you long enough. " + numOfGuesses + " guesses.");
System.out.println("Fish are dancing with youe options");
}
}
public static void main(String[] args) {
DotComBust game = new DotComBust();
game.setUpGame();
game.startPlaying();
}
}
- DotCom.java
package dotComGame;
import java.util.*;
public class DotCom {
private ArrayList<String> locationCells;
private String name;
public void setLocationCells(ArrayList<String> loc) {
locationCells = loc;
}
public void setName(String n) {
name = n;
}
public String checkYourself(String userInput) {
String result = "miss";
int index = locationCells.indexOf(userInput);
if (index >= 0) {
locationCells.remove(index); // 删除被猜中的元素
if (locationCells.isEmpty()) {
System.out.println("Ouch! You sunk + " + name + " : ( ");
} else {
result = "hit";
}
}
return result;
}
}
- GameHelper.java
package dotComGame;
import java.io.*;
import java.util.*;
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.print(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.toLowerCase();
}
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++; // 现在处理到第n个
int incr = 1; // 水平增量
if ((comCount % 2) == 1) { // 如果是单数号的
incr = gridLength; // 垂直增量
}
while ( !success && attempts++ < 20) { // 主要搜索循环
location = (int) (Math.random() * gridSize); // 随机起点
// System.out.print(" try " + location);
int x = 0; // 第n个位置
success = true; // 假定成功
while (success && x < comSize) { // 查找未使用的点
if (grid[location] == 0) { // 如果没有使用
coords[x++] = location; // 储存位置
location += incr; // 尝试下一个点
if (location >= gridSize) { // 超出下边缘
success = false; // 失败
}
} else { // 找到已经使用的位置
// System.out.print(" used " + location);
success = false; // 失败
}
}
}
int x = 0; // 将位置转换成字符串形式
int row = 0;
int column = 0;
// System.out.println("\n");
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++;
// System.out.print(" coord "+x+" = " + alphaCells.get(x-1));
}
// System.out.println("\n");
return alphaCells;
}
}