許久都沒寫博客了,些許是前段時間有些懈怠,今天來寫博客,想記錄下作過的事情,怕之後電腦換了,之前作的小項目也跟着丟了,總結下最近作的一個小遊戲:java
遊戲目的:創建一個7X7的網格,選擇其中的連續的三格來做爲一個目標,一共有3個目標,對每次的猜數,給一個hit,miss,kill,若是三個目標都殺完了,返回猜想次數,給個分數web
遊戲核心:類之間的交流 private對屬性設置後部分代碼的重構 ArrayList的使用編程
要求:main()做爲一個啓動程序 基本上全部的代碼都放到各種中dom
一個類裏面:屬性propety和方法method ide
property method main 三者竟然就構建了java的世界 佩服OO測試
headfirst中說起極限編程:意在對課題有基本瞭解後,先寫只重功能的測試碼,卻不去想怎麼實現,再去一一寫各種,在我寫這個遊戲時,確也不僅一次的先聯想到測試碼,然後再去想各種的編寫,竟是如此天然spa
注意:這其中卻遇到了一個for循環,在邏輯上必定會對res變量賦值,愚蠢的電腦卻只有在運行的時候才能知道,但是因爲res的賦值在for循環中,所以編譯器只能報錯code
所以若是之後遇到這種狀況姑且給變量賦值,由於既然邏輯上會被賦值,想必也會更改他的值,咱們就姑且打消編譯器的疑慮吧,啊哈哈哈哈blog
Game類:three
package twoClass; import java.util.ArrayList; class Game{ int numOfGuess = 0; ArrayList<DotCom> ThreeDotCom; GameHelper helper; void SetUp(){ DotCom [] Three = new DotCom[3]; helper = new GameHelper(); ArrayList<DotCom> tmp= new ArrayList<DotCom>(); for(int i = 0; i < 3; i++){ Three[i] = new DotCom(); } Three[0].nameSet("pet.com"); Three[1].nameSet("amaoza.com"); Three[2].nameSet("tmall.com"); for(int i = 0; i < 3; i++){ tmp.add(Three[i]); } ThreeDotCom = tmp; for(DotCom Cell: ThreeDotCom){ ArrayList<String> generator = helper.placeDotCom(3); Cell.setLocation(generator); } } void startPlaying(){ String guess; String res = " "; while(!ThreeDotCom.isEmpty()){ guess = helper.getUserInput("Enter a num:"); numOfGuess++; for(DotCom Cell : ThreeDotCom){ res = Cell.checkUserGuess(guess); if(res == "kill"){ ThreeDotCom.remove(Cell); } if(res != "miss") break; } System.out.println(res); } } void finishGame(){ System.out.println("you have sunk three dots by " + numOfGuess + "guess"); } public static void main(String [] args){ Game myGame = new Game(); myGame.SetUp(); myGame.startPlaying(); myGame.finishGame(); } }
DotCom類:
package twoClass; import java.util.ArrayList; public class DotCom{ private ArrayList<String> LocCell; private String name; public void nameSet(String webName){ name = webName; } public void setLocation(ArrayList<String> Loc){ LocCell = Loc; } public String checkUserGuess(String guess){ String res = "miss"; if(LocCell.contains(guess)){ res = "hit"; LocCell.remove(guess); if(LocCell.isEmpty()) res = "kill"; } if(res == "kill"){ System.out.println("ouch! You have sunk " + name); } return res; } }
以及搬來的磚:
package twoClass; import java.util.*; import java.io.*; 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; 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; } 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; int location = 0; comCount++; int incr = 1; if((comCount % 2) == 1){ incr = gridLength; } while( !success & attempts++ < 200 ){ location = (int) (Math.random() * gridSize); 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{ success = false; } } } int x = 0; int row = 0; int column = 0; while(x < comSize){ grid[coords[x]] = 1; row = (int) (coords[x] / gridLength); column = coords[x] % gridLength; temp = String.valueOf(alphabet.charAt(column)); alphaCells.add(temp.concat(Integer.toString(row))); x++; } return alphaCells; } }