Advertisement
Guest User

Tron

a guest
Feb 22nd, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.98 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class Tron3D
  5. {
  6. private static int origHeight = 0;
  7. private static int height = 0;
  8. private static int startWidth = 0;
  9. private static int otherWidth = 0;
  10.  
  11. static void Main()
  12. {
  13. int[] sizes = Console.ReadLine().Split()
  14. .Select(int.Parse).ToArray();
  15.  
  16. origHeight = sizes[0];
  17. height = origHeight + 1;
  18. startWidth = sizes[1];
  19. otherWidth = sizes[2];
  20. char[,] field = new char[height, 2 * startWidth + 2 * otherWidth];
  21.  
  22. string redMoves = Console.ReadLine();
  23. string blueMoves = Console.ReadLine();
  24.  
  25. //string[] directions = new string[] { "east", "south", "west", "north" };
  26. int[] directions = new int[] { 0, 1, 2, 3 };
  27. int[] redCoords = new int[] { origHeight / 2, startWidth / 2 }; //row, col
  28. int[] newRedCoords = new int[] { origHeight / 2, startWidth / 2 }; ;
  29. int redDir = 0;
  30. int blueDir = 2;
  31. int[] blueCoords = new int[] {origHeight / 2,
  32. startWidth + otherWidth + startWidth /2}; //row, col
  33. int[] newBlueCoords = new int[] {origHeight / 2,
  34. startWidth + otherWidth + startWidth /2};
  35.  
  36. int redMoveIndex = 0;
  37. int blueMoveIndex = 0;
  38.  
  39. bool isRedCrashed = false;
  40. bool isBlueCrashed = false;
  41. char redMove = default(char);
  42. char blueMove = default(char);
  43. bool isRedCrashedForbWall = false;
  44. double redDistance = 0;
  45.  
  46. while (true)
  47. {
  48. while (true)
  49. {
  50. if (redMoveIndex < redMoves.Length)
  51. redMove = redMoves[redMoveIndex];
  52.  
  53. switch (redMove)
  54. {
  55. case 'R':
  56. redDir++;
  57. if (redDir == directions.Length)
  58. redDir = 0;
  59. break;
  60. case 'L':
  61. redDir--;
  62. if (redDir < 0)
  63. redDir = directions.Length - 1;
  64. break;
  65. case 'M':
  66. switch (redDir)
  67. {
  68. case 0:
  69. newRedCoords[1] = redCoords[1] + 1;
  70. if (newRedCoords[1] == (2 * startWidth + 2 * otherWidth))
  71. newRedCoords[1] = 0;
  72. break;
  73. case 1:
  74. newRedCoords[0] = redCoords[0] - 1;
  75. if (newRedCoords[0] < 0)
  76. {
  77. isRedCrashed = true;
  78. isRedCrashedForbWall = true;
  79. }
  80. break;
  81. case 2:
  82. newRedCoords[1] = redCoords[1] - 1;
  83. if (newRedCoords[1] < 0)
  84. newRedCoords[1] = (2 * startWidth + 2 * otherWidth) - 1;
  85. break;
  86. case 3:
  87. newRedCoords[0] = redCoords[0] + 1;
  88. if (newRedCoords[0] == height)
  89. {
  90. isRedCrashed = true;
  91. isRedCrashedForbWall = true;
  92. }
  93. break;
  94. }
  95. break;
  96. }
  97.  
  98. redMoveIndex++;
  99.  
  100. if (redMove == 'M')
  101. break;
  102. }
  103.  
  104. while (true)
  105. {
  106. if (blueMoveIndex < blueMoves.Length)
  107. blueMove = blueMoves[blueMoveIndex];
  108.  
  109. switch (blueMove)
  110. {
  111. case 'R':
  112. blueDir++;
  113. if (blueDir == directions.Length)
  114. blueDir = 0;
  115. break;
  116. case 'L':
  117. blueDir--;
  118. if (blueDir < 0)
  119. blueDir = directions.Length - 1;
  120. break;
  121. case 'M':
  122. switch (blueDir)
  123. {
  124. case 0:
  125. newBlueCoords[1] = blueCoords[0] + 1;
  126. if (newBlueCoords[1] == (2 * startWidth + 2 * otherWidth))
  127. newBlueCoords[1] = 0;
  128. break;
  129. case 1:
  130. newBlueCoords[0] = blueCoords[0] - 1;
  131. if (newBlueCoords[0] < 0)
  132. isBlueCrashed = true;
  133. break;
  134. case 2:
  135. newBlueCoords[1] = blueCoords[1] - 1;
  136. if (newBlueCoords[1] < 0)
  137. newBlueCoords[1] = (2 * startWidth + 2 * otherWidth - 1);
  138. break;
  139. case 3:
  140. newBlueCoords[0] = blueCoords[0] + 1;
  141. if (newBlueCoords[0] == height)
  142. isBlueCrashed = true;
  143. break;
  144. }
  145. break;
  146. }
  147.  
  148. blueMoveIndex++;
  149.  
  150. if (blueMove == 'M')
  151. break;
  152. }
  153.  
  154. //check for collisions
  155. //both players hit their heads
  156. if (newRedCoords[0] == newBlueCoords[0] &&
  157. newRedCoords[1] == newBlueCoords[1])
  158. {
  159. isRedCrashed = true;
  160. isBlueCrashed = true;
  161. }
  162. else
  163. {
  164. if (newRedCoords[0] < height &&
  165. field[newRedCoords[0], newRedCoords[1]] != default(char))
  166. isRedCrashed = true;
  167.  
  168. if (newBlueCoords[0] < height &&
  169. field[newBlueCoords[0], newBlueCoords[1]] != default(char))
  170. isBlueCrashed = true;
  171. }
  172.  
  173. if (isRedCrashed && isBlueCrashed)
  174. {
  175. Console.WriteLine("DRAW");
  176.  
  177. if (isRedCrashedForbWall)
  178. redDistance = GetDistanceFromStart(redCoords[0],
  179. redCoords[1]);
  180. else
  181. redDistance = GetDistanceFromStart(newRedCoords[0],
  182. newRedCoords[1]);
  183.  
  184. Console.WriteLine(redDistance);
  185. break;
  186. }
  187. else if (isRedCrashed || isBlueCrashed)
  188. {
  189. if (isRedCrashed)
  190. {
  191. Console.WriteLine("BLUE");
  192.  
  193. redDistance = GetDistanceFromStart(newRedCoords[0],
  194. newRedCoords[1]);
  195. Console.WriteLine(redDistance);
  196. }
  197. else if (isBlueCrashed)
  198. {
  199. Console.WriteLine("RED");
  200.  
  201. if (isRedCrashedForbWall)
  202. redDistance = GetDistanceFromStart(redCoords[0],
  203. redCoords[1]);
  204. else
  205. redDistance = GetDistanceFromStart(newRedCoords[0],
  206. newRedCoords[1]);
  207.  
  208. Console.WriteLine(redDistance);
  209. }
  210. break;
  211. }
  212. //nobody crashed and game continues
  213. else
  214. {
  215. field[newRedCoords[0], newRedCoords[1]] = 'V';
  216. field[newBlueCoords[0], newBlueCoords[1]] = 'V';
  217.  
  218. redCoords[0] = newRedCoords[0];
  219. redCoords[1] = newRedCoords[1];
  220. blueCoords[0] = newBlueCoords[0];
  221. blueCoords[1] = newBlueCoords[1];
  222. }
  223. }
  224. }
  225.  
  226. //private static int CalculateRedDistance(int rowRed, int colRed)
  227. //{
  228. // int rowsPassed = Math.Abs(rowRed - origHeight / 2);
  229. // int colsPassed = 0;
  230.  
  231. // if (colRed < (startWidth + otherWidth + startWidth / 2))
  232. // colsPassed = colRed - startWidth / 2;
  233. // else
  234. // colsPassed = (2 * startWidth + 2 * otherWidth) - colRed +
  235. // startWidth / 2;
  236.  
  237. // return rowsPassed + colsPassed;
  238. //}
  239.  
  240. static double GetDistanceFromStart(int row, int col)
  241. {
  242. if (col > otherWidth + startWidth + startWidth / 2)
  243. col = 2 * otherWidth + startWidth - col;
  244.  
  245. return Math.Abs(col - startWidth / 2) + Math.Abs(row - origHeight / 2);
  246. }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement