Utilisation de LINQ. Amélioration de la sécurité (visibilité et static). Factorisation et réduction d'imbrication du code.
continuous-integration/drone/push Build is failing Details
continuous-integration/drone/pr Build is failing Details

pull/57/head
Rémi LAVERGNE 12 months ago
parent 96dee898ad
commit 619cdaf3cc
No known key found for this signature in database
GPG Key ID: CA264B55E97FD220

@ -8,108 +8,84 @@ namespace Models.Rules
{ {
public class Rules public class Rules
{ {
public bool NearCell(Cell playerChoice, List<Cell> cells) public static bool NearCell(Cell playerChoice, List<Cell> cells)
{ {
foreach (var item in cells) return cells.Any(item => playerChoice.Pos.X == item.Pos.X + 1 || playerChoice.Pos.X == item.Pos.X - 1 || playerChoice.Pos.Y == item.Pos.Y + 1 || playerChoice.Pos.Y == item.Pos.Y - 1);
{
if(playerChoice.Pos.X == item.Pos.X +1 || playerChoice.Pos.X == item.Pos.X - 1
|| playerChoice.Pos.Y == item.Pos.Y +1 || playerChoice.Pos.Y == item.Pos.Y -1 )
{
return true;
}
}
return false;
} }
public bool IsCellEmpty(Cell playerChoice) public static bool IsCellEmpty(Cell playerChoice)
{
if (playerChoice.Value == null)
{ {
return true; return playerChoice.Value == null;
}
return false;
} }
public bool IsCellValid(Cell playerChoice, List<Cell> cells) public static bool IsCellValid(Cell playerChoice, List<Cell> cells)
{ {
if (NearCell(playerChoice, cells) && IsCellEmpty(playerChoice)) return NearCell(playerChoice, cells) && IsCellEmpty(playerChoice);
{
return true;
}
return false;
} }
public bool IsZone(Cell playerChoice, List<Cell> cells) public static bool IsZone(Cell playerChoice, List<Cell> cells)
{ {
foreach (var item in cells) foreach (var item in cells)
{ {
if (playerChoice.Pos.X == item.Pos.X + 1 || playerChoice.Pos.X == item.Pos.X - 1 if (playerChoice.Pos.X != item.Pos.X + 1 && playerChoice.Pos.X != item.Pos.X - 1
|| playerChoice.Pos.Y == item.Pos.Y + 1 || playerChoice.Pos.Y == item.Pos.Y - 1) && playerChoice.Pos.Y != item.Pos.Y + 1 &&
{ playerChoice.Pos.Y != item.Pos.Y - 1) continue;
if (playerChoice.Value == item.Value) if (playerChoice.Value == item.Value)
{ {
return true; return true;
} }
} }
}
return false; return false;
} }
public bool IsCheminDeCorde(Cell playerChoice, List<Cell> cells, List<Cell> cheminsDeCorde) public static bool IsRopePath(Cell playerChoice, List<Cell> cells, List<Cell> ropePaths)
{ {
foreach (var item in cells) foreach (var item in cells)
{ {
if (playerChoice.Pos.X == item.Pos.X + 1 || playerChoice.Pos.X == item.Pos.X - 1 if (playerChoice.Pos.X != item.Pos.X + 1 && playerChoice.Pos.X != item.Pos.X - 1
|| playerChoice.Pos.Y == item.Pos.Y + 1 || playerChoice.Pos.Y == item.Pos.Y - 1) && playerChoice.Pos.Y != item.Pos.Y + 1 &&
{ playerChoice.Pos.Y != item.Pos.Y - 1) continue;
if (playerChoice.Value == item.Value+1 || playerChoice.Value == item.Value-1)
{ if (playerChoice.Value != item.Value + 1 && playerChoice.Value != item.Value - 1) continue;
foreach (var chemin in cheminsDeCorde)
foreach (var path in ropePaths)
{ {
if (chemin.Equals(playerChoice)) if (path.Equals(playerChoice))
{ {
return true; return true;
} }
} }
return IsCheminDeCorde(item, cells, cheminsDeCorde); return IsRopePath(item, cells, ropePaths);
}
}
} }
return false; return false;
} }
public int HowMany(Cell playerChoice, List<Cell> cells) public static int HowMany(Cell playerChoice, List<Cell> cells)
{ {
foreach(var pos in cells) foreach(var pos in cells)
{ {
if (pos.Equals(playerChoice)) if (pos.Equals(playerChoice))
{ {
if (pos.GetCellType()) return pos.GetCellType() ? 6 : 12;
{
return 6;
}
return 12;
} }
} }
return 0; return 0;
} }
public void SetValueAndPenalty(int valueChoice, Cell playerChoice, List<Cell> cells) public static void SetValueAndPenalty(int valueChoice, Cell playerChoice, List<Cell> cells)
{ {
int val = HowMany(playerChoice, cells); int val = HowMany(playerChoice, cells);
foreach (var pos in cells) foreach (var pos in cells)
{ {
if (pos.Equals(playerChoice)) if (!pos.Equals(playerChoice)) continue;
{
if (valueChoice > val) if (valueChoice > val)
{ {
playerChoice.Background = "notcontent"; playerChoice.Background = "penalty";
} }
playerChoice.Value = valueChoice; playerChoice.Value = valueChoice;
} }
} }
} }
} }
}

Loading…
Cancel
Save