HumanStrategy9.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.Eventing.Reader;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using MauMau.Game.Interfaces;
  8. using MauMau.Game.KI;
  9. using MauMau.Logic;
  10. namespace MauMau.Game.Players
  11. {
  12. public class HumanStrategy9 : BasicPlayStrategy
  13. {
  14. public override GameAction GetNextAction(GameState state)
  15. {
  16. var standartAction = base.GetNextAction(state);
  17. if (standartAction != null)
  18. return standartAction;
  19. var returnAction = new GameAction();
  20. var playableCards =
  21. state.PlayerHands[_id].Where(m => m.CanPlayOnCard(state.TopCard, state.LastWishedColor)).ToList();
  22. if (state.PlayerHands[(_id + 1) % state.PlayerHands.Length].Count <= 2 && playableCards.FirstOrDefault(m => m.Number == Card.CardNumber.Sieben) != null)
  23. {
  24. returnAction.ActionType = GameAction.GameActionType.PlayCard;
  25. returnAction.PlayedCard = playableCards.FirstOrDefault(m => m.Number == Card.CardNumber.Sieben);
  26. return returnAction;
  27. }
  28. if (state.PlayerHands[(_id + 1) % state.PlayerHands.Length].Count <= 2 && playableCards.FirstOrDefault(m => m.Number == Card.CardNumber.Acht) != null)
  29. {
  30. returnAction.ActionType = GameAction.GameActionType.PlayCard;
  31. returnAction.PlayedCard = playableCards.FirstOrDefault(m => m.Number == Card.CardNumber.Acht);
  32. return returnAction;
  33. }
  34. /*if (state.PlayerHands[(_id)].Count - state.PlayerHands[(_id)].Count(m => m.Number == Card.CardNumber.Bube) <=
  35. 1)
  36. {
  37. var cardNotBube = state.PlayerHands[(_id)].FirstOrDefault(m => m.Number != Card.CardNumber.Bube);
  38. // No more non Bube cards on hand
  39. if (cardNotBube == null)
  40. {
  41. returnAction.ActionType = GameAction.GameActionType.PlayCard;
  42. returnAction.PlayedCard = playableCards.FirstOrDefault(m => m.Number == Card.CardNumber.Bube);
  43. returnAction.WishedColor = Card.CardColor.Pik;
  44. return returnAction;
  45. }
  46. if (cardNotBube.CanPlayOnCard(state.TopCard, state.LastWishedColor))
  47. {
  48. returnAction.ActionType = GameAction.GameActionType.PlayCard;
  49. returnAction.PlayedCard = cardNotBube;
  50. return returnAction;
  51. }
  52. else
  53. {
  54. returnAction.ActionType = GameAction.GameActionType.PlayCard;
  55. returnAction.PlayedCard = playableCards.FirstOrDefault(m => m.Number == Card.CardNumber.Bube);
  56. returnAction.WishedColor = cardNotBube.Color;
  57. return returnAction;
  58. }
  59. }
  60. if (state.ParentState != null && state.ParentState.TopCard.CardRawValue == state.TopCard.CardRawValue)
  61. {
  62. var playableCardsWithLastColor = playableCards.Where(m => m.Color != state.TopCard.Color && m.Number != Card.CardNumber.Bube).ToList();
  63. if (playableCardsWithLastColor.Count > 0)
  64. {
  65. returnAction.ActionType = GameAction.GameActionType.PlayCard;
  66. returnAction.PlayedCard = playableCardsWithLastColor[Global.Random.Next(0, playableCardsWithLastColor.Count - 1)];
  67. return returnAction;
  68. }
  69. }*/
  70. /* if (state.PlayerHands[_id].Count <= 7 || state.CardsRemaining.Count <= 5)
  71. {
  72. var rootNode = PossiblePlayTree.GetTreeFromGameState(state);
  73. // High values => Player 0 winning
  74. // Low values => Player 1 winning
  75. PossiblePlayTree.GameStateNode currentBestNode = null;
  76. var currentBestValue = (state.PlayerIndexPlaying == 0 ? double.MinValue : double.MaxValue);
  77. foreach (var subNode in rootNode.SubNodes)
  78. {
  79. if (PossiblePlayTree.IsWinningOnlyNode(subNode, _id))
  80. {
  81. Console.WriteLine("Winning only play found!");
  82. var calculatedAction = subNode.CurrentGameState.GetGameActionFromParentState(state);
  83. return calculatedAction;
  84. }
  85. var oldBestValue = currentBestValue;
  86. var currentNodeValue = PossiblePlayTree.GetNodeRating(subNode);
  87. var currentValue = (state.PlayerIndexPlaying == 0
  88. ? Math.Max(oldBestValue, currentNodeValue)
  89. : Math.Min(oldBestValue, currentNodeValue));
  90. if (Math.Abs(currentValue - oldBestValue) > 0.000001)
  91. {
  92. currentBestValue = currentValue;
  93. currentBestNode = subNode;
  94. }
  95. }
  96. if (Math.Abs(currentBestValue) > 15f && ((_id == 0 && currentBestValue > 0) || (_id == 1 && currentBestValue < 0)))
  97. {
  98. Console.WriteLine("Use node with best node");
  99. return currentBestNode.CurrentGameState.GetGameActionFromParentState(state);
  100. }
  101. }
  102. */
  103. var playableCardsWithoutBube = playableCards.Where(m => m.Number != Card.CardNumber.Bube).ToList();
  104. if (playableCardsWithoutBube.Count != 0)
  105. {
  106. var mostColorPlayableCardsWithoutBube = playableCardsWithoutBube.OrderBy(m => m.Color).ToList();
  107. var playableCardsMostColor =
  108. mostColorPlayableCardsWithoutBube.Where(m => m.Color == mostColorPlayableCardsWithoutBube[0].Color)
  109. .ToList();
  110. returnAction.ActionType = GameAction.GameActionType.PlayCard;
  111. returnAction.PlayedCard =
  112. playableCardsMostColor[Global.Random.Next(0, playableCardsMostColor.Count - 1)];
  113. }
  114. else
  115. {
  116. var cardsNotBube = state.PlayerHands[_id].Count(m => m.Number != Card.CardNumber.Bube);
  117. if (cardsNotBube == 0)
  118. {
  119. // Play random bube
  120. returnAction.ActionType = GameAction.GameActionType.PlayCard;
  121. returnAction.PlayedCard = playableCards[0];
  122. returnAction.WishedColor = Card.CardColor.Kreuz;
  123. return returnAction;
  124. }
  125. var mostHandColorWithoutBube =
  126. state.PlayerHands[_id].Where(m => m.Number != Card.CardNumber.Bube)
  127. .OrderBy(m => m.Color)
  128. .ToList()[0];
  129. returnAction.ActionType = GameAction.GameActionType.PlayCard;
  130. returnAction.PlayedCard =
  131. playableCards[0];
  132. returnAction.WishedColor = mostHandColorWithoutBube.Color;
  133. }
  134. return returnAction;
  135. }
  136. }
  137. }