public Minimax FindNextMove(int depth) { Minimax ret = null; MiniMax(depth, int.MinValue + 1, int.MaxValue - 1, out ret); return(ret); }
//http://en.wikipedia.org/wiki/Alpha-beta_pruning public int MiniMax(int depth, int alpha, int beta, out Minimax childWithMax) { childWithMax = null; if (depth == 0 || IsTerminalNode()) { //When it is turn for PlayO, we need to find the minimum score. RecursiveScore = m_Score; return(m_TurnForPlayerX ? m_Score : -m_Score); } foreach (Minimax cur in GetChildren()) { Minimax dummy; int score = -cur.MiniMax(depth - 1, -beta, -alpha, out dummy); if (alpha < score) { alpha = score; childWithMax = cur; if (alpha >= beta) { break; } } } RecursiveScore = alpha; return(alpha); }
/// <summary> /// Uses dynamic difficulty adjustment and the minimax algorithum to make take a move. /// </summary> public void DynamicTakeTurn() { m_Difficulty = 5; r = new Random(); int loses = totalGames - wins; if (loses > wins) { int differece = loses - wins; m_Difficulty = 5 - differece; if (m_Difficulty < 1) { m_Difficulty = 1; } } else if (wins > loses) { int difference = wins - loses; m_Difficulty = 5 + wins; if (winStreak > 3) { m_Difficulty++; } if (m_Difficulty > 8) { m_Difficulty = 8; } } else { if (winStreak > 3) { m_Difficulty = 6; } else { m_Difficulty = 5; } } int ranChance = 8 - m_Difficulty; ranChance = r.Next(ranChance); if (ranChance < m_Difficulty) { m_M.UpdateBoard(this.m_Arr); Minimax max = m_M.FindNextMove(m_Difficulty); this.m_Arr = max.MG; m_Turn = 'X'; } else { Randomisation(); } }
/// <summary> /// Assigns the starting inital values for the game and calls the gameplay loop /// </summary> /// <param name="dynamic">true or false, dynamic or static</param> /// <param name="difficulty">The static difficulty value if applicable else 99</param> /// <param name="UI">Display UI? True/False</param> public void StartGame(bool dynamic, int difficulty, bool UI) { m_End = false; m_Turn = 'X'; m_Arr = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' }; m_B = new Board(); m_M = new Minimax((char[])m_Arr.Clone(), false); m_Dynamic = dynamic; if (difficulty != 99) { m_Difficulty = difficulty; } if (UI) { if (ted == null || !ted.IsAlive) { ted = new Thread(LoadUI); ted.Start(); } } GameplayLoop(); }