Esempio n. 1
0
        public ChessOnlineForm(UserModel user, Uri host, ChessGameInfo gameInfo)
        {
            this.user   = user;
            this.host   = host;
            this.gameId = gameInfo.GameId;

            if (gameInfo.White?.Name == user.Player.Name)
            {
                playerColor = PieceColor.White;
            }
            else if (gameInfo.Black?.Name == user.Player.Name)
            {
                playerColor = PieceColor.Black;
            }

            if (gameInfo.White != null && gameInfo.Black != null)
            {
                gameState = new ChessGameState(gameInfo);
            }

            InitializeComponent();
            InitializeBoard();
            InitHttpClient();
            InitHubConnection();

            Icon = Icon.FromHandle(Properties.Resources.knight_white.GetHicon());
            Text = $"Chess - {user.Player.Name}";

            if (playerColor == null)
            {
                grbPlayerColor.Text  = "Spectating";
                cfbPlayerColor.Image = Properties.Resources.eye;
            }
            else
            {
                grbPlayerColor.Text  = "Your color";
                cfbPlayerColor.Image = PieceImageUtils.Pawn(playerColor.Value);
            }
        }
Esempio n. 2
0
        private void RefreshViews()
        {
            if (board == null)
            {
                return;
            }

            labPlayerNames.Text = $"White: \n{gameState?.GameInfo?.White?.Name} \n\nBlack: \n{gameState?.GameInfo?.Black?.Name}";

            if (gameState == null)
            {
                btnAddBot.Visible = true;
                return;
            }

            btnAddBot.Visible = false;

            if (gameState.IsCheckmate)
            {
                panTable.Enabled = false;
                grbPlaying.Text  = "Checkmate";
                cfbPlaying.Image = PieceImageUtils.King(gameState.OnTurn.Other());
                if (playerColor != null)
                {
                    cfbPlaying.BackColor = gameState.OnTurn != playerColor ? Color.YellowGreen : Color.OrangeRed;
                }
            }
            else if (gameState.IsDraw)
            {
                panTable.Enabled     = false;
                grbPlaying.Text      = "Draw";
                cfbPlaying.Image     = Properties.Resources.king_multi;
                cfbPlaying.BackColor = Control.DefaultBackColor;
            }
            else
            {
                panTable.Enabled = true;
                grbPlaying.Text  = "On turn";
                cfbPlaying.Image = PieceImageUtils.Pawn(gameState.OnTurn);
                if (playerColor != null)
                {
                    cfbPlaying.BackColor = gameState.OnTurn == playerColor ? Color.YellowGreen : Control.DefaultBackColor;
                }
            }

            pieceListPanelBlack.SetPieceChars(gameState.CapturedPieces.Where(c => c >= 'a' && c <= 'z').ToList());
            pieceListPanelWhite.SetPieceChars(gameState.CapturedPieces.Where(c => c >= 'A' && c <= 'Z').ToList());

            for (int i = 0; i < 8; ++i)
            {
                for (int j = 0; j < 8; ++j)
                {
                    var fieldBox = fieldBoxes[i, j];
                    fieldBox.SuspendLayout();
                    fieldBox.Style = ChessFieldBox.BoxStyle.None;
                    fieldBox.Image = board[i, j]?.Image();
                }
            }

            if (gameState.CheckingPieces?.Count > 0)
            {
                var king = board.King(board.TurnColor);
                fieldBoxes[king.X, king.Y].Style = ChessFieldBox.BoxStyle.Checked;
                foreach (var piece in gameState.CheckingPieces)
                {
                    var field = ChessGameUtils.FieldFromString(piece);
                    fieldBoxes[field.Item1, field.Item2].Style = ChessFieldBox.BoxStyle.Checking;
                }
            }

            if (selectedField != null)
            {
                fieldBoxes[selectedField.Item1, selectedField.Item2].Style = ChessFieldBox.BoxStyle.Selected;
                if (possibleMoves != null)
                {
                    var selectedPiece = board[selectedField.Item1, selectedField.Item2];
                    foreach (var field in possibleMoves)
                    {
                        fieldBoxes[field.Item1, field.Item2].Style =
                            board[field.Item1, field.Item2] != null || (selectedPiece is Pawn && selectedPiece.X != field.Item1) ?
                            ChessFieldBox.BoxStyle.Attack : ChessFieldBox.BoxStyle.Move;
                    }
                }
            }

            if (board.LastMove != null)
            {
                var lastMove = board.LastMove;
                if (lastMove.ConnectedMove)
                {
                    lastMove = lastMove.LastMove;
                }
                labLastMove.Text = $"Last move:\n{ChessGameUtils.FieldToString(lastMove.FromX, lastMove.FromY)} => {ChessGameUtils.FieldToString(lastMove.ToX, lastMove.ToY)}";
                if (showLastMove)
                {
                    fieldBoxes[lastMove.FromX, lastMove.FromY].Style = ChessFieldBox.BoxStyle.MoveFrom;
                    fieldBoxes[lastMove.ToX, lastMove.ToY].Style     = lastMove.CapturedPiece != null ?
                                                                       ChessFieldBox.BoxStyle.MoveToCapture : ChessFieldBox.BoxStyle.MoveTo;
                }
            }

            foreach (var fieldBox in fieldBoxes)
            {
                fieldBox.ResumeLayout(false);
                fieldBox.Invalidate();
            }
        }
Esempio n. 3
0
        private void RefreshViews()
        {
            if (board.IsCheckmate)
            {
                panTable.Enabled = false;
                labPlaying.Text  = "Victory";
                cfbPlaying.Image = PieceImageUtils.King(board.TurnColor.Other());
            }
            else if (board.IsDraw)
            {
                panTable.Enabled = false;
                labPlaying.Text  = "Draw";
                cfbPlaying.Image = null;
            }
            else
            {
                panTable.Enabled = true;
                labPlaying.Text  = "Now playing";
                cfbPlaying.Image = PieceImageUtils.Pawn(board.TurnColor);
            }

            pieceListPanelBlack.SetPieces(board.GetCaptured(PieceColor.Black));
            pieceListPanelWhite.SetPieces(board.GetCaptured(PieceColor.White));

            for (int i = 0; i < 8; ++i)
            {
                for (int j = 0; j < 8; ++j)
                {
                    var fieldBox = fieldBoxes[i, j];
                    fieldBox.SuspendLayout();
                    fieldBox.Style = ChessFieldBox.BoxStyle.None;
                    fieldBox.Image = board[i, j]?.Image();
                }
            }

            if (board.IsCheck)
            {
                var king = board.King(board.TurnColor);
                fieldBoxes[king.X, king.Y].Style = ChessFieldBox.BoxStyle.Checked;
                foreach (var piece in board.CheckingPieces)
                {
                    fieldBoxes[piece.X, piece.Y].Style = ChessFieldBox.BoxStyle.Checking;
                }
            }

            if (selectedPiece != null)
            {
                fieldBoxes[selectedPiece.X, selectedPiece.Y].Style = ChessFieldBox.BoxStyle.Selected;
                foreach (var field in possibleMoves)
                {
                    fieldBoxes[field.Item1, field.Item2].Style =
                        board[field.Item1, field.Item2] != null || (selectedPiece is Pawn && selectedPiece.X != field.Item1) ?
                        ChessFieldBox.BoxStyle.Attack : ChessFieldBox.BoxStyle.Move;
                }
            }

            foreach (var fieldBox in fieldBoxes)
            {
                fieldBox.ResumeLayout(false);
                fieldBox.Invalidate();
            }
        }