改进了一下这个游戏的输出及思路,是不是好玩多了??:)
2015-05-04 23:20
405 查看
终于看了两天才摸 熟悉,还是有点笨啊。。。
GameHelper.java
DotCom.java
DotComBurst.java
GameHelper.java
import java.io.*; import java.util.*; public class GameHelper { int comCount = 0; public String getUserInput(String prompt) { String inputLine = null; System.out.println(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) { final String alphabet = "abcdefg"; int gridLength = 7; int gridSize = 49; int [] grid = new int[gridSize]; ArrayList<String> alphaCells = new ArrayList<String>(); String temp = null; int [] coords = new int[comSize]; int attempts = 0; boolean success = false; int location = 0; int incr; if (((int) (Math.random() * 2) % 2) == 1) { incr = gridLength; } else { incr = 1; } //System.out.println("incr is: " + incr); while ( !success & attempts++ < 200) { location = (int) (Math.random() * gridSize); //System.out.println("location: " +location); int x = 0; success = true; while (success && x < comSize) { if (grid[location] == 0) { coords[x++] = location; location += incr; //System.out.println("location: " +location); if (location >= gridSize) { success = false; } if (x > 0 && (location % gridLength == 0)) { success = false; } } else { success = false; } //System.out.println("sucess status is :" + success); //System.out.println("x value is :" + x); } } //System.out.print("coords array is :"); //for (int item : coords) { // System.out.print(item + " "); //} //System.out.println(""); int x = 0; int row = 0; int column = 0; while (x < comSize) { grid[coords[x]] = 1; row = (int) (coords[x] / gridLength); //System.out.println("row value is :" + row); column = (int) (coords[x] % gridLength); temp = String.valueOf(alphabet.charAt(column)); //System.out.println("temp value is :" + temp); alphaCells.add(temp.concat(Integer.toString(row))); x++; } //for (String item : alphaCells) { // System.out.print(item + " "); //} return alphaCells; } }
DotCom.java
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 getName() { return name; } 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; } }
DotComBurst.java
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 doc coms."); System.out.println("Pets.com, eToys.com, Go2.com"); System.out.println("Try to sink them all in the fewsest number of guesses."); for(DotCom dotComToSet : dotComsList) { ArrayList<String> newLocation = helper.placeDotCom(3); dotComToSet.setLocationCells(newLocation); for (String item : newLocation) { System.out.print( item +" "); } System.out.println(dotComToSet.getName()); System.out.println(); } } 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's 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 you options.."); } } public static void main (String[] args) { DotComBust game = new DotComBust(); game.setUpGame(); game.startPlaying(); System.out.println("Finish"); } }
![](http://images.cnitblog.com/blog2015/465438/201505/042320094235709.png)
相关文章推荐
- 收到Sybase公司PowerDesigner产品的律师函后,只能改进一下思路了
- 第一章-第四题(ACM 比赛的程序是软件么? “写程序” 和 ”做软件“ 有区别么?软件工程是不是教那些不怎么会写程序的人开发软件? 你怎么看?这个游戏团队, 有很好的软件,但是商业模式和其他软件之外的因素呢?有没有考虑到)--By梁旭晖
- 今天来和大家一起分享一下好玩的游戏---美国农场主
- 收到Sybase公司PowerDesigner产品的律师函后,只能改进一下思路了
- 好久没回来看看了,想着是不是要重新启用一下我的这个老blog呢~~~
- 谁有m55啊?帮忙测试一下我的这个游戏是否能在m55上运行。高分相送!
- 这个题目我重写了好几遍,虽然思路很清晰,但始终没有写出来,借鉴了一下别人的,日后再研究
- 【C语言】迷宫游戏设计思路与功能体会及简单的改进报告
- 今天和大家讲一下怎么实现秒表这个功能Chronometer。很多地方都要用到秒表这个功能,比如视频录制,游戏(1)
- 在扑克牌游戏中,每次游戏开始都要求把54张牌重新排列一下,称为洗牌。试编写程序将一副扑克牌(用54个整数1~54表示)随机洗好后,顺序输出54张牌的情况。
- 输出对象的各个公开方法的返回值,不深入递归,建议仅在测试时使用,比如你想了解一下这个类的返回值
- 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!
- 在扑克牌游戏中,每次游戏开始都要求把54张牌重新排列一下,称为洗牌。试编写程序将一副扑克牌(用54个整数1~54表示)随机洗好后,顺序输出54张牌的情况。
- Windows上安装Redmine-2.6.0的日志(话说这个软件还是不如JIRA好用,话说是不是再尝试一下trac?)
- session.getAttribute("Visited"),jsp里面的这个Visited是不是内置的?请详细说一下
- 尝试一下用这个方法输出二进制
- 微信小程序(游戏)----拼图游戏(设计思路)
- 记一下实在受不了这个错误了:LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- 收藏一个好玩的游戏先,以后可以经常玩
- 2017(秋)软工作业: (2)硬币游戏—— 代码分析与改进