[关闭]
@Senl 2017-03-23T11:58:17.000000Z 字数 6016 阅读 1267

3.22 学习进度

技术学习


JAVA

(上午)

自己的简版战舰改进(DeBug)

  1. if(guess>=1 && guess<=9)
  2. if(guess>=10 && guess <=0){
  3. result = "Out of Range input again";
  4. }
  5. //检测用户输入的值是否存在于战场域中,如果输入越界则将提醒用户输入越界
  6. //用户输入不越界才进行检测
  7. //并定义了一个实例变量check,当第一次击中时候,check=guess以保护这个方块
  8. //再次击中则不算入击中数中
  9. if(guess>=1 && guess<=9){
  10. for (int cell : locationCells){
  11. if (guess == cell){
  12. //增加一个check变量防止多次击中同一个部位造成Kill
  13. if(check==guess){
  14. result= "You have already hit it ";
  15. }
  16. if(check != guess){
  17. result = "hit";
  18. check = guess;
  19. num_Hits ++;
  20. }
  21. break;}
  22. }// break for loops
  23. }

(晚上)

书上的简版战舰DeBug

  1. //战舰游戏简版游戏类
  2. // 优化版的游戏战舰设计类
  3. import java.util.ArrayList;
  4. /*
  5. ArrayList 一些方法
  6. @ add(Object) 向List中加入对象参数
  7. @ remove(int) 在索引参数中删除对应的对象
  8. @remove(Object) 移除该对象
  9. @contains(Object) 如果和对象参数匹配返回“true”
  10. @isEmpty() 如果list中没有元素返回true
  11. @index(Object) 返回对象参数的索引或-1
  12. @size 返回list元素中的一个数
  13. @get(int) 返回当前索引参数的对象
  14. */
  15. public class Dotc {
  16. private ArrayList<String> locationCells;
  17. public void setLocationCells(ArrayList<String> loc){
  18. locationCells = loc;
  19. }
  20. public String checkYourself (String stringGuess){
  21. String result = "miss";
  22. int index = locationCells.indexOf(userInput);
  23. if(index >= 0){
  24. locationCells.remove(index);
  25. if(locationCells.isEmpty()) {
  26. result = "kill";
  27. }else{
  28. result = " hit";
  29. }
  30. }
  31. return result ;
  32. }
  33. }

豪华版战舰游戏——战舰类

  1. //战舰类,创造三个战舰并为他们命名
  2. package Test; //好像不加这个就过不编译 但是不懂原理
  3. import java.util.ArrayList;
  4. public class DotCom {
  5. private ArrayList<String> locationCells;
  6. public void setLocationCells(ArrayList<String> loc)
  7. {
  8. locationCells = loc;
  9. }
  10. public String checkYourself(String userInput)
  11. {
  12. String result = "miss";
  13. int index = locationCells.indexOf(userInput);
  14. if (index >= 0) {
  15. locationCells.remove(index);
  16. if (locationCells.isEmpty()) {
  17. result = "kill";
  18. }
  19. else
  20. {
  21. result = "hit";
  22. }
  23. }
  24. return result;
  25. }
  26. //TODO: all the following code was added and should have been included in the book
  27. private String name;
  28. public void setName(String string) {
  29. name = string;
  30. }
  31. }

豪华版战舰游戏——游戏主类


  1. //游戏主类,定义了创造游戏(创造对象并放入ArrayList),开始游戏(检查战舰是否存活,输入和检查),检查输入,和结束游戏4个方法 并且是作为main方法入口
  2. package Test;
  3. import java.util.*;
  4. public class DotComBust {
  5. private GameHelper helper = new GameHelper();
  6. private ArrayList<DotCom> dotComsList = new ArrayList<DotCom>();
  7. private int numOfGuesses = 0;
  8. private void setUpGame() {
  9. DotCom one = new DotCom();
  10. one.setName("Pets.com");
  11. DotCom two = new DotCom();
  12. two.setName("eToys.com");
  13. DotCom three = new DotCom();
  14. three.setName("Go2.com");
  15. dotComsList.add(one);
  16. dotComsList.add(two);
  17. dotComsList.add(three);
  18. System.out.println("Your goal is to sink three dot coms.");
  19. System.out.println("Pets.com, eToys.com, Go2.com");
  20. System.out.println("Try to sink them all in the fewest number of guesses");
  21. for (DotCom dotComSet : dotComsList) {
  22. ArrayList<String> newLocation = helper.placeDotCom(3);
  23. dotComSet.setLocationCells(newLocation);
  24. }
  25. }
  26. private void startPlaying() {
  27. while (!dotComsList.isEmpty()) {
  28. String userGuess = helper.getUserInput("Enter a guess");
  29. checkUserGuess(userGuess);
  30. }
  31. finishGame();
  32. }
  33. private void checkUserGuess(String userGuess)
  34. {
  35. numOfGuesses++;
  36. String result = "miss";
  37. for (DotCom dotComToTest : dotComsList)
  38. {
  39. result = dotComToTest.checkYourself(userGuess);
  40. if (result.equals("hit"))
  41. {
  42. break;
  43. }
  44. if (result.equals("kill"))
  45. {
  46. dotComsList.remove(dotComToTest);
  47. break;
  48. }
  49. }
  50. System.out.println(result);
  51. }
  52. private void finishGame() {
  53. System.out.println("All Dot Coms are dead! Your stock is now worthless");
  54. if (numOfGuesses <= 18) {
  55. System.out.println("It only took you " + numOfGuesses + " guesses");
  56. System.out.println("You got out before your options sank.");
  57. }
  58. else
  59. {
  60. System.out.println("Took you long enough. " + numOfGuesses + " guesses.");
  61. System.out.println("Fish are dancing with your options.");
  62. }
  63. }
  64. public static void main(String[] args) {
  65. DotComBust game = new DotComBust();
  66. game.setUpGame();
  67. game.startPlaying();
  68. }
  69. }

豪华版战舰游戏——游戏辅助类


  1. //GameHelper类 处理输入,设置战舰位置
  2. //搬的砖头,有些不太理解,日后回头再改
  3. package Test;
  4. import java.io.*;
  5. import java.util.*;
  6. public class GameHelper {
  7. private static final String alphabet = "abcdefg";
  8. private int gridLength = 7;
  9. private int gridSize = 49;
  10. private int [] grid = new int[gridSize];
  11. private int comCount = 0;
  12. public String getUserInput(String prompt) {
  13. String inputLine = null;
  14. System.out.print(prompt + " ");
  15. try {
  16. BufferedReader is = new BufferedReader(
  17. new InputStreamReader(System.in));
  18. inputLine = is.readLine();
  19. if (inputLine.length() == 0 ) return null;
  20. } catch (IOException e) {
  21. System.out.println("IOException: " + e);
  22. }
  23. return inputLine.toLowerCase();
  24. }
  25. public ArrayList<String> placeDotCom(int comSize) { // line 19
  26. ArrayList<String> alphaCells = new ArrayList<String>();
  27. String [] alphacoords = new String [comSize]; // holds 'f6' type coords
  28. String temp = null; // temporary String for concat
  29. int [] coords = new int[comSize]; // current candidate coords
  30. int attempts = 0; // current attempts counter
  31. boolean success = false; // flag = found a good location ?
  32. int location = 0; // current starting location
  33. comCount++; // nth dot com to place
  34. int incr = 1; // set horizontal increment
  35. if ((comCount % 2) == 1) { // if odd dot com (place vertically)
  36. incr = gridLength; // set vertical increment
  37. }
  38. while ( !success & attempts++ < 200 ) { // main search loop (32)
  39. location = (int) (Math.random() * gridSize); // get random starting point
  40. //System.out.print(" try " + location);
  41. int x = 0; // nth position in dotcom to place
  42. success = true; // assume success
  43. while (success && x < comSize) { // look for adjacent unused spots
  44. if (grid[location] == 0) { // if not already used
  45. coords[x++] = location; // save location
  46. location += incr; // try 'next' adjacent
  47. if (location >= gridSize){ // out of bounds - 'bottom'
  48. success = false; // failure
  49. }
  50. if (x>0 & (location % gridLength == 0)) { // out of bounds - right edge
  51. success = false; // failure
  52. }
  53. } else { // found already used location
  54. // System.out.print(" used " + location);
  55. success = false; // failure
  56. }
  57. }
  58. } // end while
  59. int x = 0; // turn good location into alpha coords
  60. int row = 0;
  61. int column = 0;
  62. // System.out.println("\n");
  63. while (x < comSize) {
  64. grid[coords[x]] = 1; // mark master grid pts. as 'used'
  65. row = (int) (coords[x] / gridLength); // get row value
  66. column = coords[x] % gridLength; // get numeric column value
  67. temp = String.valueOf(alphabet.charAt(column)); // convert to alpha
  68. alphaCells.add(temp.concat(Integer.toString(row)));
  69. x++;
  70. // System.out.print(" coord "+x+" = " + alphaCells.get(x-1)); //将注释去掉就可以获得三个战舰的位置
  71. }
  72. //System.out.println("\n");//获得三个战舰位置前取消去掉注释可以使格式更好看
  73. return alphaCells;
  74. }
  75. }

Android

(上午)

(晚上)


添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注