#7
by かずま » 8年前
C++ に書き直してみました。
コード:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdlib>
using namespace std;
class CELL {
public:
int pram;
int x;
int y;
};
enum DIR { DIR_LEFT, DIR_RIGHT, DIR_UP, DIR_DOWN };
vector<vector<int>> stageMapDatas = {
{ 1, 2, 2, 2, 2 },
{ 0, 0, 0, 0, 2 },
{ 0, 3, 0, 0, 2 },
{ 2, 0, 0, 0, 0 },
{ 2, 2, 2, 2, 2 },
};
struct DEBUG {
void Log(const char *s1, int x, const char *s2, int y, const char *s3, DIR dir) {
cout << s1 << x << s2 << y << s3 << dir << endl;
}
void Log(const char *s1, int x, const char *s2, int y) {
cout << s1 << x << s2 << y << endl;
}
void Log(const char *s) {
cout << s << endl;
}
} Debug;
CELL CheckCell(int x, int y, DIR dir)
{
// Cellの初期化
CELL& cell = *(new CELL());
cell.x = -1;
cell.y = -1;
Debug.Log ("cc ", x, ", ", y, ": ", dir);
// 範囲外
if(x < 0 || x >= stageMapDatas[0].size()){
cell.x = x;
cell.y = y;
Debug.Log ("step 1 ", cell.x, ",", cell.y);
return cell;
}
if(y < 0 || y >= stageMapDatas.size()){
cell.x = x;
cell.y = y;
Debug.Log ("step 2 ", cell.x, ",", cell.y);
return cell;
}
// 判定
if(stageMapDatas[y][x] > 0){
cell.x = x;
cell.y = y;
Debug.Log ("step 3 ", x, ",", y);
return cell;
}else{
int xx = x;
int yy = y;
switch (dir) {
case DIR::DIR_LEFT:
xx -= 1;
break;
case DIR::DIR_RIGHT:
xx += 1;
break;
case DIR::DIR_UP:
yy -= 1;
break;
case DIR::DIR_DOWN:
yy += 1;
break;
}
CheckCell(xx, yy, dir);
Debug.Log ("step 4 ", cell.x, ",", cell.y);
return cell; // ここが再帰回数分呼ばれてしまいます。
}
Debug.Log ("step 5 ", cell.x, ",", cell.y);
return cell;
}
// メインの判定部分 cellには検索の基準点を保存
int main()
{
int diffX = 0, diffY = -1;
CELL cell = { 0 };
CELL check;
if (abs(diffX) > abs(diffY)) {
if (diffX > 0) {
check = CheckCell (cell.x+1, cell.y, DIR::DIR_RIGHT);
Debug.Log ("→");
} else {
check = CheckCell (cell.x-1, cell.y, DIR::DIR_LEFT);
Debug.Log ("←");
}
} else {
if (diffY > 0) {
check = CheckCell (cell.x, cell.y-1, DIR::DIR_UP);
Debug.Log ("↑");
} else {
check = CheckCell (cell.x, cell.y+1, DIR::DIR_DOWN);
Debug.Log ("↓");
}
}
Debug.Log ("", check.x, ",", check.y);
}
実行結果
コード:
cc 0, 1: 3
cc 0, 2: 3
cc 0, 3: 3
step 3 0,3
step 4 -1,-1
step 4 -1,-1
↓
-1,-1
書いたとおりに動いています。
C++ に書き直してみました。
[code]
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdlib>
using namespace std;
class CELL {
public:
int pram;
int x;
int y;
};
enum DIR { DIR_LEFT, DIR_RIGHT, DIR_UP, DIR_DOWN };
vector<vector<int>> stageMapDatas = {
{ 1, 2, 2, 2, 2 },
{ 0, 0, 0, 0, 2 },
{ 0, 3, 0, 0, 2 },
{ 2, 0, 0, 0, 0 },
{ 2, 2, 2, 2, 2 },
};
struct DEBUG {
void Log(const char *s1, int x, const char *s2, int y, const char *s3, DIR dir) {
cout << s1 << x << s2 << y << s3 << dir << endl;
}
void Log(const char *s1, int x, const char *s2, int y) {
cout << s1 << x << s2 << y << endl;
}
void Log(const char *s) {
cout << s << endl;
}
} Debug;
CELL CheckCell(int x, int y, DIR dir)
{
// Cellの初期化
CELL& cell = *(new CELL());
cell.x = -1;
cell.y = -1;
Debug.Log ("cc ", x, ", ", y, ": ", dir);
// 範囲外
if(x < 0 || x >= stageMapDatas[0].size()){
cell.x = x;
cell.y = y;
Debug.Log ("step 1 ", cell.x, ",", cell.y);
return cell;
}
if(y < 0 || y >= stageMapDatas.size()){
cell.x = x;
cell.y = y;
Debug.Log ("step 2 ", cell.x, ",", cell.y);
return cell;
}
// 判定
if(stageMapDatas[y][x] > 0){
cell.x = x;
cell.y = y;
Debug.Log ("step 3 ", x, ",", y);
return cell;
}else{
int xx = x;
int yy = y;
switch (dir) {
case DIR::DIR_LEFT:
xx -= 1;
break;
case DIR::DIR_RIGHT:
xx += 1;
break;
case DIR::DIR_UP:
yy -= 1;
break;
case DIR::DIR_DOWN:
yy += 1;
break;
}
CheckCell(xx, yy, dir);
Debug.Log ("step 4 ", cell.x, ",", cell.y);
return cell; // ここが再帰回数分呼ばれてしまいます。
}
Debug.Log ("step 5 ", cell.x, ",", cell.y);
return cell;
}
// メインの判定部分 cellには検索の基準点を保存
int main()
{
int diffX = 0, diffY = -1;
CELL cell = { 0 };
CELL check;
if (abs(diffX) > abs(diffY)) {
if (diffX > 0) {
check = CheckCell (cell.x+1, cell.y, DIR::DIR_RIGHT);
Debug.Log ("→");
} else {
check = CheckCell (cell.x-1, cell.y, DIR::DIR_LEFT);
Debug.Log ("←");
}
} else {
if (diffY > 0) {
check = CheckCell (cell.x, cell.y-1, DIR::DIR_UP);
Debug.Log ("↑");
} else {
check = CheckCell (cell.x, cell.y+1, DIR::DIR_DOWN);
Debug.Log ("↓");
}
}
Debug.Log ("", check.x, ",", check.y);
}
[/code]
実行結果
[code=text]
cc 0, 1: 3
cc 0, 2: 3
cc 0, 3: 3
step 3 0,3
step 4 -1,-1
step 4 -1,-1
↓
-1,-1
[/code]
書いたとおりに動いています。