Advertisement
Guest User

Untitled

a guest
May 11th, 2012
1,180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace _95.Aplikace
  8. {
  9. class Place
  10. {
  11. public bool moved = false;
  12. public int type = 0;
  13. }
  14. class Player
  15. {
  16. public int x;
  17. public int y;
  18. public string model = "☺";
  19. public Stack<string> path = new Stack<string>();
  20. public Player(int x, int y)
  21. {
  22. this.x = x;
  23. this.y = y;
  24. }
  25. public void Move(Place[,] map)
  26. {
  27. this.path.Push(this.x + "-" + this.y);
  28. map[this.x, this.y].moved = true;
  29. if (this.CanMove(map) == true)
  30. {
  31. for (int i = 0; i < 1; i++)
  32. {
  33. if (this.x != map.GetLength(0) - 1) if (map[this.x + 1, this.y].type != 1 && map[this.x + 1, this.y].moved == false) { this.x++; break; }
  34. if (this.y != 0) if (map[this.x, this.y - 1].type != 1 && map[this.x, this.y - 1].moved == false) { this.y--; break; }
  35. if (this.y != map.GetLength(1) - 1) if (map[this.x, this.y + 1].type != 1 && map[this.x, this.y + 1].moved == false) { this.y++; break; }
  36. if (this.x != 0) if (map[this.x - 1, this.y].type != 1 && map[this.x - 1, this.y].moved == false) { this.x--; break; }
  37. }
  38. Program.Debug(map, this); System.Threading.Thread.Sleep(50);
  39. }
  40. else { this.GoBack(map); this.GoBack(map); }
  41. }
  42. public bool CanMove(Place[,] map)
  43. {
  44. if (this.x != map.GetLength(0) - 1) if (map[this.x + 1, this.y].type != 1 && map[this.x + 1, this.y].moved == false) return true;
  45. if (this.y != 0) if (map[this.x, this.y - 1].type != 1 && map[this.x, this.y - 1].moved == false) return true;
  46. if (this.y != map.GetLength(1) - 1) if (map[this.x, this.y + 1].type != 1 && map[this.x, this.y + 1].moved == false) return true;
  47. if (this.x != 0) if (map[this.x - 1, this.y].type != 1 && map[this.x - 1, this.y].moved == false) return true;
  48. return false;
  49. }
  50. public void GoBack(Place[,] map)
  51. {
  52. string coords = this.path.Pop();
  53. this.x = Convert.ToInt16(coords.Split('-')[0]);
  54. this.y = Convert.ToInt16(coords.Split('-')[1]);
  55. Program.Debug(map, this);
  56. System.Threading.Thread.Sleep(15);
  57. }
  58. }
  59. class Program
  60. {
  61. public static void Debug(Place[,] map, Player player)
  62. {
  63. Console.SetCursorPosition(0, 0);
  64. for (int i = 0; i < map.GetLength(0); i++)
  65. {
  66. for (int ii = 0; ii < map.GetLength(1); ii++)
  67. {
  68. if (map[i, ii].type == 1)
  69. {
  70. Console.BackgroundColor = ConsoleColor.Gray;
  71. Console.Write(map[i, ii].type);
  72. Console.BackgroundColor = ConsoleColor.Black;
  73. }
  74. else if (map[i, ii].type == 0) Console.Write(".");
  75. else if (map[i, ii].type == 2)
  76. {
  77. Console.ForegroundColor = ConsoleColor.Green;
  78. Console.Write(map[i, ii].type);
  79. Console.ForegroundColor = ConsoleColor.Gray;
  80. }
  81. else if (map[i, ii].type == 3)
  82. {
  83. Console.ForegroundColor = ConsoleColor.Red;
  84. Console.Write(map[i, ii].type);
  85. Console.ForegroundColor = ConsoleColor.Gray;
  86. }
  87. else Console.Write(map[i, ii].type);
  88. }
  89. Console.WriteLine();
  90. }
  91. Console.SetCursorPosition(player.y, player.x);
  92. Console.ForegroundColor = ConsoleColor.Cyan;
  93. Console.Write(player.model);
  94. Console.ForegroundColor = ConsoleColor.Gray;
  95. Console.SetCursorPosition(0, map.GetLength(0));
  96. }
  97. static void Main(string[] args)
  98. {
  99. Console.CursorVisible = false;
  100. Stream stream = new FileStream(@"C:\Users\DragonCz\Desktop\Visual Studio Projects\Soubory\95. Aplikace - Maze.txt", FileMode.Open, FileAccess.Read);
  101. StreamReader read = new StreamReader(stream);
  102. int height = 1;
  103. int width = 0;
  104. string radek = "";
  105. Player player = new Player(0,0);
  106. width = read.ReadLine().Length;
  107. while ((radek = read.ReadLine()) != null)
  108. height++;
  109. Place[,] map = new Place[height, width];
  110. stream.Seek(0, 0);
  111. for (int i = 0; i < map.GetLength(0); i++)
  112. {
  113. radek = read.ReadLine();
  114. for (int ii = 0; ii < map.GetLength(1); ii++)
  115. {
  116. map[i, ii] = new Place();
  117. map[i, ii].type = Convert.ToInt16(radek[ii] - '0');
  118. if (map[i, ii].type == 2) { player = new Player(i, ii); }
  119. }
  120. }
  121. while (true)
  122. {
  123. player.Move(map);
  124. if (map[player.x, player.y].type == 3)
  125. {
  126. Console.SetCursorPosition(0, height);
  127. Console.WriteLine("Cesta úspěšně nalezena, délka: " + player.path.Count);
  128. break;
  129. }
  130. else if (player.path.Count == 0 && player.CanMove(map) == false)
  131. {
  132. Console.SetCursorPosition(0, height);
  133. Console.WriteLine("Cesta nenalezena");
  134. break;
  135. }
  136. }
  137. int length = player.path.Count;
  138. for (int i = 0; i < length; i++)
  139. {
  140. int a = Convert.ToInt16(player.path.Peek().Split('-')[1]);
  141. int b = Convert.ToInt16(player.path.Pop().Split('-')[0]);
  142. Console.SetCursorPosition(a, b);
  143. Console.ForegroundColor = ConsoleColor.Red;
  144. Console.BackgroundColor = ConsoleColor.Green;
  145. Console.Write("■");
  146. System.Threading.Thread.Sleep(25);
  147. }
  148. Console.SetCursorPosition(0, height);
  149. Console.Read();
  150. }
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement