新しく作成したオブジェクトをグリッドとグラフに追加しようとしています。特に、for ループを使用してノードをグラフに効率的に追加する方法。初期ビューを更新するために、グリッドは二重配列として設定されています。(オブジェクト グリッドはモデルであり、プッシュによってビューを更新します)。また、オブジェクトのキーを定義するために、for ループ内で i と j のアイテムを使用して HashMap を設定します。ただし、後でノードからノードへの最短経路を計算するためにグラフを設定するには、これらのノードをグラフに追加する必要があります。最短経路を計算するためにジクストラのアルゴリズムを使用したいと思います。条件付きステートメントを作成して、グリッド内のコーナー ノードとエッジ ノードを定義し、双方向エッジを持つアイテムを定義できますが、これは「長いカット」のようです。
以下は、最初の 2 つの項目を作成する方法 (double 配列と HashMap の作成) に関するコンストラクター コードです。
// **Constructor
// Construct a new Grid object. Sets up a HashMap of square object in order efficiently to get
// and add Square objects later.
public ObjectGrid(Integer width, Integer height)
{
// View
gui = new GUI(); // Instantiate GUI
boardView = new BoardView(width,height); // Instantiate BoardView
// Initialize Gui, set time and add simulation event listener to model (ObjectGrid)
gui.initGUI(BoardView);
gui.addSimulationEventListener(this);
// Initialize turnInSteps variable count to 0
turnInSteps = 0;
// Initialize numberOfDays variable count to 0
numberOfDays = 1;
// Instantiate HashMap
objectHashMap = new HashMap();
// Declare new object grid using double array of type objects.
// Size determined by parameter Integer values in constructor
myObjectGrid = new ObjectType[width][height];
// Instantiate Graph object
Graph graph = new ListGraph();
// For loop sets up the initial grid with ObejctType using a double array. After
// the completion of this loop, the grid will have XXX objects in the grid
// each with a reference to an object. Objects are also added
// to HashMap using coordinates as keys and square objects as values.
for(int i = 0; i < width; i++)
{
// Iterate through rows
for(int j = 0; j < height; j++)
{
// Iterate through columns
myObjectGrid[i][j] = new ObjectType(); // Instantiate a new Square at each row/column using default constructor
gridView.addObjectView(myObjectGrid[i][j].getObjectView(), i, j); // Add object view to each row/column placement
String hashMapKey = (i + ", " + j); // Use values from i and j as Key for objects HashMap
myObjectGrid[i][j].setID(hashMapKey); // Add ID's for each ObjectView to display in object using values from i and j
objectHashMap.add(hashMapKey, myObjectGrid[i][j]); // Add object to HashMap using string value of coordinates as key
listGraph.add(myObjectGrid[i][j]);
// Pseudo code
if (i != (width-height) && (j != height) etc)
{
listGraph.addBidirectionalEdge(mySquareGrid[i][j], (mySquareGrid[i][j+1]), 1);
listGraph.addBidirectionalEdge(mySquareGrid[i][j], (mySquareGrid[i+1][j]), 1);
listGraph.addBidirectionalEdge(mySquareGrid[i][j], (mySquareGrid[i+1][j+1]), 1);
}
}
}
}