代码示例:
主类

package dot.com;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() {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) {//对list中所有的DitCom重复ArrayList<String> newLocation=helper.placeDotCom(3);//要求DotCom的位置dotComToSet.setLocationCells(newLocation);//调用这个DotCom的setter方法来指派刚取得的位置}}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 your options.");}}public static void main(String[] args) {DotComBust game=new DotComBust();game.setUpGame();game.startPlaying();}
}

代码:GameHelper类

package dot.com;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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));//java.io里面的类:BufferedReader(readLine是其里面的方法)和InputStreamReaderinputLine=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>();@SuppressWarnings("unused")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;}}}//while结束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;}
}

代码:DotCom类

package dot.com;import java.util.ArrayList;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()) {result="kill";System.out.println("Ouch! You sunk"+name+" :(");}else {result="hit";}}return result;}
}

运行结果:

Your goal is to sink three dot coms.
Pets.com,eToys.com,Go2.com
Try to sink them all in the fewest number of guesses
Enter a guess a3
miss
//略掉。。。。。
Enter a guess c7
miss
Enter a guess d6
hit
Enter a guess e6
Ouch! You sunkeToys.com :(
kill
Enter a guess d1
miss
Enter a guess d2
hit
Enter a guess d3
hit
Enter a guess d4
Ouch! You sunkPets.com :(
kill
Enter a guess d5
miss
//略掉。。。。。
Enter a guess g1
miss
Enter a guess g2
hit
Enter a guess g3
hit
Enter a guess g5
miss
Enter a guess g4
Ouch! You sunkGo2.com :(
kill
All Dot Coms are dead! Your stock is now worthless.
Took you long enough.52guesses.
Fish are dancing with your options.

Head First java——战舰游戏代码相关推荐

  1. java猜拳游戏代码_Java实现简单猜拳游戏

    本文实例为大家分享了java实现简单猜拳游戏的具体代码,供大家参考,具体内容如下 看网上的猜拳游戏那么多,但都是用switch输入数字,所以用if嵌套,写一个简单的猜拳游戏 package game; ...

  2. 复习Java小球游戏代码分享Java面试题MySQL中常用的锁生活【记录一个咸鱼大学生三个月的奋进生活】021

    记录一个咸鱼大学生三个月的奋进生活021 复习Java小球游戏 游戏界面的代码 小球运动线程的代码 运行游戏的代码 运行结果 代码分享 学习Java面试题(MySQL中常用的锁) 照片分享 复习Jav ...

  3. java打字游戏代码_牛逼啊!一个随时随地写Python代码的神器

    现在学Python的人越来越多,很多小伙伴都非常有激情.利用碎片时间随时随地学习Python, 大家知道Python是一门编程语言,但是学语言光看不练是没有用的.最好能编程并运行,有没有什么好的神器可 ...

  4. java猜拳游戏代码_猜拳游戏 - java代码库 - 云代码

    [java]代码库public class Computer { String name; int score; public int showfist(){ int quan; quan=(int) ...

  5. java弹球游戏代码_Java实现简单的弹球游戏

    本文实例为大家分享了Java实现简单的弹球游戏的具体代码,供大家参考,具体内容如下 该程序主要是用于对java图形化界面编程进行联系,程序实现全部采用的是AWT包下的类. 程序仅做参考,供学习使用. ...

  6. Java扫雷游戏代码

    实现扫雷游戏控制台版 扫雷游戏 a.游戏的分析 在游戏中需要存在对象包含哪些. 格子对象(Grid): 属性:内容(content).状态(type) b.工程架构 设计工程包结构 bean:存放实体 ...

  7. java小游戏代码压缩包_java 贪吃蛇小游戏 源码下载

    [实例简介] 使用java程序设计语言制作的一个贪吃蛇小游戏.游戏的控制模块应该做到易懂.易操作,以给玩家一个很好的游戏环境.在这个游戏的设计中,牵涉到图形界面的显示与更新.数据的收集与更新,还要应用 ...

  8. java猜拳游戏代码

    猜拳游戏: import java.util.Scanner; public class Scissors_Stone_Cloth{public static void main(String[] a ...

  9. java魔方游戏代码_java swing实现的魔方小游戏源码附带视频指导运行教程

    <p> <span style="color:#555555;font-family:"font-size:16px;background-color:#FFFF ...

最新文章

  1. UITextField长度限制的写法
  2. 京东的商品搜索功能是如何实现的_【干货小知识】京东商家如何优化搜索流量?...
  3. 笔记-JavaWeb学习之旅2
  4. 校验金额、大小写字母、大写字母、合法uri、email
  5. SAP UI5 OData Json model name
  6. 升级.Net Core RC1的类库项目
  7. 玩JerseyTest(Jersey 2.5.1和DI)
  8. python表达式的值是 y 和n是什么意思_python中^是什么意思
  9. dubbo分布式系统链路追踪_zipkin
  10. Flume监控几种方式
  11. 嵌入式Linux系统编程学习之二十四消息队列
  12. 小操作_js调出outlook
  13. (转)ATOM介绍和使用
  14. missing required library libmysql_e.dll,126 解决办法
  15. [BZOJ5336]-[TJOI2018]游园会-dp套dp
  16. 干货丨如何优雅地设计并控制一台协作机械臂
  17. 研报复现系列(六)【国泰君安】基于CCK模型的股票市场羊群效应研究
  18. Python免费发短信
  19. 我的奋斗之黑马第一天
  20. Boosting和Bagging区别

热门文章

  1. 我国AGV工业编年史(图谱)从1975年第一台磁导航AGV开始
  2. 苏州企业如何确定科技成果的权利归属
  3. 中华人民共和国档案法
  4. 通过openssl学习ssl证书。
  5. UD LAN-401 信号处理模块
  6. ios 有很多种cell时的写法 以及 masonry 的使用
  7. 数据分析与挖掘:财政收入影响因素分析及预测模型
  8. 【Error解决实录】UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xfc in position 7: invalid start byt
  9. 吃鸡用什么游戏蓝牙耳机?适合吃鸡用的低延迟蓝牙耳机推荐
  10. 成功的背后!(给所有IT人)(转载来自http://blog.csdn.net/ysuncn/archive/2007/10/07/1814127.aspx)