Advertisement
Guest User

autism.cs

a guest
Mar 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Diagnostics;
  5. using System.Text;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Audio;
  10. using System.IO;
  11.  
  12.  
  13. namespace Pathfinder
  14. {
  15. //loads a map from an ascii text file
  16. class Level
  17. {
  18. private const int gridSize = 40; //set the map grid size
  19. public int [,] tiles; //a 2d array of 0's and 1's: 0 = free cell, 1 = blocked cell
  20.  
  21. //constructor initialises the grid array
  22. public Level()
  23. {
  24. tiles = new int[gridSize, gridSize];
  25. for (int i = 0; i < gridSize; i++)
  26. for (int j = 0; j < gridSize; j++)
  27. tiles[i,j] = 0;
  28. }
  29.  
  30. //accessors
  31. public int GridSize
  32. {
  33. get { return gridSize; }
  34. }
  35.  
  36. //validates a grid position (passed as a 2d vector): returns false if position is blocked, or if x or y
  37. //positions are greater than grid size, or less than 0
  38. public bool ValidPosition(Coord2 pos)
  39. {
  40. if (pos.X < 0) return false;
  41. if (pos.X >= gridSize) return false;
  42. if (pos.Y < 0) return false;
  43. if (pos.Y >= gridSize) return false;
  44. return (tiles[pos.X,pos.Y] == 0);
  45. }
  46.  
  47. //loads the map from a text file
  48. public void Loadmap(string path)
  49. {
  50. List<string> lines = new List<string>();
  51. using (StreamReader reader = new StreamReader(path))
  52. {
  53. string line = reader.ReadLine();
  54. //Debug.Assert(line.Length == gridSize, "loaded map string line width must be 30");
  55. Debug.Assert(line.Length == gridSize, String.Format("loaded map string line width must be {0}.", gridSize));
  56. while (line != null)
  57. {
  58. lines.Add(line);
  59. line = reader.ReadLine();
  60. }
  61. }
  62. Debug.Assert(lines.Count == gridSize, String.Format("loaded map string must have {0} lines.",gridSize));
  63.  
  64. // Loop over every tile position,
  65. for (int y = 0; y < gridSize; ++y)
  66. {
  67. for (int x = 0; x < gridSize; ++x)
  68. {
  69. // to load each tile.
  70. char tileType = lines[y][x];
  71. if (tileType == '.') tiles[x, y] = 0;
  72. else tiles[x, y] = 1;
  73. }
  74. }
  75. }
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement