markdown
#說明
前一篇有些操作一些關於 shooting 專案的物件,以及砲彈的移動,這篇則是比較完整的呈現。
- 設定戰場大小
- 設定武器庫位置
- 設定砲台位置
- 設定砲彈速度
- 砲彈移動
- 判斷有沒有擊中武器庫 
- if 擊中結束 else 沒有擊中就繼續移動直到超出戰場
- 遊戲結束
#CODE
```
package shooting;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Igloo {//武器庫
 int x,y;
 
 //進行爆炸的動作
 void exploding() {
  System.out.println("在 ( " +x + "," + y + ") 的武器庫被摧毀");
  
 }
}
class Cannonball { //砲彈
 int x,y ;//砲彈位置
 int stepX, stepY; //砲彈移動速度及方向
 
 void move() { //依據移動速度換到下一位置
  x+= stepX;
  y+= stepY;
  System.out.println("砲彈移到(" + x + "," + y + ")");
 }
}
class Flanker { //砲台
 int x,y;//砲台位置
 
 Cannonball fire(int stepX, int stepY ) {
  Cannonball c =new Cannonball() ;
  c.stepX= stepX;
  c.stepY =stepY;
  c.x =x;
  c.y =y;
  return c;
 }
}
class Battlefield {// 戰場
 int width,height; //戰場大小
 
 Igloo i ; //武器庫
 
 Flanker f ; // 砲台
 Cannonball c ; //砲彈
 
 boolean hitIgloo(Cannonball c ) { // 是否擊中武器庫
  if ((c.x==i.x ) && (c.y==i.y)) {
   //擊中
   i.exploding(); //武器庫爆炸
   i= null ; // 清除武器庫
   return true; //傳回擊中了
   
  }
  return false; //  傳回沒有擊中  
 }
 
 boolean outOfField(Cannonball c) {
  if (c.x <0 ||  c.y < 0 ) // 超過左邊界 或超過上邊界
   return true ; 
  if (c.x >=width || c.y >= height ) //超過右邊界或超過上邊界
   return true ;
  return false;
 }
 }
 
//程式邏輯
//程式開始 (main) > 設定戰場大小 >放置武器庫 
//放置砲台 >發射砲彈 >移動砲彈 >
//如果有擊中就結束遊戲 >沒有擊中就繼續發射直到擊中遊戲結束
 
public class shooting8 {
 public static void main (String [] argv ) throws IOException{
  Battlefield b =new Battlefield(); //產生戰場
  b.width =5 ; //設為 5*5 大小
  b.height=5 ;
  
  // 用來讀取輸入資料
  BufferedReader br = new BufferedReader (new InputStreamReader ( System.in));
 
    // 產生武器庫
  b.i=new Igloo();
  
  // 取得使用者輸入的武器庫位置
  System.out.println("please input Igloo location : ");
  System.out.print("x ->");
  b.i.x =java.lang.Integer.parseInt(br.readLine());
  System.out.print("y -> ");
  b.i.y =java.lang.Integer.parseInt(br.readLine());
  
  // 取得使用者輸入的砲彈位置
  b.f =new Flanker();
  System.out.println("please input flanker location : ");
  System.out.println(" x->");
  b.f.x=java.lang.Integer.parseInt(br.readLine());
  System.out.print("y-> ");
  b.f.y=java.lang.Integer.parseInt(br.readLine());
  
  //取得使用者輸入砲彈的移動速度
  System.out.println("please input velocity of fire");
  System.out.print("x-> ");
  
  int x =java.lang.Integer.parseInt(br.readLine());
  System.out.print("y-> ");
  int y =java.lang.Integer.parseInt(br.readLine());
  
  // 發射砲彈
  b.c =b.f.fire(x, y);
  
  do {
   //移動砲彈
   b.c.move();
   if (b.outOfField(b.c) || b.hitIgloo(b.c))
    b.c =null ; //清除砲彈
  }
  while (b.c != null  );
  System.out.println(" game over! ");
   
  }
```
##debug
一開始操作一直有 bug
```
java.lang.NoClassDefFoundError
```
其實看不太懂這個的意思,蠻不清楚的error,問了 google 大神,有些解答是sdk install 的問題
但我這邊執行其他支 java code 是可以執行的,所以排除這個問題
後來去檢查 code 出現紅字的地方,發現是 沒有import package 
在紅字處按下右鍵去選可以自動 import 需要的 package 
解決這個package 問題就可以執行了
透過這個project 大致學習物件導向的簡單應用
##Demo1: 沒有擊中武器庫
##Demo2 :擊中武器庫
- code : https://github.com/SYkkk55/java_shooting
- refer :https://stackoverflow.com/questions/17973970/how-to-solve-java-lang-noclassdeffounderror
- refer : https://stackoverflow.com/questions/1401111/noclassdeffounderror-could-not-initialize-class-error
留言
張貼留言