fork(2) download
  1. /*
  2. ===============
  3. Catacomb Commander The First Game (making use of a two-dimensional array)
  4. CataEngine.h
  5. ===============
  6. */
  7.  
  8. #pragma once
  9. #include<iostream>
  10. #include<conio.h> //for getch [for visual C++ compiler only]
  11. #include<dos.h> //for textcolor [for visual C++ compiler only]
  12. #include<stdio.h> //for fileinput
  13.  
  14. using namespace std;
  15.  
  16. #define COLUMNS 19 //columns
  17. #define ROWS 19 //rows
  18.  
  19. /*
  20. ===============
  21. CLASS DECLARATIONS
  22. ===============
  23. */
  24.  
  25. class CEngine
  26. {
  27. public:
  28. CEngine();
  29. ~CEngine();
  30. void PrintMaze(int [][COLUMNS], int);
  31. void Control(int map[][COLUMNS], int, int);
  32. };
  33.  
  34. /*
  35. ===============
  36. Catacomb Commander
  37. CataEngine.cpp
  38. ===============
  39. */
  40.  
  41. #include"CataEngine.h"
  42.  
  43. /*
  44. ======================
  45. CEngine
  46. -Empty Default Constructor
  47. ======================
  48. */
  49. CEngine::CEngine()
  50. {
  51. }
  52.  
  53. /*
  54. ======================
  55. ~CEngine
  56. -Empty Destructor
  57. ======================
  58. */
  59. CEngine::~CEngine()
  60. {
  61. }
  62.  
  63. /*
  64. ======================
  65. Control
  66. -Game Movement
  67. ======================
  68. */
  69. void CEngine::Control(int map[][COLUMNS], int x, int y) //via number keypad
  70. {
  71. int user_input;
  72. map[x][y] = 3;
  73. PrintMaze(map,COLUMNS); //prints map on each cycle
  74. map[x][y] = 0;
  75. /* use getch built in function to grap input from command line */
  76. user_input = getch();
  77.  
  78. /* recursively analyse user input */
  79.  
  80. switch(user_input)
  81. {
  82. case '6':
  83. if(map[x][y+1] == 0)
  84. Control(map,x,y+1); //move right
  85. else
  86. if(map[x][y+1] == 1)
  87. Control(map,x,y); //don't move
  88. break;
  89.  
  90. case '4': //move left
  91. if(map[x][y-1] == 0)
  92. Control(map,x,y-1); //move left
  93. else
  94. if(map[x][y-1] == 1)
  95. Control(map,x,y); //don't move
  96. break;
  97.  
  98.  
  99. case '2':
  100. if(map[x+1][y] == 0)
  101. Control(map,x+1,y); //move down
  102. else
  103. if(map[x+1][y] == 1)
  104. Control(map,x,y); //don't move
  105. break;
  106.  
  107. case '8':
  108. if(map[x-1][y] == 0)
  109. Control(map,x-1,y); //move up
  110. else
  111. if(map[x-1][y] == 1)
  112. Control(map,x,y); //don't move
  113. break;
  114.  
  115. case 'x': case 'X': break;
  116. default :
  117. Control(map,x,y); //don't move
  118. }
  119. }
  120.  
  121. /*
  122. ======================
  123. PrintMaze
  124. -Draws the Level Map
  125. ======================
  126. */
  127. void CEngine::PrintMaze(int maze[][COLUMNS], int size) //map creator
  128. {
  129. /* clear the screen by printing 25 newlines */
  130. for(int k = 0; k <= 25; k++)
  131. std::cout << std::endl;
  132.  
  133. /* draw the map and player */
  134. for(int i = 0; i <= size - 1; i++)
  135. {
  136. for(int j = 0; j <= size - 1; j++)
  137. {
  138. if (maze[i][j] == 3)
  139. cout << "*"; //draw player
  140. else
  141. if(maze[i][j] == 1)
  142. cout <<"#"; //draw walls
  143. else
  144. cout << " "; //draw halls
  145. }
  146. std::cout << std::endl;
  147. }
  148. }
  149.  
  150. /*
  151. ===============
  152. Catacomb Commander
  153. To play: 2 = down, 6 = right ,8 = up, 4 = left
  154. ===============
  155. */
  156.  
  157. #include"CataEngine.h"
  158.  
  159. int main()
  160. {
  161. /* init the map - a 2D array */
  162. int map[ROWS][COLUMNS] = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  163. {1,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1},
  164. {0,0,1,0,1,0,1,1,1,1,0,1,1,1,1,1,1,1,1},
  165. {1,1,1,0,1,0,0,0,0,1,0,1,1,1,1,1,1,1,1},
  166. {1,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0,0,0,1},
  167. {1,1,1,1,0,1,0,1,0,1,0,1,0,0,1,1,1,1,1},
  168. {1,0,0,1,0,1,0,1,0,1,0,1,0,0,1,1,1,1,1},
  169. {1,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1},
  170. {1,0,0,0,0,0,0,0,0,1,0,1,1,0,0,0,1,1,1},
  171. {1,1,1,1,1,1,0,1,1,1,0,1,1,0,0,0,1,1,1},
  172. {1,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,1,1},
  173. {1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0,1,1,1},
  174. {1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1},
  175. {1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,1},
  176. {1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,1},
  177. {1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1},
  178. {1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1},
  179. {1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1},
  180. {1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1}};
  181.  
  182. int x = 2, y = 0; //starting position on the map ROW 2, COLUMN 0
  183.  
  184. CEngine instance;
  185. instance.Control(map,x,y);
  186. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:8:9: warning: #pragma once in main file
 #pragma once
         ^
prog.cpp:10:61: fatal error: conio.h: No such file or directory
 #include<conio.h> //for getch [for visual C++ compiler only]
                                                             ^
compilation terminated.
stdout
Standard output is empty