private void updateAttacksButton_Click(object sender, EventArgs e)
        {
            MoveLogic logic = new MoveLogic();

            logic.ClearChessBoardColors(board);
            logic.HighlightAttackedSquares(board);
        }
        private void clearBoard_Click(object sender, EventArgs e)
        {
            MoveLogic logic = new MoveLogic();

            logic.ClearChessBoardColors(board);

            foreach (Button square in board)
            {
                square.Image = null;
                square.Tag   = TagStrings[(int)PieceEnum.None];
            }
        }
Ejemplo n.º 3
0
        public GameSession(User Me, User Them, NetworkStream networkStream, Session session, LobbyForm Lobby, MoveLogic ml, CustomGame game = null)
        {
            me = Me;
            int squareSize = 65;
            int offset     = 50;

            InitializeComponent();
            lobby     = Lobby;
            moveLogic = ml;

            // Create timers
            timer          = new System.Timers.Timer(1000);
            timer.Elapsed += async(sender, e) => await Timer_Elapsed();

            stream      = networkStream;
            sessionInfo = session;

            originalTotalTimeRemaining = eTotalTimeRemaining = totalTimeRemaining = sessionInfo.GameTimerSeconds;
            originalTimeRemaining      = eTotalTimeRemaining = turnTimeRemaining = sessionInfo.MoveTimerSeconds;

            myTotalTimeRemaining.Text    = ChessUtils.ConvertSecondsToTimeString(totalTimeRemaining);
            myTimeRemaining.Text         = ChessUtils.ConvertSecondsToTimeString(turnTimeRemaining);
            enemyTotalTimeRemaining.Text = ChessUtils.ConvertSecondsToTimeString(totalTimeRemaining);
            enemyTimeRemaining.Text      = ChessUtils.ConvertSecondsToTimeString(turnTimeRemaining);

            myName       = myUsername.Text = me.Username;
            opponentName = enemyUsername.Text = Them.Username;
            customGame   = game;
            // set up the array of buttons (chess grid) into an array
            board = new Button[, ]
            {
                { square00, square01, square02, square03, square04, square05, square06, square07 },
                { square10, square11, square12, square13, square14, square15, square16, square17 },
                { square20, square21, square22, square23, square24, square25, square26, square27 },
                { square30, square31, square32, square33, square34, square35, square36, square37 },
                { square40, square41, square42, square43, square44, square45, square46, square47 },
                { square50, square51, square52, square53, square54, square55, square56, square57 },
                { square60, square61, square62, square63, square64, square65, square66, square67 },
                { square70, square71, square72, square73, square74, square75, square76, square77 }
            };

            // Set up the board to look pretty
            for (int i = 0; i < 8; ++i)
            {
                for (int j = 0; j < 8; ++j)
                {
                    Point       p = new Point(j * squareSize + offset, i * squareSize + (offset * 2));
                    Size        s = new Size(squareSize, squareSize);
                    Coordinates c = ChessUtils.Settings.GetCoordinatesOfButton(board[i, j]);

                    board[i, j].Size     = s;
                    board[i, j].Location = p;
                }
            }

            ChessUtils.Settings.Image.UpdateBoardImages(board);
            ChessUtils.Settings.Color.UpdateChessBoardColors(board);

            UpdatePlayerPieces(whitePieces, blackPieces);
            moveLogic.UpdateAttackedSquares(board);

            SetUpButtons();
            moveLogic.ClearChessBoardColors(board);

            // I am the guest, both people are in the game, now set the game to started
            if (Me.Username != "" && Them.Username != "")
            {
                moveLogic.gameStarted = true;
            }

            checkLabel.Hide();

            gameWorker.RunWorkerAsync();
        }
