ETrobocon2017 - 片山研究所モデルベース開発推進事業部
PuzzleExplorer.cpp
[詳解]
1 
2 #include "PuzzleExplorer.h"
3 
4 // コンストラクタ
6 {
7  myPosition = 0;
8 }
9 
10 // デストラクタ
12 
13 // ブロック並べエリアのマップ、ブロックの初期配置などの初期設定
14 void PuzzleExplorer::init(int *blockPositions)
15 {
16 
17  //機体初期位置10
18  setMyPosition(10);
19 
20  // ノードの設定
21  setNodes();
22 
23  //ブロックの位置設定
24  setBlocks(blockPositions);
25 
26  // 隣接ノード設定
27  setNeighborNode();
28 
29 }
30 
31 // 現在地の変更
33 {
34  myPosition = num;
35  return;
36 }
37 
38 // ノードの設定(位置番号、色、位置座標)
39 void PuzzleExplorer::setNodes()
40 {
41  for(int i=0; i<16; i++)
42  {
43  Node node;
44  node.setNum(i);
45  node.setColor();
46  node.setPosition(nodePositionList[i][0], nodePositionList[i][1]);
47  nodeList[i] = node;
48 
49  }
50 }
51 
52 // ブロック設定
53 void PuzzleExplorer::setBlocks(int *blockPositions)
54 {
55  for(int i=0; i<5; i++)
56  {
57  Block block(blockPositions[i]);
58  blockList[i] = block;
59 
60  // ブロックを持つノード設定
61  nodeList[blockPositions[i]].setHasBlock(true);
62  }
63 
64  blockList[0].setColor(BlockColor::Black);
65  blockList[1].setColor(BlockColor::Red);
66  blockList[2].setColor(BlockColor::Yellow);
67  blockList[3].setColor(BlockColor::Blue);
68  blockList[4].setColor(BlockColor::Green);
69 
70  return;
71 }
72 
73 // 隣接ノードの設定
74 void PuzzleExplorer::setNeighborNode()
75 {
76  //nodeListから隣接ノードを取得
77  for(int i=0; i<16;i++)
78  {
79  Node* neighbor[5] = {};
80 
81  for(int j=0; j<5; j++)
82  {
83  // 隣接ノード番号が-1の時(隣接ノードがないとき)nullポインタを入れる
84  if(neighborList[i][j] == -1)
85  {
86  neighbor[j] = nullptr;
87  continue;
88  }
89  Node* node = &nodeList[neighborList[i][j]];
90  neighbor[j] = node;
91  }
92 
93  nodeList[i].setNeighbor(neighbor);
94  neighbor[5] = {};
95  }
96  return;
97 }
98 
99 // 位置番号からノードの色を返す
101 {
102  return nodeList[num].getColor();
103 }
104 
105 // 現在地の隣接ノード取得
107 {
108  return nodeList[myPosition].getNeighbor();
109 }
110 
111 // 現地点から最寄りのブロックの位置を返す
113 {
114  Node node = nodeList[myPosition];
115  queue<Node> que;
116 
117  // ブロックを持つノードが見つかるまで幅優先探索
118  while(!node.getHasBlock())
119  {
120  Node** nodes = node.getNeighbor();
121 
122  // 隣接ノードをキューに格納
123  for(int i=0; i<5; i++){
124 
125  // 隣接ノードがnullの時は飛ばす
126  if(nodes[i] == nullptr)continue;
127 
128  que.push(*nodes[i]);
129  }
130 
131  node = que.front();
132 
133  // キューの先頭がブロックを持っていたら終了
134  if(que.front().getHasBlock())
135  {
136  break;
137  }
138 
139  // 先頭要素の削除
140  que.pop();
141  }
142 
143  return node.getNum();
144 }
145 
146 // 2つのノード間の経路探索
147 int* PuzzleExplorer::getRoot(int startNode, int goalNode)
148 {
149  Node node = nodeList[startNode];
150  Node goal = nodeList[goalNode];
151  int i=0;
152 
153  // rootに経路を格納
154  root[i] = node.getNum();
155  i++;
156 
157  // ゴールノードに到着するまで探索
158  while(node.getNum() != goal.getNum())
159  {
160  int cost = 1000;
161  Node** explorerNodes = node.getNeighbor();
162  Node nextNode;
163 
164  // 隣接ノードからゴールまでのコストが低いノードを次の探索ノードに選択
165  for(int j=0; j<5; j++)
166  {
167  //隣接ノードがnullの時飛ばす
168  if(explorerNodes[j] == nullptr)continue;
169 
170  // ゴールノードまでの距離コストが小さければ更新
171  int neighborCost = getCost(*explorerNodes[j], goal);
172  if(cost > neighborCost)
173  {
174  cost = neighborCost;
175  nextNode = *explorerNodes[j];
176  }
177  }
178 
179  root[i] = nextNode.getNum();
180  node = nextNode;
181  i++;
182  }
183 
184  return root;
185 }
186 
187 // ノード間の距離コストを計算
189 {
190  int cost = (node1.getPositionX() - node2.getPositionX())*(node1.getPositionX() - node2.getPositionX()) + (node1.getPositionY() - node2.getPositionY())*(node1.getPositionY() - node2.getPositionY());
191 
192  return cost;
193 }
void setPosition(int x, int y)
Definition: Node.cpp:85
void setMyPosition(int num)
int getCost(Node node1, Node node2)
Definition: Node.h:9
int getPositionX()
Definition: Node.cpp:91
Node ** getNeighbor()
Definition: Node.cpp:57
void setHasBlock(bool exists)
Definition: Node.cpp:75
BlockColor
Definition: BlockColor.h:5
void init(int *blockPositions)
BlockColor getColor()
Definition: Node.cpp:69
bool getHasBlock()
Definition: Node.cpp:80
void setColor()
Definition: Node.cpp:25
int getPositionY()
Definition: Node.cpp:96
void setColor(BlockColor blockColor)
Definition: Block.cpp:19
Definition: Block.h:9
Node ** getMyNeighbor()
BlockColor getNodeColor(int num)
void setNum(int num)
Definition: Node.cpp:20
void setNeighbor(Node **nodes)
Definition: Node.cpp:47
int * getRoot(int startNode, int goalNode)
int getNum()
Definition: Node.cpp:63
int getNearestBlockPosition()