Beispiel #1
0
        /*
         * Handles the GameBoard Tile Selecting event and creates the neccasary feedback for players to see what moves they can make and what they have selected
         */
        public void TileSelecting(GameBoard sender, TileSelectingEventArgs e)
        {
            if (aPlayerHasWon)
            {
                e.CancelEvent();
                return;
            }
            //The selected tile must be either a piece or a move of the previously selected piece

            if (sender.getSelectedTile() != null && sender.getSelectedTile() == e.tile) //Reselected the previous tile
            {                                                                           //Deselect the previous move and prevent the event
                DeselectTile(sender.getSelectedTile());
                sender.DeselectTile();


                e.CancelEvent();
                return;
            }
            else if (sender.getSelectedTile() != null && sender.getSelectedPiece() != null && sender.getSelectedMoves().Contains(sender.getSelectedLocation(), e.location)) //Selecting a move from the previous selected piece
            {                                                                                                                                                               //Execute the move and clean up the GUI
                //Removes the gui elements
                DeselectTile(sender.getSelectedTile());


                //Execute the planned move on the board
                if (gameBoard.movePiece(sender.getSelectedLocation(), e.location))
                {
                    //Get ready for next move
                    sender.DeselectTile();
                }
                else
                {//Moving failed, reset to last position
                    SelectTile(sender.getSelectedTile());
                }

                e.CancelEvent();
                return;
            }
            else if (sender.pieceAtLocation(e.location) && sender.getPiece(e.location).color == PlayerColor) //Selecting a new piece
            {                                                                                                //Deselect the previous tile
                if (sender.getSelectedTile() != null)
                {
                    DeselectTile(sender.getSelectedTile());
                }
            }
            else
            {//Else invalid selection
                e.CancelEvent();
                return;
            }
        }