Ejemplo n.º 4
0
        private void ButtonClicked(object sender, EventArgs args)
        {
            Button currentButton = sender as Button;

            selectedButton = currentButton;
            Coordinates c = ChessUtils.Settings.GetCoordinatesOfButton(currentButton);
            Coordinates pc;
            Coordinates cc;
            Piece       piece     = moveLogic.GetPieceOnSquare(c.X, c.Y, board);
            bool        isMyPiece = moveLogic.myColor == piece.Color;

            // If there is a piece on the selected square that is current player's color
            if (isMyPiece)
            {
                List <Button> possibleMoves = moveLogic.GetPossibleMovesForPiece(currentButton, board);
                moveLogic.HighlightButtons(possibleMoves);
            }

            // Potential square to move to from a selected piece
            else
            {
                if (SelectedPieceCanMoveTo(currentButton))
                {
                    pc = ChessUtils.Settings.GetCoordinatesOfButton(previousButton);
                    cc = ChessUtils.Settings.GetCoordinatesOfButton(currentButton);
                    Piece thisPiece = moveLogic.GetPieceOnSquare(pc.X, pc.Y, board);
                    isMyPiece = moveLogic.myColor == thisPiece.Color;

                    if (isMyPiece)
                    {
                        // Move piece to new square
                        currentButton.Tag   = previousButton.Tag;
                        currentButton.Image = previousButton.Image;

                        // Take piece off selected square
                        previousButton.Tag   = "";
                        previousButton.Image = null;

                        bool promoted = false;

                        // Check for pawn promotion
                        if (cc.X % 7 == 0 && currentButton.Tag.ToString().Contains("Pawn"))
                        {
                            // White promotion
                            if (thisPiece.Color == pieceColor.white)
                            {
                                promoted = true;
                                var whiteForm = new PawnPromotionFormWhite();

                                // Pick based off of button
                                if (whiteForm.ShowDialog() == DialogResult.OK)
                                {
                                    switch (whiteForm.GetValue())
                                    {
                                    case "Queen":
                                        currentButton.Tag   = "wQueen";
                                        currentButton.Image = ChessUtils.Settings.Image.WhiteQueen;
                                        break;

                                    case "Rook":
                                        currentButton.Tag   = "wRook";
                                        currentButton.Image = ChessUtils.Settings.Image.WhiteRook;
                                        break;

                                    case "Knight":
                                        currentButton.Tag   = "wKnight";
                                        currentButton.Image = ChessUtils.Settings.Image.WhiteKnight;
                                        break;

                                    case "Bishop":
                                        currentButton.Tag   = "wBishop";
                                        currentButton.Image = ChessUtils.Settings.Image.WhiteBishop;
                                        break;
                                    }
                                }

                                // If form is closed, default to a queen
                                else
                                {
                                    currentButton.Tag   = "wQueen";
                                    currentButton.Image = ChessUtils.Settings.Image.WhiteQueen;
                                }
                            }

                            // Black promotion
                            else
                            {
                                promoted = true;
                                var blackForm = new PawnPromotionFormBlack();
                                // Pick based off of button
                                if (blackForm.ShowDialog() == DialogResult.OK)
                                {
                                    switch (blackForm.GetValue())
                                    {
                                    case "Queen":
                                        currentButton.Tag   = "bQueen";
                                        currentButton.Image = ChessUtils.Settings.Image.BlackQueen;
                                        break;

                                    case "Rook":
                                        currentButton.Tag   = "bRook";
                                        currentButton.Image = ChessUtils.Settings.Image.BlackRook;
                                        break;

                                    case "Knight":
                                        currentButton.Tag   = "bKnight";
                                        currentButton.Image = ChessUtils.Settings.Image.BlackKnight;
                                        break;

                                    case "Bishop":
                                        currentButton.Tag   = "bBishop";
                                        currentButton.Image = ChessUtils.Settings.Image.BlackBishop;
                                        break;
                                    }
                                }

                                // If form is closed, default to a queen
                                else
                                {
                                    currentButton.Tag   = "bQueen";
                                    currentButton.Image = ChessUtils.Settings.Image.BlackQueen;
                                }
                            }
                        }

                        // switch turns
                        moveLogic.myTurn = !moveLogic.myTurn;

                        // Send the move to the server to send to the other client
                        Piece sendPieceLimitedInfo = new Piece();
                        sendPieceLimitedInfo.Coordinates = thisPiece.Coordinates;

                        string pieceName;
                        if (promoted)
                        {
                            pieceName = currentButton.Tag.ToString();
                        }
                        else
                        {
                            pieceName = (moveLogic.myColor == pieceColor.white) ?
                                        "w" : "b";
                            pieceName += thisPiece.Name;
                        }
                        sendPieceLimitedInfo.Name = pieceName;

                        Move move = new Move()
                        {
                            Source      = sendPieceLimitedInfo,
                            Destination = cc
                        };

                        TCPSignal signal = new TCPSignal()
                        {
                            SignalType = Signal.MakeAMove,
                            PlayerMove = move
                        };

                        // Send the json over to the server
                        string json      = JsonConvert.SerializeObject(signal) + "\r";
                        byte[] jsonBytes = ASCIIEncoding.ASCII.GetBytes(json);
                        stream.Write(jsonBytes, 0, jsonBytes.Length);

                        // Clear highlights
                        moveLogic.ClearChessBoardColors(board);
                    }
                }
                else
                {
                    moveLogic.ClearChessBoardColors(board);
                }
            }

            previousButton = currentButton;
        }
        private void hideAttackButton_Click(object sender, EventArgs e)
        {
            MoveLogic logic = new MoveLogic();

            logic.ClearChessBoardColors(board);
        }