fork(1) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #define NOMINMAX
  5. #include <Windows.h>
  6.  
  7. //Question by ttp://toro.2ch.net/test/read.cgi/tech/1354393458/909-910
  8.  
  9. //License is GPLv3
  10. //if need chenge license to fish me.
  11. //this is Dev for FillField Algorithm for me.
  12. //dev on windows!
  13. //need more debug!!!!!
  14.  
  15. //oh this puzzle algorithm license is i dont know.fish and tell to auther!
  16.  
  17. static const int ScreenWidth = 80;
  18. static const int ScreenHeight = 20;
  19. static const int Width = 3;
  20. static const int Height = 2;
  21. static const int FireValue = 6;
  22. static const int FirstValue = 7;
  23.  
  24. enum class MoveState{
  25. None,
  26. MoveRight,
  27. MoveLeft,
  28. MoveUp,//コンソールで開発してる関係で2次元配列的にマイナス側が上
  29. MoveDown,//MoveUpの逆側
  30. DrawRight,
  31. DrawLeft,
  32. DrawUp,
  33. DrawDown,
  34. Wait,
  35. End,
  36. GiveUp,
  37. Confuse,
  38. };
  39. namespace Console{//環境依存部分!
  40. bool MoveCursor(int X, int Y){
  41. COORD Pos = { X, Y };
  42. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
  43. return true;
  44. }
  45. bool Clear(char(&Screen)[ScreenHeight][ScreenWidth], int X, int Y, int Width = 16, int Height = 16){
  46. Console::MoveCursor(X, Y);
  47. for (int i = Y; i < Y + Height; i++){
  48. Console::MoveCursor(X, i);
  49. for (int j = X; j < X + Width; j++){
  50. std::cout << ' ';
  51. }
  52. }
  53. Console::MoveCursor(X, Y);
  54. return true;
  55. }
  56. void Wait(DWORD Time){
  57. Sleep(Time);
  58. }
  59. }
  60. bool Fill(char (&Screen)[ScreenHeight][ScreenWidth],char Ch=' '){
  61. for (int i = 0; i < ScreenHeight; i++){
  62. for (int j = 0; j < ScreenWidth; j++){
  63. Screen[i][j] = Ch;
  64. }
  65. }
  66. return true;
  67. }
  68.  
  69. bool ShowField(char (&Screen)[ScreenHeight][ScreenWidth],int Width,int Height){
  70. int w = std::max(std::min(Width, ScreenWidth), 0);
  71. int h = std::max(std::min(Height, ScreenHeight), 0);
  72.  
  73. Console::MoveCursor(0, 0);
  74. for (int i = 0; i < h; i++){
  75. Console::MoveCursor(0, i);
  76. for (int j = 0; j < w; j++){
  77. if (Screen[i][j] == 0) std::cout << 'o';
  78. if (Screen[i][j] == 1) std::cout << '1';
  79. if (Screen[i][j] == 2) std::cout << '2';
  80. if (Screen[i][j] == 3) std::cout << '3';
  81. if (Screen[i][j] == 4) std::cout << '4';
  82. if (Screen[i][j] == 5) std::cout << '5';
  83. if (Screen[i][j] == FireValue) std::cout << 'F';
  84. if (Screen[i][j] == FirstValue) std::cout << '+';
  85. }
  86. }
  87. return true;
  88. }
  89. bool ShowPlayer(int PX,int PY){
  90. int X = std::max(std::min(PX, ScreenWidth), 0);
  91. int Y = std::max(std::min(PY, ScreenHeight), 0);
  92.  
  93. Console::MoveCursor(X, Y);
  94. std::cout << 'P';
  95. return true;
  96. }
  97. bool ProcessCoolDown(char (&Screen)[ScreenHeight][ScreenWidth],int Width,int Height){
  98. int w = std::max(std::min(Width, ScreenWidth), 0);
  99. int h = std::max(std::min(Height, ScreenHeight), 0);
  100.  
  101. for (int i = 0; i < h; i++){
  102. for (int j = 0; j < w; j++){
  103. if (Screen[i][j] == 0) continue;
  104. if (Screen[i][j] < FirstValue) Screen[i][j]--;
  105. }
  106. }
  107. return true;
  108. }
  109.  
  110. bool IsClear(char(&Screen)[ScreenHeight][ScreenWidth], int Width, int Height){
  111. int w = std::max(std::min(Width, ScreenWidth), 0);
  112. int h = std::max(std::min(Height, ScreenHeight), 0);
  113.  
  114. for (int i = 0; i < h; i++){
  115. for (int j = 0; j < w; j++){
  116. if (Screen[i][j] == FirstValue) return false;
  117. }
  118. }
  119. return true;
  120. }
  121.  
  122. bool ProcessOne(MoveState MS, char(&Screen)[ScreenHeight][ScreenWidth], int& Width, int& Height, int& PX, int& PY){
  123.  
  124. int X = PX;
  125. int Y = PY;
  126.  
  127. switch (MS)
  128. {
  129. case MoveState::MoveRight:
  130. if (X < Width)X++;
  131. if (!((Screen[Y][X]>0) && (Screen[Y][X] <= FireValue))) PX = X;
  132. return true;
  133. break;
  134. case MoveState::MoveLeft:
  135. if (X > 0 ) X--;
  136. if (!((Screen[Y][X]>0) && (Screen[Y][X] <= FireValue))) PX = X;
  137. return true;
  138. break;
  139. case MoveState::MoveUp:
  140. if (Y > 0) Y--;
  141. if (!((Screen[Y][X]>0) && (Screen[Y][X] <= FireValue))) PY = Y;
  142. return true;
  143. break;
  144. case MoveState::MoveDown:
  145. if (Y < Height) Y++;
  146. if (!((Screen[Y][X]>0) && (Screen[Y][X] <= FireValue))) PY = Y;
  147. break;
  148. case MoveState::DrawRight:
  149. if (X + 1 < Width) Screen[Y][X + 1] = FireValue;
  150. break;
  151. case MoveState::DrawLeft:
  152. if (X - 1 >= 0) Screen[Y][X - 1] = FireValue;
  153. break;
  154. case MoveState::DrawUp:
  155. if (Y - 1 >= 0) Screen[Y - 1][X] = FireValue;
  156. break;
  157. case MoveState::DrawDown:
  158. if (Y+1<Height) Screen[Y + 1][X] = FireValue;
  159. break;
  160. case MoveState::Wait:
  161. return true;
  162. break;
  163. case MoveState::End:
  164. return true;
  165. break;
  166. case MoveState::GiveUp:
  167. return true;
  168. break;
  169. case MoveState::Confuse://未対応
  170. return true;
  171. break;
  172. default:
  173. break;
  174. }
  175.  
  176. return false;
  177. }
  178. MoveState Think_by_hand(char(&Screen)[ScreenHeight][ScreenWidth], int Width, int Height, int PX, int PY){
  179. static int i = 0;
  180. //std::vector<MoveState> Vec{MoveState::Wait, MoveState::MoveRight, MoveState::MoveLeft, MoveState::MoveDown, MoveState::MoveUp ,MoveState::End};
  181. std::vector<MoveState> Vec{MoveState::DrawDown, MoveState::MoveRight, MoveState::DrawLeft, MoveState::DrawRight, MoveState::MoveDown ,MoveState::DrawUp,MoveState::DrawRight,MoveState::MoveLeft,MoveState::DrawRight,MoveState::End};
  182. return Vec[i++];
  183. }
  184.  
  185. MoveState Think(char(&Screen)[ScreenHeight][ScreenWidth], int Width, int Height, int PX, int PY){
  186. MoveState MS = MoveState::End;
  187.  
  188. //Write Code Here...
  189. //this is example.
  190. //MS = Think_by_hand(Screen, Width, Height, PX, PY);
  191.  
  192. return MS;
  193. }
  194. int Process(char (&Screen)[ScreenHeight][ScreenWidth],int Width,int Height,int PX, int PY){
  195. int X = PX, Y = PY;
  196. Fill(Screen, FirstValue);
  197. int Count = 0;
  198. MoveState MS = MoveState::None;
  199.  
  200. while (MS != MoveState::End){
  201.  
  202. MS = Think(Screen, Width, Height, X, Y);
  203. ProcessOne(MS, Screen, Width, Height, X, Y);
  204. ProcessCoolDown(Screen, Width, Height);
  205. ShowField(Screen, Width, Height);
  206. ShowPlayer(X, Y);
  207. Console::Wait(1000);
  208. if (MS == MoveState::End) goto End;//break の 代わりにgoto使ってる。今回は開発効率の問題で・・・。Orz
  209.  
  210. Count++;
  211.  
  212. if (MS == MoveState::GiveUp) goto End;
  213. if (MS == MoveState::Confuse) goto End;
  214.  
  215. }
  216. End:
  217.  
  218. return Count;
  219. }
  220.  
  221. int main(){
  222. char Screen[ScreenHeight][ScreenWidth] = { 0, };
  223. int X = 0, Y = 0;
  224.  
  225. Fill(Screen, FirstValue);
  226. Console::Clear(Screen, 0, 0, ScreenWidth, ScreenHeight);
  227.  
  228. ShowField(Screen, Width, Height);
  229. ShowPlayer(X, Y);
  230.  
  231. int C = Process(Screen,Width,Height,X, Y);
  232.  
  233. Console::MoveCursor(0, Height + 1);
  234. bool F = IsClear(Screen, Width, Height);
  235. char *str[] = { "This is Valid!", "This is Invalid..." };
  236. std::cout << "I use " << C << " Steps!" << std::endl << (F ? str[0] : str[1]) << std::endl;
  237.  
  238. return 0;
  239. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:5:21: fatal error: Windows.h: No such file or directory
 #include <Windows.h>
                     ^
compilation terminated.
stdout
Standard output is empty