標籤:
1 import java.io.*; 2 3 public class GameHelper { 4 public String getUserInput(String prompt){ 5 String inputLine = null; 6 System.out.println(prompt + " "); 7 try { 8 BufferedReader is = new BufferedReader(new InputStreamReader(System.in)); 9 inputLine = is.readLine();10 if(inputLine.length() == 0) return null;11 } catch (IOException e) {12 // TODO Auto-generated catch block13 System.out.println("IoException: "+e);14 }15 return inputLine;16 }17 18 }
1 public class SimpleDotCom { 2 int[] locationCells; 3 int numOfHits = 0; 4 5 public void setLocationCells(int[] locs) { 6 locationCells = locs; 7 } 8 9 public String checkYourself(String stringGuess){10 int guess = Integer.parseInt(stringGuess);11 String result = "miss";12 for (int cell : locationCells){13 if (guess == cell){14 result = "hit";15 numOfHits++;16 break;17 }18 }19 if(numOfHits == locationCells.length){20 result = "kill";21 }22 System.out.println(result);23 return result;24 25 }26 27 }
1 public class SimpleDotComGame { 2 3 public static void main(String[] args) { 4 int numOfGuesses = 0; 5 GameHelper helper = new GameHelper(); 6 7 SimpleDotCom theDotCom = new SimpleDotCom(); 8 int randomNum = (int)(Math.random() * 5); 9 10 int[] location = {randomNum,randomNum+1,randomNum+2};11 theDotCom.setLocationCells(location);12 boolean isAlive = true;13 14 while(isAlive == true) {15 String guess = helper.getUserInput("enter a number");16 String result = theDotCom.checkYourself(guess);17 numOfGuesses++;18 if(result.equals("kill")) {19 isAlive = false;20 System.out.println("You took"+numOfGuesses+"guesses");21 }22 }23 24 }25 26 }
簡單版攻擊網站設計!