2017-02-22 5 views
-2

DotInfo.javaというクラスがあります。このクラスには、3つのパラメータを持つコンストラクタがあります。ゲームにドットを格納するための配列を作成することに混乱しています。

DotInfo(int x, int y, int color) { 
    ... 
} 

GameModel.javaという別のクラスがあります。 DotInfoのインスタンスを配列に格納する方法を理解しようとしています。また、私はボードに円を置くことによってxとyを増やす必要があるか分からない。誰も助けることができますか?

これは私のDotInfoクラスです:

/** 
* The class <b>DotInfo</b> is a simple helper class to store the initial color and state 
* (captured or not) at the dot position (x,y) 
*/ 

public class DotInfo { 


private int x; 
private int y; 
private int color; 
private boolean captured; 


/** 
* Constructor 
* 
* @param x 
*   the x coordinate 
* @param y 
*   the y coordinate 
* @param color 
*   the initial color 
*/ 
public DotInfo(int x, int y, int color){ 
    this.x = x; 
    this.y = y; 
    this.color = color; 
    captured = false; 
} 

/** 
* Getter method for the attribute x. 
* 
* @return the value of the attribute x 
*/ 
public int getX(){ 
    return x; 
} 

/** 
* Getter method for the attribute y. 
* 
* @return the value of the attribute y 
*/ 
public int getY(){ 
    return y; 
} 


/** 
* Setter for captured 
* @param captured 
*   the new value for captured 
*/ 
public void setCaptured(boolean captured) { 
    this.captured = captured; 
} 

/** 
* Get for captured 
* 
* @return captured 
*/ 
public boolean isCaptured(){ 
    return captured; 
} 

/** 
* Get for color 
* 
* @return color 
*/ 
public int getColor() { 
    return color; 
} 

} 

これは私のGameModel.javaクラスです:

import java.util.Random; 

/** 
* The class GameModel holds the model, the state of the systems. 
* It stores the followiung information: 
* - the state of all the ``dots'' on the board (color, captured or not) 
* - the size of the board 
* - the number of steps since the last reset 
* - the current color of selection 
* 
* The model provides all of this informations to the other classes through 
* appropriate Getters. 
* The controller can also update the model through Setters. 
* Finally, the model is also in charge of initializing the game 
*/ 
public class GameModel { 


/** 
* predefined values to capture the color of a DotInfo 
*/ 
public static final int COLOR_0   = 0; 
public static final int COLOR_1   = 1; 
public static final int COLOR_2   = 2; 
public static final int COLOR_3   = 3; 
public static final int COLOR_4   = 4; 
public static final int COLOR_5   = 5; 
public static final int NUMBER_OF_COLORS = 6; 

private DotInfo[][] dots; 
private int size; 
private Random generator; 

/** 
* Constructor to initialize the model to a given size of board. 
* 
* @param size 
*   the size of the board 
*/ 
public GameModel(int size) { 
    this.size = size; 
    dots = new DotInfo[size][size]; 
} 


/** 
* Resets the model to (re)start a game. The previous game (if there is one) 
* is cleared up . 
*/ 
public void reset(){ 
    dots = new DotInfo[size]; 
    for (int i=0;i<dots.length;i++) { 
     for (int j=0;j<dots[i].length;j++) { 
      dots[i][j] = 
     } 
    } 
} 


/** 
* Getter method for the size of the game 
* 
* @return the value of the attribute sizeOfGame 
*/ 
public int getSize(){ 
    return size; 
} 

/** 
* returns the current color of a given dot in the game 
* 
* @param i 
*   the x coordinate of the dot 
* @param j 
*   the y coordinate of the dot 
* @return the status of the dot at location (i,j) 
*/ 
public int getColor(int i, int j){ 
    dot = new DotInfo(i, j, COLOR_0); 
    return dot.getColor(); 
} 

/** 
* returns true is the dot is captured, false otherwise 
* 
* @param i 
*   the x coordinate of the dot 
* @param j 
*   the y coordinate of the dot 
* @return the status of the dot at location (i,j) 
*/ 
public boolean isCaptured(int i, int j){ 
    dot = new DotInfo(i, j, COLOR_0); 

} 

/** 
* Sets the status of the dot at coordinate (i,j) to captured 
* 
* @param i 
*   the x coordinate of the dot 
* @param j 
*   the y coordinate of the dot 
*/ 
public void capture(int i, int j){ 

//ここ

}

/** 
* Getter method for the current number of steps 
* 
* @return the current number of steps 
*/ 
public int getNumberOfSteps(){ 

をあなたのコードを追加します。 //ここにコードを追加してください

} 

/** 
* Setter method for currentSelectedColor 
* 
* @param val 
*   the new value for currentSelectedColor 
*/ 
public void setCurrentSelectedColor(int val) { 

//ここにコード

} 

/** 
* Getter method for currentSelectedColor 
* 
* @return currentSelectedColor 
*/ 
public int getCurrentSelectedColor() { 

//が

} 

/** ここにコードを追加します//ここにコード

} 


/** 
* Getter method for the model's dotInfo reference 
* at location (i,j) 
* 
    * @param i 
*   the x coordinate of the dot 
* @param j 
*   the y coordinate of the dot 
* 
* @return model[i][j] 
*/ 
public DotInfo get(int i, int j) { 

を追加* metod ステップはステップ数を更新します。支払人が新しい色を選択した後にモデルが更新されたら、それは *と呼ばれなければなりません。 */ ます。public voidステップ(){

//は、 *であることを、

} 

/** は* metod が終了したゲームの場合はtrueリターンをisFinishedここにコードを追加します。すべてのデータがキャプチャされます。 * * @return真のゲームが終了した場合は、そうでない場合はfalse */ 公共ブールはisFinished(){

//ここに/ **

} 

をあなたのコードを追加するには、*文字列を作成しますモデルのモデル * * @return String表現の表現*/ パブリック文字列のtoString(){

// YOUR Cを追加ODE HERE

} 

}

+0

で動作します;' –

+0

私は残念ながら –

+0

をリストを使用することはできませんので、これは宿題であることがありますか? –

答えて

0

私はあなたの問題がある正確に理解していないが、私はとにかく答えをしてみてください。私は洪水のために最高のゲームは二次元配列であると思う。だから、GameModelに、あなたは

DotInfo[][] dotInfos = new DotInfos[ROWS][COLS]; 

やループのための2つでそれを埋めるよりも、持っている:

for (int y = 0; y < ROWS; y++) { 
    for (int x = 0; x < COLS; x++) { 
     int color = ...; // Some random color 
     dotInfos[y][x] = new DotInfo(x, y, color); 
    } 
} 

をしかし、あなたは二つの場所で、xとyの情報を保存しますので、私はDotInfoクラスなしで動作することを示唆しています - DotInfoクラスとdotInfosのインデックス。だから私は `一覧 リスト=新しいArrayListを を使用することを検討してちょうど

int[][] colors = new int[ROWS][COLS]; 

for (int y = 0; y < ROWS; y++) { 
    for (int x = 0; x < COLS; x++) { 
     colors[y][x] = ...; // Some random color 
    } 
} 
+0

ありがとうございました!私は最初の方法を考えていたが、それを理解できなかった –

関連する問題