博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
改进了一下这个游戏的输出及思路,是不是好玩多了??:)
阅读量:6858 次
发布时间:2019-06-26

本文共 6240 字,大约阅读时间需要 20 分钟。

终于看了两天才摸 熟悉,还是有点笨啊。。。

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
placeDotCom(int comSize) { final String alphabet = "abcdefg"; int gridLength = 7; int gridSize = 49; int [] grid = new int[gridSize]; ArrayList
alphaCells = new ArrayList
(); 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
locationCells; private String name; public void setLocationCells(ArrayList
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
dotComsList = new ArrayList
(); 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
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://foiyl.baihongyu.com/

你可能感兴趣的文章
MySQL卸载及安装
查看>>
Ubuntu 11.04 下安装配置 JDK 7
查看>>
Linux下使用rsync最快速删除海量文件的方法。
查看>>
2015-2016寒假 第一、二周学习总结
查看>>
页面跳转 [转自: http://www.mamicode.com/info-detail-469709.html]
查看>>
linux系统文件管理与查找
查看>>
如何设计上十亿的用户表
查看>>
Scrum 冲刺博客第七篇
查看>>
Python 性能优化
查看>>
设计的网页 如何在ie8中 避免 兼容性视图 的按钮出现?
查看>>
我的友情链接
查看>>
(转) eclipse下导入android源码
查看>>
解决embed标签设置z-index无效
查看>>
1.3 保持最后N个元素
查看>>
我的友情链接
查看>>
Python的运算符
查看>>
resin安装
查看>>
我的友情链接
查看>>
Windows Server 2008 将计算机加入到指定组织单元
查看>>
设置域用户帐户密码策略
查看>>