Guest User

Untitled

a guest
May 20th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <conio.h>
  6. #include <windows.h>
  7. #include <time.h>
  8. #include <fstream>
  9. #include <iostream>
  10. #include <deque>
  11.  
  12. using namespace std;
  13.  
  14. struct position {
  15. int x;
  16. int y;
  17. int moveno;
  18. };
  19.  
  20. #define MAX_X 77 // maximum x position for player
  21. #define SCROLL_POS 24 // the point that scrolling occurs
  22.  
  23. void Init_Graphics(void);
  24. inline void Set_Color(int fcolor, int bcolor);
  25. inline void Draw_String(int x,int y, char *string);
  26.  
  27. CONSOLE_SCREEN_BUFFER_INFO con_info; // holds screen info
  28.  
  29. HANDLE hconsole; // handle to console
  30.  
  31. int grid[11][11]={0};
  32.  
  33. void Init_Graphics(void)
  34. {
  35. // this function initializes the console graphics engine
  36.  
  37. COORD console_size = {80,30}; // size of console
  38.  
  39. // seed the random number generator with time
  40. srand((unsigned)time(NULL));
  41.  
  42. // open i/o channel to console screen
  43. hconsole=CreateFile(TEXT("CONOUT$"),GENERIC_WRITE | GENERIC_READ,
  44. FILE_SHARE_READ | FILE_SHARE_WRITE,
  45. 0L, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L);
  46.  
  47. // make sure we are in 80x25
  48. SetConsoleScreenBufferSize(hconsole,console_size);
  49.  
  50. // get details for console screen
  51. GetConsoleScreenBufferInfo(hconsole,&con_info);
  52.  
  53. } // end Init_Graphics
  54.  
  55. inline void Set_Color(int fcolor, int bcolor=0)
  56. {
  57. // this function sets the color of the console output
  58. SetConsoleTextAttribute(hconsole,(WORD)((bcolor << 4) |
  59. fcolor));
  60.  
  61. } // Set_Color
  62.  
  63.  
  64. inline void Draw_String(int x,int y, char *string)
  65. {
  66. // this function draws a string at the given x,y
  67.  
  68. COORD cursor_pos; // used to pass coords
  69.  
  70. // set printing position
  71. cursor_pos.X = x;
  72. cursor_pos.Y = y;
  73. SetConsoleCursorPosition(hconsole,cursor_pos);
  74.  
  75. // print the string in current color
  76. printf("%s",string);
  77.  
  78. } // end Draw_String
  79.  
  80. inline void Clear_Screen(void)
  81. {
  82. // this function clears the screen
  83.  
  84. // set color to white on black
  85. Set_Color(15,0);
  86.  
  87. // clear the screen
  88. for (int index=0; index<=25; index++)
  89. Draw_String(0, SCROLL_POS,"\n");
  90.  
  91. } // end Clear_Screen
  92.  
  93. void draw_grid() {
  94. int i, j;
  95. void drawch(int, int, int);
  96. int map(int);
  97.  
  98. Init_Graphics();
  99. Set_Color(15,15); // white background
  100.  
  101.  
  102. //top row
  103. Set_Color(15, 0);
  104. drawch(0, 0, 218);
  105. for(i=1; i<24; i++)
  106. {
  107. drawch(i, 0, 196);
  108. }
  109. drawch(24, 0, 191);
  110.  
  111. // upper middle row
  112. /*drawch(0, 8, 195);
  113. for(i=1; i<24; i++)
  114. if(i==8 || i==16)
  115. drawch(i, 8, 197);
  116. else
  117. drawch(i, 8, 196);
  118. drawch(24, 8, 180);*/
  119.  
  120. // lower middle row
  121. /*drawch(0, 16, 195);
  122. for(i=1; i<24; i++)
  123. drawch(i, 16, 196);
  124. drawch(24, 16, 180);*/
  125.  
  126. // left side
  127. for(i=1; i<24; i++)
  128. drawch(0, i, 179);
  129.  
  130. // left middle vertical
  131. /*drawch(0, 8, 196);
  132. for(i=1; i<24; i++)
  133. if(i==8 || i==16)
  134. drawch(8, i, 197);
  135. else
  136. drawch(8, i, 179);
  137. drawch(8, 24, 193);
  138.  
  139. // right middle vertical
  140. drawch(0, 16, 196);
  141. for(i=1; i<24; i++)
  142. if(i==8 || i==16)
  143. drawch(16, i, 197);
  144. else
  145. drawch(16, i, 179);
  146. drawch(16, 24, 193);*/
  147.  
  148. // right side
  149. for(i=1; i<24; i++)
  150. drawch(24, i, 179);
  151.  
  152. // bottom row
  153. drawch(0, 24, 192);
  154. for(i=1; i<24; i++)
  155. {
  156. drawch(i, 24, 196);
  157. }
  158. drawch(24, 24, 217);
  159.  
  160. //drawch(0, 8, 195);
  161. //drawch(0, 16, 195);
  162. //drawch(24, 8, 180);
  163. //drawch(24, 16, 180);
  164.  
  165. int cell;
  166. for(i=1; i<24; i++)
  167. for(j=1; j<24; j++){
  168. if((i%2 == 0))
  169. if((j%2 == 0)){
  170. Draw_String(i, j, "");
  171. cell = grid[map(j)][map(i)];
  172. if(cell == 300){
  173. cout << "S";
  174. }
  175. else if(cell == 301){
  176. cout << "T";
  177. }
  178. else
  179. cout << cell;
  180. }
  181. Draw_String(78, 24, "");
  182. }
  183. }
  184.  
  185. void drawch(int x, int y, int ch){
  186. char str[] = "?";
  187. str[0] = ch;
  188. Draw_String(x, y, str);
  189. }
  190.  
  191. int map(int k){
  192. int m = k/2;
  193. return m-1;
  194. }
  195.  
  196. void main(){
  197. int runsearch = 1;
  198.  
  199. deque<position> wset;
  200.  
  201. struct position wposition;
  202. struct position cposition;
  203.  
  204. grid[5][5] = 300;
  205.  
  206. grid[2][8] = 301;
  207.  
  208. draw_grid();
  209.  
  210. wposition.x = 5;
  211. wposition.y = 5;
  212.  
  213. wposition.moveno = 0;
  214.  
  215. while(runsearch==1){
  216. draw_grid();
  217. Sleep(500);
  218. wposition.moveno++;
  219. wset.push_back(wposition);
  220. cposition = wset.front();
  221. wset.pop_front();
  222.  
  223. //Search North
  224. if(cposition.y > 0)
  225. {
  226. wposition.y = cposition.y-1;
  227.  
  228. if(grid[wposition.y][cposition.x]==301)
  229. {
  230. runsearch=0;
  231. }
  232. else
  233. {
  234. if(grid[wposition.y][cposition.x] == 0)
  235. {
  236. grid[wposition.y][cposition.x]=wposition.moveno;
  237. wposition.x=cposition.x;
  238. wset.push_back(wposition);
  239. }
  240. }
  241. }
  242. //Search South
  243. if(cposition.y < 10)
  244. {
  245. wposition.y = cposition.y+1;
  246.  
  247. if(grid[wposition.y][cposition.x]==301)
  248. {
  249. runsearch=0;
  250. }
  251. else
  252. {
  253. if(grid[wposition.y][cposition.x] == 0)
  254. {
  255. grid[wposition.y][cposition.x]=wposition.moveno;
  256. wposition.x=cposition.x;
  257. wset.push_back(wposition);
  258. }
  259. }
  260. }
  261.  
  262. //Search East
  263. if(cposition.x > 0)
  264. {
  265. wposition.x = cposition.x-1;
  266.  
  267. if(grid[cposition.y][wposition.x]==301)
  268. {
  269. runsearch=0;
  270. }
  271. else
  272. {
  273. if(grid[cposition.y][wposition.x] == 0)
  274. {
  275. grid[cposition.y][wposition.x]=wposition.moveno;
  276. wposition.y=cposition.y;
  277. wset.push_back(wposition);
  278. }
  279. }
  280. }
  281.  
  282. //Search West
  283. if(cposition.x < 10)
  284. {
  285. wposition.x = cposition.x+1;
  286.  
  287. if(grid[cposition.y][wposition.x]==301)
  288. {
  289. runsearch=0;
  290. }
  291. else
  292. {
  293. if(grid[wposition.y][cposition.x] == 0)
  294. {
  295. grid[cposition.y][wposition.x]=wposition.moveno;
  296. wposition.y=cposition.y;
  297. wset.push_back(wposition);
  298. }
  299. }
  300. }
  301.  
  302. }
  303. }
Add Comment
Please, Sign In to add comment