Example #1
0
 public RandomTicTacToeLearning(TicTacToeNetwork network) : base(network)
 {
     random = new Random();
 }
Example #2
0
 public TicTacToeLearning(TicTacToeNetwork network)
 {
     _network = network;
 }
Example #3
0
        public void SOMLearning()
        {
            var               turnNumber     = 1;
            var               random         = new Random();
            var               playCount      = 0;
            var               player1Wins    = 0;
            var               player2Wins    = 0;
            var               ties           = 0;
            var               gameBoard      = new GameBoard();
            var               moveValidator  = new MoveValidator();
            var               game           = new Game(gameBoard, moveValidator);
            var               playerOne      = new Player("Player 1", "X");
            var               playerTwo      = new Player("Player 2", "O");
            IPlayer           curPlayer      = null;
            TicTacToeLearning currentTrainer = null;
            TicTacToeNetwork  network1       = new TicTacToeNetwork(10, 9);

            TicTacToeNetwork network2 = new TicTacToeNetwork(10, 9);
            //DistanceNetwork

            //network1.Output = output;

            // create learning algorithm
            TicTacToeLearning trainer1 = new TicTacToeLearning(network1);
            TicTacToeLearning trainer2 = new RandomTicTacToeLearning(network2);

            //TicTacToeLearning trainer2 = new TicTacToeLearning(network2);
            //// network's input
            double[] input = new double[10];
            // loop
            while (playCount < 100000)
            {
                if (curPlayer == null)
                {
                    var goesFirst = random.Next(1, 3);

                    if (goesFirst == 1)
                    {
                        curPlayer      = playerOne;
                        currentTrainer = trainer1;
                    }
                    else
                    {
                        curPlayer      = playerTwo;
                        currentTrainer = trainer2;
                    }
                }
                else if (curPlayer.Name == "Player 1")
                {
                    curPlayer      = playerTwo;
                    currentTrainer = trainer2;
                }
                else
                {
                    curPlayer      = playerOne;
                    currentTrainer = trainer1;
                }

                input = ConvertGameBoard(turnNumber, game);

                var playerInput = currentTrainer.Run(input);

                while (game.MakeMove(curPlayer, Convert.ToInt32(playerInput)) == false)
                {
                    //currentTrainer.AddPoints(-1);
                    playerInput = currentTrainer.Run(input);
                }

                //currentTrainer.AddMove(playerInput, turnNumber);
                turnNumber++;
                if (game.IsOver)
                {
                    if (moveValidator.HasWinner(gameBoard))
                    {
                        input = ConvertGameBoard(turnNumber, game);
                        if (curPlayer.Name == "Player 1")
                        {
                            player1Wins++;
                            trainer1.AddResult(1, input);
                            trainer2.AddResult(-1, input);
                        }
                        else
                        {
                            player2Wins++;
                            trainer1.AddResult(-1, input);
                            trainer2.AddResult(1, input);
                        }
                    }
                    else
                    {
                        ties++;
                        trainer1.AddResult(0, input);
                        trainer2.AddResult(0, input);
                    }

                    turnNumber    = 1;
                    gameBoard     = new GameBoard();
                    moveValidator = new MoveValidator();
                    game          = new Game(gameBoard, moveValidator);
                    curPlayer     = null;
                    playCount++;
                }
            }

            Console.WriteLine(string.Format("Player 1: {0}", player1Wins));
            Console.WriteLine(string.Format("Player 2: {0}", player2Wins));
            Console.WriteLine(string.Format("Cats: {0}", ties));
        }