许久都没写博客了,些许是前段时间有些懈怠,今天来写博客,想记录下作过的事情,怕之后电脑换了,之前作的小项目也跟着丢了,总结下最近作的一个小游戏: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; } }