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

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

parent 3f572de6
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Linq;
namespace BotWrapper
{
class BotWrapper
{
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, int uType);
const int MB_ICONERROR = 0x00000010;
static void Main(string[] args)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = Path.GetFullPath("WindBot");
startInfo.FileName = startInfo.WorkingDirectory + "\\WindBot.exe";
if (args.Length == 3)
{
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
string arg = args[0];
Match match = Regex.Match(arg, "Random=(.*)");
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Linq;
namespace BotWrapper
{
class BotWrapper
{
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, int uType);
const int MB_ICONERROR = 0x00000010;
static void Main(string[] args)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = Path.GetFullPath("WindBot");
startInfo.FileName = startInfo.WorkingDirectory + "\\WindBot.exe";
if (args.Length == 3)
{
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
string arg = args[0];
Match match = Regex.Match(arg, "Random=(.*)");
if (match.Success)
{
string randomFlag = match.Groups[1].Value;
......@@ -39,42 +39,42 @@ namespace BotWrapper
{
MessageBox((IntPtr)0, "Can't find random bot with this flag!\n\nA totally random bot will appear instead.", "WindBot", MB_ICONERROR);
}
}
arg = arg.Replace("'", "\"");
if (int.Parse(args[1]) == 1)
{
arg += " Hand=1";
}
arg += " Port=" + args[2];
startInfo.Arguments = arg;
}
try
{
Process.Start(startInfo);
}
catch
{
MessageBox((IntPtr)0, "WindBot can't be started!", "WindBot", MB_ICONERROR);
}
}
}
arg = arg.Replace("'", "\"");
if (int.Parse(args[1]) == 1)
{
arg += " Hand=1";
}
arg += " Port=" + args[2];
startInfo.Arguments = arg;
}
try
{
Process.Start(startInfo);
}
catch
{
MessageBox((IntPtr)0, "WindBot can't be started!", "WindBot", MB_ICONERROR);
}
}
public class BotInfo
{
public string name;
public string command;
public string desc;
public string[] flags;
}
static public IList<BotInfo> Bots = new List<BotInfo>();
}
static public IList<BotInfo> Bots = new List<BotInfo>();
static void ReadBots()
{
using (StreamReader reader = new StreamReader("bot.conf"))
{
while (!reader.EndOfStream)
{
using (StreamReader reader = new StreamReader("bot.conf"))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine().Trim();
if (line.Length > 0 && line[0] == '!')
{
......@@ -88,8 +88,8 @@ namespace BotWrapper
}
}
}
}
}
static string GetRandomBot(string flag)
{
IList<BotInfo> foundBots = Bots.Where(bot => bot.flags.Contains(flag)).ToList();
......@@ -100,6 +100,6 @@ namespace BotWrapper
return bot.command;
}
return "";
}
}
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using YGOSharp.OCGWrapper.Enums;
namespace WindBot.Game.AI
{
......@@ -46,13 +47,7 @@ namespace WindBot.Game.AI
/// </summary>
public int GetTotalAttackingMonsterAttack(int player)
{
int atk = 0;
foreach (ClientCard m in Duel.Fields[player].GetMonsters())
{
if (m.IsAttack())
atk += m.Attack;
}
return atk;
return Duel.Fields[player].GetMonsters().Where(m => m.IsAttack()).Sum(m => (int?)m.Attack) ?? 0;
}
/// <summary>
/// Get the best ATK or DEF power of the field.
......@@ -61,17 +56,9 @@ namespace WindBot.Game.AI
/// <param name="onlyATK">Only calculate attack.</param>
public int GetBestPower(ClientField field, bool onlyATK = false)
{
int bestPower = -1;
for (int i = 0; i < 7; ++i)
{
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;
return field.MonsterZone.GetMonsters()
.Where(card => !onlyATK || card.IsAttack())
.Max(card => (int?)card.GetDefensePower()) ?? -1;
}
public int GetBestAttack(ClientField field)
......@@ -81,36 +68,14 @@ namespace WindBot.Game.AI
public bool IsOneEnemyBetterThanValue(int value, bool onlyATK)
{
int bestValue = -1;
bool nomonster = true;
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;
return Enemy.MonsterZone.GetMonsters()
.Any(card => card.GetDefensePower() > value && (!onlyATK || card.IsAttack()));
}
public bool IsAllEnemyBetterThanValue(int value, bool onlyATK)
{
bool nomonster = true;
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 <= value)
return false;
}
return !nomonster;
return Enemy.MonsterZone.GetMonsters()
.All(card => card.GetDefensePower() > value && (!onlyATK || card.IsAttack()));
}
/// <summary>
......@@ -146,59 +111,24 @@ namespace WindBot.Game.AI
public ClientCard GetBestBotMonster(bool onlyATK = false)
{
int bestPower = -1;
ClientCard bestMonster = null;
for (int i = 0; i < 7; ++i)
{
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;
return Bot.MonsterZone.GetMonsters()
.Where(card => !onlyATK || card.IsAttack())
.OrderByDescending(card => card.GetDefensePower())
.FirstOrDefault();
}
public ClientCard GetWorstBotMonster(bool onlyATK = false)
{
int WorstPower = -1;
ClientCard WorstMonster = null;
for (int i = 0; i < 7; ++i)
{
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;
return Bot.MonsterZone.GetMonsters()
.Where(card => !onlyATK || card.IsAttack())
.OrderBy(card => card.GetDefensePower())
.FirstOrDefault();
}
public ClientCard GetOneEnemyBetterThanValue(int value, bool onlyATK = false, bool canBeTarget = false)
{
ClientCard bestCard = null;
int bestValue = value;
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;
return Enemy.MonsterZone.GetMonsters()
.FirstOrDefault(card => card.GetDefensePower() > value && (!onlyATK || card.IsAttack()) && (!canBeTarget || !card.IsShouldNotBeTarget()));
}
public ClientCard GetOneEnemyBetterThanMyBest(bool onlyATK = false, bool canBeTarget = false)
......@@ -289,21 +219,10 @@ namespace WindBot.Game.AI
public ClientCard GetWorstEnemyMonster(bool onlyATK = false)
{
int WorstPower = -1;
ClientCard WorstMonster = null;
for (int i = 0; i < 7; ++i)
{
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;
return Enemy.MonsterZone.GetMonsters()
.Where(card => !onlyATK || card.IsAttack())
.OrderBy(card => card.GetDefensePower())
.FirstOrDefault();
}
public ClientCard GetBestEnemySpell(bool onlyFaceup = false)
......@@ -312,14 +231,11 @@ namespace WindBot.Game.AI
if (card != null)
return card;
List<ClientCard> spells = Enemy.GetSpells();
var spells = Enemy.GetSpells();
foreach (ClientCard ecard in spells)
{
if (ecard.IsFaceup() && ecard.HasType(CardType.Continuous)||
ecard.IsFaceup() && ecard.HasType(CardType.Field))
return ecard;
}
card = spells.FirstOrDefault(ecard => ecard.IsFaceup() && (ecard.HasType(CardType.Continuous) || ecard.HasType(CardType.Field)));
if (card != null)
return card;
if (spells.Count > 0 && !onlyFaceup)
return spells[0];
......@@ -351,14 +267,7 @@ namespace WindBot.Game.AI
public bool IsChainTarget(ClientCard card)
{
foreach (ClientCard target in Duel.ChainTargets)
{
if (card.Equals(target))
{
return true;
}
}
return false;
return Duel.ChainTargets.Any(card.Equals);
}
public bool IsChainTargetOnly(ClientCard card)
......@@ -368,86 +277,65 @@ namespace WindBot.Game.AI
public bool ChainContainsCard(int id)
{
foreach (ClientCard card in Duel.CurrentChain)
{
if (card.Id == id)
return true;
}
return false;
return Duel.CurrentChain.Any(card => card.Id == id);
}
public int ChainCountPlayer(int player)
{
int count = 0;
foreach (ClientCard card in Duel.CurrentChain)
{
if (card.Controller == player)
count++;
}
return count;
return Duel.CurrentChain.Count(card => card.Controller == player);
}
public bool ChainContainPlayer(int player)
{
foreach (ClientCard card in Duel.CurrentChain)
{
if (card.Controller == player)
return true;
}
return false;
{
return Duel.CurrentChain.Any(card => card.Controller == player);
}
public bool HasChainedTrap(int player)
{
foreach (ClientCard card in Duel.CurrentChain)
{
if (card.Controller == player && card.HasType(CardType.Trap))
return true;
}
return false;
return Duel.CurrentChain.Any(card => card.Controller == player && card.HasType(CardType.Trap));
}
public ClientCard GetLastChainCard()
{
if (Duel.CurrentChain.Count > 0)
return Duel.CurrentChain[Duel.CurrentChain.Count - 1];
return null;
return Duel.CurrentChain.LastOrDefault();
}
/// <summary>
/// Select cards listed in preferred.
/// </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)
{
selected.Add(preferred);
}
return selected;
}
/// <summary>
/// Select cards listed in preferred.
/// </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)
{
if (card.Id== preferred && selected.Count < max)
selected.Add(card);
}
return selected;
}
/// <summary>
/// Select cards listed in preferred.
/// </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>();
foreach (ClientCard card in cards)
{
// clone
avail.Add(card);
}
IList<ClientCard> selected = new List<ClientCard>();
IList<ClientCard> avail = cards.ToList(); // clone
while (preferred.Count > 0 && avail.IndexOf(preferred[0]) > 0 && selected.Count < max)
{
ClientCard card = preferred[0];
......@@ -455,30 +343,36 @@ namespace WindBot.Game.AI
avail.Remove(card);
selected.Add(card);
}
return selected;
}
/// <summary>
/// Select cards listed in preferred.
/// </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)
{
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);
}
if (selected.Count >= max)
break;
}
return selected;
}
/// <summary>
/// Check and fix selected to make sure it meet the count requirement.
/// </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)
{
foreach (ClientCard card in cards)
......@@ -493,6 +387,8 @@ namespace WindBot.Game.AI
{
selected.RemoveAt(selected.Count - 1);
}
return selected;
}
}
}
\ No newline at end of file
using System.Collections.Generic;
using YGOSharp.OCGWrapper.Enums;
using System;
using System.Linq;
namespace WindBot.Game.AI
......@@ -8,185 +9,90 @@ namespace WindBot.Game.AI
{
public static ClientCard GetHighestAttackMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false)
{
int highestAtk = 0;
ClientCard selected = null;
foreach (ClientCard card in cards)
{
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;
return cards
.Where(card => card?.Data != null && card.HasType(CardType.Monster) && card.IsFaceup() && !(canBeTarget && card.IsShouldNotBeTarget()))
.OrderBy(card => card.Attack).FirstOrDefault();
}
public static ClientCard GetHighestDefenseMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false)
{
int highestDef = 0;
ClientCard selected = null;
foreach (ClientCard card in cards)
{
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;
return cards
.Where(card => card?.Data != null && card.HasType(CardType.Monster) && card.IsFaceup() && !(canBeTarget && card.IsShouldNotBeTarget()))
.OrderBy(card => card.Defense).FirstOrDefault();
}
public static ClientCard GetLowestAttackMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false)
{
int lowestAtk = 0;
ClientCard selected = null;
foreach (ClientCard card in cards)
{
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;
return cards
.Where(card => card?.Data != null && card.HasType(CardType.Monster) && card.IsFaceup() && !(canBeTarget && card.IsShouldNotBeTarget()))
.OrderByDescending(card => card.Attack).FirstOrDefault();
}
public static ClientCard GetLowestDefenseMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false)
{
int lowestDef = 0;
ClientCard selected = null;
foreach (ClientCard card in cards)
{
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;
return cards
.Where(card => card?.Data != null && card.HasType(CardType.Monster) && card.IsFaceup() && !(canBeTarget && card.IsShouldNotBeTarget()))
.OrderByDescending(card => card.Defense).FirstOrDefault();
}
public static bool ContainsMonsterWithLevel(this IEnumerable<ClientCard> cards, int level)
{
foreach (ClientCard card in cards)
{
if (card == null)
continue;
if (!card.HasType(CardType.Xyz) && card.Level == level)
return true;
}
return false;
return cards.Where(card => card?.Data != null).Any(card => !card.HasType(CardType.Xyz) && card.Level == level);
}
public static bool ContainsMonsterWithRank(this IEnumerable<ClientCard> cards, int rank)
{
foreach (ClientCard card in cards)
{
if (card == null)
continue;
if (card.HasType(CardType.Xyz) && card.Rank == rank)
return true;
}
return false;
return cards.Where(card => card?.Data != null).Any(card => card.HasType(CardType.Xyz) && card.Rank == rank);
}
public static bool ContainsCardWithId(this IEnumerable<ClientCard> cards, int id)
{
foreach (ClientCard card in cards)
{
if (card == null)
continue;
if (card.Id == id)
return true;
}
return false;
return cards.Where(card => card?.Data != null).Any(card => card.Id == id);
}
public static int GetCardCount(this IEnumerable<ClientCard> cards, int id)
{
int count = 0;
foreach (ClientCard card in cards)
{
if (card == null)
continue;
if (card.Id == id)
count++;
}
return count;
return cards.Where(card => card?.Data != null).Count(card => card.Id == id);
}
public static List<ClientCard> GetMonsters(this IEnumerable<ClientCard> cards)
{
List<ClientCard> cardlist = new List<ClientCard>();
foreach (ClientCard card in cards)
{
if (card == null)
continue;
if (card.HasType(CardType.Monster))
cardlist.Add(card);
}
return cardlist;
return cards.Where(card => card?.Data != null && card.HasType(CardType.Monster)).ToList();
}
public static List<ClientCard> GetFaceupPendulumMonsters(this IEnumerable<ClientCard> cards)
{
List<ClientCard> cardlist = new List<ClientCard>();
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;
return cards.Where(card => card?.Data != null && card.HasType(CardType.Monster) && card.IsFaceup() && card.HasType(CardType.Pendulum)).ToList();
}
public static ClientCard GetInvincibleMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false)
{
foreach (ClientCard card in cards)
{
if (card != null && card.IsMonsterInvincible() && card.IsFaceup() && (!canBeTarget || !card.IsShouldNotBeTarget()))
return card;
}
return null;
return cards.FirstOrDefault(card => card?.Data != null && card.IsMonsterInvincible() && card.IsFaceup() && (!canBeTarget || !card.IsShouldNotBeTarget()));
}
public static ClientCard GetDangerousMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = false)
{
foreach (ClientCard card in cards)
{
if (card != null && card.IsMonsterDangerous() && card.IsFaceup() && (!canBeTarget || !card.IsShouldNotBeTarget()))
return card;
}
return null;
return cards.FirstOrDefault(card => card?.Data != null && card.IsMonsterDangerous() && card.IsFaceup() && (!canBeTarget || !card.IsShouldNotBeTarget()));
}
public static ClientCard GetFloodgate(this IEnumerable<ClientCard> cards, bool canBeTarget = false)
{
foreach (ClientCard card in cards)
{
if (card != null && card.IsFloodgate() && card.IsFaceup() && (!canBeTarget || !card.IsShouldNotBeTarget()))
return card;
}
return null;
return cards.FirstOrDefault(card => card?.Data != null && card.IsFloodgate() && card.IsFaceup() && (!canBeTarget || !card.IsShouldNotBeTarget()));
}
public static ClientCard GetFirstMatchingCard(this IEnumerable<ClientCard> cards, Func<ClientCard, bool> filter)
{
return cards.FirstOrDefault(card => card?.Data != null && filter.Invoke(card));
}
public static ClientCard GetFirstMatchingFaceupCard(this IEnumerable<ClientCard> cards, Func<ClientCard, bool> filter)
{
return cards.FirstOrDefault(card => card?.Data != null && card.IsFaceup() && filter.Invoke(card));
}
public static ClientCard GetShouldBeDisabledBeforeItUseEffectMonster(this IEnumerable<ClientCard> cards)
public static ClientCard GetShouldBeDisabledBeforeItUseEffectMonster(this IEnumerable<ClientCard> cards, bool canBeTarget = true)
{
foreach (ClientCard card in cards)
{
if (card != null && card.IsMonsterShouldBeDisabledBeforeItUseEffect() && card.IsFaceup())
return card;
}
return null;
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)
......
using YGOSharp.OCGWrapper.Enums;
using System.Collections.Generic;
using System.Linq;
using WindBot;
using WindBot.Game;
using WindBot.Game.AI;
......@@ -140,25 +141,9 @@ namespace WindBot.Game.AI.Decks
Logger.DebugWriteLine("OnSelectCard MelodyOfAwakeningDragon");
IList<ClientCard> result = new List<ClientCard>();
if (!Bot.HasInHand(CardId.WhiteDragon))
{
foreach (ClientCard card in cards)
{
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;
result.Add(cards.FirstOrDefault(card => card.Id == CardId.WhiteDragon));
result = result.Concat(cards.Where(card => card.Id == CardId.AlternativeWhiteDragon)).ToList();
return AI.Utils.CheckSelectCount(result, cards, min, max);
}
Logger.DebugWriteLine("Use default.");
return null;
......@@ -167,10 +152,8 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{
Logger.DebugWriteLine("OnSelectXyzMaterial " + cards.Count + " " + min + " " + max);
IList<ClientCard> result = new List<ClientCard>();
AI.Utils.SelectPreferredCards(result, UsedAlternativeWhiteDragon, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
IList<ClientCard> result = AI.Utils.SelectPreferredCards(UsedAlternativeWhiteDragon, cards, min, max);
return AI.Utils.CheckSelectCount(result, cards, min, max);
}
public override IList<ClientCard> OnSelectSynchroMaterial(IList<ClientCard> cards, int sum, int min, int max)
......
......@@ -114,8 +114,8 @@ namespace WindBot.Game.AI.Decks
if (result.Count >= max)
break;
}
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
return AI.Utils.CheckSelectCount(result, cards, min, max);
}
private bool ReinforcementOfTheArmyEffect()
......
......@@ -353,13 +353,13 @@ namespace WindBot.Game.AI.Decks
}
return false;
}
private bool GamecieltheSeaTurtleKaijusp()
{
if (!Bot.HasInMonstersZone(CardId.UltimateConductorTytanno))
return DefaultKaijuSpsummon();
return false;
}
private bool GamecieltheSeaTurtleKaijusp()
{
if (!Bot.HasInMonstersZone(CardId.UltimateConductorTytanno))
return DefaultKaijuSpsummon();
return false;
}
private bool RadiantheMultidimensionalKaijusp()
......@@ -409,9 +409,9 @@ namespace WindBot.Game.AI.Decks
}
private bool MonsterRepos()
{
{
if (Card.Id == CardId.UltimateConductorTytanno && Card.IsFacedown()) return true;
if (Card.Id == CardId.ElShaddollConstruct && Card.IsFacedown()) return true;
if (Card.Id == CardId.ElShaddollConstruct && Card.IsFacedown()) return true;
if (Card.Id == CardId.ElShaddollConstruct && Card.IsAttack()) return false;
if (Card.Id == CardId.GlowUpBulb && Card.IsDefense()) return false;
if (Card.Id == CardId.ShaddollDragon && Card.IsFacedown() && Enemy.GetMonsterCount() >= 0) return true;
......@@ -526,8 +526,8 @@ namespace WindBot.Game.AI.Decks
private bool FairyTailSnowsummon()
{
ClientCard target = AI.Utils.GetBestEnemyMonster(true, true);
{
ClientCard target = AI.Utils.GetBestEnemyMonster(true, true);
if(target != null)
{
return true;
......@@ -549,13 +549,13 @@ namespace WindBot.Game.AI.Decks
int spell_count = 0;
IList<ClientCard> grave = Bot.Graveyard;
IList<ClientCard> all = new List<ClientCard>();
foreach (ClientCard check in grave)
{
if (check.Id == CardId.GiantRex)
{
all.Add(check);
}
IList<ClientCard> all = new List<ClientCard>();
foreach (ClientCard check in grave)
{
if (check.Id == CardId.GiantRex)
{
all.Add(check);
}
}
foreach (ClientCard check in grave)
{
......@@ -572,8 +572,8 @@ namespace WindBot.Game.AI.Decks
all.Add(check);
}
}
if (AI.Utils.ChainContainsCard(CardId.FairyTailSnow)) return false;
if (AI.Utils.ChainContainsCard(CardId.FairyTailSnow)) return false;
if ( Duel.Player == 1 && Duel.Phase == DuelPhase.BattleStart && Bot.BattlingMonster == null && Enemy_atk >=Bot.LifePoints ||
Duel.Player == 0 && Duel.Phase==DuelPhase.BattleStart && Enemy.BattlingMonster == null && Enemy.LifePoints<=1850
)
......@@ -590,9 +590,9 @@ namespace WindBot.Game.AI.Decks
private bool SouleatingOviraptoreff()
{
if (!OvertexCoatlseff_used && Bot.GetRemainingCount(CardId.OvertexCoatls, 3) > 0)
{
AI.SelectCard(CardId.OvertexCoatls);
AI.SelectOption(0);
{
AI.SelectCard(CardId.OvertexCoatls);
AI.SelectOption(0);
}
else
{
......@@ -992,7 +992,7 @@ namespace WindBot.Game.AI.Decks
private bool LostWindeff()
{
if (Card.Location == CardLocation.Grave)
if (Card.Location == CardLocation.Grave)
return true;
List<ClientCard> check = Enemy.GetMonsters();
foreach (ClientCard m in check)
......
......@@ -172,8 +172,8 @@ namespace WindBot.Game.AI.Decks
if (result.Count > 0)
break;
}
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
return AI.Utils.CheckSelectCount(result, cards, min, max);
}
private bool UnexpectedDaiEffect()
......
......@@ -120,15 +120,13 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{
IList<ClientCard> result = new List<ClientCard>();
AI.Utils.SelectPreferredCards(result, new[] {
IList<ClientCard> result = AI.Utils.SelectPreferredCards(new[] {
CardId.MistArchfiend,
CardId.PanzerDragon,
CardId.SolarWindJammer,
CardId.StarDrawing
}, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
return AI.Utils.CheckSelectCount(result, cards, min, max);
}
private bool NormalSummon()
......
......@@ -181,10 +181,8 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{
IList<ClientCard> result = new List<ClientCard>();
AI.Utils.SelectPreferredCards(result, CardId.YosenjuTsujik, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
IList<ClientCard> result = AI.Utils.SelectPreferredCards(CardId.YosenjuTsujik, cards, min, max);
return AI.Utils.CheckSelectCount(result, cards, min, max);
}
private bool PotOfDualityEffect()
......
......@@ -138,14 +138,12 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{
IList<ClientCard> result = new List<ClientCard>();
AI.Utils.SelectPreferredCards(result, new[] {
IList<ClientCard> result = AI.Utils.SelectPreferredCards(new[] {
CardId.StarDrawing,
CardId.SolarWindJammer,
CardId.Goblindbergh
}, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
return AI.Utils.CheckSelectCount(result, cards, min, max);
}
private bool Number39Utopia()
......
......@@ -127,7 +127,7 @@ namespace WindBot.Game.AI
public void SendOnDirectAttack(string attacker)
{
if (attacker == "" || attacker == null)
if (string.IsNullOrEmpty(attacker))
{
attacker = _facedownmonstername;
}
......
using System;
using System.Collections.Generic;
using System.Linq;
using YGOSharp.OCGWrapper.Enums;
using WindBot;
using WindBot.Game;
......@@ -82,12 +83,12 @@ namespace WindBot.Game.AI
public virtual void OnChaining(int player, ClientCard card)
{
// For overriding
}
public virtual void OnChainEnd()
{
// For overriding
}
public virtual void OnNewPhase()
{
......@@ -239,12 +240,7 @@ namespace WindBot.Game.AI
private bool DefaultNoExecutor()
{
foreach (CardExecutor exec in Executors)
{
if (exec.Type == Type && exec.CardId == Card.Id)
return false;
}
return true;
return Executors.All(exec => exec.Type != Type || exec.CardId != Card.Id);
}
}
}
\ No newline at end of file
......@@ -151,27 +151,27 @@ namespace WindBot.Game
public bool HasLinkMarker(int dir)
{
return ((LinkMarker & dir) != 0);
return (LinkMarker & dir) != 0;
}
public bool HasLinkMarker(LinkMarker dir)
{
return ((LinkMarker & (int)dir) != 0);
return (LinkMarker & (int)dir) != 0;
}
public bool HasType(CardType type)
{
return ((Type & (int)type) != 0);
return (Type & (int)type) != 0;
}
public bool HasPosition(CardPosition position)
{
return ((Position & (int)position) != 0);
return (Position & (int)position) != 0;
}
public bool HasAttribute(CardAttribute attribute)
{
return ((Attribute & (int)attribute) != 0);
return (Attribute & (int)attribute) != 0;
}
public bool IsMonster()
......@@ -221,7 +221,7 @@ namespace WindBot.Game
public bool IsDisabled()
{
return (Disabled != 0);
return Disabled != 0;
}
public bool HasXyzMaterial()
......
using System.Collections.Generic;
using System.Linq;
using WindBot.Game.AI;
using YGOSharp.OCGWrapper.Enums;
namespace WindBot.Game
......@@ -94,20 +96,18 @@ namespace WindBot.Game
}
return count;
}
public int GetFieldCount()
{
return GetSpellCount() + GetMonsterCount();
}
public int GetFieldHandCount()
{
return GetSpellCount() + GetMonsterCount() + GetHandCount();
}
public bool IsFieldEmpty()
{
return GetMonsters().Count == 0 && GetSpells().Count == 0;
......@@ -117,7 +117,6 @@ namespace WindBot.Game
{
return GetCards(MonsterZone);
}
public List<ClientCard> GetGraveyardMonsters()
{
......@@ -141,23 +140,12 @@ namespace WindBot.Game
public List<ClientCard> GetMonstersInExtraZone()
{
List<ClientCard> cards = new List<ClientCard>();
if (MonsterZone[5] != null)
cards.Add(MonsterZone[5]);
if (MonsterZone[6] != null)
cards.Add(MonsterZone[6]);
return cards;
return GetMonsters().Where((card, i) => i >= 5).ToList();
}
public List<ClientCard> GetMonstersInMainZone()
{
List<ClientCard> cards = new List<ClientCard>();
for (int i = 0; i < 5; i++)
{
if (MonsterZone[i] != null)
cards.Add(MonsterZone[i]);
}
return cards;
return GetMonsters().Where((card, i) => i < 5).ToList();
}
public bool HasInHand(int cardId)
......@@ -202,24 +190,12 @@ namespace WindBot.Game
public bool HasAttackingMonster()
{
IList<ClientCard> monsters = GetMonsters();
foreach (ClientCard card in monsters)
{
if (card.IsAttack())
return true;
}
return false;
return GetMonsters().Any(card => card.IsAttack());
}
public bool HasDefendingMonster()
{
IList<ClientCard> monsters = GetMonsters();
foreach (ClientCard card in monsters)
{
if (card.IsDefense())
return true;
}
return false;
return GetMonsters().Any(card => card.IsDefense());
}
public bool HasInMonstersZone(int cardId, bool notDisabled = false, bool hasXyzMaterial = false)
......@@ -315,94 +291,46 @@ namespace WindBot.Game
public int GetRemainingCount(int cardId, int initialCount)
{
int remaining = initialCount;
foreach (ClientCard card in Hand)
if (card != null && card.Id == cardId)
remaining--;
foreach (ClientCard card in SpellZone)
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--;
remaining = remaining - Hand.Count(card => card != null && card.Id == cardId);
remaining = remaining - SpellZone.Count(card => card != null && card.Id == cardId);
remaining = remaining - Graveyard.Count(card => card != null && card.Id == cardId);
remaining = remaining - Banished.Count(card => card != null && card.Id == cardId);
return (remaining < 0) ? 0 : remaining;
}
private static int GetCount(IEnumerable<ClientCard> cards)
{
int count = 0;
foreach (ClientCard card in cards)
{
if (card != null)
count++;
}
return count;
return cards.Count(card => card != null);
}
public int GetCountCardInZone(IEnumerable<ClientCard> cards, int cardId)
{
int count = 0;
foreach (ClientCard card in cards)
{
if (card != null && card.Id == cardId)
count++;
}
return count;
return cards.Count(card => card != null && card.Id == cardId);
}
public int GetCountCardInZone(IEnumerable<ClientCard> cards, List<int> cardId)
{
int count = 0;
foreach (ClientCard card in cards)
{
if (card != null && cardId.Contains(card.Id))
count++;
}
return count;
return cards.Count(card => card != null && cardId.Contains(card.Id));
}
private static List<ClientCard> GetCards(IEnumerable<ClientCard> cards, CardType type)
{
List<ClientCard> nCards = new List<ClientCard>();
foreach (ClientCard card in cards)
{
if (card != null && card.HasType(type))
nCards.Add(card);
}
return nCards;
return cards.Where(card => card != null && card.HasType(type)).ToList();
}
private static List<ClientCard> GetCards(IEnumerable<ClientCard> cards)
{
List<ClientCard> nCards = new List<ClientCard>();
foreach (ClientCard card in cards)
{
if (card != null)
nCards.Add(card);
}
return nCards;
return cards.Where(card => card != null).ToList();
}
private static bool HasInCards(IEnumerable<ClientCard> cards, int cardId, bool notDisabled = false, bool hasXyzMaterial = false)
{
foreach (ClientCard card in cards)
{
if (card != null && card.Id == cardId && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()))
return true;
}
return false;
return cards.Any(card => card != null && card.Id == cardId && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()));
}
private static bool HasInCards(IEnumerable<ClientCard> cards, IList<int> cardId, bool notDisabled = false, bool hasXyzMaterial = false)
{
foreach (ClientCard card in cards)
{
if (card != null && cardId.Contains(card.Id) && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()))
return true;
}
return false;
return cards.Any(card => card != null && cardId.Contains(card.Id) && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()));
}
}
}
\ No newline at end of file
......@@ -83,8 +83,7 @@ namespace WindBot.Game
}
catch (Exception)
{
if (reader != null)
reader.Close();
reader?.Close();
return null;
}
}
......
......@@ -360,10 +360,7 @@ namespace WindBot.Game
return result;
result = new List<ClientCard>();
// TODO: use selector
for (int i = 0; i < cards.Count; i++)
{
result.Add(cards[i]);
}
result = cards.ToList();
return result;
}
......@@ -550,10 +547,15 @@ namespace WindBot.Game
}
else
{
if (hint == HINTMSG_SMATERIAL)
selected = Executor.OnSelectSynchroMaterial(cards, sum, min, max);
if (hint == HINTMSG_RELEASE)
selected = Executor.OnSelectRitualTribute(cards, sum, min, max);
switch (hint)
{
case HINTMSG_SMATERIAL:
selected = Executor.OnSelectSynchroMaterial(cards, sum, min, max);
break;
case HINTMSG_RELEASE:
selected = Executor.OnSelectRitualTribute(cards, sum, min, max);
break;
}
}
if (selected != null)
{
......@@ -1017,12 +1019,7 @@ namespace WindBot.Game
/// <returns>A list of the selected attributes.</returns>
public virtual IList<CardAttribute> OnAnnounceAttrib(int count, IList<CardAttribute> attributes)
{
IList<CardAttribute> foundAttributes = new List<CardAttribute>();
foreach (CardAttribute attribute in m_attributes)
{
if(attributes.Contains(attribute))
foundAttributes.Add(attribute);
}
IList<CardAttribute> foundAttributes = m_attributes.Where(attributes.Contains).ToList();
if (foundAttributes.Count > 0)
return foundAttributes;
......@@ -1037,12 +1034,7 @@ namespace WindBot.Game
/// <returns>A list of the selected races.</returns>
public virtual IList<CardRace> OnAnnounceRace(int count, IList<CardRace> races)
{
IList<CardRace> foundRaces = new List<CardRace>();
foreach (CardRace race in m_races)
{
if (races.Contains(race))
foundRaces.Add(race);
}
IList<CardRace> foundRaces = m_races.Where(races.Contains).ToList();
if (foundRaces.Count > 0)
return foundRaces;
......@@ -1080,12 +1072,10 @@ namespace WindBot.Game
private bool ShouldExecute(CardExecutor exec, ClientCard card, ExecutorType type, int desc = -1)
{
Executor.SetCard(type, card, desc);
if (card != null &&
exec.Type == type &&
(exec.CardId == -1 || exec.CardId == card.Id) &&
(exec.Func == null || exec.Func()))
return true;
return false;
return card != null &&
exec.Type == type &&
(exec.CardId == -1 || exec.CardId == card.Id) &&
(exec.Func == null || exec.Func());
}
}
}
......@@ -619,7 +619,7 @@ namespace WindBot.Game
_duel.Fields[attackcard.Controller].BattlingMonster = attackcard;
_duel.Fields[1 - attackcard.Controller].BattlingMonster = defendcard;
if (ld == 0 && (attackcard != null) && (ca != 0))
if (ld == 0 && ca != 0)
{
_ai.OnDirectAttack(attackcard);
}
......@@ -732,9 +732,8 @@ namespace WindBot.Game
packet.ReadInt32(); // ???
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)
......@@ -1448,11 +1447,7 @@ namespace WindBot.Game
ClientCard equipCard = _duel.GetCard(equipCardControler, (CardLocation)equipCardLocation, equipCardSequence);
ClientCard targetCard = _duel.GetCard(targetCardControler, (CardLocation)targetCardLocation, targetCardSequence);
if (equipCard == null || targetCard == null) return;
if (equipCard.EquipTarget != null)
{
equipCard.EquipTarget.EquipCards.Remove(equipCard);
}
equipCard.EquipTarget?.EquipCards.Remove(equipCard);
equipCard.EquipTarget = targetCard;
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