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

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)。

转载于:https://my.oschina.net/u/2411436/blog/809142

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值