// 建立測試File public void CreateTestFile() { var fileModel = new FileParam { Creator = "Test", FileName = "TestFile", Row = 5, Column = 5, Editor = new List <UserParam>() }; List <CellParam> textList = new List <CellParam>(); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { var cellModel = new CellParam(); cellModel.LockState = true; cellModel.Text = ""; cellModel.CellName = $"{i}{j}"; textList.Add(cellModel); } } fileModel.TextList = textList; list.Add(fileModel); }
public virtual CellParam ChoiceCell(Steps step, int line, int dept, List <List <CellParam> > cellArray, int playerPoints, int compPoints) { dept++; var bestScore1 = -9999; var bestScore2 = -9999; var bestChoice = new CellParam() { Id = -1 }; var currScore1 = MemScore1; var currScore2 = MemScore2; int temp; for (int i = 0; i < _poleRazmer; i++) { if (step == Steps.player) { if (!cellArray[line][i].Touch) { var checkNumber = cellArray[line][i].Number; if (Mathf.Abs(checkNumber) == 99) { continue; } temp = checkNumber; checkNumber = 99; MemScore1 = currScore1; MemScore2 = currScore2 + temp; if (dept < _maxDepth) { var choice = ChoiceCell(Steps.ai, i, dept, cellArray, playerPoints, compPoints); if ((MemScore1 - MemScore2 < bestScore1 - bestScore2 && (choice.Id != -1 || (choice.Id == -1 && MemScore1 + playerPoints < MemScore2 + compPoints)) || bestScore1 == -9999)) { bestScore1 = MemScore1; bestScore2 = MemScore2; bestChoice = cellArray[line][i]; if (choice.Id == -1) { bestScore2 += _poleRazmer * 5; } } } else { if ((MemScore1 - MemScore2 < bestScore1 - bestScore2)) { bestScore1 = MemScore1; bestScore2 = MemScore2; bestChoice = cellArray[line][i]; } } checkNumber = temp; } } else { if (!cellArray[i][line].Touch) { var checkNumber = cellArray[i][line].Number; if (Mathf.Abs(checkNumber) == 99) { continue; } temp = checkNumber; checkNumber = 99; MemScore1 = currScore1 + temp; MemScore2 = currScore2; if (dept < _maxDepth) { var choice = ChoiceCell(Steps.player, i, dept, cellArray, playerPoints, compPoints); if ((MemScore1 - MemScore2 > bestScore1 - bestScore2 && (choice.Id != -1 || (choice.Id == -1 && MemScore1 + playerPoints > MemScore2 + compPoints)) || bestScore1 == -9999)) { bestScore1 = MemScore1; bestScore2 = MemScore2; bestChoice = cellArray[i][line]; if (choice.Id == -1) { bestScore1 += _poleRazmer * 5; } } } else { if ((MemScore1 - MemScore2 > bestScore1 - bestScore2)) { bestScore1 = MemScore1; bestScore2 = MemScore2; bestChoice = cellArray[i][line]; } } checkNumber = temp; } } } if (bestScore1 != -9999) { MemScore1 = bestScore1; MemScore2 = bestScore2; } return(bestChoice); }
public virtual CellParam ChoiceCell(Steps step, int line, int dept, List<List<CellParam>> cellArray, int playerPoints, int compPoints) { dept++; var bestScore1 = -9999; var bestScore2 = -9999; var bestChoice = new CellParam() { Id = -1 }; var currScore1 = MemScore1; var currScore2 = MemScore2; int temp; for (int i = 0; i < _poleRazmer; i++) { if (step == Steps.player) { if (!cellArray[line][i].Touch) { var checkNumber = cellArray[line][i].Number; if (Mathf.Abs(checkNumber) == 99) continue; temp = checkNumber; checkNumber = 99; MemScore1 = currScore1; MemScore2 = currScore2 + temp; if (dept < _maxDepth) { var choice = ChoiceCell(Steps.ai, i, dept, cellArray, playerPoints, compPoints); if ((MemScore1 - MemScore2 < bestScore1 - bestScore2 && (choice.Id != -1 || (choice.Id == -1 && MemScore1 + playerPoints < MemScore2 + compPoints)) || bestScore1 == -9999)) { bestScore1 = MemScore1; bestScore2 = MemScore2; bestChoice = cellArray[line][i]; if (choice.Id == -1) bestScore2 += _poleRazmer * 5; } } else { if ((MemScore1 - MemScore2 < bestScore1 - bestScore2)) { bestScore1 = MemScore1; bestScore2 = MemScore2; bestChoice = cellArray[line][i]; } } checkNumber = temp; } } else { if (!cellArray[i][line].Touch) { var checkNumber = cellArray[i][line].Number; if (Mathf.Abs(checkNumber) == 99) continue; temp = checkNumber; checkNumber = 99; MemScore1 = currScore1 + temp; MemScore2 = currScore2; if (dept < _maxDepth) { var choice = ChoiceCell(Steps.player, i, dept, cellArray, playerPoints, compPoints); if ((MemScore1 - MemScore2 > bestScore1 - bestScore2 && (choice.Id != -1 || (choice.Id == -1 && MemScore1 + playerPoints > MemScore2 + compPoints)) || bestScore1 == -9999)) { bestScore1 = MemScore1; bestScore2 = MemScore2; bestChoice = cellArray[i][line]; if (choice.Id == -1) bestScore1 += _poleRazmer * 5; } } else { if ((MemScore1 - MemScore2 > bestScore1 - bestScore2)) { bestScore1 = MemScore1; bestScore2 = MemScore2; bestChoice = cellArray[i][line]; } } checkNumber = temp; } } } if (bestScore1 != -9999) { MemScore1 = bestScore1; MemScore2 = bestScore2; } return bestChoice; }
private bool SearchInList(List<CellParam> List, CellParam Element) { foreach (CellParam el in List) { if (el.ID == Element.ID) return true; } return false; }
public void GeneratePath(Cell startPoint, Cell finalPoint) { CellParam currentCell = new CellParam(); CellParam finalCell = new CellParam(); List<CellParam> bannedList = new List<CellParam>(); Path = new List<Cell>(); openList = new List<CellParam>(); closeList = new List<CellParam>(); // return empty Way at points on different levels OR // start and final points were not defined if (startPoint.Location.Level != finalPoint.Location.Level || startPoint.ID == 0 && finalPoint.ID == 0) return; currentCell.InitializeCell(); finalCell.InitializeCell(); currentCell.ID = startPoint.ID; openList.Add(currentCell); finalCell.ID = finalPoint.ID; openList.Add(finalCell); while (currentCell.ID != finalPoint.ID) { Cell block = FindCell(openList, currentCell); FindNeighbors(block, finalCell); CellParam CellWithMinF = currentCell; if (!SearchInList(closeList, finalCell)) { CellWithMinF = GetCellWithMinF(bannedList); if (CellWithMinF.Equals(currentCell)) bannedList.Add(currentCell); currentCell = CellWithMinF; } else break; } // No path was found if (closeList.Count == 0) return; currentCell = closeList[closeList.Count - 1]; Path.Add(FindCell(openList, currentCell)); for (int i = closeList.Count - 1; i >= 0; --i) // CloseList[0] == Start Cell { if (closeList[i].ID == FindCell(closeList, Path[Path.Count - 1]).MID) { Cell tempCell = new Cell(); tempCell = FindCell(closeList, closeList[i]); if (!SearchInList(Path, tempCell)) { Path.Add(tempCell); i = closeList.Count; } } } }
private CellParam GetCellWithMinF(List<CellParam> BannedList) { CellParam Cell = new CellParam(); Cell.InitializeCell(); //if (BannedList.Count == 0) // return Cell; int F = Cell.F; int i = openList.Count - 1; while (i != 0) { if (!BannedList.Contains(openList[i])) { Cell = openList[i]; F = Cell.F; break; } --i; } for (i = openList.Count - 1; i > 0; --i) // OpenList[0] == FinishPoint { if (openList[i].F < F && !BannedList.Contains(openList[i])) { F = openList[i].F; Cell = openList[i]; } } return Cell; }
// Find all passable neighbors private void FindNeighbors(Cell Block, CellParam FinalPoint) { CellParam currentCell = FindCell(openList, Block); for (int i = -1; i <= 1; ++i) { for (int j = -1; j <= 1; ++j) { // Skip this cell if (i == 0 && j == 0) continue; GridLocation location = new GridLocation(); location.X = Block.Location.X + i; location.Y = Block.Location.Y + j; location.Level = Block.Location.Level; // Skip non-existed cell if (Map.Instance.GetCell(location).Type == 16) continue; bool isNeighbourReachable = false; switch (i * j) { case -1: if (i == -1) // diagonal moving - left and down { GridLocation locDown = Block.Location; locDown.Y++; GridLocation locLeft = Block.Location; locLeft.X--; // Check cell's passability if ((Block.CanMoveTo(Directions.Down) && Map.Instance.GetCell(locDown).CanMoveTo(Directions.Left)) && (Block.CanMoveTo(Directions.Left) && Map.Instance.GetCell(locLeft).CanMoveTo(Directions.Down))) { isNeighbourReachable = true; } } else // diagonal moving - right and up { GridLocation locUp = Block.Location; locUp.Y--; GridLocation locRight = Block.Location; locRight.X++; if ((Block.CanMoveTo(Directions.Up) && Map.Instance.GetCell(locUp).CanMoveTo(Directions.Right)) && (Block.CanMoveTo(Directions.Right) && Map.Instance.GetCell(locRight).CanMoveTo(Directions.Up))) { isNeighbourReachable = true; } } break; case 1: if (i == -1) // diagonal moving - left and up { GridLocation locUp = Block.Location; locUp.Y--; GridLocation locLeft = Block.Location; locLeft.X--; if ((Block.CanMoveTo(Directions.Left) && Map.Instance.GetCell(locLeft).CanMoveTo(Directions.Up)) && (Block.CanMoveTo(Directions.Up) && Map.Instance.GetCell(locUp).CanMoveTo(Directions.Left))) { isNeighbourReachable = true; } } else // diagonal moving - right and down { GridLocation locDown = Block.Location; locDown.Y++; GridLocation locRight = Block.Location; locRight.X++; if ((Block.CanMoveTo(Directions.Down) && Map.Instance.GetCell(locDown).CanMoveTo(Directions.Right)) && (Block.CanMoveTo(Directions.Right) && Map.Instance.GetCell(locRight).CanMoveTo(Directions.Down))) { isNeighbourReachable = true; } } break; case 0: if (i == 0) // moving up or down { if ((j == -1 && Block.CanMoveTo(Directions.Up)) || (j == 1 && Block.CanMoveTo(Directions.Down))) { isNeighbourReachable = true; } } else // moving - right or left { if ((i == 1 && Block.CanMoveTo(Directions.Right)) || (i == -1 && Block.CanMoveTo(Directions.Left))) { isNeighbourReachable = true; } } break; } if (!isNeighbourReachable) continue; CellParam Cell = new CellParam(); Cell.ID = Map.Instance.GetCell(location).ID; Cell.MID = Block.ID; if (i == 0 || j == 0) Cell.G = currentCell.G + 10; // Vertical or horizontal movement else Cell.G = currentCell.G + 14; // Diagonal movement Cell.H = (Math.Abs(location.X - FindCell(openList, FinalPoint).Location.X) + Math.Abs(location.Y - FindCell(openList, FinalPoint/*, CurrentMap*/).Location.Y)) * 10; Cell.F = Cell.G + Cell.H; if (!SearchInList(openList, Cell) && !SearchInList(closeList, Cell)) { openList.Add(Cell); if (!SearchInList(closeList, currentCell)) closeList.Add(currentCell); if (SearchInList(openList, currentCell)) openList.Remove(currentCell); } if (Cell.ID == FinalPoint.ID) { if (!SearchInList(closeList, currentCell)) closeList.Add(currentCell); closeList.Add(Cell); return; } } } }
private CellParam FindInList(List<CellParam> List, int X, int Y) { CellParam defaultElement = new CellParam(); defaultElement.InitializeCell(); foreach (CellParam element in List) { if (FindCell(List, element).Location.X == X || FindCell(List, element).Location.Y == Y) return element; } return defaultElement; }
private Cell FindCell(List<CellParam> List, CellParam Cell) { Cell Block = new Cell(); Block.Initialize(); for (int i = 0; i < List.Count; ++i) { if (Cell.ID == List[i].ID) { Block = Map.Instance.GetCell(List[i].ID); break; } } return Block; }
private CellParam FindCell(List<CellParam> List, Cell Block) { CellParam Cell = new CellParam(); Cell.InitializeCell(); for (int i = 0; i < List.Count; ++i) { if (Block.ID == List[i].ID) return List[i]; } return Cell; }