//Event callers
        protected void OnTileSelecting(GameBoard t, TileSelectingEventArgs e)
        {
            TileSelectingHandler handler = TileSelecting;

            if (handler != null)
            {
                handler(t, e);
            }
        }
Exemple #2
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;
            }
        }
        public void TileClicked(object obj, EventArgs e)
        {
            Tile tile = (Tile)obj;

            //Invoke the pre tile selected event
            TileSelectingEventArgs eventArgs = new TileSelectingEventArgs(tile);

            OnTileSelecting(this, eventArgs);

            //Allow the event to be cancelled from within selecting
            if (eventArgs.IsEventCancelled())
            {
                return;
            }

            //Set as the new seleceted tile
            this._selectedTile = tile;

            //Invoke the post tile selected event
            OnTileSelected(this, new TileSelectedEventArgs(tile));
        }