public void OnDrop(PointerEventData eventData) { if (eventData.pointerDrag != null) { if (eventData.pointerDrag != null) { //board Logic BoardLogic boardLogic = PiecesCreation.BoardLogic; char turn = boardLogic.Turn; int oldX = eventData.pointerDrag.GetComponent <Coordinates>().X; int oldY = eventData.pointerDrag.GetComponent <Coordinates>().Y; int newX = GetComponent <Coordinates>().X; int newY = GetComponent <Coordinates>().Y; Debug.Log(oldX + " " + oldY + " " + newX + " " + newY); if (boardLogic.MoveChecker(boardLogic.Checkers, boardLogic.FindChecker(boardLogic.Checkers, oldX, oldY), newX, newY) == true) { eventData.pointerDrag.GetComponent <Coordinates>().X = newX; eventData.pointerDrag.GetComponent <Coordinates>().Y = newY; if (boardLogic.delX != -1 && boardLogic.delY != -1) { GameObject piece = FindCheckerToDestroy(boardLogic); if (piece != null) { Destroy(piece); } } if (boardLogic.FindChecker(boardLogic.Checkers, newX, newY).isQueen == true) { eventData.pointerDrag.GetComponent <RectTransform>().sizeDelta = new Vector2(45, 45); } MovePieceToField(eventData.pointerDrag, gameObject); /* Debug.Log(boardLogic.Turn); * Debug.Log(boardLogic.IsMoveAtack); * Debug.Log(boardLogic.FindChecker(boardLogic.Checkers, newX, newY).isQueen); */ if (boardLogic.Turn != turn && GameObject.Find("AI").GetComponent <AILogic>().IsAIOn == true) { StartCoroutine("UpdateTree"); //opoznienie // StartCoroutine("UpdateTree"); } // GameObject.Find("TreeBoard").GetComponent<TreeCreation>().UpdateTree(); } } } }
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(); }