Program.cs 1.4 KB

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