2014-04-22 88 views
0

我應該用Java編寫一些遊戲戰艦的方法嗎?不知道它是如何被稱爲英文。矢量,點,戰列艦,2D

我們將創建方法:

private static Point[] createSchiff(int laenge) 

與長度laenge,在球場上和隨機方向的任意位置創建一個船,並返回船的參考。

船舶應爲Point,我們將使用物體Vector作爲我們創建的所有船舶的集裝箱。

全programmcode我至今是:

import java.awt.*; 
import java.awt.Point; 
import java.lang.*; 
import java.util.*; 
public class Programm34 { 
    public int anzahlschiffe=5; 
    public Point[] schiffe = new Point[anzahlschiffe]; 
    public static void main(String[]args){ 



     Vector v=new Vector(); 

     int i=10; 
     double random=0; 
     System.out.println("Zufallszahlen: "); 
     for(i=anzahlschiffe;i>0;i--){ 
      random=random(0,10); 
      System.out.print((int)random+" "); 
      System.out.println(""); 
     } //Test für random Methode!! 
     int[][] Spielfeld= new int[10][10]; 



     init(Spielfeld); 
     for(int x=9;x>=0;x--){ 
      for(int y=9;y>=0;y--){ 
       System.out.print(Spielfeld[x][y]); 
       if(y!=0){ 
        System.out.print(" "); 
       } 
       if(y==0 && x!=0){ 
        System.out.print("\n"); 
       } 
      } 
     } 



    } 




    private static long random(int u, int o){ 
     double random; 
     random=Math.random()*(u-o)+o; 
     return (int)random; 
    } 

    private static Point[] createSchiff(int laenge){ 

     point(1,2); 


    } 

    private static boolean positionOk(Vector<Point[]> schiffe,Point[] schiff){ 

    } 

    private static boolean grenze(Point[] schiff){ 

    } 

    private static boolean konflikt(Vector<Point[]> schiffe,Point[] schiff){ 

    } 

    private static boolean nachbar(Vector<Point[]> schiffe,Point[] schiff){ 

    } 

    private static boolean increment(Vector<Point[]> schiffe,int laenge){ 

    } 

    private static void init(int[][] spielfeld){ 
     for(int i=9;i>=0;i--){ 
      for(int j=9;j>=0;j--){ 
       spielfeld[i][j]=0; 
      } 
     } 
    } 

    private static void add(int[][] spielfeld, Vector<Point[]> schiffe){ 

    } 
} 

所有這些方法和主要應進行編程,但什麼事情北京時間的createschiff(「createship」)之一。

你能幫我解釋一下這個矢量嗎?點?以及整個東西是如何工作的?我現在坐在這裏2小時沒有進展... thx很多

+1

我無法理解這一點。即使標題不提供信息。投票結束。 – Leri

+0

什麼不清楚?我盡力解釋我的問題。我將創建一個創建一艘船的方法,這艘船應該來自包含在Vector中的類型Point。我尋求幫助,而你只是關閉它?多數民衆贊成在... – user3556093

+0

@ user3556093:你沒有解釋你想要做什麼,你已經做了什麼,爲什麼它不是你想要的。 –

回答

1

首先一個小的(但有幫助)的事情:你應該添加泛型參數向量。 (我懷疑這是需要使用Vector一個List應該是足夠的,但是當它在分配時,你必須這樣做......。)

Vector<Point[]> v=new Vector<Point[]>(); 

關於createSchiff方法:你必須創建幾個Point對象。也就是說,對於船上所覆蓋的每個領域都有一個。這可以大致是這樣的:

private static Point[] createSchiff(int laenge) 
{ 
    int positionX = ... // Some random position 
    int positionY = ... // Some random position 

    // Create the array that will contain the fields 
    // covered by the ship: 
    Point result[] = new Point[laenge]; 

    // Fill the array 
    for (int i=0; i<laenge; i++) 
    { 
     result[i] = new Point(positionX, positionY); 

     // Change the position for the next field, 
     // depending on the direction of the ship: 
     positionX += ... 
     positionY += ... 
    } 

    return result; 
} 

然後,你可以調用這個方法是這樣的:

Point ship[] = createSchiff(3); 
v.add(ship); 

(我可以插入,而不是佔位符...更實際的代碼,但你應該試試這個在您的擁有)

+0

我非常感謝你,我認爲這已經幫助我:) – user3556093