编写一个程序的流程
程序概述
棋盘类战舰游戏,猜测对方战舰的坐标,然后轮流开炮攻击,命中数发就可以打沉战舰。
用网站名代替战舰:
- 游戏目标:以最少的猜测次数打掉计算机所安排的网站。
- 初始设置:计算机在虚拟的7*7方格上安排3个网站。安排完成后,游戏要求玩家开始猜坐标。
- 进行游戏:玩家输入坐标,计算机反馈”miss”(未命中)、”hit”(命中)或”kill”(击沉)等回应。当玩家打掉所有网站时,游戏计算分数并结束。
设计流程
高层设计
首先要了解游戏流程:
了解了游戏流程后,下面要设想需要哪些对象。要用面向对象的方式来思考;专注于程序中出现的事物而不是过程。
简单的开始
至少需要两个类:Game类和DotCom类。
先从一个简单版本开始开发:只使用横列,只设置一个网站。
简单版也需要具有完全版的基本功能,是开发完全版的踏脚石。
开发类
方法:
- 找出类应该做的事;
- 列出实例变量和方法;
- 编写方法的伪码;
- 编写方法的测试用程序;
- 实现类;
- 测试方法;
- 除错或重新设计。
编写伪码
伪码是用近似编程的语言对程序进行描述,Java伪码大致有三个部分:
- 实例变量的声明;
- 方法的声明:
- 方法的逻辑(最重要)
SimpleDotCom类的伪码
声明 一个int数组loctionCells来存储位置信息。
声明 一个int类型的数字变量numOfHits来记录打中的数目,初始化为0。
声明 checkYourself()方法,接收一个String类型输入(玩家猜测),与网站坐标比对后返回"hit","miss"或"kill"。
声明 setLocationCells()方法来获得网站的位置信息(接收一个i内含3个元素的int类型数组)
String checkYourself(String userGuess)
接受玩家输入(字符串形式)
将字符串转换成int
遍历loctionCells存储的坐标
比较玩家的猜测和loctionCells存储的坐标
if 两者相同
numOfHits++
观察玩家的猜测是否是loctionCells中最后一个坐标元素
if numOfHits是3,返回"kill",跳出循环。
else 返回"hit"
else
没猜中,返回"miss"
void setLocationCells(int[] cellLocations)
接受一个int类型数组作为输入
将实例变量loctionCells设为输入的数组
编写测试方法用的程序代码
目的:更容易更快的写出程序代码。
对于SimpleDotCom类来说,主要需要测试的就是checkYourself()这个方法,这就需要能创建并初始化一个SimpleDotCom类的对象,然后给它一个初始坐标(setLocationCells()),列出结果来观察是否正确。
SimpleDotCom的测试码
public class SimpleDotComTestDrive {
public static void main(String[] args) {
SimpleDotCom dot = new SimpleDotCom();//初始化一个对象
int[] locations = {2,3,4};
dot.setLocationCells(locations);//调用setter
String userGuess = "2";//假的猜测
String result = dot.checkYourself(userGuess);//调用被测方法并传入假的数据
}
}
实现类
public class SimpleDotCom {
int[] locationCells;
int numOfHits = 0;
public void setLocationCells(int[] locs) {
locationCells = locs;
}
public String checkYourself(String stringGuess) {
int guess = Integer.parseInt(stringGuess);//字符串变为int
String result = "miss";
for (int cell : locationCells) {
if (guess == cell) {
result = "hit";
numOfHits++;
break;
}
}
if (numOfHits == locationCells.length) {
result = "kill";
}
System.out.println(result);
return result;
}
}
编写SimpleDotComGame类的伪码
public static void main (String[] args)
声明int类型变量numOfGuess来储存玩家猜测次数,初始化为0
初始化一个SimpleDotCom实例
获取0-4的一个随机数来作为网站的第一个位置坐标
声明一个3元素int数组来存放生成的随机位置(由上面获得的随机数递增)
将位置用setter赋给SimpleDotCom实例
声明一个boolean变量isAlive来控制游戏进程,初始化为true
while (isAlive == true):
获取用户输入
调用checkYourself()方法
numOfGuess++
if (result是"kill")
isAlive设为false
打印numOfGuess
游戏的main()方法
public class SimpleDotComGame {
public static void main(String[] args) {
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");
}
}
}
}
GameHelper
import java.io.*;
public class GameHelper {
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;
}
}
一个bug(下篇文章处理)
新知识点
- 使用Interger.parseInt()来取得Ttring的整数值;
- str.equal(str2)来比较两个字符串;