学习Java08
来源:互联网 发布:电脑安装录音软件 编辑:程序博客网 时间:2024/05/01 19:12
CODE:
import java.util.*;import java.io.*;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("please Guess!:");for (DotCom dotComToSet : dotComsList){ArrayList<String>newLocation = helper.placeDotCom(3);dotComToSet.setLocationCells(newLocation);}}private void StartPlaying(){while(!dotComsList.isEmpty()){String userGuess=helper.getUserInput("Enter a guess!");checkUserGuess(userGuess);}finishGame();}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)); 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) { ArrayList<String> alphaCells = new ArrayList<String>(); //String [] alphacoords = new String [comSize]; // 保存字符串 String temp = null; // 临时字符串 int [] coords = new int[comSize]; // 现有字符串 int attempts = 0; // 目前测试的字符串 boolean success = false; // flag = 找到了合适的字符吗 ? int location = 0; // 目前起点 comCount++; int incr = 1; if ((comCount % 2) == 1) { incr = gridLength; } while ( !success & attempts++ < 200 ) { location = (int) (Math.random() * gridSize); //System.out.print(" try " + location);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 { // System.out.print(" used " + location); success = false; } } } int x = 0; int row = 0; int column = 0; // System.out.println("\n"); while (x < comSize) { grid[coords[x]] = 1; // mark master grid pts. as 'used' row = (int) (coords[x] / gridLength); // get row value column = coords[x] % gridLength; // get numeric column value temp = String.valueOf(alphabet.charAt(column)); // convert to alpha alphaCells.add(temp.concat(Integer.toString(row))); x++; // System.out.print(" coord "+x+" = " + alphaCells.get(x-1)); } // System.out.println("\n"); return alphaCells; }}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")){break;}}System.out.println(result);}private void finishGame(){System.out.println("ALL DOT HAVE BEEN KILLED!YOU WIN THE GAME!");System.out.println("It took you "+numOfGuesses+"guesses.");}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;}}public class Day { public static void main(String args[]) {DotComBust game = new DotComBust();game.setUpGame();game.StartPlaying(); } }}
0 0
- 学习Java08
- Java08-5
- java08-24
- java08总结
- java08作业
- 重拾Java回忆录(八):Java08集合的使用
- 高效Java08:覆盖equals时遵守通用约定
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 平台碎片化,Android应用开发该如何取舍?
- ListView记事本效果
- AutoMapper对象映射工具
- 2014.02.12 JFinal 之Model
- 90后程序媛想对80、70大拿说
- 学习Java08
- In-App Purchase iap 内付费 二次验证代码 (java 服务器端)
- 过年小事记
- PHP会员登录,和判断用户权限,登录超时踢除用户
- hibernate Query对象查询集合
- oracle数据库函数-NVL(字符串,替换字符)
- 云应用
- 如何查看Linux版本号(内核版本号和发行版本号)
- SpringMVC入门实例及详细讲解