Commit b14a08df authored by mercury233's avatar mercury233 Committed by GitHub

refactor: use Linq and other code style improvements (#75)

parent 3f572de6
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using YGOSharp.OCGWrapper.Enums; using YGOSharp.OCGWrapper.Enums;
namespace WindBot.Game.AI namespace WindBot.Game.AI
{ {
...@@ -46,13 +47,7 @@ namespace WindBot.Game.AI ...@@ -46,13 +47,7 @@ namespace WindBot.Game.AI
/// </summary> /// </summary>
public int GetTotalAttackingMonsterAttack(int player) public int GetTotalAttackingMonsterAttack(int player)
{ {
int atk = 0; return Duel.Fields[player].GetMonsters().Where(m => m.IsAttack()).Sum(m => (int?)m.Attack) ?? 0;
foreach (ClientCard m in Duel.Fields[player].GetMonsters())
{
if (m.IsAttack())
atk += m.Attack;
}
return atk;
} }
/// <summary> /// <summary>
/// Get the best ATK or DEF power of the field. /// Get the best ATK or DEF power of the field.
...@@ -61,17 +56,9 @@ namespace WindBot.Game.AI ...@@ -61,17 +56,9 @@ namespace WindBot.Game.AI
/// <param name="onlyATK">Only calculate attack.</param> /// <param name="onlyATK">Only calculate attack.</param>
public int GetBestPower(ClientField field, bool onlyATK = false) public int GetBestPower(ClientField field, bool onlyATK = false)
{ {
int bestPower = -1; return field.MonsterZone.GetMonsters()
for (int i = 0; i < 7; ++i) .Where(card => !onlyATK || card.IsAttack())
{ .Max(card => (int?)card.GetDefensePower()) ?? -1;
ClientCard card = field.MonsterZone[i];
if (card == null || card.Data == null) continue;
if (onlyATK && card.IsDefense()) continue;
int newPower = card.GetDefensePower();
if (newPower > bestPower)
bestPower = newPower;
}
return bestPower;
} }
public int GetBestAttack(ClientField field) public int GetBestAttack(ClientField field)
...@@ -81,36 +68,14 @@ namespace WindBot.Game.AI ...@@ -81,36 +68,14 @@ namespace WindBot.Game.AI
public bool IsOneEnemyBetterThanValue(int value, bool onlyATK) public bool IsOneEnemyBetterThanValue(int value, bool onlyATK)
{ {
int bestValue = -1; return Enemy.MonsterZone.GetMonsters()
bool nomonster = true; .Any(card => card.GetDefensePower() > value && (!onlyATK || card.IsAttack()));
for (int i = 0; i < 7; ++i)
{
ClientCard card = Enemy.MonsterZone[i];
if (card == null || card.Data == null) continue;
if (onlyATK && card.IsDefense()) continue;
nomonster = false;
int enemyValue = card.GetDefensePower();
if (enemyValue > bestValue)
bestValue = enemyValue;
}
if (nomonster) return false;
return bestValue > value;
} }
public bool IsAllEnemyBetterThanValue(int value, bool onlyATK) public bool IsAllEnemyBetterThanValue(int value, bool onlyATK)
{ {
bool nomonster = true; return Enemy.MonsterZone.GetMonsters()
for (int i = 0; i < 7; ++i) .All(card => card.GetDefensePower() > value && (!onlyATK || card.IsAttack()));
{
ClientCard card = Enemy.MonsterZone[i];
if (card == null || card.Data == null) continue;
if (onlyATK && card.IsDefense()) continue;
nomonster = false;
int enemyValue = card.GetDefensePower();
if (enemyValue <= value)
return false;
}
return !nomonster;
} }
/// <summary> /// <summary>
...@@ -146,59 +111,24 @@ namespace WindBot.Game.AI ...@@ -146,59 +111,24 @@ namespace WindBot.Game.AI
public ClientCard GetBestBotMonster(bool onlyATK = false) public ClientCard GetBestBotMonster(bool onlyATK = false)
{ {
int bestPower = -1; return Bot.MonsterZone.GetMonsters()
ClientCard bestMonster = null; .Where(card => !onlyATK || card.IsAttack())
for (int i = 0; i < 7; ++i) .OrderByDescending(card => card.GetDefensePower())
{ .FirstOrDefault();
ClientCard card = Bot.MonsterZone[i];
if (card == null || card.Data == null) continue;
if (onlyATK && card.IsDefense()) continue;
int newPower = card.GetDefensePower();
if (newPower > bestPower)
{
bestPower = newPower;
bestMonster = card;
}
}
return bestMonster;
} }
public ClientCard GetWorstBotMonster(bool onlyATK = false) public ClientCard GetWorstBotMonster(bool onlyATK = false)
{ {
int WorstPower = -1; return Bot.MonsterZone.GetMonsters()
ClientCard WorstMonster = null; .Where(card => !onlyATK || card.IsAttack())
for (int i = 0; i < 7; ++i) .OrderBy(card => card.GetDefensePower())
{ .FirstOrDefault();
ClientCard card = Bot.MonsterZone[i];
if (card == null || card.Data == null) continue;
if (onlyATK && card.IsDefense()) continue;
int newPower = card.GetDefensePower();
if (newPower < WorstPower)
{
WorstPower = newPower;
WorstMonster = card;
}
}
return WorstMonster;
} }
public ClientCard GetOneEnemyBetterThanValue(int value, bool onlyATK = false, bool canBeTarget = false) public ClientCard GetOneEnemyBetterThanValue(int value, bool onlyATK = false, bool canBeTarget = false)
{ {
ClientCard bestCard = null; return Enemy.MonsterZone.GetMonsters()
int bestValue = value; .FirstOrDefault(card => card.GetDefensePower() > value && (!onlyATK || card.IsAttack()) && (!canBeTarget || !card.IsShouldNotBeTarget()));
for (int i = 0; i < 7; ++i)
{
ClientCard card = Enemy.MonsterZone[i];
if (card == null || card.Data == null || (canBeTarget && card.IsShouldNotBeTarget())) continue;
if (onlyATK && card.IsDefense()) continue;
int enemyValue = card.GetDefensePower();
if (enemyValue >= bestValue)
{
bestCard = card;
bestValue = enemyValue;
}
}
return bestCard;
} }
public ClientCard GetOneEnemyBetterThanMyBest(bool onlyATK = false, bool canBeTarget = false) public ClientCard GetOneEnemyBetterThanMyBest(bool onlyATK = false, bool canBeTarget = false)
...@@ -289,21 +219,10 @@ namespace WindBot.Game.AI ...@@ -289,21 +219,10 @@ namespace WindBot.Game.AI
public ClientCard GetWorstEnemyMonster(bool onlyATK = false) public ClientCard GetWorstEnemyMonster(bool onlyATK = false)
{ {
int WorstPower = -1; return Enemy.MonsterZone.GetMonsters()
ClientCard WorstMonster = null; .Where(card => !onlyATK || card.IsAttack())
for (int i = 0; i < 7; ++i) .OrderBy(card => card.GetDefensePower())
{ .FirstOrDefault();
ClientCard card = Enemy.MonsterZone[i];
if (card == null || card.Data == null) continue;
if (onlyATK && card.IsDefense()) continue;
int newPower = card.GetDefensePower();
if (newPower < WorstPower)
{
WorstPower = newPower;
WorstMonster = card;
}
}
return WorstMonster;
} }
public ClientCard GetBestEnemySpell(bool onlyFaceup = false) public ClientCard GetBestEnemySpell(bool onlyFaceup = false)
...@@ -312,14 +231,11 @@ namespace WindBot.Game.AI ...@@ -312,14 +231,11 @@ namespace WindBot.Game.AI
if (card != null) if (card != null)
return card; return card;
List<ClientCard> spells = Enemy.GetSpells(); var spells = Enemy.GetSpells();
foreach (ClientCard ecard in spells) card = spells.FirstOrDefault(ecard => ecard.IsFaceup() && (ecard.HasType(CardType.Continuous) || ecard.HasType(CardType.Field)));
{ if (card != null)
if (ecard.IsFaceup() && ecard.HasType(CardType.Continuous)|| return card;
ecard.IsFaceup() && ecard.HasType(CardType.Field))
return ecard;
}
if (spells.Count > 0 && !onlyFaceup) if (spells.Count > 0 && !onlyFaceup)
return spells[0]; return spells[0];
...@@ -351,14 +267,7 @@ namespace WindBot.Game.AI ...@@ -351,14 +267,7 @@ namespace WindBot.Game.AI
public bool IsChainTarget(ClientCard card) public bool IsChainTarget(ClientCard card)
{ {
foreach (ClientCard target in Duel.ChainTargets) return Duel.ChainTargets.Any(card.Equals);
{
if (card.Equals(target))
{
return true;
}
}
return false;
} }
public bool IsChainTargetOnly(ClientCard card) public bool IsChainTargetOnly(ClientCard card)
...@@ -368,86 +277,65 @@ namespace WindBot.Game.AI ...@@ -368,86 +277,65 @@ namespace WindBot.Game.AI
public bool ChainContainsCard(int id) public bool ChainContainsCard(int id)
{ {
foreach (ClientCard card in Duel.CurrentChain) return Duel.CurrentChain.Any(card => card.Id == id);
{
if (card.Id == id)
return true;
}
return false;
} }
public int ChainCountPlayer(int player) public int ChainCountPlayer(int player)
{ {
int count = 0; return Duel.CurrentChain.Count(card => card.Controller == player);
foreach (ClientCard card in Duel.CurrentChain)
{
if (card.Controller == player)
count++;
}
return count;
} }
public bool ChainContainPlayer(int player) public bool ChainContainPlayer(int player)
{ {
foreach (ClientCard card in Duel.CurrentChain) return Duel.CurrentChain.Any(card => card.Controller == player);
{
if (card.Controller == player)
return true;
}
return false;
} }
public bool HasChainedTrap(int player) public bool HasChainedTrap(int player)
{ {
foreach (ClientCard card in Duel.CurrentChain) return Duel.CurrentChain.Any(card => card.Controller == player && card.HasType(CardType.Trap));
{
if (card.Controller == player && card.HasType(CardType.Trap))
return true;
}
return false;
} }
public ClientCard GetLastChainCard() public ClientCard GetLastChainCard()
{ {
if (Duel.CurrentChain.Count > 0) return Duel.CurrentChain.LastOrDefault();
return Duel.CurrentChain[Duel.CurrentChain.Count - 1];
return null;
} }
/// <summary> /// <summary>
/// Select cards listed in preferred. /// Select cards listed in preferred.
/// </summary> /// </summary>
public void SelectPreferredCards(IList<ClientCard> selected, ClientCard preferred, IList<ClientCard> cards, int min, int max) public IList<ClientCard> SelectPreferredCards(ClientCard preferred, IList<ClientCard> cards, int min, int max)
{ {
IList<ClientCard> selected = new List<ClientCard>();
if (cards.IndexOf(preferred) > 0 && selected.Count < max) if (cards.IndexOf(preferred) > 0 && selected.Count < max)
{ {
selected.Add(preferred); selected.Add(preferred);
} }
return selected;
} }
/// <summary> /// <summary>
/// Select cards listed in preferred. /// Select cards listed in preferred.
/// </summary> /// </summary>
public void SelectPreferredCards(IList<ClientCard> selected, int preferred, IList<ClientCard> cards, int min, int max) public IList<ClientCard> SelectPreferredCards(int preferred, IList<ClientCard> cards, int min, int max)
{ {
IList<ClientCard> selected = new List<ClientCard>();
foreach (ClientCard card in cards) foreach (ClientCard card in cards)
{ {
if (card.Id== preferred && selected.Count < max) if (card.Id== preferred && selected.Count < max)
selected.Add(card); selected.Add(card);
} }
return selected;
} }
/// <summary> /// <summary>
/// Select cards listed in preferred. /// Select cards listed in preferred.
/// </summary> /// </summary>
public void SelectPreferredCards(IList<ClientCard> selected, IList<ClientCard> preferred, IList<ClientCard> cards, int min, int max) public IList<ClientCard> SelectPreferredCards(IList<ClientCard> preferred, IList<ClientCard> cards, int min, int max)
{ {
IList<ClientCard> avail = new List<ClientCard>(); IList<ClientCard> selected = new List<ClientCard>();
foreach (ClientCard card in cards) IList<ClientCard> avail = cards.ToList(); // clone
{
// clone
avail.Add(card);
}
while (preferred.Count > 0 && avail.IndexOf(preferred[0]) > 0 && selected.Count < max) while (preferred.Count > 0 && avail.IndexOf(preferred[0]) > 0 && selected.Count < max)
{ {
ClientCard card = preferred[0]; ClientCard card = preferred[0];
...@@ -455,30 +343,36 @@ namespace WindBot.Game.AI ...@@ -455,30 +343,36 @@ namespace WindBot.Game.AI
avail.Remove(card); avail.Remove(card);
selected.Add(card); selected.Add(card);
} }
return selected;
} }
/// <summary> /// <summary>
/// Select cards listed in preferred. /// Select cards listed in preferred.
/// </summary> /// </summary>
public void SelectPreferredCards(IList<ClientCard> selected, IList<int> preferred, IList<ClientCard> cards, int min, int max) public IList<ClientCard> SelectPreferredCards(IList<int> preferred, IList<ClientCard> cards, int min, int max)
{ {
for (int i = 0; i < preferred.Count; i++) IList<ClientCard> selected = new List<ClientCard>();
foreach (int id in preferred)
{ {
foreach (ClientCard card in cards) foreach (ClientCard card in cards)
{ {
if (card.Id == preferred[i] && selected.Count < max && selected.IndexOf(card) <= 0) if (card.Id == id && selected.Count < max && selected.IndexOf(card) <= 0)
selected.Add(card); selected.Add(card);
} }
if (selected.Count >= max) if (selected.Count >= max)
break; break;
} }
return selected;
} }
/// <summary> /// <summary>
/// Check and fix selected to make sure it meet the count requirement. /// Check and fix selected to make sure it meet the count requirement.
/// </summary> /// </summary>
public void CheckSelectCount(IList<ClientCard> selected, IList<ClientCard> cards, int min, int max) public IList<ClientCard> CheckSelectCount(IList<ClientCard> _selected, IList<ClientCard> cards, int min, int max)
{ {
var selected = _selected.ToList();
if (selected.Count < min) if (selected.Count < min)
{ {
foreach (ClientCard card in cards) foreach (ClientCard card in cards)
...@@ -493,6 +387,8 @@ namespace WindBot.Game.AI ...@@ -493,6 +387,8 @@ namespace WindBot.Game.AI
{ {
selected.RemoveAt(selected.Count - 1); selected.RemoveAt(selected.Count - 1);
} }
return selected;
} }
} }
} }
\ No newline at end of file
using System.Collections.Generic; using System.Collections.Generic;
using YGOSharp.OCGWrapper.Enums; using YGOSharp.OCGWrapper.Enums;
using System;
using System.Linq; using System.Linq;
namespace WindBot.Game.AI namespace WindBot.Game.AI
...@@ -8,185 +9,90 @@ namespace WindBot.Game.AI ...@@ -8,185 +9,90 @@ namespace WindBot.Game.AI
{ {
public static ClientCard GetHighestAttackMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false) public static ClientCard GetHighestAttackMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false)
{ {
int highestAtk = 0; return cards
ClientCard selected = null; .Where(card => card?.Data != null && card.HasType(CardType.Monster) && card.IsFaceup() && !(canBeTarget && card.IsShouldNotBeTarget()))
foreach (ClientCard card in cards) .OrderBy(card => card.Attack).FirstOrDefault();
{
if (card == null || card.Data == null || card.IsFacedown() || (canBeTarget && card.IsShouldNotBeTarget())) continue;
if (card.HasType(CardType.Monster) && card.Attack > highestAtk)
{
highestAtk = card.Attack;
selected = card;
}
}
return selected;
} }
public static ClientCard GetHighestDefenseMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false) public static ClientCard GetHighestDefenseMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false)
{ {
int highestDef = 0; return cards
ClientCard selected = null; .Where(card => card?.Data != null && card.HasType(CardType.Monster) && card.IsFaceup() && !(canBeTarget && card.IsShouldNotBeTarget()))
foreach (ClientCard card in cards) .OrderBy(card => card.Defense).FirstOrDefault();
{
if (card == null || card.Data == null || card.IsFacedown() || (canBeTarget && card.IsShouldNotBeTarget())) continue;
if (card.HasType(CardType.Monster) && card.Defense > highestDef)
{
highestDef = card.Defense;
selected = card;
}
}
return selected;
} }
public static ClientCard GetLowestAttackMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false) public static ClientCard GetLowestAttackMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false)
{ {
int lowestAtk = 0; return cards
ClientCard selected = null; .Where(card => card?.Data != null && card.HasType(CardType.Monster) && card.IsFaceup() && !(canBeTarget && card.IsShouldNotBeTarget()))
foreach (ClientCard card in cards) .OrderByDescending(card => card.Attack).FirstOrDefault();
{
if (card == null || card.Data == null || card.IsFacedown() || (canBeTarget && card.IsShouldNotBeTarget())) continue;
if (lowestAtk == 0 && card.HasType(CardType.Monster) ||
card.HasType(CardType.Monster) && card.Attack < lowestAtk)
{
lowestAtk = card.Attack;
selected = card;
}
}
return selected;
} }
public static ClientCard GetLowestDefenseMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false) public static ClientCard GetLowestDefenseMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false)
{ {
int lowestDef = 0; return cards
ClientCard selected = null; .Where(card => card?.Data != null && card.HasType(CardType.Monster) && card.IsFaceup() && !(canBeTarget && card.IsShouldNotBeTarget()))
foreach (ClientCard card in cards) .OrderByDescending(card => card.Defense).FirstOrDefault();
{
if (card == null || card.Data == null || card.IsFacedown() || (canBeTarget && card.IsShouldNotBeTarget())) continue;
if (lowestDef == 0 && card.HasType(CardType.Monster) ||
card.HasType(CardType.Monster) && card.Defense < lowestDef)
{
lowestDef = card.Defense;
selected = card;
}
}
return selected;
} }
public static bool ContainsMonsterWithLevel(this IEnumerable<ClientCard> cards, int level) public static bool ContainsMonsterWithLevel(this IEnumerable<ClientCard> cards, int level)
{ {
foreach (ClientCard card in cards) return cards.Where(card => card?.Data != null).Any(card => !card.HasType(CardType.Xyz) && card.Level == level);
{
if (card == null)
continue;
if (!card.HasType(CardType.Xyz) && card.Level == level)
return true;
}
return false;
} }
public static bool ContainsMonsterWithRank(this IEnumerable<ClientCard> cards, int rank) public static bool ContainsMonsterWithRank(this IEnumerable<ClientCard> cards, int rank)
{ {
foreach (ClientCard card in cards) return cards.Where(card => card?.Data != null).Any(card => card.HasType(CardType.Xyz) && card.Rank == rank);
{
if (card == null)
continue;
if (card.HasType(CardType.Xyz) && card.Rank == rank)
return true;
}
return false;
} }
public static bool ContainsCardWithId(this IEnumerable<ClientCard> cards, int id) public static bool ContainsCardWithId(this IEnumerable<ClientCard> cards, int id)
{ {
foreach (ClientCard card in cards) return cards.Where(card => card?.Data != null).Any(card => card.Id == id);
{
if (card == null)
continue;
if (card.Id == id)
return true;
}
return false;
} }
public static int GetCardCount(this IEnumerable<ClientCard> cards, int id) public static int GetCardCount(this IEnumerable<ClientCard> cards, int id)
{ {
int count = 0; return cards.Where(card => card?.Data != null).Count(card => card.Id == id);
foreach (ClientCard card in cards)
{
if (card == null)
continue;
if (card.Id == id)
count++;
}
return count;
} }
public static List<ClientCard> GetMonsters(this IEnumerable<ClientCard> cards) public static List<ClientCard> GetMonsters(this IEnumerable<ClientCard> cards)
{ {
List<ClientCard> cardlist = new List<ClientCard>(); return cards.Where(card => card?.Data != null && card.HasType(CardType.Monster)).ToList();
foreach (ClientCard card in cards)
{
if (card == null)
continue;
if (card.HasType(CardType.Monster))
cardlist.Add(card);
}
return cardlist;
} }
public static List<ClientCard> GetFaceupPendulumMonsters(this IEnumerable<ClientCard> cards) public static List<ClientCard> GetFaceupPendulumMonsters(this IEnumerable<ClientCard> cards)
{ {
List<ClientCard> cardlist = new List<ClientCard>(); return cards.Where(card => card?.Data != null && card.HasType(CardType.Monster) && card.IsFaceup() && card.HasType(CardType.Pendulum)).ToList();
foreach (ClientCard card in cards)
{
if (card == null)
continue;
if (card.HasType(CardType.Monster) && card.IsFaceup() && card.HasType(CardType.Pendulum))
cardlist.Add(card);
}
return cardlist;
} }
public static ClientCard GetInvincibleMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false) public static ClientCard GetInvincibleMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false)
{ {
foreach (ClientCard card in cards) return cards.FirstOrDefault(card => card?.Data != null && card.IsMonsterInvincible() && card.IsFaceup() && (!canBeTarget || !card.IsShouldNotBeTarget()));
{
if (card != null && card.IsMonsterInvincible() && card.IsFaceup() && (!canBeTarget || !card.IsShouldNotBeTarget()))
return card;
}
return null;
} }
public static ClientCard GetDangerousMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false) public static ClientCard GetDangerousMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false)
{ {
foreach (ClientCard card in cards) return cards.FirstOrDefault(card => card?.Data != null && card.IsMonsterDangerous() && card.IsFaceup() && (!canBeTarget || !card.IsShouldNotBeTarget()));
{
if (card != null && card.IsMonsterDangerous() && card.IsFaceup() && (!canBeTarget || !card.IsShouldNotBeTarget()))
return card;
}
return null;
} }
public static ClientCard GetFloodgate(this IEnumerable<ClientCard> cards, bool canBeTarget = false) public static ClientCard GetFloodgate(this IEnumerable<ClientCard> cards, bool canBeTarget = false)
{ {
foreach (ClientCard card in cards) return cards.FirstOrDefault(card => card?.Data != null && card.IsFloodgate() && card.IsFaceup() && (!canBeTarget || !card.IsShouldNotBeTarget()));
{
if (card != null && card.IsFloodgate() && card.IsFaceup() && (!canBeTarget || !card.IsShouldNotBeTarget()))
return card;
}
return null;
} }
public static ClientCard GetShouldBeDisabledBeforeItUseEffectMonster(this IEnumerable<ClientCard> cards) public static ClientCard GetFirstMatchingCard(this IEnumerable<ClientCard> cards, Func<ClientCard, bool> filter)
{ {
foreach (ClientCard card in cards) return cards.FirstOrDefault(card => card?.Data != null && filter.Invoke(card));
}
public static ClientCard GetFirstMatchingFaceupCard(this IEnumerable<ClientCard> cards, Func<ClientCard, bool> filter)
{ {
if (card != null && card.IsMonsterShouldBeDisabledBeforeItUseEffect() && card.IsFaceup()) return cards.FirstOrDefault(card => card?.Data != null && card.IsFaceup() && filter.Invoke(card));
return card;
} }
return null;
public static ClientCard GetShouldBeDisabledBeforeItUseEffectMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = true)
{
return cards.FirstOrDefault(card => card?.Data != null && card.IsMonsterShouldBeDisabledBeforeItUseEffect() && card.IsFaceup() && (!canBeTarget || !card.IsShouldNotBeTarget()));
} }
public static IEnumerable<IEnumerable<T>> GetCombinations<T>(this IEnumerable<T> elements, int k) public static IEnumerable<IEnumerable<T>> GetCombinations<T>(this IEnumerable<T> elements, int k)
......
using YGOSharp.OCGWrapper.Enums; using YGOSharp.OCGWrapper.Enums;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using WindBot; using WindBot;
using WindBot.Game; using WindBot.Game;
using WindBot.Game.AI; using WindBot.Game.AI;
...@@ -140,25 +141,9 @@ namespace WindBot.Game.AI.Decks ...@@ -140,25 +141,9 @@ namespace WindBot.Game.AI.Decks
Logger.DebugWriteLine("OnSelectCard MelodyOfAwakeningDragon"); Logger.DebugWriteLine("OnSelectCard MelodyOfAwakeningDragon");
IList<ClientCard> result = new List<ClientCard>(); IList<ClientCard> result = new List<ClientCard>();
if (!Bot.HasInHand(CardId.WhiteDragon)) if (!Bot.HasInHand(CardId.WhiteDragon))
{ result.Add(cards.FirstOrDefault(card => card.Id == CardId.WhiteDragon));
foreach (ClientCard card in cards) result = result.Concat(cards.Where(card => card.Id == CardId.AlternativeWhiteDragon)).ToList();
{ return AI.Utils.CheckSelectCount(result, cards, min, max);
if (card.Id == CardId.WhiteDragon)
{
result.Add(card);
break;
}
}
}
foreach (ClientCard card in cards)
{
if (card.Id == CardId.AlternativeWhiteDragon && result.Count < max)
{
result.Add(card);
}
}
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
} }
Logger.DebugWriteLine("Use default."); Logger.DebugWriteLine("Use default.");
return null; return null;
...@@ -167,10 +152,8 @@ namespace WindBot.Game.AI.Decks ...@@ -167,10 +152,8 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max) public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{ {
Logger.DebugWriteLine("OnSelectXyzMaterial " + cards.Count + " " + min + " " + max); Logger.DebugWriteLine("OnSelectXyzMaterial " + cards.Count + " " + min + " " + max);
IList<ClientCard> result = new List<ClientCard>(); IList<ClientCard> result = AI.Utils.SelectPreferredCards(UsedAlternativeWhiteDragon, cards, min, max);
AI.Utils.SelectPreferredCards(result, UsedAlternativeWhiteDragon, cards, min, max); return AI.Utils.CheckSelectCount(result, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
} }
public override IList<ClientCard> OnSelectSynchroMaterial(IList<ClientCard> cards, int sum, int min, int max) public override IList<ClientCard> OnSelectSynchroMaterial(IList<ClientCard> cards, int sum, int min, int max)
......
...@@ -114,8 +114,8 @@ namespace WindBot.Game.AI.Decks ...@@ -114,8 +114,8 @@ namespace WindBot.Game.AI.Decks
if (result.Count >= max) if (result.Count >= max)
break; break;
} }
AI.Utils.CheckSelectCount(result, cards, min, max);
return result; return AI.Utils.CheckSelectCount(result, cards, min, max);
} }
private bool ReinforcementOfTheArmyEffect() private bool ReinforcementOfTheArmyEffect()
......
...@@ -172,8 +172,8 @@ namespace WindBot.Game.AI.Decks ...@@ -172,8 +172,8 @@ namespace WindBot.Game.AI.Decks
if (result.Count > 0) if (result.Count > 0)
break; break;
} }
AI.Utils.CheckSelectCount(result, cards, min, max);
return result; return AI.Utils.CheckSelectCount(result, cards, min, max);
} }
private bool UnexpectedDaiEffect() private bool UnexpectedDaiEffect()
......
...@@ -120,15 +120,13 @@ namespace WindBot.Game.AI.Decks ...@@ -120,15 +120,13 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max) public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{ {
IList<ClientCard> result = new List<ClientCard>(); IList<ClientCard> result = AI.Utils.SelectPreferredCards(new[] {
AI.Utils.SelectPreferredCards(result, new[] {
CardId.MistArchfiend, CardId.MistArchfiend,
CardId.PanzerDragon, CardId.PanzerDragon,
CardId.SolarWindJammer, CardId.SolarWindJammer,
CardId.StarDrawing CardId.StarDrawing
}, cards, min, max); }, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max); return AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
} }
private bool NormalSummon() private bool NormalSummon()
......
...@@ -181,10 +181,8 @@ namespace WindBot.Game.AI.Decks ...@@ -181,10 +181,8 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max) public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{ {
IList<ClientCard> result = new List<ClientCard>(); IList<ClientCard> result = AI.Utils.SelectPreferredCards(CardId.YosenjuTsujik, cards, min, max);
AI.Utils.SelectPreferredCards(result, CardId.YosenjuTsujik, cards, min, max); return AI.Utils.CheckSelectCount(result, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
} }
private bool PotOfDualityEffect() private bool PotOfDualityEffect()
......
...@@ -138,14 +138,12 @@ namespace WindBot.Game.AI.Decks ...@@ -138,14 +138,12 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max) public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{ {
IList<ClientCard> result = new List<ClientCard>(); IList<ClientCard> result = AI.Utils.SelectPreferredCards(new[] {
AI.Utils.SelectPreferredCards(result, new[] {
CardId.StarDrawing, CardId.StarDrawing,
CardId.SolarWindJammer, CardId.SolarWindJammer,
CardId.Goblindbergh CardId.Goblindbergh
}, cards, min, max); }, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max); return AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
} }
private bool Number39Utopia() private bool Number39Utopia()
......
...@@ -127,7 +127,7 @@ namespace WindBot.Game.AI ...@@ -127,7 +127,7 @@ namespace WindBot.Game.AI
public void SendOnDirectAttack(string attacker) public void SendOnDirectAttack(string attacker)
{ {
if (attacker == "" || attacker == null) if (string.IsNullOrEmpty(attacker))
{ {
attacker = _facedownmonstername; attacker = _facedownmonstername;
} }
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using YGOSharp.OCGWrapper.Enums; using YGOSharp.OCGWrapper.Enums;
using WindBot; using WindBot;
using WindBot.Game; using WindBot.Game;
...@@ -82,12 +83,12 @@ namespace WindBot.Game.AI ...@@ -82,12 +83,12 @@ namespace WindBot.Game.AI
public virtual void OnChaining(int player, ClientCard card) public virtual void OnChaining(int player, ClientCard card)
{ {
// For overriding
} }
public virtual void OnChainEnd() public virtual void OnChainEnd()
{ {
// For overriding
} }
public virtual void OnNewPhase() public virtual void OnNewPhase()
{ {
...@@ -239,12 +240,7 @@ namespace WindBot.Game.AI ...@@ -239,12 +240,7 @@ namespace WindBot.Game.AI
private bool DefaultNoExecutor() private bool DefaultNoExecutor()
{ {
foreach (CardExecutor exec in Executors) return Executors.All(exec => exec.Type != Type || exec.CardId != Card.Id);
{
if (exec.Type == Type && exec.CardId == Card.Id)
return false;
}
return true;
} }
} }
} }
\ No newline at end of file
...@@ -151,27 +151,27 @@ namespace WindBot.Game ...@@ -151,27 +151,27 @@ namespace WindBot.Game
public bool HasLinkMarker(int dir) public bool HasLinkMarker(int dir)
{ {
return ((LinkMarker & dir) != 0); return (LinkMarker & dir) != 0;
} }
public bool HasLinkMarker(LinkMarker dir) public bool HasLinkMarker(LinkMarker dir)
{ {
return ((LinkMarker & (int)dir) != 0); return (LinkMarker & (int)dir) != 0;
} }
public bool HasType(CardType type) public bool HasType(CardType type)
{ {
return ((Type & (int)type) != 0); return (Type & (int)type) != 0;
} }
public bool HasPosition(CardPosition position) public bool HasPosition(CardPosition position)
{ {
return ((Position & (int)position) != 0); return (Position & (int)position) != 0;
} }
public bool HasAttribute(CardAttribute attribute) public bool HasAttribute(CardAttribute attribute)
{ {
return ((Attribute & (int)attribute) != 0); return (Attribute & (int)attribute) != 0;
} }
public bool IsMonster() public bool IsMonster()
...@@ -221,7 +221,7 @@ namespace WindBot.Game ...@@ -221,7 +221,7 @@ namespace WindBot.Game
public bool IsDisabled() public bool IsDisabled()
{ {
return (Disabled != 0); return Disabled != 0;
} }
public bool HasXyzMaterial() public bool HasXyzMaterial()
......
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using WindBot.Game.AI;
using YGOSharp.OCGWrapper.Enums; using YGOSharp.OCGWrapper.Enums;
namespace WindBot.Game namespace WindBot.Game
...@@ -101,13 +103,11 @@ namespace WindBot.Game ...@@ -101,13 +103,11 @@ namespace WindBot.Game
return GetSpellCount() + GetMonsterCount(); return GetSpellCount() + GetMonsterCount();
} }
public int GetFieldHandCount() public int GetFieldHandCount()
{ {
return GetSpellCount() + GetMonsterCount() + GetHandCount(); return GetSpellCount() + GetMonsterCount() + GetHandCount();
} }
public bool IsFieldEmpty() public bool IsFieldEmpty()
{ {
return GetMonsters().Count == 0 && GetSpells().Count == 0; return GetMonsters().Count == 0 && GetSpells().Count == 0;
...@@ -118,7 +118,6 @@ namespace WindBot.Game ...@@ -118,7 +118,6 @@ namespace WindBot.Game
return GetCards(MonsterZone); return GetCards(MonsterZone);
} }
public List<ClientCard> GetGraveyardMonsters() public List<ClientCard> GetGraveyardMonsters()
{ {
return GetCards(Graveyard, CardType.Monster); return GetCards(Graveyard, CardType.Monster);
...@@ -141,23 +140,12 @@ namespace WindBot.Game ...@@ -141,23 +140,12 @@ namespace WindBot.Game
public List<ClientCard> GetMonstersInExtraZone() public List<ClientCard> GetMonstersInExtraZone()
{ {
List<ClientCard> cards = new List<ClientCard>(); return GetMonsters().Where((card, i) => i >= 5).ToList();
if (MonsterZone[5] != null)
cards.Add(MonsterZone[5]);
if (MonsterZone[6] != null)
cards.Add(MonsterZone[6]);
return cards;
} }
public List<ClientCard> GetMonstersInMainZone() public List<ClientCard> GetMonstersInMainZone()
{ {
List<ClientCard> cards = new List<ClientCard>(); return GetMonsters().Where((card, i) => i < 5).ToList();
for (int i = 0; i < 5; i++)
{
if (MonsterZone[i] != null)
cards.Add(MonsterZone[i]);
}
return cards;
} }
public bool HasInHand(int cardId) public bool HasInHand(int cardId)
...@@ -202,24 +190,12 @@ namespace WindBot.Game ...@@ -202,24 +190,12 @@ namespace WindBot.Game
public bool HasAttackingMonster() public bool HasAttackingMonster()
{ {
IList<ClientCard> monsters = GetMonsters(); return GetMonsters().Any(card => card.IsAttack());
foreach (ClientCard card in monsters)
{
if (card.IsAttack())
return true;
}
return false;
} }
public bool HasDefendingMonster() public bool HasDefendingMonster()
{ {
IList<ClientCard> monsters = GetMonsters(); return GetMonsters().Any(card => card.IsDefense());
foreach (ClientCard card in monsters)
{
if (card.IsDefense())
return true;
}
return false;
} }
public bool HasInMonstersZone(int cardId, bool notDisabled = false, bool hasXyzMaterial = false) public bool HasInMonstersZone(int cardId, bool notDisabled = false, bool hasXyzMaterial = false)
...@@ -315,94 +291,46 @@ namespace WindBot.Game ...@@ -315,94 +291,46 @@ namespace WindBot.Game
public int GetRemainingCount(int cardId, int initialCount) public int GetRemainingCount(int cardId, int initialCount)
{ {
int remaining = initialCount; int remaining = initialCount;
foreach (ClientCard card in Hand) remaining = remaining - Hand.Count(card => card != null && card.Id == cardId);
if (card != null && card.Id == cardId) remaining = remaining - SpellZone.Count(card => card != null && card.Id == cardId);
remaining--; remaining = remaining - Graveyard.Count(card => card != null && card.Id == cardId);
foreach (ClientCard card in SpellZone) remaining = remaining - Banished.Count(card => card != null && card.Id == cardId);
if (card != null && card.Id == cardId)
remaining--;
foreach (ClientCard card in Graveyard)
if (card != null && card.Id == cardId)
remaining--;
foreach (ClientCard card in Banished)
if (card != null && card.Id == cardId)
remaining--;
return (remaining < 0) ? 0 : remaining; return (remaining < 0) ? 0 : remaining;
} }
private static int GetCount(IEnumerable<ClientCard> cards) private static int GetCount(IEnumerable<ClientCard> cards)
{ {
int count = 0; return cards.Count(card => card != null);
foreach (ClientCard card in cards)
{
if (card != null)
count++;
}
return count;
} }
public int GetCountCardInZone(IEnumerable<ClientCard> cards, int cardId) public int GetCountCardInZone(IEnumerable<ClientCard> cards, int cardId)
{ {
int count = 0; return cards.Count(card => card != null && card.Id == cardId);
foreach (ClientCard card in cards)
{
if (card != null && card.Id == cardId)
count++;
}
return count;
} }
public int GetCountCardInZone(IEnumerable<ClientCard> cards, List<int> cardId) public int GetCountCardInZone(IEnumerable<ClientCard> cards, List<int> cardId)
{ {
int count = 0; return cards.Count(card => card != null && cardId.Contains(card.Id));
foreach (ClientCard card in cards)
{
if (card != null && cardId.Contains(card.Id))
count++;
}
return count;
} }
private static List<ClientCard> GetCards(IEnumerable<ClientCard> cards, CardType type) private static List<ClientCard> GetCards(IEnumerable<ClientCard> cards, CardType type)
{ {
List<ClientCard> nCards = new List<ClientCard>(); return cards.Where(card => card != null && card.HasType(type)).ToList();
foreach (ClientCard card in cards)
{
if (card != null && card.HasType(type))
nCards.Add(card);
}
return nCards;
} }
private static List<ClientCard> GetCards(IEnumerable<ClientCard> cards) private static List<ClientCard> GetCards(IEnumerable<ClientCard> cards)
{ {
List<ClientCard> nCards = new List<ClientCard>(); return cards.Where(card => card != null).ToList();
foreach (ClientCard card in cards)
{
if (card != null)
nCards.Add(card);
}
return nCards;
} }
private static bool HasInCards(IEnumerable<ClientCard> cards, int cardId, bool notDisabled = false, bool hasXyzMaterial = false) private static bool HasInCards(IEnumerable<ClientCard> cards, int cardId, bool notDisabled = false, bool hasXyzMaterial = false)
{ {
foreach (ClientCard card in cards) return cards.Any(card => card != null && card.Id == cardId && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()));
{
if (card != null && card.Id == cardId && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()))
return true;
}
return false;
} }
private static bool HasInCards(IEnumerable<ClientCard> cards, IList<int> cardId, bool notDisabled = false, bool hasXyzMaterial = false) private static bool HasInCards(IEnumerable<ClientCard> cards, IList<int> cardId, bool notDisabled = false, bool hasXyzMaterial = false)
{ {
foreach (ClientCard card in cards) return cards.Any(card => card != null && cardId.Contains(card.Id) && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()));
{
if (card != null && cardId.Contains(card.Id) && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()))
return true;
}
return false;
} }
} }
} }
\ No newline at end of file
...@@ -83,8 +83,7 @@ namespace WindBot.Game ...@@ -83,8 +83,7 @@ namespace WindBot.Game
} }
catch (Exception) catch (Exception)
{ {
if (reader != null) reader?.Close();
reader.Close();
return null; return null;
} }
} }
......
...@@ -360,10 +360,7 @@ namespace WindBot.Game ...@@ -360,10 +360,7 @@ namespace WindBot.Game
return result; return result;
result = new List<ClientCard>(); result = new List<ClientCard>();
// TODO: use selector // TODO: use selector
for (int i = 0; i < cards.Count; i++) result = cards.ToList();
{
result.Add(cards[i]);
}
return result; return result;
} }
...@@ -550,10 +547,15 @@ namespace WindBot.Game ...@@ -550,10 +547,15 @@ namespace WindBot.Game
} }
else else
{ {
if (hint == HINTMSG_SMATERIAL) switch (hint)
{
case HINTMSG_SMATERIAL:
selected = Executor.OnSelectSynchroMaterial(cards, sum, min, max); selected = Executor.OnSelectSynchroMaterial(cards, sum, min, max);
if (hint == HINTMSG_RELEASE) break;
case HINTMSG_RELEASE:
selected = Executor.OnSelectRitualTribute(cards, sum, min, max); selected = Executor.OnSelectRitualTribute(cards, sum, min, max);
break;
}
} }
if (selected != null) if (selected != null)
{ {
...@@ -1017,12 +1019,7 @@ namespace WindBot.Game ...@@ -1017,12 +1019,7 @@ namespace WindBot.Game
/// <returns>A list of the selected attributes.</returns> /// <returns>A list of the selected attributes.</returns>
public virtual IList<CardAttribute> OnAnnounceAttrib(int count, IList<CardAttribute> attributes) public virtual IList<CardAttribute> OnAnnounceAttrib(int count, IList<CardAttribute> attributes)
{ {
IList<CardAttribute> foundAttributes = new List<CardAttribute>(); IList<CardAttribute> foundAttributes = m_attributes.Where(attributes.Contains).ToList();
foreach (CardAttribute attribute in m_attributes)
{
if(attributes.Contains(attribute))
foundAttributes.Add(attribute);
}
if (foundAttributes.Count > 0) if (foundAttributes.Count > 0)
return foundAttributes; return foundAttributes;
...@@ -1037,12 +1034,7 @@ namespace WindBot.Game ...@@ -1037,12 +1034,7 @@ namespace WindBot.Game
/// <returns>A list of the selected races.</returns> /// <returns>A list of the selected races.</returns>
public virtual IList<CardRace> OnAnnounceRace(int count, IList<CardRace> races) public virtual IList<CardRace> OnAnnounceRace(int count, IList<CardRace> races)
{ {
IList<CardRace> foundRaces = new List<CardRace>(); IList<CardRace> foundRaces = m_races.Where(races.Contains).ToList();
foreach (CardRace race in m_races)
{
if (races.Contains(race))
foundRaces.Add(race);
}
if (foundRaces.Count > 0) if (foundRaces.Count > 0)
return foundRaces; return foundRaces;
...@@ -1080,12 +1072,10 @@ namespace WindBot.Game ...@@ -1080,12 +1072,10 @@ namespace WindBot.Game
private bool ShouldExecute(CardExecutor exec, ClientCard card, ExecutorType type, int desc = -1) private bool ShouldExecute(CardExecutor exec, ClientCard card, ExecutorType type, int desc = -1)
{ {
Executor.SetCard(type, card, desc); Executor.SetCard(type, card, desc);
if (card != null && return card != null &&
exec.Type == type && exec.Type == type &&
(exec.CardId == -1 || exec.CardId == card.Id) && (exec.CardId == -1 || exec.CardId == card.Id) &&
(exec.Func == null || exec.Func())) (exec.Func == null || exec.Func());
return true;
return false;
} }
} }
} }
...@@ -619,7 +619,7 @@ namespace WindBot.Game ...@@ -619,7 +619,7 @@ namespace WindBot.Game
_duel.Fields[attackcard.Controller].BattlingMonster = attackcard; _duel.Fields[attackcard.Controller].BattlingMonster = attackcard;
_duel.Fields[1 - attackcard.Controller].BattlingMonster = defendcard; _duel.Fields[1 - attackcard.Controller].BattlingMonster = defendcard;
if (ld == 0 && (attackcard != null) && (ca != 0)) if (ld == 0 && ca != 0)
{ {
_ai.OnDirectAttack(attackcard); _ai.OnDirectAttack(attackcard);
} }
...@@ -732,9 +732,8 @@ namespace WindBot.Game ...@@ -732,9 +732,8 @@ namespace WindBot.Game
packet.ReadInt32(); // ??? packet.ReadInt32(); // ???
ClientCard card = _duel.GetCard(player, (CardLocation)loc, seq); ClientCard card = _duel.GetCard(player, (CardLocation)loc, seq);
if (card == null) return;
card.Update(packet, _duel); card?.Update(packet, _duel);
} }
private void OnUpdateData(BinaryReader packet) private void OnUpdateData(BinaryReader packet)
...@@ -1448,11 +1447,7 @@ namespace WindBot.Game ...@@ -1448,11 +1447,7 @@ namespace WindBot.Game
ClientCard equipCard = _duel.GetCard(equipCardControler, (CardLocation)equipCardLocation, equipCardSequence); ClientCard equipCard = _duel.GetCard(equipCardControler, (CardLocation)equipCardLocation, equipCardSequence);
ClientCard targetCard = _duel.GetCard(targetCardControler, (CardLocation)targetCardLocation, targetCardSequence); ClientCard targetCard = _duel.GetCard(targetCardControler, (CardLocation)targetCardLocation, targetCardSequence);
if (equipCard == null || targetCard == null) return; if (equipCard == null || targetCard == null) return;
if (equipCard.EquipTarget != null) equipCard.EquipTarget?.EquipCards.Remove(equipCard);
{
equipCard.EquipTarget.EquipCards.Remove(equipCard);
}
equipCard.EquipTarget = targetCard; equipCard.EquipTarget = targetCard;
targetCard.EquipCards.Add(equipCard); targetCard.EquipCards.Add(equipCard);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment