public bool InRange(Place place) { if (place.x < 0 || place.x >= w) return false; if (place.y < 0 || place.y >= h) return false; if (map[place.x, place.y] == Cell.none) return true; if (map[place.x, place.y] == Cell.here) return true; return false; }
public bool Init(int level_nr, out int widht, out int height) { LevelFile level = new LevelFile(); map = level.LoadLevel(level_nr); if (map == null) { widht = 0; height = 0; return false; } widht = w = map.GetLength(0); height = h= map.GetLength(1); top = new Cell[widht, height]; for (int x = 0; x < w; x++) for (int y = 0; y < h; y++) { switch(map[x,y]) { case Cell.user: mouse = new Place(x, y); map[x, y] = Cell.none; top[x, y] = Cell.user; break; case Cell.none: case Cell.wall: case Cell.here: top[x, y] = Cell.none; break; case Cell.abox: top[x, y] = Cell.abox; map[x, y] = Cell.none; break; case Cell.done: top[x, y] = Cell.abox; map[x, y] = Cell.here; break; } } return true; }
public string MoveMouse(Place start, Place finish) { Queue<Brain> queue = new Queue<Brain>(); // очередь для организации алгоритма поиска вширину List<Place> visited = new List<Place>(); // список вершин, гду мы уже были queue.Clear(); visited.Clear(); Brain brain; brain.x = start.x; brain.y = start.y; brain.path = ""; Place place; queue.Enqueue(brain); while (queue.Count > 0) { brain = queue.Dequeue(); foreach (Brain side in directions) { place.x = brain.x + side.x; place.y = brain.y + side.y; if (!InRange(place)) continue; if (visited.Contains(place)) continue; visited.Add(place); Brain step = new Brain(place, brain.path + side.path); if (place.Equals(finish)) return step.path; queue.Enqueue(step); } } return "-"; }
public void ShowItem(Place place, Cell item) { box[place.x, place.y].Image = CellToPicture(item); }
private void Picture_MouseDorbleClick(object sender, MouseEventArgs e) { abox = (Place)((PictureBox)sender).Tag; }
public Brain(Place place, string p) { this.x = place.x; this.y = place.y; path = p; }
public string MoveAbox(Place mouse, Place start, Place finish) { // mouse - где находится мышка, // start - где находится ящик, // finish - куда поместить ящик if (start.x == finish.x && start.y == finish.y) return ""; top[start.x, start.y] = Cell.none; bool[, , ,] visited = new bool[w, h, w, h]; Queue<Brain> queue = new Queue<Brain>(); // очередь для организации алгоритма поиска вширину // Dictionary<MouseAbox, bool> visited = new Dictionary<MouseAbox, bool>(); // список положения мышки и ящика, гду мы уже были queue.Clear(); //visited.Clear(); Brain brain; brain.mouse = mouse; brain.abox = start; brain.path = ""; Place newMouse; Place newAbox; queue.Enqueue(brain); while (queue.Count > 0) { brain = queue.Dequeue(); foreach (Dirs side in directions) { newMouse.x = brain.mouse.x + side.x; newMouse.y = brain.mouse.y + side.y; if (!InRange(newMouse)) continue; if (newMouse.x == brain.abox.x && newMouse.y == brain.abox.y) { newAbox.x = newMouse.x + side.x; newAbox.y = newMouse.y + side.y; if (!InRange(newAbox)) continue; } else newAbox = brain.abox; #region для работы со списком Dictionary // MouseAbox ma; //ma.mouse = newMouse; //ma.abox = newAbox; //if (visited.ContainsKey(ma)) // continue; //visited.Add(ma, false); #endregion if (visited[newMouse.x, newMouse.y, newAbox.x, newAbox.y]) continue; visited[newMouse.x, newMouse.y, newAbox.x, newAbox.y] = true; Brain step = new Brain(newMouse, newAbox, brain.path + side.path); if (newAbox.x == finish.x && newAbox.y == finish.y) { top[start.x, start.y] = Cell.abox; return step.path; } queue.Enqueue(step); } } top[start.x, start.y] = Cell.abox; return "-"; }
public Brain(Place mouse, Place abox, string p) { this.mouse = mouse; this.abox = abox; path = p; }
bool InRange(Place place) { if (place.x < 0 || place.x >= w) return false; if (place.y < 0 || place.y >= h) return false; if (map[place.x, place.y] == Cell.none && (top[place.x, place.y] == Cell.none || top[place.x, place.y] == Cell.user1)) return true; if (map[place.x, place.y] == Cell.here && (top[place.x, place.y] == Cell.none || top[place.x, place.y] == Cell.user1)) return true; return false; }
public void Step(int sx, int sy) { Place place = new Place(mouse.x + sx, mouse.y + sy); if (!InRange(place)) return; if(top[place.x, place.y] == Cell.none) { top[mouse.x, mouse.y] = Cell.none; ShowMapTop(mouse.x, mouse.y); top[place.x, place.y] = Cell.user; ShowMapTop(place.x, place.y); mouse = place; } if (top[place.x, place.y] == Cell.abox) { Place after = new Place(place.x + sx, place.y + sy); if (!InRange(after)) return; if (top[after.x, after.y] != Cell.none) return; if (map[place.x, place.y] == Cell.here) placed--; if (map[after.x, after.y] == Cell.here) placed++; ShowStat(placed, totals); top[mouse.x, mouse.y] = Cell.none; ShowMapTop(mouse.x, mouse.y); top[place.x, place.y] = Cell.user; ShowMapTop(place.x, place.y); top[after.x, after.y] = Cell.abox; ShowMapTop(after.x, after.y); mouse = place; } }
public string SolveMouse(Place to_place) { if (!InRange(to_place)) return ""; MouseSolver solver = new MouseSolver(map, top); return solver.MoveMouse(mouse, to_place); }
public string SolveAbox(Place abox, Place to_place) { if (top[abox.x, abox.y] != Cell.abox) return ""; if (!InRange(to_place)) return ""; AboxSolver solver = new AboxSolver(map, top); return solver.MoveAbox(mouse, abox, to_place); }