Exemplo n.º 1
0
 /// <summary>
 /// Совершить ход.
 /// </summary>
 public static FigureMove AiMove(BoardState boardState)
 {
     BoardState.CheckState checkState = boardState.GetCheckState();
     if (checkState == BoardState.CheckState.mate)
     {
         throw new InvalidOperationException("Невозможно сделать ход: поставлен мат");
     }
     else if (checkState == BoardState.CheckState.stalemate)
     {
         throw new InvalidOperationException("Невозможно сделать ход: пат");
     }
     return(Minimax(boardState, 2));
 }
Exemplo n.º 2
0
    /// <summary>
    /// Вызывется после завершения анимации перемещения фигуры.
    /// </summary>
    public void EndMove()
    {
        // Делаем ход на BoardState
        boardState.ExecuteMove(currentMove);

        // Перемещаем спрайт на слой ниже
        SpriteRenderer spriteRenderer = currentMovingPiece.GetChild(0).GetComponent <SpriteRenderer>();

        spriteRenderer.sortingLayerName = "Pieces";
        currentMovingPiece = null;

        // Обновляем список разрешенных ходов
        boardState.UpdateLegalMoves();
        Debug.Log($"{boardState.turnColor} has {boardState.moveList.FindAll(move => move.attackingFigures.Count == 0).Count} moves");

        // Шах
        King          king             = boardState.FindKingByColor(boardState.turnColor);
        List <Figure> attackingFigures = boardState.GetFiguresAttackingFigure(king);

        if (attackingFigures.Count > 0)
        {
            Debug.Log($"CHECK TO {boardState.turnColor} KING");
            // Рисуем красные линии
            ShakeAnimation shakeAnimation = FindTransformByPos(king.Pos).GetComponent <ShakeAnimation>();
            foreach (Figure figure in attackingFigures)
            {
                // Добавляем объект
                GameObject newRedLine = Instantiate(redLine);
                redLines.Add(newRedLine);
                // Запускаем анимацию
                RedLineAnimation redLineAnimation = newRedLine.GetComponent <RedLineAnimation>();
                redLineAnimation.StartAnimation(
                    beginPos: new Vector3(figure.Pos.x, figure.Pos.y),
                    endPos: new Vector3(king.Pos.x, king.Pos.y),
                    finishedCallback: shakeAnimation.StartAnimation
                    );
            }
        }

        BoardState.CheckState checkState = boardState.GetCheckState();
        // Мат
        if (checkState == BoardState.CheckState.mate)
        {
            Debug.Log($"MATE TO {boardState.turnColor} KING");
            gameEnded = true;
            endgameText.gameObject.SetActive(true);
            string text = boardState.turnColor == Figure.FigureColor.black ? "Белые выиграли" : "Черные выграли";
            endgameText.transform.GetChild(0).GetComponent <Text>().text = text;
            return;
        }
        // Пат
        else if (checkState == BoardState.CheckState.stalemate)
        {
            Debug.Log($"STALEMATE TO {boardState.turnColor} KING");
            gameEnded = true;
            endgameText.gameObject.SetActive(true);
            string text = "Ничья";
            endgameText.transform.GetChild(0).GetComponent <Text>().text = text;
            return;
        }

        // Снимаем блокировку интерфейса
        interfaceLocked = false;
    }