Program.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Diagnostics;
  2. using FourInARow;
  3. using MinMaxAB;
  4. internal class Program
  5. {
  6. const int depth = 9;
  7. const bool maxPlayer = false;
  8. const int alpha = int.MinValue;
  9. const int beta = int.MaxValue;
  10. private static void Main()
  11. {
  12. var board = new GameBoard(new(7, 6));
  13. var minmax = new MinMaxABFourInARowBoard();
  14. while (true)
  15. {
  16. AiMove(board, minmax);
  17. HumanMove(board, minmax);
  18. }
  19. }
  20. private static void AiMove(GameBoard board, MinMaxABFourInARowBoard minmax)
  21. {
  22. //Console.WriteLine(board);
  23. Console.WriteLine("Thinking...");
  24. var sw = new Stopwatch();
  25. sw.Start();
  26. var move = minmax.GetBestMove(board, depth);
  27. sw.Stop();
  28. Console.WriteLine($"Decision made in {sw.Elapsed}");
  29. //Console.Clear();
  30. board.PlaceCoin(Player.Computer, move);
  31. Console.WriteLine($"AI Moves {move + 1}");
  32. //Console.WriteLine(board);
  33. if (board.GetWinner() == Player.Computer)
  34. {
  35. Console.WriteLine("AI Wins");
  36. Environment.Exit(0);
  37. }
  38. if (!board.HasEmptyColumns())
  39. {
  40. Console.WriteLine("TIE");
  41. Environment.Exit(0);
  42. }
  43. }
  44. private static void HumanMove(GameBoard board, MinMaxABFourInARowBoard minmax)
  45. {
  46. Console.WriteLine(board);
  47. Console.Write("Your move: ");
  48. GC.Collect(2, GCCollectionMode.Aggressive);
  49. GC.WaitForFullGCComplete();
  50. board.PlaceCoin(Player.Human, int.Parse(Console.ReadLine()!) - 1);
  51. Console.Clear();
  52. if (board.GetWinner() == Player.Human)
  53. {
  54. Console.WriteLine("Human Wins");
  55. Environment.Exit(0);
  56. }
  57. if (!board.HasEmptyColumns())
  58. {
  59. Console.WriteLine("TIE");
  60. Environment.Exit(0);
  61. }
  62. }
  63. }