Esempio n. 1
0
        /// <summary>
        /// Uses ML.NET library
        /// with the data of approximately 1000 games, and few thousand proven good moves to "predict" a "good" move
        /// </summary>
        private void MachineLearningMove()
        {
            // what was the tile placed previously
            string tileplayed = (Game.PlayerTurn == 1) ? "X" : "O";

            // Add input data
            // The gameboard is a 2D array of size n
            // Needs updating if support for >3 size is added in future
            var input = new ModelInput();

            input.Tile1      = Game.Gameboard.getboard()[0, 0].Value.ToString();
            input.Tile2      = Game.Gameboard.getboard()[1, 0].Value.ToString();
            input.Tile3      = Game.Gameboard.getboard()[2, 0].Value.ToString();
            input.Tile4      = Game.Gameboard.getboard()[0, 1].Value.ToString();
            input.Tile5      = Game.Gameboard.getboard()[1, 1].Value.ToString();
            input.Tile6      = Game.Gameboard.getboard()[2, 1].Value.ToString();
            input.Tile7      = Game.Gameboard.getboard()[0, 2].Value.ToString();
            input.Tile8      = Game.Gameboard.getboard()[1, 2].Value.ToString();
            input.Tile9      = Game.Gameboard.getboard()[2, 2].Value.ToString();
            input.TilePlayed = tileplayed;

            // Load model and predict output of sample data
            ModelOutput result = ConsumeModel.Predict(input);

            // Get a list of legal moves (tiles that do not have a value)
            List <Tile> legalMoves = new List <Tile>();

            foreach (Tile tile in Game.Gameboard.getboard())
            {
                if (tile.Value == Tile.TileValue.NaN)
                {
                    legalMoves.Add(tile);
                }
            }

            // result of prediction
            int ID = Convert.ToInt32(result.Prediction);

            // Find the tile with same ID
            Tile MoveTile = legalMoves.Find(X => X.ID == ID);

            // machine predicted an ID thats not a legal move, give a random move instead...
            if (MoveTile == null)
            {
                Random r = new Random();
                MoveTile = legalMoves[r.Next(0, legalMoves.Count - 1)];
            }
            if (MoveTile.CheckTileState())
            {
                Game.MakeMove(MoveTile.X, MoveTile.Y, Game.Players[Game.PlayerTurn].PlayerTile);
            }
            else
            {
                // Somehow the tile was taken use a random move
                Game.RANDOM_MOVE();
            }
        }