Exemplo n.º 1
0
    public void OnPointerClick(PointerEventData eventData)
    {
        if (IsRunning)
        {
            return;
        }
        // If we are dragging, OnEndDrag will be called after OnPointerClick,
        // so IsDragging will be set.
        if (IsDragging)
        {
            return;
        }
        BoardPosition pos = FindPosition(eventData);

        //Debug.LogFormat("OnPointerClick: {0}", pos);
        if (!MouseDownPos.Equals(pos))
        {
            return;
        }
        if (pos.place == BoardPosition.Place.BOARD)
        {
            // Click on board. LClick rotate forward, RClick rotate backward.
            Board.Cell currentCell = TheBoard[pos];
            if (eventData.button == PointerEventData.InputButton.Left)
            {
                SetBoardAndTile(pos, currentCell.RotateForward());
            }
            else if (eventData.button == PointerEventData.InputButton.Right)
            {
                SetBoardAndTile(pos, currentCell.RotateBackward());
            }
        }
    }
Exemplo n.º 2
0
        public override bool CanAttack(BoardPosition position, BoardPosition from, Side side, uint turnLastMoved, IGameState state)
        {
            if (position.Equals(from))
            {
                return(false);
            }

            uint start, end;

            if (position.Row == from.Row)
            {
                if (position.Column > from.Column)
                {
                    start = from.Column + 1;
                    end   = position.Column - 1;
                }
                else
                {
                    start = position.Column + 1;
                    end   = from.Column - 1;
                }
                for (uint col = start; col <= end; col++)
                {
                    if (state.GetPieceAt(new BoardPosition(col, position.Row)) != null)
                    {
                        return(false);
                    }
                }

                return(true);
            }
            else if (position.Column == from.Column)
            {
                if (position.Row > from.Row)
                {
                    start = from.Row + 1;
                    end   = position.Row - 1;
                }
                else
                {
                    start = position.Row + 1;
                    end   = from.Row - 1;
                }
                for (uint row = start; row <= end; row++)
                {
                    if (state.GetPieceAt(new BoardPosition(position.Column, row)) != null)
                    {
                        return(false);
                    }
                }

                return(true);
            }

            return(false);
        }
 private bool isPassable(int toX, int toY, int fromX, int fromY)
 {
     if (!BoardManager.instance.IsWithinBounds(toX, toY))
     {
         return(false);
     }
     if (BoardManager.instance.IsPassable(toX, toY))
     {
         return(true);
     }
     return(target.Equals(BoardManager.instance.GetOccupied(new IntVector2(toX, toY))));
 }
Exemplo n.º 4
0
    private void SetAllowedMoves()
    {
        allowedMoves = new bool[GameManager.Instance.BoardSize, GameManager.Instance.BoardSize];
        if (currentPosition.Equals(moveStart) || !GameManager.Instance.OfficialRules)
        {
            if (currentPosition.x > 0)
            {
                allowedMoves[currentPosition.x - 1, currentPosition.y] = CheckStoneTypes(currentPosition.x - 1, currentPosition.y);
            }

            if (currentPosition.y > 0)
            {
                allowedMoves[currentPosition.x, currentPosition.y - 1] = CheckStoneTypes(currentPosition.x, currentPosition.y - 1);
            }

            if (currentPosition.x < GameManager.Instance.BoardSize - 1)
            {
                allowedMoves[currentPosition.x + 1, currentPosition.y] = CheckStoneTypes(currentPosition.x + 1, currentPosition.y);
            }

            if (currentPosition.y < GameManager.Instance.BoardSize - 1)
            {
                allowedMoves[currentPosition.x, currentPosition.y + 1] = CheckStoneTypes(currentPosition.x, currentPosition.y + 1);
            }

            if (!(selectedStones.Count == movingStones.Count))
            {
                allowedMoves[currentPosition.x, currentPosition.y] = true;
            }
        }
        else
        {
            allowedMoves[currentPosition.x, currentPosition.y] = true;

            if (moveStart.x > currentPosition.x && currentPosition.x > 0) // Left
            {
                allowedMoves[currentPosition.x - 1, currentPosition.y] = CheckStoneTypes(currentPosition.x - 1, currentPosition.y);
            }
            else if (moveStart.x < currentPosition.x && currentPosition.x < GameManager.Instance.BoardSize - 1) // Right
            {
                allowedMoves[currentPosition.x + 1, currentPosition.y] = CheckStoneTypes(currentPosition.x + 1, currentPosition.y);
            }
            else if (moveStart.y > currentPosition.y && currentPosition.y > 0) // Down
            {
                allowedMoves[currentPosition.x, currentPosition.y - 1] = CheckStoneTypes(currentPosition.x, currentPosition.y - 1);
            }
            else if (moveStart.y < currentPosition.y && currentPosition.y < GameManager.Instance.BoardSize - 1) // Up
            {
                allowedMoves[currentPosition.x, currentPosition.y + 1] = CheckStoneTypes(currentPosition.x, currentPosition.y + 1);
            }
        }
    }
        //private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        //{
        //    Border b = sender as Border;
        //    var square = b.DataContext as ChessSquare;
        //    var vm = FindResource("vm") as ChessViewModel;
        //    //Console.WriteLine($"Current player{vm.CurrentPlayer} square player{square.Player}");
        //    ChessSquare cs = vm.hasSelected();
        //    Console.WriteLine("!!!!!!!!!!");
        //    foreach(var x in vm.PossibleMoves)
        //        Console.WriteLine(x.ToString());
        //    if (vm.checking(square.Position))
        //        square.IsChecked = true;
        //    else if (cs == null) //if not selected, selected
        //        square.IsSelected = true;

        //    else //if (vm.PossibleMoves.Contains(new ChessMove(square.Position, bp)))
        //    {
        //        BoardPosition bp = cs.Position;
        //        ChessMove cm = new ChessMove(bp, square.Position);
        //        foreach (var x in vm.PossibleMoves)
        //        {
        //            if (bp.Equals(x.StartPosition) && square.Position.Equals(x.EndPosition))
        //            {

        //                vm.ApplyMove(new ChessMove(bp, square.Position));
        //                Console.WriteLine("!!!!!!!!!!");
        //                foreach (var xxx in vm.PossibleMoves)
        //                    Console.WriteLine(xxx.ToString());
        //                if ((square.Position.Row == 0 && vm.GetPiece(square.Position).PieceType == ChessPieceType.Pawn && vm.GetPiece(square.Position).Player == 1)
        //                    || (square.Position.Row == 7 && vm.GetPiece(square.Position).PieceType == ChessPieceType.Pawn && vm.GetPiece(square.Position).Player == 2))
        //                {
        //                    //vm.makePromotion();
        //                    if(vm.GetPiece(square.Position).Player == 1)
        //                    {
        //                        var promotion = new PromotionWhite(vm);
        //                        promotion.ShowDialog();
        //                    }
        //                    else
        //                    {
        //                         var promotion = new Promotion(vm);
        //                                                    //promotion.Closed += promotion_closed;
        //                        promotion.ShowDialog();
        //                    }


        //                    //this.Hide();
        //                }
        //                square.IsHovered = false;
        //                cs.IsSelected = false;
        //                if (vm.checking(square.Position))
        //                    square.IsChecked = true;
        //                return;
        //            }
        //        }
        //        cs.IsSelected = false;

        //    }
        //    //else if (vm.PossibleMoves.Contains(new ChessMove(square.Position, bp)) == false)
        //    //square.IsSelected = false;
        //    if (vm.checking(square.Position))
        //        square.IsChecked = true;
        //}

        private void Border_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Border      b      = sender as Border;
            var         square = b.DataContext as ChessSquare;
            var         vm     = FindResource("vm") as ChessViewModel;
            ChessSquare cs     = vm.hasSelected();

            if (cs == null) //if not selected, selected
            {
                square.IsSelected = true;
            }

            else //if (vm.PossibleMoves.Contains(new ChessMove(square.Position, bp)))
            {
                BoardPosition bp = cs.Position;
                ChessMove     cm = new ChessMove(bp, square.Position);
                foreach (var x in vm.PossibleMoves)
                {
                    if (bp.Equals(x.StartPosition) && square.Position.Equals(x.EndPosition))
                    {
                        vm.ApplyMove(new ChessMove(bp, square.Position, x.MoveType));

                        if ((square.Position.Row == 0 && vm.GetPiece(square.Position).PieceType == ChessPieceType.Pawn && vm.GetPiece(square.Position).Player == 1) ||
                            (square.Position.Row == 7 && vm.GetPiece(square.Position).PieceType == ChessPieceType.Pawn && vm.GetPiece(square.Position).Player == 2))
                        {
                            //vm.makePromotion();
                            if (vm.GetPiece(square.Position).Player == 1)
                            {
                                var promotion = new PromotionWhite(vm);
                                promotion.ShowDialog();
                            }
                            else
                            {
                                var promotion = new Promotion(vm);
                                //promotion.Closed += promotion_closed;
                                promotion.ShowDialog();
                            }


                            //this.Hide();
                        }
                        square.IsHovered = false;
                        cs.IsSelected    = false;
                        //if (vm.checking(square.Position))
                        //    square.IsChecked = true;
                        return;
                    }
                }
                cs.IsSelected = false;
            }
        }
Exemplo n.º 6
0
    /// <summary>
    /// Tries to move the piece to a new position
    /// </summary>
    /// <param name="newPosition"></param>
    public bool Move(BoardPosition newPosition)
    {
        //Check if the piece can move at all
        List <BoardPosition> movableSpaces = pieceBehaviour.GetMovableSpaces(boardPosition, team);

        if (movableSpaces.Count > 0)
        {
            //Check if that position is a viable position (using IPieceBehaviour)
            foreach (BoardPosition position in pieceBehaviour.GetMovableSpaces(boardPosition, team))
            {
                if (newPosition.Equals(position))
                {
                    //Stores the current board state
                    BoardState currentState = new BoardState();
                    currentState.boardData = (string[, ])BoardStateManager.Instance.currentBoardState.boardData.Clone();

                    //Moves the piece to the new position
                    ExcecuteMove(newPosition);

                    //If the move puts the team in check
                    if (team.isInCheck)
                    {
                        //Revert back to the old state
                        ChessBoardManager.Instance.LoadBoardState(currentState);
                    }
                    else
                    {
                        //Switches the turns
                        TeamManager.Instance.ChangeTurn();
                        //Resets board highlights
                        Square.ResetBoardHighlights();
                        return(true);
                    }
                }
            }
        }

        return(false);
    }
Exemplo n.º 7
0
 public override bool CanAttack(BoardPosition position, BoardPosition from, Side side, uint turnLastMoved, IGameState state)
 {
     return(!from.Equals(position) && Math.Abs(position.Column - from.Column) <= 1 && Math.Abs(position.Row - from.Row) <= 1);
 }
Exemplo n.º 8
0
    private void HandleGame(RaycastHit[] hits, bool selectable, BoardPosition position, UnitPiece unit, Vector3 tileVector)
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (hits.Length == 0)
            {
                highlightCube.SetActive(false);
                previous = position;
            }
            else if (selectable && !unit.Equals(UnitPiece.UNKNOWN))
            {
                //We've selected a unit
                //Check if we're the owner
                bool owner = false;
                if (GameManager.CurrentMode.Equals(GameMode.PlayerOne))
                {
                    owner = unit.Owner.Equals(GameManager.PlayerOne);
                }
                else if (GameManager.CurrentMode.Equals(GameMode.PlayerTwo))
                {
                    owner = unit.Owner.Equals(GameManager.PlayerTwo);
                }

                if (owner)
                {
                    //If the previous position was off the board, this is our first selection
                    if (previous.Equals(BoardPosition.OFF_BOARD))
                    {
                        previous = position;
                        MoveHighlight(tileVector);

                        //Determine what the valid next positions are
                        ClearValid();
                        //Add current position, for deselection
                        validPositions.Add(position);
                        ValidateNextPosition(new BoardPosition(position.X - 1, position.Y));
                        ValidateNextPosition(new BoardPosition(position.X, position.Y - 1));
                        ValidateNextPosition(new BoardPosition(position.X + 1, position.Y));
                        ValidateNextPosition(new BoardPosition(position.X, position.Y + 1));
                        MoveValid();
                    }
                    else if (previous.Equals(position))
                    {
                        //We selected the same space, deselect
                        previous = BoardPosition.OFF_BOARD;
                        highlightCube.SetActive(false);
                        ClearValid();
                    }
                }
                else if (validPositions.Contains(position))
                {
                    //If our previous position contains a piece that we own, then we should do battle
                    UnitPiece previousPiece = UnitManager.GetUnitPieceForPosition(previous);
                    if (!previousPiece.Equals(UnitPiece.UNKNOWN))
                    {
                        bool previousOwner = false;
                        if (GameManager.CurrentMode.Equals(GameMode.PlayerOne))
                        {
                            previousOwner = previousPiece.Owner.Equals(GameManager.PlayerOne);
                        }
                        else if (GameManager.CurrentMode.Equals(GameMode.PlayerTwo))
                        {
                            previousOwner = previousPiece.Owner.Equals(GameManager.PlayerTwo);
                        }

                        if (previousOwner)
                        {
                            // instigate attack, with previous piece as attacker and current piece as defender
                            GameManager.HandleBattle(previousPiece, previous, unit, position);
                            previous = BoardPosition.OFF_BOARD;
                            highlightCube.SetActive(false);
                            ClearValid();
                        }
                    }
                }
            }
            else if (selectable && !position.Equals(BoardPosition.OFF_BOARD) && !previous.Equals(BoardPosition.OFF_BOARD))
            {
                //We've selected a place not off the board, the previous position was not off the board either, but there is no piece occupying the space
                if(validPositions.Contains(position))
                {
                    //Move the piece
                    UnitManager.MovePiece(previous, position);
                    ClearValid();
                    highlightCube.SetActive(false);
                    action = true;
                }
            }
        }
        else if (Input.GetMouseButtonDown(1))
        {
            //Just clear selection
            previous = BoardPosition.OFF_BOARD;
            highlightCube.SetActive(false);
            ClearValid();
        }
    }
Exemplo n.º 9
0
    private void HandleGame(RaycastHit[] hits, bool selectable, BoardPosition position, UnitPiece unit, Vector3 tileVector)
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (hits.Length == 0)
            {
                highlightCube.SetActive(false);
                previous = position;
            }
            else if (selectable && !unit.Equals(UnitPiece.UNKNOWN))
            {
                //We've selected a unit
                //Check if we're the owner
                bool owner = false;
                if (GameManager.CurrentMode.Equals(GameMode.PlayerOne))
                {
                    owner = unit.Owner.Equals(GameManager.PlayerOne);
                }
                else if (GameManager.CurrentMode.Equals(GameMode.PlayerTwo))
                {
                    owner = unit.Owner.Equals(GameManager.PlayerTwo);
                }

                if (owner)
                {
                    //If the previous position was off the board, this is our first selection
                    if (previous.Equals(BoardPosition.OFF_BOARD))
                    {
                        previous = position;
                        MoveHighlight(tileVector);

                        //Determine what the valid next positions are
                        ClearValid();
                        //Add current position, for deselection
                        validPositions.Add(position);
                        ValidateNextPosition(new BoardPosition(position.X - 1, position.Y));
                        ValidateNextPosition(new BoardPosition(position.X, position.Y - 1));
                        ValidateNextPosition(new BoardPosition(position.X + 1, position.Y));
                        ValidateNextPosition(new BoardPosition(position.X, position.Y + 1));
                        MoveValid();
                    }
                    else if (previous.Equals(position))
                    {
                        //We selected the same space, deselect
                        previous = BoardPosition.OFF_BOARD;
                        highlightCube.SetActive(false);
                        ClearValid();
                    }
                }
                else if (validPositions.Contains(position))
                {
                    //If our previous position contains a piece that we own, then we should do battle
                    UnitPiece previousPiece = UnitManager.GetUnitPieceForPosition(previous);
                    if (!previousPiece.Equals(UnitPiece.UNKNOWN))
                    {
                        bool previousOwner = false;
                        if (GameManager.CurrentMode.Equals(GameMode.PlayerOne))
                        {
                            previousOwner = previousPiece.Owner.Equals(GameManager.PlayerOne);
                        }
                        else if (GameManager.CurrentMode.Equals(GameMode.PlayerTwo))
                        {
                            previousOwner = previousPiece.Owner.Equals(GameManager.PlayerTwo);
                        }

                        if (previousOwner)
                        {
                            // instigate attack, with previous piece as attacker and current piece as defender
                            GameManager.HandleBattle(previousPiece, previous, unit, position);
                            previous = BoardPosition.OFF_BOARD;
                            highlightCube.SetActive(false);
                            ClearValid();
                        }
                    }
                }
            }
            else if (selectable && !position.Equals(BoardPosition.OFF_BOARD) && !previous.Equals(BoardPosition.OFF_BOARD))
            {
                //We've selected a place not off the board, the previous position was not off the board either, but there is no piece occupying the space
                if (validPositions.Contains(position))
                {
                    //Move the piece
                    UnitManager.MovePiece(previous, position);
                    ClearValid();
                    highlightCube.SetActive(false);
                    action = true;
                }
            }
        }
        else if (Input.GetMouseButtonDown(1))
        {
            //Just clear selection
            previous = BoardPosition.OFF_BOARD;
            highlightCube.SetActive(false);
            ClearValid();
        }
    }
Exemplo n.º 10
0
 public static bool IsValidPosition()
 {
     return(CurrentPosition.Equals(invalidPosition) == false);
 }