⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 circularlinkedlist.cs

📁 十分完善的华容道程序,有多种布局的求解!
💻 CS
字号:
using System;
using HRD.Core;

namespace HRD.CircularList
{
	public class CircularLinkedList : ICircularList
	{
		protected class LinkedListNode
		{
			public Layout info;
			public LinkedListNode link;
		}

		protected LinkedListNode current;
		protected LinkedListNode last;
		protected LinkedListNode allocate;
		protected LinkedListNode end;

		protected int count;
		protected ILayoutFactory layoutFactory;
		protected Mediator _mediator;

		private bool stop = false;
		private int stepCounter = 1;

		#region Propertys

		public Mediator mediator 
		{
			set 
			{
				_mediator = value;
			}
		}

		public int Length
		{
			get 
			{
				return count;
			}
		}

		public bool IsEmpty
		{
			get 
			{
				return (current == null);
			}
		}

		#endregion

		//======================================================
		// 构造函数
		//======================================================
		public CircularLinkedList(ILayoutFactory layoutFactory)
		{
			last = null;
			current = null;
			allocate = null;
			end = null;
			stepCounter = 1;
			count = 0;
			this.layoutFactory = layoutFactory;
		}

		//======================================================
		// 清空节点
		//======================================================
		public void ClearAll()
		{
			last = null;
			current = null;
			allocate = null;
			end = null;
			stop = false;
			count = 0;
			stepCounter = 1;
		}

		//======================================================
		// 初始化各指针
		//======================================================
		public ChessStep Initialize()
		{
			if(current == null)
				throw new Exception("Empty CircularLinked List!");

			last = current;
			allocate = current;
			stop = false;

			current.info.InitLayoutMap();
			
			ChessStep c = new ChessStep();
			c.layout = current.info.ToInt();

			stepCounter = 1;
			return c;
		}

		//======================================================
		// 获取下一个可用空节点
		//======================================================
		public Layout AllocateLayout()
		{
			if(allocate.link == current)
			{
				end = allocate;
				insertNode(layoutFactory.Create());
			}
			
			return allocate.link.info;
		}

		//======================================================
		// 确认分配的空间
		//======================================================
		public void ConfirmAllocation()
		{
			allocate = allocate.link;
		}

		//======================================================
		// 处理
		//======================================================
		public bool BeginProcess()
		{
			while(current != last.link)
			{
				current.info.CheckAvailableSteps();

				if(stop)
					return true;

				current = current.link;
				if(current != last.link)
					_mediator.MoveCurrentToNext();
			}
			if(this.NextStep())
			{
				return BeginProcess();
			}
			else
				return false;
		}

		//======================================================
		// 停止
		//======================================================
		public void Stop()
		{
			this.stop = true;
		}

		//======================================================
		// 插入节点
		//======================================================
		public void insertNode()
		{
			this.insertNode(layoutFactory.Create());
		}

		private void insertNode(Layout newitem)
		{
			LinkedListNode trail;
			LinkedListNode newNode;
    
			newNode = new LinkedListNode();
			newNode.info = newitem;
			newNode.link = null;
    
			if(current == null)
			{   
				current = newNode;
				current.link = newNode;
				end = current;
			}
			else
			{
				trail = end.link;
				end.link = newNode;
				end = newNode;
				newNode.link = trail;
			}
			count++;
		}

		//======================================================
		// 到下一步
		//======================================================
		private bool NextStep()
		{
			stepCounter++;
			_mediator.HandleInfo(stepCounter);

			if(allocate == last)
				return false; //棋局无解

			current = last.link;
			last = allocate;
			_mediator.MoveCurrentToNext();
			return true;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -