Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using CommonPart;
- namespace herodll
- {
- public class Hero : IHero
- {
- private int heroX = 0;
- private int heroY = 0;
- public ILabirint Labirint { get; set; }
- private Stack<int> stackWay = new Stack<int>();
- private Stack<int> stackCrossWay = new Stack<int>();
- private List<List<int>> usedCells = new List<List<int>>();
- private bool onExit = false;
- public void GameOver(object sender, EventArgs e) //2 - exit
- {
- onExit = true;
- }
- public void CrossWay()
- {
- if (!stackCrossWay.Contains(stackWay.Count))
- {
- int countCrossWay = 0;
- if (Labirint.IsDownFree())
- {
- countCrossWay++;
- }
- if (Labirint.IsLeftFree())
- {
- countCrossWay++;
- }
- if (Labirint.IsRightFree())
- {
- countCrossWay++;
- }
- if (Labirint.IsUpFree())
- {
- countCrossWay++;
- }
- if (countCrossWay > 2)
- {
- stackCrossWay.Push(stackWay.Count);
- }
- }
- }
- //right - 1 down - 2 left - 3 up - 4
- public void SearchPath() //1 - wall, 0 - road
- {
- Labirint.OnExit += GameOver;
- stackWay.Push(0);
- bool right = false;
- bool down = false;
- bool left = false;
- bool up = false;
- while (onExit == false)
- {
- CrossWay();
- if (Labirint.IsRightFree() && stackWay.Peek() != 3 && right == false)
- {
- stackWay.Push(1);
- Labirint.MoveRight();
- }
- else if (Labirint.IsDownFree() && stackWay.Peek() != 4 && down == false)
- {
- stackWay.Push(2);
- Labirint.MoveDown();
- }
- else if (Labirint.IsLeftFree() && stackWay.Peek() != 1 && left == false)
- {
- stackWay.Push(3);
- Labirint.MoveLeft();
- }
- else if (Labirint.IsUpFree() && stackWay.Peek() != 2 && up == false)
- {
- stackWay.Push(4);
- Labirint.MoveUp();
- }
- else //Тупик = возврат к предыдущей развилке.
- {
- int lastStep = 0;
- while (stackWay.Count != stackCrossWay.Peek())
- {
- if (stackWay.Peek() == 1)
- {
- Labirint.MoveLeft();
- stackWay.Pop();
- lastStep = 1;
- }
- else if (stackWay.Peek() == 2)
- {
- Labirint.MoveUp();
- stackWay.Pop();
- lastStep = 2;
- }
- else if (stackWay.Peek() == 3)
- {
- Labirint.MoveRight();
- stackWay.Pop();
- lastStep = 3;
- }
- else if (stackWay.Peek() == 4)
- {
- Labirint.MoveDown();
- stackWay.Pop();
- lastStep = 4;
- }
- }
- if (lastStep == 1)
- {
- right = true;
- }
- else if (lastStep == 2)
- {
- down = true;
- }
- else if (lastStep == 3)
- {
- left = true;
- }
- else if (lastStep == 4)
- {
- up = true;
- }
- if (stackWay.Peek() == 1)
- {
- right = true;
- }
- else if (stackWay.Peek() == 2)
- {
- down = true;
- }
- else if (stackWay.Peek() == 3)
- {
- left = true;
- }
- else if (stackWay.Peek() == 4)
- {
- up = true;
- }
- if (right && down && left && up)
- {
- stackCrossWay.Pop();
- while (stackWay.Count != stackCrossWay.Peek())
- {
- if (stackWay.Peek() == 1)
- {
- Labirint.MoveLeft();
- stackWay.Pop();
- }
- else if (stackWay.Peek() == 2)
- {
- Labirint.MoveUp();
- stackWay.Pop();
- }
- else if (stackWay.Peek() == 3)
- {
- Labirint.MoveRight();
- stackWay.Pop();
- }
- else if (stackWay.Peek() == 4)
- {
- Labirint.MoveDown();
- stackWay.Pop();
- }
- }
- right = false;
- down = false;
- left = false;
- up = false;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement