private void ParseSaveFormat(string saveInput) { string[] dimensions = saveInput.Split('\n'); List <List <double> > rehydratedList = new List <List <double> >((int)(boardType.FOUR_BY_FOUR) + 1); //Clear existing list. for (int i = 0; i < rehydratedList.Capacity; i++) { rehydratedList.Add(new List <double>(0)); } for (int i = 0; i < dimensions.Length - 1; i += 2) { string[] dimension = dimensions[i].Split(':'); boardType type = GetValueFromDescription <boardType>(dimension[0]); string[] lengths = dimensions[i + 1].Split(' '); List <double> lengthList = new List <double>(); foreach (string length in lengths) { double valueToAdd; if (Double.TryParse(length, out valueToAdd)) { lengthList.Add(valueToAdd); } } rehydratedList[(int)type] = lengthList; } //Finally, now that we know we won't have any errors, go ahead and replace the current values... lengthsNeeded = rehydratedList; //And update the display. UpdateLengthsBox(); }
internal log(int pieceX, int pieceY, boardType state, boardType lastState, actionType action, int count) { this.pieceX = pieceX; this.pieceY = pieceY; this.state = state; this.lastState = lastState; this.action = action; this.count = count; }
/// <summary> /// 递归判断 /// </summary> /// <param name="pieceX"></param> /// <param name="pieceY"></param> /// <param name="boardType"></param> /// <param name="cp"></param> private void CheckPoint(int pieceX, int pieceY, boardType boardType, List <changePoint> cp) { if (cp.Where(o => o.life > 0).Count() > 0) { return; } changePoint p = new changePoint(pieceX, pieceY, boardType); cp.Add(p); for (int X = -1; X <= 1; X++) { for (int Y = -1; Y <= 1; Y++) { //只剩下上下左右四个位置 if (X != 0 && Y != 0 || X == 0 && Y == 0) { continue; } if (pieceX + X < 0 || pieceX + X >= this.boardX || pieceY + Y < 0 || pieceY + Y >= this.boardY) { continue; } //集合里已有了就跳过 if (cp.Where(o => o.pieceX == pieceX + X && o.pieceY == pieceY + Y).Count() > 0) { continue; } if (state[pieceX + X, pieceY + Y] == boardType.Blank) { p.setLife(); } else if (state[pieceX + X, pieceY + Y] == boardType) { CheckPoint(pieceX + X, pieceY + Y, state[pieceX + X, pieceY + Y], cp); } else { continue; } } } }
/// <summary> /// 下棋 /// </summary> /// <param name="pieceX"></param> /// <param name="pieceY"></param> /// <param name="boardType"></param> /// <returns></returns> public bool SetState(int pieceX, int pieceY, boardType boardType) { //已被占用返回false if (this.state[pieceX, pieceY] != boardType.Blank) { return(false); } //如有获胜状态,游戏结束 if (this.logs.Where(o => o.action == actionType.Victory).Count() > 0) { return(false); } this.state[pieceX, pieceY] = boardType; this.count++; this.logs.Add(new log(pieceX, pieceY, boardType, boardType.Blank, actionType.Add, this.count)); DoJudgmentLogic(pieceX, pieceY, boardType); return(true); }
/// <summary> /// 判断逻辑 /// </summary> /// <param name="pieceX"></param> /// <param name="pieceY"></param> /// <param name="boardType"></param> protected override void DoJudgmentLogic(int pieceX, int pieceY, boardType boardType) { //待处理集合 List <changePoint> changePoints = new List <changePoint>(); //上下左右相同棋子集合 List <changePoint> equalPoints = new List <changePoint>(); changePoint pOriginal = new changePoint(pieceX, pieceY, boardType); equalPoints.Add(pOriginal); for (int X = -1; X <= 1; X++) { for (int Y = -1; Y <= 1; Y++) { //只剩下上下左右四个位置 if (X != 0 && Y != 0 || X == 0 && Y == 0) { continue; } if (pieceX + X < 0 || pieceX + X >= this.boardX || pieceY + Y < 0 || pieceY + Y >= this.boardY) { continue; } //有气 if (state[pieceX + X, pieceY + Y] == boardType.Blank) { pOriginal.setLife(); } //相同棋子则递归看有没有气(相同棋子默认连在一起) else if (state[pieceX + X, pieceY + Y] == boardType) { CheckPoint(pieceX + X, pieceY + Y, state[pieceX + X, pieceY + Y], equalPoints); } //不同棋子则分开递归看有没有气(不同棋子默认不连在一起) else if (state[pieceX + X, pieceY + Y] != boardType) { List <changePoint> unequalPoints = new List <changePoint>(); CheckPoint(pieceX + X, pieceY + Y, state[pieceX + X, pieceY + Y], unequalPoints); if (unequalPoints.Where(o => o.life > 0).Count() == 0) { changePoints.AddRange(unequalPoints); } } else { continue; } } } //如果没有吃子而且本身没气了那就是自杀 if (changePoints.Count() == 0 && equalPoints.Where(o => o.life > 0).Count() == 0) { changePoints.AddRange(equalPoints); } foreach (var changePoint in changePoints.GroupBy(o => new { o.pieceX, o.pieceY, o.state }).Select(o => o.First())) { this.logs.Add(new log(changePoint.pieceX, changePoint.pieceY, boardType.Blank, changePoint.state, actionType.Remove, this.count)); this.state[changePoint.pieceX, changePoint.pieceY] = boardType.Blank; } this.records.Add((boardType[, ]) this.state.Clone()); }
internal changePoint(int pieceX, int pieceY, boardType state) : base(pieceX, pieceY, state) { this.life = 0; }
/// <summary> /// 判断逻辑 /// </summary> /// <param name="pieceX"></param> /// <param name="pieceY"></param> /// <param name="boardType"></param> protected abstract void DoJudgmentLogic(int pieceX, int pieceY, boardType boardType);
internal pendingPoint(int pieceX, int pieceY, boardType state) { this.pieceX = pieceX; this.pieceY = pieceY; this.state = state; }
/// <summary> /// 判断逻辑 /// </summary> /// <param name="pieceX"></param> /// <param name="pieceY"></param> /// <param name="boardType"></param> protected override void DoJudgmentLogic(int pieceX, int pieceY, boardType boardType) { List <pendingPoint> winPoints = new List <pendingPoint>(); //横向 List <pendingPoint> t = new List <pendingPoint>(); //纵向 List <pendingPoint> p = new List <pendingPoint>(); //左 List <pendingPoint> l = new List <pendingPoint>(); //右 List <pendingPoint> r = new List <pendingPoint>(); for (int X = 0; X < this.boardX; X++) { for (int Y = 0; Y < this.boardY; Y++) { //横向 if (X != 0 && state[X, Y] != state[X - 1, Y]) { if (t.Where(o => o.pieceY == Y).Count() >= this.winLength) { if (state[X - 1, Y] != boardType.Blank) { winPoints.AddRange(t.Where(o => o.pieceY == Y)); } } t.RemoveAll(o => o.pieceY == Y); } t.Add(new pendingPoint(X, Y, state[X, Y])); if (X == this.boardX - 1) { if (t.Where(o => o.pieceY == Y).Count() >= this.winLength) { if (state[X - 1, Y] != boardType.Blank) { winPoints.AddRange(t.Where(o => o.pieceY == Y)); } } t.RemoveAll(o => o.pieceY == Y); } //纵向 if (Y != 0 && state[X, Y] != state[X, Y - 1]) { if (p.Where(o => o.pieceX == X).Count() >= this.winLength) { if (state[X, Y - 1] != boardType.Blank) { winPoints.AddRange(p.Where(o => o.pieceX == X)); } } p.RemoveAll(o => o.pieceX == X); } p.Add(new pendingPoint(X, Y, state[X, Y])); if (Y == this.boardY - 1) { if (p.Where(o => o.pieceX == X).Count() >= this.winLength) { if (state[X, Y - 1] != boardType.Blank) { winPoints.AddRange(p.Where(o => o.pieceX == X)); } } p.RemoveAll(o => o.pieceX == X); } //左 if (X != 0 && Y < this.boardY - 1 && state[X, Y] != state[X - 1, Y + 1]) { if (l.Where(o => o.pieceY == X + Y - o.pieceX).Count() >= this.winLength) { if (state[X - 1, Y + 1] != boardType.Blank) { winPoints.AddRange(l.Where(o => o.pieceY == X + Y - o.pieceX)); } } l.RemoveAll(o => o.pieceY == X + Y - o.pieceX); } l.Add(new pendingPoint(X, Y, state[X, Y])); if (X == this.boardX - 1 || Y == 0) { if (l.Where(o => o.pieceY == X + Y - o.pieceX).Count() >= this.winLength) { if (state[X - 1, Y + 1] != boardType.Blank) { winPoints.AddRange(l.Where(o => o.pieceY == X + Y - o.pieceX)); } } l.RemoveAll(o => o.pieceY == X + Y - o.pieceX); } //右 if (X != 0 && Y != 0 && state[X, Y] != state[X - 1, Y - 1]) { if (r.Where(o => o.pieceY == Y - X + o.pieceX).Count() >= this.winLength) { if (state[X - 1, Y - 1] != boardType.Blank) { winPoints.AddRange(r.Where(o => o.pieceY == Y - X + o.pieceX)); } } r.RemoveAll(o => o.pieceY == Y - X + o.pieceX); } r.Add(new pendingPoint(X, Y, state[X, Y])); if (X == this.boardX - 1 || Y == this.boardY - 1) { if (r.Where(o => o.pieceY == Y - X + o.pieceX).Count() >= this.winLength) { if (state[X - 1, Y - 1] != boardType.Blank) { winPoints.AddRange(r.Where(o => o.pieceY == Y - X + o.pieceX)); } } r.RemoveAll(o => o.pieceY == Y - X + o.pieceX); } } } foreach (var winPoint in winPoints) { logs.Add(new log(winPoint.pieceX, winPoint.pieceY, winPoint.state, winPoint.state, actionType.Victory, this.count)); } this.records.Add((boardType[, ]) this.state.Clone()); }