Example #1
0
        //When a user has selected a square this event is called to update the other player on network
        protected virtual void OnSelectionMadeEvent(selectionMadeArgs e)
        {
            EventHandler <selectionMadeArgs> handler = selectionMadeEvent;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Example #2
0
        //Event driven logic - user clicking on a chess piece drives the whole program
        private void dgv_chessBoard_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //We can only move a piece if we've selected one to move
            if (selectionMade)
            {
                ChessPiece  selectedPiece  = chessBoard[selectedX, selectedY];
                chessColour selectedColour = chessBoard[selectedX, selectedY].Colour;

                // check that the move is legal for the piece selected,
                // and that the piece is the correct colour to move,
                if (currentGame.Turn == selectedColour &&
                    selectedPiece.isValidMove(e.ColumnIndex, e.RowIndex, chessBoard))

                {
                    //If we're playing single player with no networking we can move black or white pieces
                    if (!isNetworked)
                    {
                        movePiece(selectedX, selectedY, e.ColumnIndex, e.RowIndex);
                    }
                    else if (thisPlayerColour == currentGame.Turn)  //If we're playing a networked game, we can only move our own pieces
                    {
                        movePiece(selectedX, selectedY, e.ColumnIndex, e.RowIndex);

                        // Use an event to call the networkingWindow to record and send the move to other player
                        if (isNetworked)
                        {
                            //"MOVE|oldCol,oldRow|newCol,newRow\n"
                            chessMoveArgs args = new chessMoveArgs();
                            args.oldX = selectedX;
                            args.oldY = selectedY;
                            args.newX = e.ColumnIndex;
                            args.newY = e.RowIndex;

                            OnChessMoveEvent(args);
                        }
                    }
                }

                //if the move wasn't valid, clear selection for next use
                selectionMade = false;

                //report selection to other player
                if (isNetworked)
                {
                    selectionMadeArgs args = new selectionMadeArgs();
                    args.selection = selectionMade;
                    args.intX      = selectedX;
                    args.intY      = selectedY;
                    OnSelectionMadeEvent(args);
                }
            }
            else
            {
                selectionMade = true;           //saves the selection
                selectedX     = e.ColumnIndex;
                selectedY     = e.RowIndex;

                //report selection to other player
                if (isNetworked)
                {
                    selectionMadeArgs args = new selectionMadeArgs();
                    args.selection = selectionMade;
                    args.intX      = selectedX;
                    args.intY      = selectedY;
                    OnSelectionMadeEvent(args);
                }
            }

            updateSelectionSquare();               //updates the gameboard if a square has been selected/released
        }