Example #1
0
        private void RandomMoveButton_Click(object sender, EventArgs e)
        {
            IOthelloAgent myAgent = new RandomAgent();

            _myGame.MakeMove(myAgent.MakeMove(_myGame, _myGame.WhosTurn));
            RefreshControls();
        }
Example #2
0
        public static void TestMinMax(OthelloGame _myGame, int minimaxDepth = 3)
        {
            const int testCount = 100;

            object wonGamesLock = new object();
            int    wonGames     = 0;
            object tieGamesLock = new object();
            int    tieGames     = 0;

            var stopwatch = Stopwatch.StartNew();

            //Parallel.For(0, testCount, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },index =>
            //{
            for (int index = 0; index < testCount; index++)
            {//non-parallel for loop to debug
                BoardStates player = (index % 2 == 0) ? BoardStates.black : BoardStates.white;

                OthelloGame testGame     = new OthelloGame();
                MinMaxAgent othelloAgent = new MinMaxAgent(2);
                RandomAgent randAgent    = new RandomAgent();
                while (!testGame.GameComplete)
                {
                    if (testGame.WhosTurn == player)
                    {
                        testGame.MakeMove(othelloAgent.MakeMove(testGame, player));
                    }
                    else
                    {
                        testGame.MakeMove(randAgent.MakeMove(testGame, ~player));
                    }
                }
                if (testGame.GameComplete)//just gotta check
                {
                    if (testGame.FinalWinner == player)
                    {
                        lock (wonGamesLock) { wonGames++; }
                    }
                    else if (testGame.FinalWinner == BoardStates.empty)
                    {
                        lock (tieGamesLock) { tieGames++; }
                    }
                    Console.WriteLine("Finished Game " + index + ", " + testGame.FinalWinner.ToString()
                                      + " won " + testGame.GetPieceCount(testGame.FinalWinner) + " to "
                                      + testGame.GetPieceCount(OthelloGame.OpposingPlayer(testGame.FinalWinner)));;
                }
                else
                {
                    throw new Exception("MiniMax Testing didn't complete a game");
                }
            }

            //});
            stopwatch.Stop();
            Console.WriteLine("Won " + wonGames + " / " + testCount + " games, " + ((double)wonGames / testCount) * 100 + " %");
            Console.WriteLine("Tied " + tieGames + " / " + testCount + " games, " + ((double)tieGames / testCount) * 100 + " %");
            Console.WriteLine("Lost " + (testCount - wonGames - tieGames) + " / " + testCount + " games, " + ((double)(testCount - wonGames - tieGames) / testCount) * 100 + " %");
            Console.WriteLine("Elapsed time for games : {0}", stopwatch.Elapsed);
        }
Example #3
0
 private void RandomGameButton_Click(object sender, EventArgs e)
 {
     EnableControlButtons(false);
     new Thread(() =>
     {
         IOthelloAgent myAgent = new RandomAgent();
         while (!_myGame.GameComplete)
         {
             _myGame.MakeMove(myAgent.MakeMove(_myGame, _myGame.WhosTurn));
         }
         Invoke(new Action(() =>
         {
             RefreshControls();
             EnableControlButtons();
         }));
     }).Start();
 }
Example #4
0
        private void RunGamesButton_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GameCountTextBox.Text.Length; i++)
            {
                if (!char.IsNumber((char)(GameCountTextBox.Text[i])))
                {
                    GameCountTextBox.BackColor = Color.Red;
                    _twoSecondTimer.Elapsed   += new System.Timers.ElapsedEventHandler(GameCountTextBox_ColorControl);
                    _twoSecondTimer.Enabled    = true;
                    return;
                }
            }

            int gameCount = Convert.ToInt32(GameCountTextBox.Text);

            EnableControlButtons(false);

            new Thread(() =>
            {
                IOthelloAgent myAgent = new RandomAgent();
                for (int i = 0; i < gameCount; i++)
                {
                    while (!_myGame.GameComplete)
                    {
                        _myGame.MakeMove(myAgent.MakeMove(_myGame, _myGame.WhosTurn));
                    }
                    Invoke(new Action(() =>
                    {
                        RefreshControls();
                    }));
                    _myGame.ResetBoard();
                    Thread.Sleep(5);
                }
                Invoke(new Action(() =>
                {
                    RefreshControls();
                    EnableControlButtons();
                }));
            }).Start();
        }