You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
5.0 KiB
160 lines
5.0 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Models.Game;
|
|
using Models.Interfaces;
|
|
|
|
namespace Models.Rules
|
|
{
|
|
public class Rules : IRules
|
|
{
|
|
public bool IsCellAdjacent(Cell choosenCell, Cell targetCell)
|
|
{
|
|
if (Math.Abs(choosenCell.X - targetCell.X) > 1 || Math.Abs(choosenCell.Y - targetCell.Y) > 1)
|
|
return false;
|
|
if (Math.Abs(choosenCell.X - targetCell.X) > 1 && Math.Abs(choosenCell.Y - targetCell.Y) > 1)
|
|
return false;
|
|
if (choosenCell.X == 0 && targetCell.X == 4)
|
|
return false;
|
|
if (choosenCell.Y == 0 && targetCell.Y == 4)
|
|
return false;
|
|
if (choosenCell.X == 4 && targetCell.X == 0)
|
|
return false;
|
|
if (choosenCell.Y == 4 && targetCell.Y == 0)
|
|
return false;
|
|
if (choosenCell.X == targetCell.X && choosenCell.Y == targetCell.Y)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool NearCellIsValid(Cell choosenCell, List<Cell> cells)
|
|
{
|
|
if (choosenCell == null || cells == null) return false;
|
|
|
|
IEnumerable<Cell> PlayedCellsQuery =
|
|
from cell in cells
|
|
where cell.Value != null
|
|
select cell;
|
|
|
|
foreach (var cell in PlayedCellsQuery)
|
|
{
|
|
if(!IsCellAdjacent(choosenCell, cell)) continue;
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public bool IsZoneValidAndAddToZones(Cell chosenCell, Map map)
|
|
{
|
|
if (chosenCell == null ||chosenCell.Value == null) return false;
|
|
|
|
List<Cell> adjacentCells = new List<Cell>();
|
|
|
|
adjacentCells = EveryAdjacentCells(chosenCell, map.Boards);
|
|
|
|
foreach(var cells in adjacentCells)
|
|
{
|
|
if (cells.Value == chosenCell.Value)
|
|
{
|
|
if(IsValueInZones(cells, map.Zones))
|
|
{
|
|
AddToZone(chosenCell, map.Zones);
|
|
}
|
|
else
|
|
{
|
|
NewZoneIsCreated(chosenCell, cells, map);
|
|
}
|
|
//return true; // Il y a une cellule adjacente avec la même valeur donc une zone est créée si elle n'est pas déjà existante
|
|
// Si il return true, tout c'est bien passer
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public bool IsValueInZones(Cell chosenCell, List<List<Cell>> zones)
|
|
{
|
|
if (chosenCell == null) return false;
|
|
|
|
for (int i = 0; i < zones.Count; i++)
|
|
{
|
|
if (zones[i][0].Value == chosenCell.Value)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool IsCellInZone(Cell chosenCell, List<List<Cell>> zones)
|
|
{
|
|
if (chosenCell == null) return false;
|
|
|
|
for (int i = 0; i < zones.Count; i++)
|
|
{
|
|
if (zones[i].Contains(chosenCell))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void AddToZone(Cell chosenCell, List<List<Cell>> zones)
|
|
{
|
|
if (chosenCell == null || chosenCell.Value == null) return;
|
|
|
|
if (IsCellInZone(chosenCell, zones)) return;
|
|
|
|
for (int i = 0; i < zones.Count; i++)
|
|
{
|
|
if (zones[i][0].Value == chosenCell.Value)
|
|
{
|
|
zones[i].Add(chosenCell);
|
|
return;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
public void NewZoneIsCreated(Cell firstCell, Cell secondCell, Map map)
|
|
{
|
|
if (firstCell == null || secondCell == null || firstCell.Value == null || secondCell.Value == null) return;
|
|
List<Cell> newZone = new List<Cell>();
|
|
newZone.Add(firstCell);
|
|
newZone.Add(secondCell);
|
|
map.Zones.Add(newZone);
|
|
}
|
|
|
|
public List<Cell> EveryAdjacentCells(Cell choosenCell, List<Cell> cells)
|
|
{
|
|
List<Cell> adjacentCells = new List<Cell>();
|
|
|
|
foreach (var cell in cells)
|
|
{
|
|
if (IsCellAdjacent(choosenCell, cell))
|
|
{
|
|
adjacentCells.Add(cell);
|
|
}
|
|
}
|
|
|
|
return adjacentCells;
|
|
}
|
|
|
|
public int? FinalCalculusOfZones(List<List<Cell>> zones)
|
|
{
|
|
int? calculus = 0;
|
|
for(int i = 0; i < zones.Count; i++)
|
|
{
|
|
calculus += zones[i].Count * zones[i][0].Value;
|
|
}
|
|
return calculus;
|
|
}
|
|
|
|
}
|
|
} |