Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. public double EvaluationFunction()
  2. {
  3. //throw new Exception("Aceasta metoda trebuie implementata");
  4. double c = 0;
  5. double o = 0;
  6. int i;
  7. for (i = 0; i < Size; ++i)
  8. {
  9. c += Size - Pieces[i].Y;
  10. o += Pieces[i + Size].Y;
  11. }
  12. return o + c;
  13. }
  14.  
  15. ----------------------------
  16.  
  17. public static Board FindNextBoard(Board currentBoard)
  18. {
  19. //throw new Exception("Aceasta metoda trebuie implementata");
  20. List<Board> configurations = new List<Board>();
  21. List<Board> finalConfiguration = new List<Board>();
  22. double max = 0;
  23. foreach (Piece p in currentBoard.Pieces)
  24. {
  25. if (p.Id >= 0 && p.Id <= 3)
  26. {
  27. List<Move> movesPiece = p.ValidMoves(currentBoard);
  28. foreach (Move m in movesPiece)
  29. {
  30. configurations.Add(currentBoard.MakeMove(m));
  31. }
  32. }
  33. }
  34. foreach (Board b in configurations)
  35. {
  36. double evalStatic = b.EvaluationFunction();
  37. if (max < evalStatic)
  38. {
  39. max = evalStatic;
  40. }
  41. }
  42. foreach (Board b in configurations)
  43. {
  44. double evalStatic = b.EvaluationFunction();
  45. if (max == evalStatic)
  46. {
  47. finalConfiguration.Add(b);
  48. }
  49. }
  50.  
  51. int r = _rand.Next(finalConfiguration.Count);
  52. if (finalConfiguration.Count == 0)
  53. return currentBoard;
  54. return finalConfiguration[r];
  55. }
  56.  
  57. ----------------------------------
  58.  
  59. public List<Move> ValidMoves(Board currentBoard)
  60. {
  61. //throw new Exception("Aceasta metoda trebuie implementata");
  62. int boardSize = currentBoard.Size;
  63. List<Move> validMovesList = new List<Move>();
  64. for (int x = 0; x < boardSize; ++x)
  65. {
  66. for (int y = 0; y < boardSize; ++y)
  67. {
  68. Move m = new Move(Id, x, y);
  69. if (IsValidMove(currentBoard, m))
  70. {
  71. validMovesList.Add(m);
  72. }
  73. }
  74. }
  75. return validMovesList;
  76. }
  77.  
  78. /// <summary>
  79. /// Testeaza daca o mutare este valida intr-o anumita configuratie
  80. /// </summary>
  81. public bool IsValidMove(Board currentBoard, Move move)
  82. {
  83. //throw new Exception("Aceasta metoda trebuie implementata");
  84. int boardSize = currentBoard.Size;
  85. if (move.NewX >= boardSize || move.NewY >= boardSize || move.NewX < 0 || move.NewY < 0)
  86. return false;
  87. if (move.NewX > X + 1 || move.NewY > Y + 1 || move.NewX < X - 1 || move.NewY < Y - 1)
  88. return false;
  89. if (move.NewX == X && move.NewY == Y)
  90. return false;
  91. foreach (Piece p in currentBoard.Pieces)
  92. {
  93. if (p.X == move.NewX && p.Y == move.NewY)
  94. {
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement