Ejemplo n.º 1
0
 private void OthelloPeice_Click(object sender, MouseEventArgs e)
 {
     try
     {
         PiecePanel thisPanel = (PiecePanel)sender;
         if (_myGame.WhosTurn == _myPlayer)
         {
             _myGame.MakeMove(_myPlayer, new byte[] { (byte)thisPanel.location[0], (byte)thisPanel.location[1] });
             _myGame.MakeMove(_myAgent.MakeMove(_myGame, ~_myPlayer));
         }
         else
         {
             _myGame.MakeMove(~_myPlayer, _myAgent.MakeMove(_myGame, ~_myPlayer));
         }
         RefreshControls();
     }
     catch
     {
         throw new NotSupportedException("OthelloPeice_Click is not to be used with a control other than a PiecePanel");
     }
     if (_myGame.GameComplete)
     {
         SetGameCompleteVisuals();
     }
 }
Ejemplo n.º 2
0
        private void SetupGame()
        {
            _myGame.ResetBoard();
            _myAgent     = GetAgent();
            _myPlayer    = PlayerSelectComboBox.SelectedItem.ToString() == BoardStates.white.ToString() ? BoardStates.black : BoardStates.white;
            _boardPanels = new PiecePanel[OthelloGame.BOARD_SIZE, OthelloGame.BOARD_SIZE];
            int tileSize = ((Size.Width > Size.Height) ? Size.Height - 45 : Size.Width - 45) / OthelloGame.BOARD_SIZE;

            for (int i = 0; i < OthelloGame.BOARD_SIZE; i++)
            {
                for (int j = 0; j < OthelloGame.BOARD_SIZE; j++)
                {
                    var newPanel = new PiecePanel(new int[] { i, j })
                    {
                        Size     = new Size(tileSize, tileSize),
                        Location = new Point(tileSize * i, tileSize * j)
                    };

                    newPanel.MouseClick += new MouseEventHandler(OthelloPeice_Click);
                    Controls.Add(newPanel);
                    _boardPanels[i, j] = newPanel;

                    Color panelcolor = Color.Red;
                    if (BoardColorDictionary.BoardStateColors.TryGetValue(_myGame.GetBoard()[i, j], out panelcolor))
                    {
                        _boardPanels[i, j].ReColor(panelcolor);
                    }
                }
            }
            if (_myGame.WhosTurn != _myPlayer)
            {
                _myGame.MakeMove(_myAgent.MakeMove(_myGame, ~_myPlayer));
            }
        }
Ejemplo n.º 3
0
        public double Evaluate(IChromosome chromosome)
        {//Play n games vs a random (to be Neural Net), % win is Fitness
            FloatingPointChromosome myChromosome = (FloatingPointChromosome)chromosome;

            double[] genes = myChromosome.ToFloatingPoints();

            double fitness      = 0;
            double wonCount     = 0;
            object wonCountLock = new object();

            for (int index = 0; index < TEST_COUNT; index++)
            {
                //Parallel.For(0, TEST_COUNT, new ParallelOptions() { MaxDegreeOfParallelism = 2},
                //   (index) => {
                BoardStates player = (index % 2 == 0) ? BoardStates.black : BoardStates.white;

                OthelloGame      othelloGame = new OthelloGame();
                IEvaluationAgent heurAgent   = new HeuristicAgent(genes);


                while (!othelloGame.GameComplete)
                {
                    if (othelloGame.WhosTurn == player)
                    {
                        othelloGame.MakeMove(heurAgent.MakeMove(othelloGame, player));
                    }
                    else
                    {
                        othelloGame.MakeMove(opposingAgent.MakeMove(othelloGame, ~player));
                    }
                }
                if (othelloGame.GameComplete)//just gotta check
                {
                    if (othelloGame.FinalWinner == player)
                    {
                        lock (wonCountLock)
                        {
                            wonCount++;
                        }
                    }
                    else if (othelloGame.FinalWinner == BoardStates.empty)
                    {
                        lock (wonCountLock)
                        {
                            wonCount += .5;
                        }
                    }
                }
                else
                {
                    throw new Exception("EvaluationFitness didn't complete a game");
                }
                // });
            }
            fitness = (double)wonCount / TEST_COUNT;
            //System.Diagnostics.Debug.WriteLine("Fitness: " + fitness);
            return(fitness);
        }
Ejemplo n.º 4
0
        private void EducatedPlayButton_Click(object sender, EventArgs e)
        {
            EnableControlButtons(false);
            new Thread(() =>
            {
                int unused = 0;
                _myAgent   = new MinMaxAgent(new HeuristicAgent(), Convert.ToInt32(MoveDepth.Text));
                _myGame.MakeMove(_myGame.WhosTurn, _myAgent.MakeMove(_myGame, _myGame.WhosTurn));

                Invoke(new Action(() =>
                {
                    RefreshControls();
                    EnableControlButtons();
                }));
            }).Start();
        }