예제 #1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button          clicked    = sender as Button;
            FieldCoordinate coordinate = (FieldCoordinate)clicked.Tag;
            int             clickHoriz = coordinate.Horiz;
            int             clickVert  = coordinate.Vert;

            int playerNumber = engine.NextMovePlayerNumber;

            if (engine.PutStone(clickHoriz, clickVert))
            {
                AgreeFieldContent();
                switch (playerNumber)
                {
                case 1:
                    moveGreenList.Items.Add(FieldSymbol(clickHoriz, clickVert));
                    break;

                case 2:
                    moveBrownList.Items.Add(FieldSymbol(clickHoriz, clickVert));
                    break;
                }
            }
            moveGreenList.SelectedIndex = moveGreenList.Items.Count - 1;
            moveBrownList.SelectedIndex = moveBrownList.Items.Count - 1;

            ReversiEngine.FieldSituation fieldSituation = engine.CheckFieldSituation();
            bool gameOver = false;

            switch (fieldSituation)
            {
            case ReversiEngine.FieldSituation.ActualPlayerCantMove:
                MessageBox.Show("Gracz " + playerName[engine.NextMovePlayerNumber] + " zmuszony jest do oddania ruchu");
                engine.Pass();
                AgreeFieldContent();
                break;

            case ReversiEngine.FieldSituation.BothPlayerCantMove:
                MessageBox.Show("Obaj gracze nie mogą wykonac ruchu");
                gameOver = true;
                break;

            case ReversiEngine.FieldSituation.AllPointOfFieldAreTaken:
                gameOver = true;
                break;
            }

            if (gameOver)
            {
                int winner = engine.PlayerNumberWithAdventage;
                if (winner != 0)
                {
                    MessageBox.Show("Wygrał gracz " + playerName[winner], Title, MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    MessageBox.Show("Remis", Title, MessageBoxButton.OK, MessageBoxImage.Information);
                }

                if (MessageBox.Show("Czy rozpocząć gre od nowa?", "Reversi",
                                    MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes) == MessageBoxResult.Yes)
                {
                    PrepareNewGame(1, engine.FieldWidth, engine.FieldHeight);
                }
                else
                {
                    fieldGrid.IsEnabled         = false;
                    colorPlayerButton.IsEnabled = false;
                }
            }
            else
            {
                if (gameAgainstComputer && engine.NextMovePlayerNumber == 2)
                {
                    if (timer == null)
                    {
                        timer          = new DispatcherTimer();
                        timer.Interval = new TimeSpan(0, 0, 0, 0, 300);
                        timer.Tick    += (_sender, _e) => { timer.IsEnabled = false; DoBestMove(); };
                    }
                    timer.Start();
                }
            }
        }
예제 #2
0
        private void boardControl_ClickingField(object sender, BoardEventArgs e)
        {
            int clickedHorizontal = e.FieldCoordinates.Horizontal;
            int clickedVertical   = e.FieldCoordinates.Vertical;

            //doing movement
            int rememberedPlayerNumber = engine.PlayerNumberMakingNextMove;

            if (engine.PutStone(clickedHorizontal, clickedVertical))
            {
                AgreeBoardContent();

                //list of moves
                switch (rememberedPlayerNumber)
                {
                case 1: listOfMoveGreen.Items.Add(FieldBadge(clickedHorizontal, clickedVertical));
                    break;

                case 2: listOfMoveBrown.Items.Add(FieldBadge(clickedHorizontal, clickedVertical));
                    break;
                }
                listOfMoveGreen.SelectedIndex = listOfMoveGreen.Items.Count - 1;
                listOfMoveBrown.SelectedIndex = listOfMoveBrown.Items.Count - 1;
            }

            //special situation
            SituationOnBoard situationOnBoard = engine.InspectSituationOnBoard();
            bool             gameOver         = false;

            switch (situationOnBoard)
            {
            case SituationOnBoard.BieżącyGraczNieMożeWykonaćRuchu:
                MessageBox.Show("Gracz " + playersNames[engine.PlayerNumberMakingNextMove] + " zmuszony jest do oddania ruchu");
                engine.Pass();
                AgreeBoardContent();
                break;

            case SituationOnBoard.ObajGraczeNieMogąWykonaćRuchu:
                MessageBox.Show("Obaj gracze nie mogą wykonać ruchu");
                gameOver = true;
                break;

            case SituationOnBoard.WszystkiePolaPlanszyZajęte:
                gameOver = true;
                break;
            }

            if (gameOver)
            {
                int winnerNumber = engine.PlayerNumberWithAdvantage;
                if (winnerNumber != 0)
                {
                    MessageBox.Show("Wygrał gracz " + playersNames[winnerNumber], Title, MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    MessageBox.Show("Remis", Title, MessageBoxButton.OK, MessageBoxImage.Information);
                }
                if (MessageBox.Show("Czy rozpocząć grę od nowa?", "Reversi", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    PrepareBoardToNewGame(1, engine.BoardWidth, engine.BoardHeight);
                }
                else
                {
                    boardControl.IsEnabled      = false;
                    buttonPlayerColor.IsEnabled = false;
                }
            }
            else
            {
                if (_gameAgainstComputer && engine.PlayerNumberMakingNextMove == 2)
                {
                    if (timer == null)
                    {
                        timer          = new DispatcherTimer();
                        timer.Interval = new TimeSpan(0, 0, 0, 0, 300);
                        timer.Tick    += (_sender, _e) => { timer.IsEnabled = false; MakeTheBestMove(); };
                    }
                    timer.Start();
                }
            }
        }