Advertisement
Guest User

Untitled

a guest
Jun 4th, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _02_Tron_Racers
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int n = int.Parse(Console.ReadLine());
  10. var matrix = new string[n, n];
  11.  
  12. var firstPlayerRow = 0;
  13. var secondPlayerRow = 0;
  14.  
  15. var firstPlayerCol = 0;
  16. var secondPlayerCol = 0;
  17.  
  18. for (int row = 0; row < n; row++)
  19. {
  20. string input = Console.ReadLine();
  21. for (int col = 0; col < n; col++)
  22. {
  23. matrix[row, col] = input[col].ToString();
  24. if (matrix[row, col] == "f")
  25. {
  26. firstPlayerRow = row;
  27. firstPlayerCol = col;
  28. }
  29. else if (matrix[row, col] == "s")
  30. {
  31. secondPlayerRow = row;
  32. secondPlayerCol = col;
  33. }
  34.  
  35. }
  36. }
  37.  
  38. bool isLive = false;
  39.  
  40. while (true)
  41. {
  42. var commands = Console.ReadLine()
  43. .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  44.  
  45. var commandFirstPlayer = commands[0];
  46.  
  47. Moves(matrix, ref firstPlayerRow, ref firstPlayerCol, commandFirstPlayer);
  48.  
  49. if (matrix[firstPlayerRow, firstPlayerCol] == "*")
  50. {
  51. matrix[firstPlayerRow, firstPlayerCol] = "f";
  52. }
  53. else if (matrix[firstPlayerRow, firstPlayerCol] == "s")
  54. {
  55. matrix[firstPlayerRow, firstPlayerCol] = "x";
  56. isLive = true;
  57. }
  58. if (isLive)
  59. {
  60. break;
  61. }
  62.  
  63. var commandSecondPlayer = commands[1];
  64.  
  65. Moves(matrix, ref secondPlayerRow, ref secondPlayerCol, commandSecondPlayer);
  66.  
  67. if (matrix[secondPlayerRow, secondPlayerCol] == "*")
  68. {
  69. matrix[secondPlayerRow, secondPlayerCol] = "s";
  70. }
  71. else if (matrix[secondPlayerRow, secondPlayerCol] == "f")
  72. {
  73. matrix[secondPlayerRow, secondPlayerCol] = "x";
  74. isLive = true;
  75. }
  76. if (isLive)
  77. {
  78. break;
  79. }
  80. }
  81.  
  82. MatrixPrint(matrix);
  83. }
  84.  
  85. private static void MatrixPrint(string[,] matrix)
  86. {
  87. for (int row = 0; row < matrix.GetLength(0); row++)
  88. {
  89. for (int col = 0; col < matrix.GetLength(1); col++)
  90. {
  91. Console.Write(matrix[row, col]);
  92. }
  93. Console.WriteLine();
  94. }
  95. }
  96.  
  97. private static void Moves(string[,] matrix, ref int playerRow, ref int playerCol, string command)
  98. {
  99. if (command == "up")
  100. {
  101. playerRow --;
  102.  
  103. if (playerRow < 0)
  104. {
  105. playerRow = matrix.GetLength(0)-1;
  106. }
  107. }
  108.  
  109. else if (command == "down")
  110. {
  111. playerRow ++;
  112.  
  113. if (playerRow > matrix.GetLength(0) - 1)
  114. {
  115. playerRow = 0;
  116. }
  117. }
  118.  
  119. else if (command == "left")
  120. {
  121. playerCol --;
  122. if (playerCol < 0)
  123. {
  124. playerCol = matrix.GetLength(1)-1;
  125. }
  126. }
  127.  
  128. else if (command == "right")
  129. {
  130. playerCol ++;
  131. if (playerCol > matrix.GetLength(1) - 1)
  132. {
  133. playerCol = 0;
  134. }
  135. }
  136. }
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement