java零基礎入門第六天 ArrayList 導包 java運算符

先附上最終代碼 

import java.io.*;
import java.util.ArrayList;
import java.util.*;
public class DotComBust{//執行類

	private GameHelper helper = new GameHelper();
	private ArrayList<DotCom>dotComsList = new ArrayList<DotCom>();
	private int numOfGuesses = 0;

	private void setUpGame(){
		DotCom one = new DotCom();
		one.setName("章魚哥");
		DotCom two = new DotCom();
		two.setName("史努比");
		DotCom three = new DotCom();
		three.setName("唐老鴨");
		dotComsList.add(one);
		dotComsList.add(two);
		dotComsList.add(three);

		System.out.println("對象已經建好 名字是 唐老鴨 史努比 章魚哥");

		for(DotCom dotComToSet : dotComsList){
			ArrayList<String>newLocation = helper.placeDotCom(3);
			dotComToSet.setLocationCells(newLocation);
		}
	}

	private void startPlaying(){
		while(!dotComsList.isEmpty()){
			String userGuess = helper.getUserInput("輸入你的猜測 類似 A1 D5");
			checkUserGuess(userGuess);
		}
		finishGame();

	}

	private void checkUserGuess(String userGuess){
		numOfGuesses++;
		String result = "miss";

		for(DotCom dotComToTest : dotComsList){
			result = dotComToTest.checkYourself(userGuess);
			if(result.equals("hit")){
				break;
			}
			if(result.equals("kill")){
				dotComsList.remove(dotComToTest);
				break;
			}
		}
		System.out.println(result);
	}

	private void finishGame(){
		System.out.println("所有戰艦都被擊殺");
		if( numOfGuesses <=18){
			System.out.println("你只使用了"+ numOfGuesses +"次數猜中");
		}else{
			System.out.println("你使用了"+ numOfGuesses +"次數猜中");
		}
	}

	public static void main (String[] args){
		DotComBust game = new DotComBust();
		game.setUpGame();
		game.startPlaying();
	}

}
class DotCom{

	private ArrayList<String>locationCells;
	private String name;
	//int[] locationCells;
	//int numOfHits = 0;

	public void setLocationCells(ArrayList<String> loc){
		locationCells = loc;
	}

	public void setName(String n){
		name = n;
	}

	public String checkYourself(String userInput){
		//int guess = Integer.parseInt(stringGuess);
		String result = "miss";
		int index = locationCells.indexOf(userInput);

		if(index >= 0){
			locationCells.remove(index);
			if(locationCells.isEmpty()){
				result = "kill";
				System.out.println("乾的漂亮 你擊敗了" + name + "戰艦");
			}else{
				result = "hit";
			}//close if
		}// close if
		System.out.println("你輸入數字猜中的結果是 : "+result);
		return result;
	}//close method
}//close class

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 promt){
		String inputLine = null;
		System.out.println(promt + " ");

		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){
  		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;
     System.out.println("row: "+ row +"column : " +column);
    temp = String.valueOf(alphabet.charAt(column));

    alphaCells.add(temp.concat(Integer.toString(row)));
    x++;
     System.out.println("temp: "+ temp +"  alphaCells : " +alphaCells);
    System.out.println("每個戰艦具體位置是 "+ x +"= " +alphaCells.get(x-1));
  }
  return alphaCells;
}

}

第五天的簡單simpleDot有一個bug

書中提到了三種解決方案  其實都是說了下數組的弊端,最終解決方案採用了ArrayList,之前我們提到過封裝,java這麼多年過來,已經有人封裝了很多好用的庫,我們不需要重新去寫,直接在巨人肩膀上繼續開發就好,那怎麼引用別人封裝好的包呢?

在文件最上方引入對應的包就可以了 這個書中139頁也有介紹

import java.util.ArrayList;

書中說的很多,ArrayList與數組的區別,跟封裝好的方法,我覺得如果你還不懂,或者想了解更多ArrayList的方法或特性 可以用初級工程師必備技能,萬事用百度。

在140頁的時候 重新提了下需求,要做個7x7的格子,和3間達康公司,每個達康公司佔了3個格子

從書中141頁到145頁都是說跟simpleDot區別有哪些應該改哪些僞代碼,講了很多設計思路,在146-150中將方法實現出來。

在151頁的時候提到了一個運算符知識點,&& || !  這三個比較常用,一定要了解,

154頁開始介紹導包,引用庫。

剩下的就課後作業花點時間坐會,還有就是把我那個最終代碼運行起來 運行結果如下圖

可能代碼有點複雜,但是我覺得可讀性還是有的,你們看下能看懂多少,哪些變量對應是什麼,不懂得可以把變量值打印到控制檯出來,如果實在看不懂頁沒關係,能讀多少是多少,不要氣餒。

總結下知識點 ArrayList 導包 java運算符

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章