Пример #1
0
        private void X_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Tuple <int, int> coords = ((Tuple <int, int>)((Rectangle)sender).DataContext); //Extracting coords from Rectangle DataContext.
            int boardX = coords.Item1 - 1;                                                 //GridX to BoardX
            int boardY = coords.Item2 - 1;                                                 //GridY to BoardY

            if (board.IsPlayable(boardX, boardY, isWhiteTurn))
            {
                // push step on undostack
                stackUndo.Push(new Tuple <int[], bool>(board.GetValues(), isWhiteTurn));
                // clean redo stack
                stackRedo.Clear();

                board.PlayMove(boardX, boardY, isWhiteTurn);
                UpdateTurn();
                //board.UpdateNextPossibleMoves(isWhiteTurn ? 1 : -1);
                //DisplayBoard();

                if (IAGame)
                {
                    int[,] game = board.GetBoard();
                    Tuple <int, int> play = board.GetNextMove(game, 5, isWhiteTurn);
                    board.PlayMove(play.Item1, play.Item2, isWhiteTurn);
                    UpdateTurn();
                    board.UpdateNextPossibleMoves(isWhiteTurn ? 1 : -1);
                }

                // Blocked player part
                if (board.NextPossibleMoves.Count() == 0)
                {
                    whiteBlocked = blackBlocked = true;
                    if (isWhiteTurn)
                    {
                        swWhitePlayer.Stop();
                    }
                    else
                    {
                        swBlackPlayer.Stop();
                    }
                }
                else
                {
                    if (isWhiteTurn)
                    {
                        swWhitePlayer.Start();
                        swBlackPlayer.Stop();
                    }
                    else
                    {
                        swWhitePlayer.Stop();
                        swBlackPlayer.Start();
                    }
                }
                DisplayBoard();
            }
            UpdateScore();

            if (whiteBlocked ^ blackBlocked)
            {
                string turn    = isWhiteTurn ? "White" : "Black";
                string notTurn = isWhiteTurn ? "Black" : "White";
                MessageBox.Show($"No possible move for {notTurn} !\n{turn} can keep playing.");
            }
            if (whiteBlocked && blackBlocked)
            {
                int sWhite = Convert.ToInt32(ScoreJ1.Content);
                int sBlack = Convert.ToInt32(ScoreJ2.Content);
                if (sWhite != sBlack)
                {
                    string winner = "";
                    winner = sWhite > sBlack ? "White" : "Black";
                    MessageBox.Show($"GAME !\n{winner} wins !!!");
                }
                else
                {
                    MessageBox.Show("Tie game !");
                }
            }
        }