private void InitLocations() { locations = new Location[Utils.SUGAR_CAPACITIES.GetLength(0), Utils.SUGAR_CAPACITIES.GetLength(1)]; int height = locations.GetLength(0); int width = locations.GetLength(1); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { locations[y, x] = new Location(x, y, Utils.SUGAR_CAPACITIES[y, x]); } } for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int up = y == 0 ? height - 1 : y - 1; int down = (y + 1) % height; int left = x == 0 ? width - 1 : x - 1; int right = (x + 1) % width; locations[y, x].SetNeighbors( locations[up, x], locations[down, x], locations[y, right], locations[y, left] ); } } }
protected override object SolvePartOne() { var input = Input.SplitByNewline(); depth = int.Parse(input[0].Split(' ')[1]); string[] target = input[1].Split(new[] { ' ', ',' }); p = new Point(int.Parse(target[1]), int.Parse(target[2])); depth = 510; p = new Point(10, 10); cave = new Location[p.Y + 6, p.X + 6]; int height = cave.GetLength(0); int width = cave.GetLength(1); Console.SetBufferSize(Console.WindowWidth, height + 20); for (int i = 0; i < height; i++) { cave[i, 0] = new Location(0, i, depth); } for (int i = 0; i < width; i++) { cave[0, i] = new Location(i, 0, depth); } cave[p.Y, p.X] = new Location(p.X, p.Y, depth); cave[p.Y, p.X].GeologicIndex = 0; for (int y = 1; y < height; y++) { for (int x = 1; x < width; x++) { if (cave[y, x] != null) { continue; } cave[y, x] = new Location(x, y, depth); cave[y, x].GeologicIndex = cave[y - 1, x].ErosionLevel * cave[y, x - 1].ErosionLevel; } } WriteConsole(height, width, 20, 5, (y, x) => { int i = cave[y, x].RegionType; char c; if (i == 0) { c = '.'; } else if (i == 1) { c = '='; } else { c = '|'; } return(ConsoleColor.White, c); });
public bool IsValidCoordinate(int x, int y) { if (x >= locations.GetLength(0) || y >= locations.GetLength(1) || x < 0 || y < 0) { return(false); } return(true); }
private static void AddRandomObject(ref Location[,] world, List <string> Items) { Random random = new Random(Guid.NewGuid().GetHashCode()); int i = random.Next(world.GetLength(0)); int j = random.Next(world.GetLength(1)); world[i, j].Thing = Items[random.Next(Items.Count)]; Console.WriteLine("({0}, {1}) has {2}", i, j, world[i, j].Thing); }
public HeatMap(int gridsize, IGameState asdfstate) { state = asdfstate; heatMapGridSize = gridsize; heatMap = new float[(int)Math.Ceiling((float)state.Height / heatMapGridSize), (int)Math.Ceiling((float)state.Width / heatMapGridSize)]; heatMapCentre = new Location[heatMap.GetLength(0), heatMap.GetLength(1)]; for (int y = 0; y < heatMapCentre.GetLength(0); y++) for (int x = 0; x < heatMapCentre.GetLength(1); x++) heatMapCentre[y, x] = new Location(y * heatMapGridSize, x * heatMapGridSize); }
public GameState(int width, int height, int turntime, int loadtime, int viewradius2, int attackradius2, int spawnradius2, int seed) { Width = width; Height = height; LoadTime = loadtime; TurnTime = turntime; PlayerSeed = seed; ViewRadius2 = viewradius2; AttackRadius2 = attackradius2; ViewRadius = (int)Math.Sqrt(viewradius2); AttackRadius = (int)Math.Sqrt(attackradius2); SpawnRadius2 = spawnradius2; MyAnts = new List<Location>(); MyHills = new List<Location>(); EnemyAnts = new List<Location>(); EnemyHills = new List<Location>(); DeadTiles = new List<Location>(); FoodTiles = new List<Location>(); map = new Location[height, width]; for (int i = 0; i < map.GetLength(0); ++i) { for (int j = 0; j < map.GetLength(1); ++j) { map[i, j] = new Location(i, j); } } foreach (Location l in map) { int col = (l.Col - 1) % width; if (col < 0) col += width; int row = (l.Row - 1) % height; if (row < 0) row += height; l.Neighbors[0] = map[row, l.Col]; l.Neighbors[3] = map[l.Row, col]; row = (l.Row + 1) % height; col = (l.Col + 1) % width; l.Neighbors[1] = map[row, l.Col]; l.Neighbors[2] = map[l.Row, col]; } }
// Find unvisited neighbours to this address static List <Place> FindNeighbours(Location[,] World, Place address) { List <Place> unVisitedNeighbours = new List <Place>(); // Look west if (address.X > 0) { // If western neighbour hasn't been visited if (World[address.Y, address.X - 1].Visited == false) { // Western neighbour can be added to the list unVisitedNeighbours.Add(new Place(address.Y, address.X - 1)); } } // Look north if (address.Y > 0) { // If northern neighbour hasn't been visited if (World[address.Y - 1, address.X].Visited == false) { // Northern neighbour can be added to the list unVisitedNeighbours.Add(new Place(address.Y - 1, address.X)); } } // Look east if (address.X < World.GetLength(1) - 1) { // If eastern neighbour hasn't been visited if (World[address.Y, address.X + 1].Visited == false) { // Eastern neighbour can be added to the list unVisitedNeighbours.Add(new Place(address.Y, address.X + 1)); } } // Look south if (address.Y < World.GetLength(0) - 1) { // If southern neighbour hasn't been visited if (World[address.Y + 1, address.X].Visited == false) { // Southern neighbour can be added to the list unVisitedNeighbours.Add(new Place(address.Y + 1, address.X)); } } // Return all the valid neighbours, could be empty return(unVisitedNeighbours); }
public HeatMap(int gridsize, IGameState asdfstate) { state = asdfstate; heatMapGridSize = gridsize; heatMap = new float[(int)Math.Ceiling((float)state.Height / heatMapGridSize), (int)Math.Ceiling((float)state.Width / heatMapGridSize)]; heatMapCentre = new Location[heatMap.GetLength(0), heatMap.GetLength(1)]; for (int y = 0; y < heatMapCentre.GetLength(0); y++) { for (int x = 0; x < heatMapCentre.GetLength(1); x++) { heatMapCentre[y, x] = new Location(y * heatMapGridSize, x * heatMapGridSize); } } }
// Create default world, populate with defaults public static void CreateWorld(ref Location[,] World) { for (int i = 0; i < World.GetLength(0); i++) { for (int j = 0; j < World.GetLength(1); j++) { // This means a wall, or more specifically NO door Place wall = new Place(-1, -1); World[i, j].Address = new Place(i, j); // set address World[i, j].Visited = false; World[i, j].Neighbours = new Doors(wall, wall, wall, wall); } } }
public GameSessionViewModel(Player player, List <string> initialMessages, Location[,] gameMap, GameMapCoordinates currentLocationCoordinates) { _player = player; _messages = initialMessages; _gameMap = gameMap; _maxRows = _gameMap.GetLength(0); _maxColumns = _gameMap.GetLength(1); _currentLocationCoordinates = currentLocationCoordinates; _currentLocation = _gameMap[_currentLocationCoordinates.Row, _currentLocationCoordinates.Column]; }
private static void DisplayError(string arrowKey, Location[,] world) { //SetTextColour(); Console.SetCursorPosition(0, (world.GetLength(0)) * 3 + 5); Console.Write("You can't go {0} ", arrowKey); //ResetTextColour(); }
private static void inputError(string userInput, Location[,] world) { //SetTextColour(); Console.SetCursorPosition(0, (world.GetLength(0)) * 3 + 5); Console.Write("Please try again {0} ", userInput); //ResetTextColour }
private static void ClearError(Location[,] world) { //SetTextColour(); Console.SetCursorPosition(0, (world.GetLength(0)) * 3 + 5); Console.Write("{0}", new String(' ', Console.BufferWidth)); //ResetTextColour(); }
public void Destroy() { for (int y = 0; y < locations.GetLength(0); y++) { for (int x = 0; x < locations.GetLength(1); x++) { locations[y, x].Destroy(); } } Object.Destroy(gameObject); }
public bool Move() { List <(int fromX, int fromY, int toX, int toY)> eastMoves = new(); // Move the east-facing seafish for (int y = 0; y < _seafloor.GetLength(0); y++) { for (int x = 0; x < _seafloor.GetLength(1); x++) { int nextX = (x + 1) % _seafloor.GetLength(1); if (_seafloor[y, x] == Location.SeafishE && _seafloor[y, nextX] == Location.Empty) { eastMoves.Add((x, y, nextX, y)); } } } foreach ((int fromX, int fromY, int toX, int toY) in eastMoves) { _seafloor[toY, toX] = Location.SeafishE; _seafloor[fromY, fromX] = Location.Empty; } List <(int fromX, int fromY, int toX, int toY)> southMoves = new(); // Move the south-facing seafish for (int x = 0; x < _seafloor.GetLength(1); x++) { for (int y = 0; y < _seafloor.GetLength(0); y++) { int nextY = (y + 1) % _seafloor.GetLength(0); if (_seafloor[y, x] == Location.SeafishS && _seafloor[nextY, x] == Location.Empty) { southMoves.Add((x, y, x, nextY)); } } } foreach ((int fromX, int fromY, int toX, int toY) in southMoves) { _seafloor[toY, toX] = Location.SeafishS; _seafloor[fromY, fromX] = Location.Empty; } return(eastMoves.Any() || southMoves.Any()); }
private static string askDirChoice(Location [,] world) { Console.SetCursorPosition(0, (world.GetLength(0)) * 3 + 3); bool isValid = false; Console.WriteLine("In which direction would you like to travel? Please enter - [E] [W] [N] [S] :"); string dir = ""; while (!(isValid)) { ConsoleKey readKey = Console.ReadKey().Key; if (readKey == ConsoleKey.E) { isValid = true; dir = "East"; } if (readKey == ConsoleKey.W) { isValid = true; dir = "West"; } if (readKey == ConsoleKey.N) { isValid = true; dir = "North"; } if (readKey == ConsoleKey.S) { isValid = true; dir = "South"; } if (!(isValid)) { inputError(world); } } return(dir); }
public static void PopulateMapArray() { int[] cordarray = new int[2] { 0, 0 }; for (int row = 0; row < MapArray.GetLength(0); row++) { for (int columb = 0; columb < MapArray.GetLength(1); columb++) { foreach (Location locals in Locations) { if (locals.Coordinates.SequenceEqual(cordarray)) { MapArray[cordarray[0], cordarray[1]] = locals; } } cordarray[1]++; } cordarray[1] = 0; cordarray[0]++; } }
static List <Location> GetWalkableAdjacentSquares(int x, int y, Location[,] map) { Location Top = (y <= 0) ? null : map[x, y - 1]; Location Bottom = (y >= map.GetLength(1) - 1) ? null : map[x, y + 1]; Location Left = (x <= 0) ? null : map[x - 1, y]; Location Right = (x >= map.GetLength(0) - 1) ? null : map[x + 1, y]; Location TopLeft = ((y <= 0) || (x <= 0)) ? null : map[x - 1, y - 1]; Location TopRight = ((y <= 0) || (x >= map.GetLength(0) - 1)) ? null : map[x + 1, y - 1]; Location BottomLeft = ((y >= map.GetLength(1) - 1) || (x <= 0)) ? null : map[x - 1, y + 1]; Location BottomRight = ((y >= map.GetLength(1) - 1) || (x >= map.GetLength(0) - 1)) ? null : map[x + 1, y + 1]; var proposedLocations = new List <Location>() { Top, Bottom, Left, Right, }; if (TopLeft != null && (IsLocationWalkable(Top) || IsLocationWalkable(Left))) { TopLeft.IsSlopeMove = true; if (IsLocationWalkable(Top)) { TopLeft.SlopePath.Add(Top); } if (IsLocationWalkable(Left)) { TopLeft.SlopePath.Add(Left); } proposedLocations.Add(TopLeft); } if (TopRight != null && (IsLocationWalkable(Top) || IsLocationWalkable(Right))) { TopRight.IsSlopeMove = true; if (IsLocationWalkable(Top)) { TopRight.SlopePath.Add(Top); } if (IsLocationWalkable(Right)) { TopRight.SlopePath.Add(Right); } proposedLocations.Add(TopRight); } if (BottomLeft != null && (IsLocationWalkable(Bottom) || IsLocationWalkable(Left))) { BottomLeft.IsSlopeMove = true; if (IsLocationWalkable(Bottom)) { BottomLeft.SlopePath.Add(Bottom); } if (IsLocationWalkable(Left)) { BottomLeft.SlopePath.Add(Left); } proposedLocations.Add(BottomLeft); } if (BottomRight != null && (IsLocationWalkable(Bottom) || IsLocationWalkable(Right))) { BottomRight.IsSlopeMove = true; if (IsLocationWalkable(Bottom)) { BottomRight.SlopePath.Add(Bottom); } if (IsLocationWalkable(Right)) { BottomRight.SlopePath.Add(Right); } proposedLocations.Add(BottomRight); } return(proposedLocations.Where(l => IsLocationWalkable(l)).ToList()); }
// Draw world public static void DisplayWorld(Location[,] World) { Console.OutputEncoding = System.Text.Encoding.Unicode; Console.BackgroundColor = ConsoleColor.Black; // Set cursor colour Console.ForegroundColor = ConsoleColor.White; // Reset cursor position int x = 1; int y = 1; // Iterate through rows and columns for (int i = 0; i < World.GetLength(0); i++) { for (int j = 0; j < World.GetLength(1); j++) { // Draw all walls for each cell Console.SetCursorPosition(x, y); Console.Write("+———+"); Console.SetCursorPosition(x, y + 1); Console.Write("| |"); Console.SetCursorPosition(x, y + 2); Console.Write("| |"); Console.SetCursorPosition(x, y + 3); Console.Write("+———+"); Location room = World[i, j]; // Draw doors by over writing walls if (room.Neighbours.East.X != -1) { Console.SetCursorPosition(x + 4, y + 1); Console.Write(" "); Console.SetCursorPosition(x + 4, y + 2); Console.Write(" "); } if (room.Neighbours.West.X != -1) { Console.SetCursorPosition(x, y + 1); Console.Write(" "); Console.SetCursorPosition(x, y + 2); Console.Write(" "); } if (room.Neighbours.South.X != -1) { Console.SetCursorPosition(x, y + 3); Console.Write("+ +"); } if (room.Neighbours.North.X != -1) { Console.SetCursorPosition(x, y); Console.Write("+ +"); } // next cell x += 4; } // next row y += 3; // reset x x = 1; } // Force blank line Console.SetCursorPosition(x, y + 1); Console.WriteLine(); }
// Open the door in the direction of Dir. The neighbour in that direction // needs the opposite door opening. static void SetDoor(ref Location[,] World, string Dir, Place address) { // Get current neighbours, so we don't lose them Doors doors = World[address.Y, address.X].Neighbours; // Which door? switch (Dir) { case "East": // You can only have an eastern neighbour if not on east edge if (address.X < World.GetLength(1) - 1) { // Set the east door to point to eastern neighbour doors.East = new Place(address.Y, address.X + 1); // Set our eastern neighbour's west door Doors doorsNeighbour = World[address.Y, address.X + 1].Neighbours; doorsNeighbour.West = address; World[address.Y, address.X + 1].Neighbours = doorsNeighbour; } break; case "West": // You can only have an western neighbour if not on west edge if (address.X > 0) { // Set the east door to point to western neighbour doors.West = new Place(address.Y, address.X - 1); // Set our eastern neighbour's east door Doors doorsNeighbour = World[address.Y, address.X - 1].Neighbours; doorsNeighbour.East = address; World[address.Y, address.X - 1].Neighbours = doorsNeighbour; } break; case "South": if (address.Y < World.GetLength(0) - 1) { // Set the south door to point to southern neighbour doors.South = new Place(address.Y + 1, address.X); // Set our eastern neighbour's north door Doors doorsNeighbour = World[address.Y + 1, address.X].Neighbours; doorsNeighbour.North = address; World[address.Y + 1, address.X].Neighbours = doorsNeighbour; } break; case "North": if (address.Y > 0) { // Set the north door to point to northern neighbour doors.North = new Place(address.Y - 1, address.X); // Set our eastern neighbour's south door Doors doorsNeighbour = World[address.Y - 1, address.X].Neighbours; doorsNeighbour.South = address; World[address.Y - 1, address.X].Neighbours = doorsNeighbour; } break; } // Put this address's modified doors back World[address.Y, address.X].Neighbours = doors; }
public InfluenceMap(Location[,] map) { this.map = map; influence = new float[map.GetLength (0), map.GetLength (1)]; }
private static void DisplayFound(string found, Location[,] world) { Console.SetCursorPosition(0, (world.GetLength(0)) * 3 + 5); Console.WriteLine("You have found: {0}", found); }
/// <summary> /// This event will be fired immediately after the Direct3D device has been /// created, which will happen during application initialization and windowed/full screen /// toggles. This is the best location to create Pool.Managed resources since these /// resources need to be reloaded whenever the device is destroyed. Resources created /// here should be released in the Disposing event. /// </summary> private void OnCreateDevice(object sender, DeviceEventArgs e) { // Initialize the stats font statsFont = ResourceCache.GetGlobalInstance().CreateFont(e.Device, 15, 0, FontWeight.Bold, 1, false, CharacterSet.Default, Precision.Default, FontQuality.Default, PitchAndFamily.FamilyDoNotCare | PitchAndFamily.DefaultPitch , "Arial"); // Setup the camera's view parameters camera.SetViewParameters(new Vector3(0.0f, 0.0f, -5.0f), Vector3.Empty); //initialized the tile-view upperLeft = new Vector2(0, 0); zoomFactor = 1; //Generate a randmoized "game board" using the tiles in the tile set. for (int i = 0; i < gameBoard.GetLength(0); i++) { for (int j = 0; j < gameBoard.GetLength(1); j++) { //add a tile every time a location is set to an index other than "None" gameBoard[i, j].Ground = TileIndex.Grass; if (rand.Next(3) == 1) { if (rand.Next(5) == 1) { gameBoard[i, j].Middle = TileIndex.Flowers; } else { gameBoard[i, j].Middle = TileIndex.Rocks; } } else { gameBoard[i, j].Middle = TileIndex.None; } if (rand.Next(5) == 1) { gameBoard[i, j].Foreground = TileIndex.Cloud; } else { gameBoard[i, j].Foreground = TileIndex.None; } } } //Since we're casting the value 0xFF000000 as an Int32, we must specify //unchecked to avoid compiler errors. unchecked { //The texture used is in the "Managed" pool, meaning that it will automatically //restore itself on a Device.Reset(). This kind of texture requires less manual management than //a default pool texture. tileTexture = TextureLoader.FromFile(e.Device, Utility.FindMediaFile("tiles.tga"), 320, 64, 1, Usage.None, Format.A8R8G8B8, Pool.Managed, Filter.None, Filter.None, (int)0xFF000000); //create the example tile set exampleTileSet = new UniformTileSet(tileTexture, 256, 64); exampleTileSet.CreateTiles(tileWidth, tileHeight, 0, 0, 4, 1); UpdateVisibleTiles(); //Create the texture for the moving sprite mosquitoTexture = TextureLoader.FromFile(e.Device, Utility.FindMediaFile("mosquito.bmp"), 160, 100, 0, Usage.None, Format.A8R8G8B8, Pool.Managed, Filter.None, Filter.None, (int)(0xFF000000)); movingSprite = new MovingSprite(mosquitoTexture, new System.Drawing.Rectangle(0, 0, 160, 100), rand, 200f); //create the texture for our animated sprite donutTexture = TextureLoader.FromFile(e.Device, Utility.FindMediaFile("donut.bmp"), 640, 384, 0, Usage.None, Format.A8R8G8B8, Pool.Managed, Filter.None, Filter.None, (int)(0xFF000000)); animatedSprite = new AnimatedSprite(donutTexture, 64, 64, 6, 10, 60, 0); } //Create a Canvas and a particle effect object for the //canvas-based per-pixel effect canvas = new Canvas(e.Device, 640, 480); effect = new ParticleEffect(6, 6, canvas, rand, 4f, 20); }
private static void inputError(Location[,] world) { Console.SetCursorPosition(0, (world.GetLength(0)) * 3 + 5); Console.Write("Bad Key Entry, please try again!"); }