private List <int> RunMain(Game game) { Depth = 0; Width = 0; var state = new GameState2(game); var visitedStates = new List <string> { state.ToString() }; var states = new List <GameState2> { state }; while (true) { Depth++; if (!states.Any()) { return(null); } if (states.Count > Width) { Width = states.Count; } //this.handler?.Invoke(string.Format("深度{0},当前队列{1},队列峰值{2}", Depth, states.Count, Width)); var newStates = new List <GameState2>(); foreach (var stt in states) { var moves = stt.GetMovableStep(); foreach (var move in moves) { var newState = stt.Move(move); if (newState != null) { if (newState.Check()) { //var pushs = newState.GetPushs(); return(newState.GetPaths()); } var str = newState.ToString(); if (!visitedStates.Contains(str)) { visitedStates.Add(str); if (!newState.HasDeathArea()) { newStates.Add(newState); } } } } } states = newStates; } }
/// <summary> /// 移动一步 /// </summary> /// <param name="ms">方向:0左;1上;2右;3下</param> public GameState2 Move(Step ms) { var state = new GameState2(this, ms); var lp = Y * W + X; state.Map[lp] = state.Map[lp] == 6 ? 3 : 0; int i = ms.Position; state.Map[i] = state.Map[i] == 3 ? 6 : 5; var dir = ms.Direction; int v = dir == 0 ? -1 : dir == 1 ? -W : dir == 2 ? 1 : W; if (state.Map[i + v] == 1) { return(null); } if (state.Map[i + v] == 2 || state.Map[i + v] == 4) { if (state.Map[i + 2 * v] == 1 || state.Map[i + 2 * v] == 2 || state.Map[i + 2 * v] == 4) { return(null); } state.Map[i] = state.Map[i] == 6 ? 3 : 0; state.Map[i + v] = state.Map[i + v] == 4 ? 6 : 5; if (state.Map[i + 2 * v] == 3) { state.Map[i + 2 * v] = 4; } else if (state.Map[i + 2 * v] == 0) { state.Map[i + 2 * v] = 2; } state.HasPush = true; } else if (state.Map[i + v] == 0 || state.Map[i + v] == 3) { state.Map[i] = state.Map[i] == 6 ? 3 : 0; state.Map[i + v] = state.Map[i + v] == 3 ? 6 : 5; } state.X = (i + v) % W; state.Y = (i + v) / W; state.beforeMove = GetMoveFreeArea(lp, i); state.CheckFreeArea(i + v); return(state); }
public GameState2(GameState2 game, Step move) : base(game, -1) { this.LastState = game; this.LastMove = move; }