Exemplo n.º 1
0
        private void PlayGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var point = Mouse.GetPosition(PlayGrid);

            int    row = 0;
            int    col = 0;
            double accumulatedHeight = 0.0;
            double accumulatedWidth  = 0.0;

            // calc row mouse was over
            foreach (var rowDefinition in PlayGrid.RowDefinitions)
            {
                accumulatedHeight += rowDefinition.ActualHeight;
                if (accumulatedHeight >= point.Y)
                {
                    break;
                }
                row++;
            }

            // calc col mouse was over
            foreach (var columnDefinition in PlayGrid.ColumnDefinitions)
            {
                accumulatedWidth += columnDefinition.ActualWidth;
                if (accumulatedWidth >= point.X)
                {
                    break;
                }
                col++;
            }

            for (int i = Game.ROWS - 1; i >= 0; i--)
            {
                if (TTT_Game.CellIsEmpty(i, col))
                {
                    MarkBoard(i, col);
                    char winner = TTT_Game.checkWinner().Item1;

                    if (rbHuman.IsChecked == true)
                    {
                        return;
                    }
                    else if (rbAIPlayer.IsChecked == true)
                    {
                        if (winner == ' ')
                        {
                            if (rbEasy.IsChecked == true)
                            {
                                Spot S = AI.GetBestMove(TTT_Game, 2);
                                //    //MessageBox.Show(string.Format("Score is {0}!", score));
                                MarkBoard(S.row, S.col);
                            }
                            else if (rbMedium.IsChecked == true)
                            {
                                Spot S = AI.GetBestMove(TTT_Game, 4);
                                //    //MessageBox.Show(string.Format("Score is {0}!", score));
                                MarkBoard(S.row, S.col);
                            }
                            else if (rbHard.IsChecked == true)
                            {
                                Spot S = AI.GetBestMove(TTT_Game, 5);
                                //    //MessageBox.Show(string.Format("Score is {0}!", score));
                                MarkBoard(S.row, S.col);
                            }
                        }
                    }
                    break;
                }
            }
        }
Exemplo n.º 2
0
        private Spot Minimax(Game G, int level, int maxLevel, char turn)
        {
            Spot BestSpot = new Spot();

            //if current AI player
            if (level % 2 == 0)
            {
                BestSpot.score = -1000000;
            }
            //if AI opponent
            else
            {
                BestSpot.score = 1000000;
            }

            //if there is a winner or we are as deep as we are allowed to go
            if (G.checkWinner().Item1 != ' ' || G.BoardIsFull() || level >= maxLevel)
            {
                BestSpot.score = scoreGame(G, level + 1, turn);
                //MessageBox.Show(string.Format("Score is {0}, level is {1}", BestSpot.score, level));
            }
            else
            {
                //note that consts in C# are static, meaning that to access
                //them you should use the class name
                for (int i = 0; i < Game.ROWS; i++)
                {
                    for (int j = 0; j < Game.COLS; j++)
                    {
                        if (G.IsValid(i, j))
                        {
                            Game TmpGame = new Game(G);

                            TmpGame.MarkGameBoard(i, j);

                            Spot TmpSpot = new Spot();
                            TmpSpot.row = i;
                            TmpSpot.col = j;

                            //if (TmpGame.checkWinner().Item1 != ' ')
                            //{
                            TmpSpot.score = Minimax(TmpGame, level + 1, maxLevel, turn).score;

                            //MessageBox.Show(string.Format("Score is {0}, level is {1}", TmpSpot.score, level));
                            //if current AI player
                            if (level % 2 == 0)
                            {
                                if (TmpSpot.score > BestSpot.score)
                                {
                                    BestSpot = TmpSpot;
                                }
                            }
                            //if AI opponent
                            else
                            {
                                if (TmpSpot.score < BestSpot.score)
                                {
                                    BestSpot = TmpSpot;
                                }
                            }
                            //}
                        }
                    }
                }
            }
            //MessageBox.Show(string.Format("Score is {0}, level is {1}", BestSpot.score, level));
            return(BestSpot);
        }