private void ExcuteMoveLAN(Form form)
        {
            if (form.InvokeRequired)
            {
                // this is worker thread
                updateExcuteLANDelegate del = new updateExcuteLANDelegate(ExcuteMoveLAN);
                form.Invoke(del, new object[] { form });
            }
            else
            {
                // this is UI thread
                Point       position = Mode1AndMode2.moveLAN; // x la diem dau- y la diem den
                ChessPieces piece    = (ChessPieces)this.boardGui.boardLogic.GetCell(position.X).GetChessPieces().Clone();
                //  MessageBox.Show(piece.chessPieceType.chessPieceName);

                Cell cell = this.boardGui.boardLogic.GetCell(position.Y);
                Move move;

                if (cell.Occupied())
                {
                    move = new AttackMove(piece, position.Y, cell.GetChessPieces());
                }
                else
                {
                    move = new NormalMove(piece, position.Y);
                }



                CellGui cellG = this.boardGui.GetCellGui(position.X);

                // khong can thiet, cainay chi cap nhat game thoi ma, chi can hien ben nguoi choi duoc roi
                if (move.IsPromote())
                {
                    cellG.SetPromote(move);

                    //set lai icon cho ben nay-ban chat la phia ben nay board logic khong co con nay
                    //nhung chi co ben kia duoc di nen khong sao het- danh lua nguoi choi.
                }


                this.boardGui.boardLogic = move.ExcuteMove(this.boardGui.boardLogic);
                BoardGui.moveHistory.ListMoveHistory.Add(move);
                BoardGui.moveHistory.listRedo.Clear();
                if (move.TheKingDie(this.boardGui.boardLogic))
                {
                    Mode1AndMode2.status_game = statusGame.EndGame;
                }

                //set lai icon tren ban co cho nguoi xem biet duoc  da di.-> thay doi board gui
                this.boardGui.GetCellGui(position.X).SetImageIcon();
                this.boardGui.GetCellGui(position.Y).SetImageIcon();

                //reset thanh chua chon nuoc co nao va set nguoi choi tiep theo vi da danh xong nuoc co nay.
                this.boardGui.CellSelectedFirst = this.boardGui.CellSelectedSecond = null;
                this.boardGui.boardLogic.SetNextPlayer();
            }
        }
Example #2
0
    private void MakeFSM()
    {
        fsm = new FsmSystem <StateID>();
        NormalMove crawl = new NormalMove(this);

        fsm.addState(StateID.CRAWL, crawl);

        StealMode steal = new StealMode(this);

        fsm.addState(StateID.STEAL, steal);

        fsm.init(StateID.CRAWL);
    }
Example #3
0
        private async void pictureBox1_MouseClick_1(object sender, MouseEventArgs e)
        {
            int x = e.Y / 64, y = e.X / 64;

            if (twoPlayers)
            {
                if (player.getPlayerColor() == playerColor.White)
                {
                    if (mouseClickCounter == 0 && !board.isEmptyCell(x, y) && board.getBoardPieces()[x, y].getPieceColor() == pieceColor.BLack)
                    {
                        ;
                    }

                    else if (!board.isEmptyCell(x, y) && mouseClickCounter == 0 && e.Button == MouseButtons.Left)
                    {
                        ++mouseClickCounter;
                        tmp = board.getBoardPieces()[x, y].getLegalMovesWithCheck(board);
                        originalPiecePosition = Tuple.Create(x, y);
                        hightlightLegalMoves(tmp);
                    }
                    else if (mouseClickCounter == 1 && e.Button == MouseButtons.Left)
                    {
                        foreach (Move m in tmp)
                        {
                            if (m.getMovePosition().Item1 == x && m.getMovePosition().Item2 == y)
                            {
                                Piece tmpPiece = board.getBoardPieces()[originalPiecePosition.Item1, originalPiecePosition.Item2];
                                if (m.isAttackMove())
                                {
                                    KilledFromBlack(board.getBoardPieces()[x, y].getPieceType());
                                }

                                if (tmpPiece.getPieceType() == (int)pieceType.whitePawn)
                                {
                                    Utility.handleWhitePawnMove(board, tmpPiece, m);
                                }
                                else
                                {
                                    if (tmpPiece.getPieceType() == (int)pieceType.whiteKing)
                                    {
                                        if (Utility.isCastlingMove(tmpPiece, m))
                                        {
                                            Utility.handleWhiteKingCastling(board, tmpPiece, m);
                                        }
                                        else
                                        {
                                            Utility.handlePieceMove(board, tmpPiece, m);
                                        }
                                    }
                                    else
                                    {
                                        Utility.handlePieceMove(board, tmpPiece, m);
                                    }
                                }
                                whiteMoveOccurred = true;
                                break;
                            }
                        }
                        updateBoard(board);
                        if (whiteMoveOccurred)
                        {
                            Utility.checkBlackKingStatus(board, player, whitePlayerActivePieces, whitePlayerLegalMoves, blackPlayerActivePieces, blackPlayerLegalMoves);
                            player            = new BlackPlayer();
                            whiteMoveOccurred = false;
                        }
                        mouseClickCounter = 0;
                    }
                }
                else
                {
                    if (mouseClickCounter == 0 && !board.isEmptyCell(x, y) && board.getBoardPieces()[x, y].getPieceColor() == pieceColor.White)
                    {
                        ;
                    }

                    else if (!board.isEmptyCell(x, y) && mouseClickCounter == 0 && e.Button == MouseButtons.Left)
                    {
                        ++mouseClickCounter;
                        tmp = board.getBoardPieces()[x, y].getLegalMovesWithCheck(board);
                        originalPiecePosition = Tuple.Create(x, y);
                        hightlightLegalMoves(tmp);
                    }
                    else if (mouseClickCounter == 1 && e.Button == MouseButtons.Left)
                    {
                        foreach (Move m in tmp)
                        {
                            if (m.getMovePosition().Item1 == x && m.getMovePosition().Item2 == y)
                            {
                                Piece tmpPiece = board.getBoardPieces()[originalPiecePosition.Item1, originalPiecePosition.Item2];
                                if (m.isAttackMove())
                                {
                                    KilledFromWhite(board.getBoardPieces()[x, y].getPieceType());
                                }
                                if (tmpPiece.getPieceType() == (int)pieceType.blackPawn)
                                {
                                    Utility.handleBlackPawnMove(board, tmpPiece, m);
                                }
                                else
                                {
                                    if (tmpPiece.getPieceType() == (int)pieceType.blackKing)
                                    {
                                        if (Utility.isCastlingMove(tmpPiece, m))
                                        {
                                            Utility.handleblackKingCastling(board, tmpPiece, m);
                                        }
                                        else
                                        {
                                            Utility.handlePieceMove(board, tmpPiece, m);
                                        }
                                    }
                                    else
                                    {
                                        Utility.handlePieceMove(board, tmpPiece, m);
                                    }
                                }
                                blackMoveOccurred = true;
                                break;
                            }
                        }
                        updateBoard(board);
                        if (blackMoveOccurred)
                        {
                            Utility.checkWhiteKingStatus(board, player, blackPlayerActivePieces, blackPlayerLegalMoves, whitePlayerActivePieces, whitePlayerLegalMoves);
                            player            = new WhitePlayer();
                            blackMoveOccurred = false;
                        }
                        mouseClickCounter = 0;
                    }
                }
            }
            else if (!twoPlayers)
            {
                if (player.getPlayerColor() == playerColor.White)
                {
                    if (mouseClickCounter == 0 && !board.isEmptyCell(x, y) && board.getBoardPieces()[x, y].getPieceColor() == pieceColor.BLack)
                    {
                        ;
                    }

                    else if (!board.isEmptyCell(x, y) && mouseClickCounter == 0 && e.Button == MouseButtons.Left)
                    {
                        ++mouseClickCounter;
                        tmp = board.getBoardPieces()[x, y].getLegalMovesWithCheck(board);
                        originalPiecePosition = Tuple.Create(x, y);
                        hightlightLegalMoves(tmp);
                        // if (nextMove.Item1 != -1)
                        //   highlightComputerLastMove(Tuple.Create(nextMove.Item1, nextMove.Item2), Tuple.Create(nextMove.Item3, nextMove.Item4));
                    }
                    else if (mouseClickCounter == 1 && e.Button == MouseButtons.Left)
                    {
                        foreach (Move m in tmp)
                        {
                            if (m.getMovePosition().Item1 == x && m.getMovePosition().Item2 == y)
                            {
                                Piece tmpPiece = board.getBoardPieces()[originalPiecePosition.Item1, originalPiecePosition.Item2];
                                if (m.isAttackMove())
                                {
                                    KilledFromBlack(board.getBoardPieces()[x, y].getPieceType());
                                }
                                if (tmpPiece.getPieceType() == (int)pieceType.whitePawn)
                                {
                                    Utility.handleWhitePawnMove(board, tmpPiece, m);
                                }
                                else
                                {
                                    if (tmpPiece.getPieceType() == (int)pieceType.whiteKing)
                                    {
                                        if (Utility.isCastlingMove(tmpPiece, m))
                                        {
                                            Utility.handleWhiteKingCastling(board, tmpPiece, m);
                                        }
                                        else
                                        {
                                            Utility.handlePieceMove(board, tmpPiece, m);
                                        }
                                    }
                                    else
                                    {
                                        Utility.handlePieceMove(board, tmpPiece, m);
                                    }
                                }
                                whiteMoveOccurred = true;
                                break;
                            }
                        }

                        updateBoard(board);

                        if (whiteMoveOccurred)
                        {
                            Utility.checkBlackKingStatus(board, player, whitePlayerActivePieces, whitePlayerLegalMoves, blackPlayerActivePieces, blackPlayerLegalMoves);
                            player            = new BlackPlayer();
                            whiteMoveOccurred = false;
                            await Task.Delay(100);

                            // computer move
                            DisableProcessWindowsGhosting();
                            nextMove = Computer.getBestMove(board, gameDifficulty);

                            if (nextMove.Item1 == -1)
                            {
                                MessageBox.Show("THE GAME IS OVER YOU WIN"); return;
                            }
                            Move computerMove;
                            if (board.isEmptyCell(nextMove.Item3, nextMove.Item4))
                            {
                                computerMove = new NormalMove(Tuple.Create(nextMove.Item3, nextMove.Item4));
                            }
                            else
                            {
                                computerMove = new AttackMove(Tuple.Create(nextMove.Item3, nextMove.Item4));
                            }



                            Piece tmpPiece1 = board.getBoardPieces()[nextMove.Item1, nextMove.Item2];

                            if (computerMove.isAttackMove())
                            {
                                KilledFromWhite(board.getBoardPieces()[nextMove.Item3, nextMove.Item4].getPieceType());
                            }

                            if (tmpPiece1.getPieceType() == (int)pieceType.blackPawn)
                            {
                                Utility.handleBlackPawnMove(board, tmpPiece1, computerMove);
                            }
                            else
                            {
                                if (tmpPiece1.getPieceType() == (int)pieceType.blackKing)
                                {
                                    if (Utility.isCastlingMove(tmpPiece1, computerMove))
                                    {
                                        Utility.handleblackKingCastling(board, tmpPiece1, computerMove);
                                    }
                                    else
                                    {
                                        Utility.handlePieceMove(board, tmpPiece1, computerMove);
                                    }
                                }
                                else
                                {
                                    Utility.handlePieceMove(board, tmpPiece1, computerMove);
                                }
                            }
                            updateBoard(board);
                            highlightComputerLastMove(Tuple.Create(nextMove.Item1, nextMove.Item2), Tuple.Create(nextMove.Item3, nextMove.Item4));
                            Utility.checkWhiteKingStatus(board, player, blackPlayerActivePieces, blackPlayerLegalMoves, whitePlayerActivePieces, whitePlayerLegalMoves);
                            player = new WhitePlayer();
                        }
                        else
                        if (nextMove.Item1 != -1)
                        {
                            highlightComputerLastMove(Tuple.Create(nextMove.Item1, nextMove.Item2), Tuple.Create(nextMove.Item3, nextMove.Item4));
                        }
                        mouseClickCounter = 0;
                    }
                }
            }
        }
        private void Move()
        {
            //JUMP
            if (!_swim && Input.GetKey(KeyCode.Space) && !animator.GetBool(MovementParameterEnum.Jump))
            {
                if (Utils.isPressed(KeyCode.LeftShift) && Utils.isPressed(KeyCode.W))
                {
                    RunMove.JumpForward(animator, camera, transform, speed, rigidbodyPlayer, _boosted);
                }
                else if (Utils.isPressed(KeyCode.W))
                {
                    NormalMove.Jump(animator, transform, camera, speed, rigidbodyPlayer);
                }
                else
                {
                    animator.SetBool(MovementParameterEnum.Jump, true);
                    transform.position += Vector3.up * (3 * Time.deltaTime * speed);
                    rigidbodyPlayer.AddForce(Vector3.up * 3);
                }
            }
            //RUN
            else
            {
                if (!_swim)
                {
                    if (Utils.isPressed(KeyCode.LeftShift) &&
                        Utils.isPressed(KeyCode.W))
                    {
                        animator.SetFloat(MovementParameterEnum.Run, MovementValuesEnum.RUN_SLOWLY);
                    }
                    if (Utils.wasReleasedThisFrame(KeyCode.LeftShift))
                    {
                        animator.SetFloat(MovementParameterEnum.Run, MovementValuesEnum.IDLE);
                    }

                    if (Input.GetKeyDown(KeyCode.C) && animator.GetFloat(MovementParameterEnum.Crouch) < 1)
                    {
                        animator.SetFloat(MovementParameterEnum.Crouch, MovementValuesEnum.CROUCH_ANIM);
                    }

                    if (Input.GetKeyDown(KeyCode.C) && animator.GetFloat(MovementParameterEnum.Crouch) > 0)
                    {
                        animator.SetFloat(MovementParameterEnum.Crouch, MovementValuesEnum.CROUCH_IDLE);
                    }

                    if (Input.GetKeyUp(KeyCode.C))
                    {
                        animator.SetFloat(MovementParameterEnum.Crouch, MovementValuesEnum.IDLE);
                    }
                }

                //MOVE
                if (Utils.isPressed(KeyCode.W))
                {
                    if (animator.GetFloat(MovementParameterEnum.Run) > 0)
                    {
                        RunMove.MoveForward(animator, camera, transform, speed, _boosted);
                    }
                    else if (animator.GetFloat(MovementParameterEnum.Crouch) > 0)
                    {
                        CrouchMove.MoveForward(animator, camera, transform, speed);
                    }
                    else
                    {
                        NormalMove.MoveForward(animator, camera, transform, speed);
                    }
                }
                else if (Utils.isPressed(KeyCode.S))
                {
                    if (animator.GetFloat(MovementParameterEnum.Crouch) > 0)
                    {
                        CrouchMove.MoveBack(animator, camera, transform, speed);
                    }
                    else
                    {
                        NormalMove.MoveBack(animator, camera, transform, speed);
                    }
                }
                else if (Utils.isPressed(KeyCode.A))
                {
                    if (animator.GetFloat(MovementParameterEnum.Crouch) > 0)
                    {
                        CrouchMove.MoveLeft(animator, camera, transform, speed);
                    }
                    else
                    {
                        NormalMove.MoveLeft(animator, camera, transform, speed);
                    }
                }
                else if (Utils.isPressed(KeyCode.D))
                {
                    if (animator.GetFloat(MovementParameterEnum.Crouch) > 0)
                    {
                        CrouchMove.MoveRight(animator, camera, transform, speed);
                    }
                    else
                    {
                        NormalMove.MoveRight(animator, camera, transform, speed);
                    }
                }

                if (Input.GetKeyUp(KeyCode.Space) && Utils.animatorIsPlaying(animator) ||
                    !Utils.animatorIsPlaying(animator) && animator.GetBool(MovementParameterEnum.Jump))
                {
                    animator.SetBool(MovementParameterEnum.Jump, false);
                }
            }
        }