Example #1
0
        private void Play()
        {
            while (!currentState.IsDone())
            {
                currentState.Print();
                currentState = currentPlayer.GetMove(currentState);
                gameHistory.Add(currentState.GetHashString());
                AlternatePlayers();
            }

            Console.WriteLine("\nPlayer #" + (currentPlayer == players.Item1 ? "1" : "2") + " won!");
            Console.WriteLine();

            // determines scores and stores them in database
            for (int i = 1; i < gameHistory.Count; i++)
            {
                bool isWinningPlayer = currentPlayer == (i % 2 == 1 ? players.Item1 : players.Item2);

                int scoreSign, totalMoves;

                if (isWinningPlayer)
                {
                    scoreSign  = 1;
                    totalMoves = (int)Math.Floor(gameHistory.Count / 2.0);
                }
                else
                {
                    scoreSign  = -1;
                    totalMoves = (int)Math.Ceiling(gameHistory.Count / 2.0);
                }

                string key = gameHistory[i];
                StateDatabase.AddScore(key, (1.0 / totalMoves) * scoreSign);
            }
        }
Example #2
0
        public void Start()
        {
            do
            {
                SelectMenuAction();
            } while (keepPlaying);

            StateDatabase.Serialize(DATABASE_FILE);
        }
Example #3
0
        private GameState BestMove(GameState currentGameState)
        {
            List <GameState> bestMoves = new List <GameState>();

            foreach (GameState possibleGameState in currentGameState)
            {
                if (possibleGameState.GameSum() == 1)
                {
                    bestMoves = new List <GameState>();
                    bestMoves.Add(possibleGameState);
                    break;
                }

                if (bestMoves.Count == 0)
                {
                    bestMoves.Add(possibleGameState);
                }
                else
                {
                    double bestScore = StateDatabase.GetAverage(possibleGameState.GetHashString());
                    double possibleGameStateScore = StateDatabase.GetAverage(possibleGameState.GetHashString());

                    if (possibleGameStateScore > bestScore)
                    {
                        bestMoves = new List <GameState>();
                        bestMoves.Add(possibleGameState);
                    }
                    else if (possibleGameStateScore == bestScore)
                    {
                        bestMoves.Add(possibleGameState);
                    }
                }
            }

            return(bestMoves[r.Next(bestMoves.Count)]);
        }
Example #4
0
 public NimLearningGame()
 {
     StateDatabase.Deserialize(DATABASE_FILE);
     menu = new MenuManager();
     SetupMenu();
 }