Advertisement
Guest User

Untitled

a guest
Nov 29th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1.  
  2. #include "Header.h"
  3. #include <ctime>
  4. #include <cstdlib>
  5.  
  6.  
  7. Lunch::Lunch(int X, int Y) //defines lunch to have x&y coords and represented by L
  8. {
  9. Xpos = X;
  10. Ypos = Y;
  11. LunchRep = 'L';
  12. }
  13.  
  14. void school::SetSize(const int n)
  15. {
  16. ActualSize = n;
  17. return;
  18. }
  19.  
  20. bool Collision(Lunch & lunch, janitor & jan, int & DayCounter)
  21. {
  22. bool Collided = false;
  23.  
  24. if (lunch.Xpos == jan.m_xval && lunch.Ypos == jan.m_yval)
  25. {
  26. Collided = true;
  27. }
  28. return Collided;
  29. }
  30.  
  31. void place_me(school & skool, Lunch & lunch, const int Size)//randomly places
  32. //lunch position
  33. {
  34. lunch.Xpos = 1;
  35. lunch.Ypos = 1;
  36. //lunch.Xpos = (rand() % (Size - 2)) + 1;
  37. //lunch.Ypos = (rand() % (Size - 2)) + 1;
  38. skool.School[lunch.Xpos][lunch.Ypos] = lunch.LunchRep;
  39. }
  40.  
  41. void random_move(school & skool, Lunch & lunch)//sets random movement for lunch
  42. {
  43. int Movement;
  44.  
  45. skool.School[lunch.Ypos][lunch.Xpos] = ' ';
  46. Movement = rand() % 4;
  47. switch (Movement)
  48. {
  49. case 0:
  50. if (skool.School[lunch.Ypos + 1][lunch.Xpos] == ' ')//checks for wall
  51. {
  52. if (lunch.Ypos < Size - 1)
  53. {
  54. lunch.Ypos++;
  55. }
  56. }
  57. break;
  58. case 1:
  59. if (skool.School[lunch.Ypos][lunch.Xpos + 1] == ' ')//checks for wall
  60. {
  61. if (lunch.Xpos < Size - 1)
  62. {
  63. lunch.Xpos++;
  64. }
  65. }
  66. break;
  67. case 2:
  68. if (skool.School[lunch.Ypos - 1][lunch.Xpos] == ' ')//checks for wall
  69. {
  70. if (lunch.Ypos > 1)
  71. {
  72. lunch.Ypos--;
  73. }
  74. }
  75. break;
  76. case 3:
  77. if (skool.School[lunch.Ypos][lunch.Xpos - 1] == ' ')//checks for wall
  78. {
  79. if (lunch.Xpos > 1)
  80. {
  81. lunch.Xpos--;
  82. }
  83. }
  84. break;
  85. }
  86. skool.School[lunch.Ypos][lunch.Xpos] = lunch.LunchRep;
  87. cout << Movement << endl << "Lunch: " << lunch.Ypos << ", " << lunch.Xpos << endl;
  88. }
  89.  
  90.  
  91. void school::print(const int Size, const char School[MAX][MAX])//creates school
  92. {
  93. for(int i = 0;i < Size;i++)
  94. {
  95. for (int j = 0;j < Size;j++)
  96. {
  97. cout << School[i][j] << ' ';
  98. }
  99. cout << endl;
  100. }
  101. return;
  102. }
  103.  
  104. void school::clear(char School[MAX][MAX])//clears current data in school array
  105. {
  106. for (int i = 0;i < MAX;i++)
  107. {
  108. for (int j = 0;j < MAX;j++)
  109. {
  110. School[i][j] = '\0';
  111. }
  112. }
  113. return;
  114. }
  115. //places windows and walls on the border of school
  116. void school::build(const int Size, school & skool, Lunch & lunch, janitor & Jan)
  117. {
  118. char Window = 'W', Wall = 'D';
  119.  
  120. for(int i = 0; i < Size; i++)
  121. {
  122. for(int j = 0; j < Size; j++)
  123. {
  124. if(i == 0 || j == 0 || i == Size - 1 || j == Size - 1)
  125. {
  126. if ((i > 0 && i < Size - 1 && i % 5 == 0) || (j > 0 && j < Size - 1 && j % 5 == 0))
  127. {
  128. School[i][j] = Window;
  129. }
  130. else
  131. {
  132. School[i][j] = Wall;
  133. }
  134. }
  135. else
  136. {
  137. School[i][j] = ' ';
  138. }
  139. }
  140. }
  141. random_move(skool, lunch);
  142. rand_walk(skool, Jan);
  143. print(Size, skool.School);
  144.  
  145. return;
  146. }
  147.  
  148. //places janitor
  149. void place_Janitor (school & escuela, janitor & Willie, const int Size)
  150. {
  151. Willie.m_xval = Size - 2;
  152. Willie.m_yval = Size - 2;
  153. escuela.School[Willie.m_xval][Willie.m_yval] = Willie.jancharacter;
  154. }
  155.  
  156. //places willie in school and walks him in random directions with possibility of a double step
  157. void rand_walk (school & escuela, janitor & Willie)
  158. {
  159. int rand_step;
  160. int Twostep = 0;
  161.  
  162. escuela.School[Willie.m_yval][Willie.m_xval] = ' ';
  163. rand_step = rand() %4;
  164.  
  165. switch (rand_step)
  166. {
  167. case 0:
  168. if (escuela.School[Willie.m_yval + 1][Willie.m_xval] == ' ')
  169. {
  170. Willie.m_yval++;
  171. Twostep = rand() %2;
  172. if (escuela.School[Willie.m_yval + 1][Willie.m_xval] == ' ')
  173. {
  174. if (Twostep == 1)
  175. {
  176. Willie.m_yval++;
  177. }
  178. }
  179. }
  180.  
  181. case 1:
  182. if (escuela.School[Willie.m_yval][Willie.m_xval + 1] == ' ')
  183. {
  184. Willie.m_xval++;
  185. Twostep = rand() %2;
  186. if (escuela.School[Willie.m_yval][Willie.m_xval + 1] == ' ')
  187. {
  188. if (Twostep== 1)
  189. {
  190. Willie.m_xval++;
  191. }
  192. }
  193. }
  194.  
  195. case 2:
  196. if (escuela.School[Willie.m_yval - 1][Willie.m_xval] == ' ')
  197. {
  198. Willie.m_yval--;
  199. Twostep = rand() %2;
  200. if (escuela.School[Willie.m_yval - 1][Willie.m_xval] == ' ')
  201. {
  202. if (Twostep== 1)
  203. {
  204. Willie.m_yval--;
  205. }
  206. }
  207. }
  208.  
  209. case 3:
  210. if (escuela.School[Willie.m_yval][Willie.m_xval - 1] == ' ')
  211. {
  212. Willie.m_xval--;
  213. Twostep = rand() %2;
  214. if (escuela.School[Willie.m_yval][Willie.m_xval - 1] == ' ')
  215. {
  216. if (Twostep == 1)
  217. {
  218. Willie.m_xval--;
  219. }
  220. }
  221. }
  222. }
  223. escuela.School[Willie.m_yval][Willie.m_xval] = Willie.jancharacter;
  224. cout << rand_step << endl << "Janitor: " << Willie.m_yval << ", " << Willie.m_xval << endl;
  225. }
  226.  
  227.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement