Exemplo n.º 1
0
        /**
         * Evaluate the win change against an enemy piece.
         */
        private static float EvaluateWinChance(Dictionary <Square, AiChessPiece> board, float[,] strength, AiChessPiece initial, AiChessPiece target)
        {
            List <AiChessPiece> surroundings = AiMovementUtil.SurroundingPieces(board, initial.Position);
            bool addOne = false;

            switch (initial.Type)
            {
            case "knight":
            {
                if (!surroundings.Contains(target))
                {
                    addOne = true;
                }

                break;
            }

            case "rook":
                return(10 * EvaluationValues.PieceValues[target.Type]);
            }

            float minRoll = CaptureMatrix.GetMin(initial.Type, target.Type, addOne);

            if (minRoll > 6)
            {
                return(-10000);
            }
            return(minRoll);
        }
Exemplo n.º 2
0
        private void Update()
        {
            Color color;

            // Determine raycasts - if the mouse is hovering over the object and released, then the piece that is being dragged will be moved here
            RaycastHit2D[] hits     = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
            bool           hovering = false;

            if (hits.Any(ray => ray.collider.gameObject == gameObject))
            {
                if (Input.GetMouseButtonUp(0) && !Game.Controller.die.rolling)  // make sure die is not rolling
                {
                    StartCoroutine(MovementUtil.Instance.MovePiece(_chessMove, attackOnly));
                }
                hovering = true;

                if (_chessMove.Attack && !Game.Controller.die.rolling)
                {
                    ChessPiece initialPiece = ChessGrid.GetPosition(_chessMove.InitialSquare);
                    ChessPiece targetPiece  = ChessGrid.GetPosition(_chessMove.TargetSquare);
                    Game.Controller.uiManager.UpdateAttack(UIManager.BuildNotation(initialPiece.type, _chessMove.InitialSquare, _chessMove.TargetSquare, true) + " - "
                                                           + CaptureMatrix.GetMin(initialPiece.type, targetPiece.type, _chessMove.AddOne) + "+ needed");
                }
            }

            // Interpolate color based on mouse input
            if (hovering)
            {
                color = _highlighted;
            }
            else
            {
                color = _chessMove.Attack ? _attacking : _initial;
            }
            _spriteRenderer.color = Color.Lerp(_spriteRenderer.color, color, 20 * Time.deltaTime);
        }
Exemplo n.º 3
0
        // Move a piece on the board
        public IEnumerator MovePiece(ChessMove chessMove, bool attackOnly)
        {
            ChessPiece initialPiece = ChessGrid.GetPosition(chessMove.InitialSquare);
            string     notation     = UIManager.BuildNotation(initialPiece.type, chessMove.InitialSquare, chessMove.TargetSquare, chessMove.Attack);
            Color      color        = Color.white;

            // Assume it will succeed if not an attack
            bool success = true;

            if (chessMove.Attack)
            {
                // Update attacking text
                ChessPiece targetPiece = ChessGrid.GetPosition(chessMove.TargetSquare);
                int        minNeeded   = CaptureMatrix.GetMin(initialPiece.type, targetPiece.type, chessMove.AddOne);
                Game.Controller.uiManager.UpdateAttack(UIManager.BuildNotation(initialPiece.type,
                                                                               chessMove.InitialSquare, chessMove.TargetSquare, true) + " - " + minNeeded + "+ needed.");

                // Only do the die roll if it is not a king attacking a pawn (this move is automatic)
                if (!(initialPiece.type == "king" && targetPiece.type == "pawn"))
                {
                    yield return(Game.Controller.die.Roll());

                    int num = Game.Controller.die.GetResult();
                    success = num >= minNeeded;
                }

                // Set color of move history text
                color = success ? Color.green : Color.red;
            }

            // set commander as having moved
            initialPiece.GetCommander().SetMoved();

            // Add move history record
            Game.Controller.uiManager.InsertMoveRecord(notation, false, color);

            if (success)
            {
                if (chessMove.Attack)
                {
                    // Play sounds
                    SoundManger.PlayAttackSuccessSound();
                    yield return(new WaitForSeconds(0.7f));

                    SoundManger.PlayCaptureSound();

                    ChessPiece targetPiece = ChessGrid.GetPosition(chessMove.TargetSquare);
                    // if king was taken, a player has won the game
                    if (targetPiece.type == "king")
                    {
                        Game.Winner(targetPiece.Owner.Name == "player1" ? "player2" : "player1");
                    }
                    targetPiece.transform.GetChild(0).GetComponent <SpriteRenderer>().enabled = false;   // Disable commander icon on this piece

                    // transition command authority to king if bishop was captured
                    if (targetPiece.type == "bishop")
                    {
                        Player player = targetPiece.Owner;
                        player.RemainingMoves--;
                        if (targetPiece.Owner == PovManager.Instance.Pov)
                        {
                            CommandDelegation.Instance.DisableButton(targetPiece.GetDivision());
                        }
                        player.TransitionCommand(targetPiece.GetDivision());
                    }

                    // move piece to graveyard
                    Graveyard grave = targetPiece.Owner.Name == "player1" ? player1Graveyard : player2Graveyard;
                    targetPiece.moveable = false;
                    targetPiece.dragging = true;
                    ChessGrid.Captured.Add(targetPiece);
                    grave.AddToGrave(targetPiece.gameObject.transform);
                }

                // If piece moves to the square it attacked (not an archer)
                if (!attackOnly)
                {
                    SoundManger.PlayMoveSound();
                    ChessGrid.SetPositionEmpty(initialPiece.Position);
                    ChessGrid.SetPosition(chessMove.TargetSquare, initialPiece);
                    initialPiece.SpriteRenderer.sortingOrder = 1;
                    initialPiece.Position = chessMove.TargetSquare;
                }
                else
                {
                    ChessGrid.SetPositionEmpty(chessMove.TargetSquare);
                }
            }
            else
            { // Attack Failed
                Game.Controller.lastMoveFailed = true;
                SoundManger.PlayAtackFailedSound();
            }

            // Increment turn regardless of fail/success
            DestroyMovePlates();
            Game.Controller.IncrementTurnCounter();
        }