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 Maze { 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()
- {
- int countCrossWay = 0;
- if (Maze.IsDownFree())
- {
- countCrossWay++;
- }
- if (Maze.IsLeftFree())
- {
- countCrossWay++;
- }
- if (Maze.IsRightFree())
- {
- countCrossWay++;
- }
- if (Maze.IsUpFree())
- {
- countCrossWay++;
- }
- if (countCrossWay > 2)
- {
- stackCrossWay.Push(stackWay.Count);
- }
- }
- //right - 1 down - 2 left - 3 up - 4
- public void SearchPath() //1 - wall, 0 - road
- {
- Maze.OnExit += GameOver;
- while (onExit == false)
- {
- CrossWay();
- if(Maze.IsRightFree() && stackWay.Peek() != 3)
- {
- heroX++;
- stackWay.Push(1);
- Maze.MoveRight();
- }
- else if(Maze.IsDownFree() && stackWay.Peek() != 4)
- {
- heroY--;
- stackWay.Push(2);
- Maze.MoveDown();
- }
- else if(Maze.IsLeftFree() && stackWay.Peek() != 1)
- {
- heroX--;
- stackWay.Push(3);
- Maze.MoveLeft();
- }
- else if(Maze.IsUpFree() && stackWay.Peek() != 2)
- {
- heroY++;
- stackWay.Push(4);
- Maze.MoveUp();
- }
- else //Тупик = возврат к предыдущей развилке.
- {
- while(stackWay.Count != stackCrossWay.Peek())
- {
- if(stackWay.Peek() == 1)
- {
- Maze.MoveLeft();
- stackWay.Pop();
- }
- else if (stackWay.Peek() == 2)
- {
- Maze.MoveUp();
- stackWay.Pop();
- }
- else if (stackWay.Peek() == 3)
- {
- Maze.MoveRight();
- stackWay.Pop();
- }
- else if (stackWay.Peek() == 4)
- {
- Maze.MoveDown();
- stackWay.Pop();
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement