国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Head first java 第五章(一)

2019-11-06 07:42:54
字體:
來源:轉載
供稿:網友

Dotcom游戲

實現一個戰艦類的棋牌游戲 1.0 首先寫出一個測試代碼SimpleDotComTestDrive(該版本為最簡易版本,無需輸入)

public class SimpleDotComTestDrive { public static void main (String[] args){ SimpleDotCom dot = new SimpleDotCom(); //創建實例對象 int [] locations = {2,3,4}; //假設目標坐標 dot.setLocationCells(locations); //為實例對象設置坐標 String userGuess = "2"; //假設游戲者輸入坐標2 String result = dot.checkYourself(userGuess); //使用checkYourself方法檢查 }}

根據測試碼可知運行該游戲后,輸出為hit

真實碼

public class SimpleDotCom { int [] locationCells; //目標數組 int numOfHits = 0; public void setLocationCells(int[] locs){ locationCells = locs; } public String checkYourself(String stringGuess){ int guess = Integer.parseInt(stringGuess); //Sting轉換為int String result = "miss"; for(int cell : locationCells){ if(guess == cell){ result = "hit"; numOfHits++; break; } //循環檢查目標坐標是否等于輸入坐標 } if(numOfHits == locationCells.length){ result = "kill"; } System.out.PRintln(result); return result; }}

2.0 仍然保留SimpleDotCom類,刪除SimpleDotComTestDrive類,另外加入SimpleDotComGame類,該類代碼如下:

package game;public class SimpleDotComGame { public static void main (String[] args){ int numOfGuesses = 0; GameHelper helper = new GameHelper(); SimpleDotCom theDotCom = new SimpleDotCom(); int radomNum = (int) (Math.random()*5); int[] locations = {radomNum,radomNum+1,radomNum+2}; theDotCom.setLocationCells(locations); boolean isAlive = true; while (isAlive == true){ String guess = helper.getUserInput("你猜"); String result = theDotCom.checkYourself(guess); numOfGuesses++; if(result.equals("kill")){ isAlive = false; System.out.println("你猜了"+numOfGuesses); } }}}

以及GameHelper類:(該部分代碼為copy,暫且不管)

package game;import java.io.*;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)); 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) { // line 19 ArrayList<String> alphaCells = new ArrayList<String>(); String [] alphacoords = new String [comSize]; // holds 'f6' type coords String temp = null; // temporary String for concat int [] coords = new int[comSize]; // current candidate coords int attempts = 0; // current attempts counter boolean success = false; // flag = found a good location ? int location = 0; // current starting location comCount++; // nth dot com to place int incr = 1; // set horizontal increment if ((comCount % 2) == 1) { // if odd dot com (place vertically) incr = gridLength; // set vertical increment } while ( !success & attempts++ < 200 ) { // main search loop (32) location = (int) (Math.random() * gridSize); // get random starting point //System.out.print(" try " + location); int x = 0; // nth position in dotcom to place success = true; // assume success while (success && x < comSize) { // look for adjacent unused spots if (grid[location] == 0) { // if not already used coords[x++] = location; // save location location += incr; // try 'next' adjacent if (location >= gridSize){ // out of bounds - 'bottom' success = false; // failure } if (x>0 & (location % gridLength == 0)) { // out of bounds - right edge success = false; // failure } } else { // found already used location // System.out.print(" used " + location); success = false; // failure } } } // end while int x = 0; // turn good location into alpha coords 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; }}

結果為該次改動能夠實現輸入的讀取,進行互動化; 然而存在不足,為當命中目標后再次猜測統一坐標仍然有效,需要再改。

3.0


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大丰市| 津南区| 聂拉木县| 喜德县| 邹城市| 东兴市| 哈巴河县| 新源县| 若羌县| 荣成市| 盘锦市| 新田县| 水富县| 安化县| 雷州市| 勐海县| 三江| 德江县| 望城县| 张家川| 乌拉特后旗| 阿图什市| 梧州市| 屏山县| 庆阳市| 红桥区| 安泽县| 松江区| 增城市| 平凉市| 浮梁县| 遂平县| 罗田县| 上思县| 北票市| 济南市| 资源县| 白河县| 略阳县| 萨嘎县| 江川县|