public void SetNodeParent(MoveTree parentMoveTree, MoveTree moveTree) { if (TreeSize == 1) { foreach (var move in moveTree.Moves) { move.Node.transform.SetParent(gameObject.transform, false); } return; } if (parentMoveTree != null) { if (moveTree.CurrentBoardLogic != null && parentMoveTree.CurrentBoardLogic == null) { moveTree.Node.transform.SetParent(gameObject.transform, false); } } foreach (var move in moveTree.Moves) { if (moveTree.Node != null) { move.Node.transform.SetParent(moveTree.Node.transform, false); } if (move.Moves.Count == 0) { continue; } SetNodeParent(moveTree, move); } }
public int MinMax(MoveTree moveTree, char turn) { if (moveTree.MovesAhead == 0) { return(moveTree.ValueOfMovement); } if (turn == 'B') { int value = -10000; foreach (var move in moveTree.Moves) { value = Math.Max(value, MinMax(move, 'C')); } moveTree.ValueOfMovement = value; return(value); } else { int value = 10000; foreach (var move in moveTree.Moves) { value = Math.Min(value, MinMax(move, 'B')); } moveTree.ValueOfMovement = value; return(value); } }
public GameObject CreateNode(MoveTree moveTree, int numberInRow, int row) { GameObject Node = new GameObject("TreeNode" + "R(" + row + ")" + "(" + numberInRow + ")"); //Add Components Node.AddComponent <RectTransform>(); Node.GetComponent <RectTransform>().sizeDelta = new Vector2(10, 10); Node.AddComponent <CanvasRenderer>(); Node.AddComponent <Image>(); Node.GetComponent <Image>().sprite = Resources.Load <Sprite>("Checker"); Node.GetComponent <Image>().useSpriteMesh = true; Node.AddComponent <CanvasGroup>(); Node.AddComponent <NodeData>(); Node.GetComponent <NodeData>().StartingCoordinates = moveTree.StartingCoordinates; Node.GetComponent <NodeData>().Moveset = moveTree.MovesInOneTurn; Node.GetComponent <NodeData>().NumberInRow = numberInRow; Node.GetComponent <NodeData>().Row = row; Node.GetComponent <NodeData>().ValueOfMovement = moveTree.ValueOfMovement; Node.GetComponent <RectTransform>().localPosition = new Vector3(0, 0, 0); Node.GetComponent <Image>().color = new Color32(0, 0, 0, 255); return(Node); }
static void Main(string[] args) { Board board = new Board(); /* board.MakeMove(MoveParser.ParseMove("Na3", board)); * board.MakeMove(MoveParser.ParseMove("Nc6", board)); * board.MakeMove(MoveParser.ParseMove("Rb1", board)); * board.MakeMove(MoveParser.ParseMove("b6", board)); * board.MakeMove(MoveParser.ParseMove("Nb5", board)); * board.MakeMove(MoveParser.ParseMove("Ba6", board)); * board.MakeMove(MoveParser.ParseMove("Nxa7", board)); * board.MakeMove(MoveParser.ParseMove("Nxa7", board)); * board.MakeMove(MoveParser.ParseMove("Nf3", board)); * board.MakeMove(MoveParser.ParseMove("d5", board)); * board.MakeMove(MoveParser.ParseMove("Rg1", board)); * board.MakeMove(MoveParser.ParseMove("Qd6", board)); * board.MakeMove(MoveParser.ParseMove("h3", board)); * board.MakeMove(MoveParser.ParseMove("O-O-O", board)); * board.MakeMove(MoveParser.ParseMove("Nh4", board)); * board.MakeMove(MoveParser.ParseMove("Re8", board)); * */ Thread T = new Thread(() => { var calculated = new Dictionary <BigInteger, MoveTree>(); int depth = 4; MoveTree moveTree = new MoveTree(depth, board, calculated); var i = 1; while (true) { var bestMove = moveTree.BestMove(depth); Console.WriteLine($"{i}.\t{bestMove}"); board.MakeMove(bestMove); moveTree = moveTree[bestMove]; moveTree.Extend(depth - 1, board); IMove move = null; while (move == null || board.IsCheckedAfterMove(move)) { Console.Write("\t"); move = ReadMove(board); if (move == null || board.IsCheckedAfterMove(move)) { Console.WriteLine("Incorrect move, enter correct one."); } } board.MakeMove(move); moveTree = moveTree[move]; moveTree.Extend(depth - 1, board); i++; } }, 2097152); T.Start(); }
public MoveTreeCompletionData (MoveTree aInitData) { tree = aInitData; BetterList<MoveTreeItem> list = aInitData.Moves; for(int i = 0 ; i < list.size ; i++ ) { _items.Add( new MoveTreeCompletionDataRow( list[i] ) ); } for(int i = 0; i< _items.size ; i++) { _items[i].setPreceedingMove(_items); } }
public static MoveTree PickChildMove(MoveTree moveTree, Vector2Int startingCoords, List <Vector2Int> movesInTurn) { //starting coords //last coords foreach (var nextMove in moveTree.Moves) { if (startingCoords.x == nextMove.StartingCoordinates.x && startingCoords.y == nextMove.StartingCoordinates.y && CompareListOfVector2Int(movesInTurn, nextMove.MovesInOneTurn) == true) { return(nextMove); } } return(null); }
public void UpdateTree() { DeleteNodes(CurrentMoveTree); CurrentMoveTree = new MoveTree { MovesAhead = TreeSize }; CurrentMoveTree.FindAllPossibleMoves(PiecesCreation.BoardLogic, TreeSize); CurrentMoveTree.FindTheBestMove(CurrentMoveTree, PiecesCreation.BoardLogic.Turn); CreatePhysicalTree(CurrentMoveTree, 0); SetNodeParent(null, CurrentMoveTree); }
public void DeleteNodes(MoveTree moveTree) { if (moveTree.Moves.Count == 0) { Destroy(moveTree.Node); moveTree.Node = null; return; } foreach (var move in moveTree.Moves) { DeleteNodes(move); } foreach (Transform child in transform) { GameObject.Destroy(child.gameObject); } }
public MoveTree CreateMoveTreeNode(BoardLogic boardLogic, Checker checker, int movesAhead, Vector2Int startingCoords, int k) { MoveTree movement = new MoveTree(); movement.CurrentBoardLogic = boardLogic; movement.MovesInOneTurn.Add(new Vector2Int(checker.PositionX, checker.PositionY)); movement.MovesAhead = movesAhead - 1; movement.StartingCoordinates = new Vector2Int(startingCoords.x, startingCoords.y); if (boardLogic.IsMoveAtack == true) { Vector2Int firstMove = new Vector2Int(checker.PositionX, checker.PositionY); movement.MovesInOneTurn = FindFullAtackMove(boardLogic, checker, k); movement.MovesInOneTurn.Insert(0, firstMove); } return(movement); }
private int Max(MoveTree moveTree, int depth) { if (depth <= 0) { return(moveTree.board.GetValueOfBoard()); } int max = int.MinValue; foreach (MoveTree child in moveTree.children) { int score = Mini(child, depth - 1); if (score > max) { max = score; } } return(max); }
private int Mini(MoveTree moveTree, int depth) { if (depth <= 0) { return(moveTree.board.GetValueOfBoard()); } int min = int.MaxValue; foreach (MoveTree child in moveTree.children) { int score = Max(child, depth - 1); if (score < min) { min = score; } } return(min); }
//ad ro public void CreatePhysicalTree(MoveTree moveTree, int row) { if (moveTree.Moves.Count == 0) { return; } int numberInList = 0; foreach (var move in moveTree.Moves) { CreatePhysicalTree(move, row + 1); if (move.Node == null) { move.Node = CreateNode(move, numberInList, row); } numberInList++; } }
public void DrawTree(MoveTree parentMoveTree, MoveTree moveTree, int X, int Y) { Y++; foreach (var move in moveTree.Moves) { if (move.Node != null) { move.Node.transform.localPosition += new Vector3(moveTree.Moves.Count * 1 * X, -1 * Y, 0); } if (move.Moves.Count == 0) { continue; } X++; DrawTree(moveTree, move, X, Y); } }
//Deprecated : Used to physically create tree for minimax //Removed as not building the actual tree make the execution much faster /*private MoveTree CreateMoveTree(PositionManager board, (int, int) move, int depth, bool isFalsePlayer) * { * * MoveTree moveTree = new MoveTree(board, move); * * if (depth == 0) return moveTree; * * * List <(int, int)> allPM = MoveManager.GetAllPossibleMoves(board, false, isFalsePlayer); * * foreach ((int, int) iMove in allPM) * { * PositionManager iBoard = new PositionManager(board); * iBoard.MovePiece(iMove.Item1, iMove.Item2); * moveTree.AddChild(CreateMoveTree(iBoard, iMove, depth - 1, !isFalsePlayer)); * } * * return moveTree; * }*/ // Deprecated : Even for test purpose, this method should not be used as it used method CreateMoveTree #region MiniMax without pruning private (int, int) MiniMax(MoveTree moveTree, int n) { int min = int.MaxValue; (int, int)move = (-1, -1); foreach (MoveTree child in moveTree.children) { int score = Max(child, n); if (score == min && (Random.Range(0, 2) == 0)) { move = child.move; } if (score < min) { min = score; move = child.move; } } return(move); }
/// <summary> /// Gets a list of GridBlocks to move to in sequence, depending on scope behavior. /// </summary> /// <returns>List of move locations.</returns> private LinkedList<GridBlock> getMoveBlockList() { //The list of moves to perform. LinkedList<GridBlock> gridBlockMoves = new LinkedList<GridBlock>(); //Get the proper distance grid to movement target. int[,] targetGrid = getTargetGrid(); //Get the unit head location and max possible moves to determine possible intermediate and final locations. GridBlock headLoc = unit.getCurrentBlockHeadLocation(); int moves = unit.getUnitMaxMovment(); //Create a move tree to assemble a list of possible finishing move locations. MoveTree moveTree = new MoveTree(headLoc, moves); //Get the final position to move to. Position finalPos = getPositionByDirection(moveTree.finalPositions, targetGrid); //Return empty list if no final position determined. if(finalPos == null) return gridBlockMoves; //Add all gridblocks from path position data. Position currPos = finalPos; do { gridBlockMoves.AddLast(currPos.getGridBlock()); currPos = currPos.prevPos; } while(currPos != null); return gridBlockMoves; }
public MonsterLibraryRecord(TMMonstersRow aInitData) { _id = aInitData._ID; _name = aInitData._Name; _description = aInitData._Description; _evolvesToID = aInitData._EvolvesTo; _primaryElement = ElementalLibrary.REF.getElement (aInitData._PrimaryElement); _secondaryElement = ElementalLibrary.REF.getElement (aInitData._SecondaryElement); _evolvesAt = Convert.ToByte( aInitData._EvolvesAt); _hp = aInitData._HP; _hpPerLevel = aInitData._HPPerLevel; _meleeAttack = aInitData._MeleeAttack; _meleeDefense = aInitData._MeleeDefense; _rangeAttack = aInitData._RangeAttack; _rangeDefense = aInitData._RangeDefense; _speed = aInitData._Speed; // _resistance = aInitData._resistance; _accuracy = aInitData._Accuracy; _agility = aInitData._Agility; _meleeAttackPerLevel = aInitData._MeleeAttackPerLevel; _meleeDefensePerLevel = aInitData._MeleeDefensePerLevel; _rangeAttackPerLevel = aInitData._RangeAttackPerLevel; _rangeDefensePerLevel = aInitData._RangeDefensePerLevel; _energy = aInitData._Energy; _energyPerLevel = aInitData._EnergyPerLevel; this._accuracyPerLevel = aInitData._AccuracyPerLevel; _speedPerLevel = aInitData._SpeedPerLevel; _catchRate = aInitData._CatchRate; _moveTree1 = MoveTreeLibrary.REF.GetMoveTree (aInitData._MoveTree1); _moveTree2 = MoveTreeLibrary.REF.GetMoveTree (aInitData._MoveTree2); _moveTree3 = MoveTreeLibrary.REF.GetMoveTree (aInitData._MoveTree3); _moveTree4 = MoveTreeLibrary.REF.GetMoveTree (aInitData._MoveTree4); passiveClass = aInitData._PassiveEffectClasses; _expVal = aInitData._BaseExperienceYield; _xpDevModel = XPDevModelLib.REF.getXPDevModel (aInitData._XPDevModel); this.companionAString = aInitData._CompanionA; this.companionBString = aInitData._CompanionB; this.companionALevelDiff = aInitData._CompanionALevelDiff; this.companionBLevelDiff = aInitData._CompanionBLevelDiff; }
public void FindAllPossibleMovesForChecker(BoardLogic startingBoardLogic, Checker checker, int movesAhead) { Vector2Int startingCoords = new Vector2Int(checker.PositionX, checker.PositionY); int k; if (checker.isQueen == true) { k = 7; } else if (startingBoardLogic.SearchForNormalAtackForChecker(startingBoardLogic.Checkers, checker) == true) { k = 2; } else { k = 1; } BoardLogic boardLogic = startingBoardLogic.CopyBoard(); checker = boardLogic.FindChecker(boardLogic.Checkers, checker.PositionX, checker.PositionY); for (int x = -k; x <= k; x += 1) { for (int y = -k; y <= k; y += 1) { if (x == 0 || y == 0) { continue; } if (Math.Abs(x) - Math.Abs(y) != 0) { continue; } int newX = checker.PositionX + x; int newY = checker.PositionY + y; if (newX < 0 || newX > 7 || newY < 0 || newY > 7) { continue; } if (boardLogic.MoveChecker(boardLogic.Checkers, checker, newX, newY) == true) { if (newX == 5 && newY == 2) { } MoveTree movement = CreateMoveTreeNode(boardLogic, checker, movesAhead, startingCoords, k); movement.EvaluateMovement(); Moves.Add(movement); //next moves if (movement.MovesAhead > 0) { movement.FindAllPossibleMoves(boardLogic, movement.MovesAhead); } //cleanup boardLogic = startingBoardLogic.CopyBoard(); checker = boardLogic.FindChecker(boardLogic.Checkers, startingCoords.x, startingCoords.y); } } } }
public void AIMove() { BoardLogic boardLogic = PiecesCreation.BoardLogic; char turn = boardLogic.Turn; UpdateTree(); MoveTree nextMove = CurrentMoveTree; if (turn == 'B') { int value = -10000; foreach (var move in CurrentMoveTree.Moves) { if (move.ValueOfMovement > value) { nextMove = move; value = move.ValueOfMovement; } } } else { int value = 10000; foreach (var move in CurrentMoveTree.Moves) { if (move.ValueOfMovement < value) { nextMove = move; value = move.ValueOfMovement; } } } //find piece GameObject movingPiece = FindPieceByXY(nextMove.StartingCoordinates.x, nextMove.StartingCoordinates.y); //find field int numberOfLastMoveInTurn = nextMove.MovesInOneTurn.Count; if (numberOfLastMoveInTurn > 0) { numberOfLastMoveInTurn--; } GameObject fieldForPiece = FindFieldByXY(nextMove.MovesInOneTurn[numberOfLastMoveInTurn].x, nextMove.MovesInOneTurn[numberOfLastMoveInTurn].y); Vector2Int previousMove = nextMove.StartingCoordinates; foreach (var singleMove in nextMove.MovesInOneTurn) { PiecesCreation.BoardLogic.MoveChecker( boardLogic.Checkers, boardLogic.FindChecker(boardLogic.Checkers, previousMove.x, previousMove.y), singleMove.x, singleMove.y); if (boardLogic.delX != -1 && boardLogic.delY != -1) { GameObject checkerToDestroy = FieldHandler.FindCheckerToDestroy(boardLogic); if (checkerToDestroy != null) { Destroy(checkerToDestroy); } } previousMove = new Vector2Int(singleMove.x, singleMove.y); } //move piece to field FieldHandler.MovePieceToField(movingPiece, fieldForPiece); movingPiece.GetComponent <Coordinates>().X = fieldForPiece.GetComponent <Coordinates>().X; movingPiece.GetComponent <Coordinates>().Y = fieldForPiece.GetComponent <Coordinates>().Y; if (boardLogic.FindChecker(boardLogic.Checkers, movingPiece.GetComponent <Coordinates>().X, movingPiece.GetComponent <Coordinates>().Y).isQueen == true) { movingPiece.GetComponent <RectTransform>().sizeDelta = new Vector2(45, 45); } // UpdateTree(); }
public void FindTheBestMove(MoveTree moveTree, char turn) { MinMax(moveTree, turn); }