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.
Trek-12/source/Trek-12/Models/Rules/Rules.cs

112 lines
3.8 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models.Game;
namespace Models.Rules
{
public class Rules
{
public bool IsCellExist(Position playerChoicePosition, SortedDictionary<Position, Cell> cells)
{
return cells.ContainsKey(playerChoicePosition);
}
public bool NearCellIsValid(Position playerChoicePosition, SortedDictionary<Position, Cell> cells)
{
if (!IsCellExist(playerChoicePosition, cells)) return false;
foreach (var cell in cells)
{
if (Math.Abs(playerChoicePosition.X - cell.Key.X) > 1 || Math.Abs(playerChoicePosition.Y - cell.Key.Y) > 1) continue;
if (!IsCellEmpty(cell.Value))
{
return true;
}
}
return false;
}
public bool IsCellEmpty(Cell playerChoice)
{
return !playerChoice.Value.HasValue;
}
public bool IsCellValid(Position playerChoicePosition, SortedDictionary<Position, Cell> cells)
{
return NearCellIsValid(playerChoicePosition, cells) && IsCellEmpty(cells.
}
public bool IsZone(Cell playerChoice, SortedDictionary<Position, Cell> cells)
{
foreach (var k,v in cells)
{
if (playerChoice.X != item.X + 1 && playerChoice.X != item.Pos.X - 1
&& playerChoice.Pos.Y != item.Pos.Y + 1 &&
playerChoice.Pos.Y != item.Pos.Y - 1) continue;
if (playerChoice.Value == item.Value)
{
return true;
}
}
return false;
}
/// <summary>
/// Determines if a given cell is part of any rope path.
/// </summary>
/// <param name="playerChoice">The cell to check.</param>
/// <param name="ropePaths">A collection of rope paths to check against.</param>
/// <returns>True if the cell is part of any rope path, false otherwise.</returns>
public bool IsRopePath(Cell playerChoice, HashSet<RopePath> ropePaths)
{
foreach (var path in ropePaths)
{
if (path.Cells.Contains(playerChoice))
{
return true;
}
}
return false;
}
public bool IsAdjacent(Cell cell1, Cell cell2, SortedDictionary<Position, Cell> cells)
{
bool isSequence = cell1.Value.HasValue && cell2.Value.HasValue && Math.Abs(cell1.Value.Value - cell2.Value.Value) == 1;
return IsCellValid(cell1, cells) && isSequence;
}
public int HowMany(Cell playerChoice, SortedDictionary<Position, Cell> cells)
{
foreach(var pos in cells)
{
if (pos.Equals(playerChoice))
{
return pos.GetCellType() ? 6 : 12;
}
}
return 0;
}
public void SetValueAndPenalty(int valueChoice, Cell playerChoice, SortedDictionary<Position, Cell> cells)
{
int val = HowMany(playerChoice, cells);
foreach (var pos in cells)
{
if (!pos.Equals(playerChoice)) continue;
if (valueChoice > val)
{
playerChoice.Background = "penalty";
}
playerChoice.Value = valueChoice;
}
}
}
}