Head First Java (第二版)学习记录 5 - 编写程序 DotCom 初级游戏

原创
2016/12/20 16:49
阅读数 674

1. 编写真正的游戏

1.1 高层设计

    a. 玩家启动游戏

        i. 计算机创建3个达康网站。

        ii. 将此3个达康网站停在虚拟战场上。

    b. 游戏开始

        重复下面的操作直到所有达康网站被歼灭为止。

        i. 提示玩家输入坐标。

        ii. 检查是否命中、没中或击毁。如果命中就删除格子,击毁就删除达康网站。

    c. 游戏结束

        根据猜测次数给分。

-------------------------------------------------------------------------

1.2 SimpleDotCom 类

public class SimpleDotCom {
    private int[] locationCells;
    private int numOfHits;
	
    public String checkYourself(String stringGuess){
	int intGuess = Integer.parseInt(stringGuess);
	String result = "miss";
		
	for (int cell : locationCells) {//该循环内还有bug,但是能编译通过
	    if (intGuess == cell) {
		result = "hit";
		numOfHits++;
		break;
	    }
	}
	if (numOfHits == locationCells.length) {
	    result = "kill";
	}
	System.out.println(result);
	return result;
    }
	
    public void setLocationCells(int[] loc){
	locationCells = loc;
    }
}

-------------------------------------------------------------------------

1.3 SimpleDotCom 的测试类

public class SimpleDotComTestClass {

    public static void main(String[] args) {
	// TODO Auto-generated method stub
	SimpleDotCom dot = new SimpleDotCom();
		
	int[] locations = {2,3,4};
	dot.setLocationCells(locations);
	
	String userGuess = "2";
	String result = dot.checkYourself(userGuess);
	String testResult = "failed";
	if(result.equals("hit")){
	    testResult = "passed";
	}
	System.out.println(testResult);
    }
}

-------------------------------------------------------------------------

1.4 GameHelper 类

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

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

-------------------------------------------------------------------------

1.5 SimpleDotComGame 类

public class SimpleDotComGame {

    public static void main(String[] args) {
	// TODO Auto-generated method stub
	int numOfGuess = 0;
	GameHelper helper = new GameHelper();
	
	SimpleDotCom theDotCom = new SimpleDotCom();
	int randomNum = (int)(Math.random()*5);
	
	int[] locations = {randomNum,randomNum + 1,randomNum +2};
	theDotCom.setLocationCells(locations);
	boolean isAlive = true;
	
	while (isAlive == true) {
	    String guess = helper.getUserInput("enter a number:");
	    String result = theDotCom.checkYourself(guess);
	    numOfGuess++;
	    if (result.equals("kill")) {
		isAlive = false;
		System.out.println("You took " + numOfGuess + " guesses.");
	    }//close if
	}//close while
    }//close main
}

-------------------------------------------------------------------------

1.6 运行 SimpleDotComGame

    运行后就可以实现该游戏的交互了,能够依据上面的设计给出不同的反应。

    但是一旦猜中其中的一格,就可以持续的猜同一格来击毁 DotCom (这就是隐藏的 bug)。

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部