[Fix] Code visual

This commit is contained in:
2026-02-28 19:25:26 +07:00
parent bee20fd1f8
commit e24b30743b
39 changed files with 2611 additions and 2687 deletions
+69 -70
View File
@@ -2,82 +2,81 @@ using System;
namespace YachtDice.Scoring
{
public static class CategoryScorer
{
public static int Calculate(int[] dice, YachtCategory category)
public static class CategoryScorer
{
if (dice == null || dice.Length != 5)
throw new ArgumentException("Exactly 5 dice values required.");
return category switch
public static int Calculate(int[] dice, YachtCategory category)
{
YachtCategory.Ones => SumOfValue(dice, 1),
YachtCategory.Twos => SumOfValue(dice, 2),
YachtCategory.Threes => SumOfValue(dice, 3),
YachtCategory.Fours => SumOfValue(dice, 4),
YachtCategory.Fives => SumOfValue(dice, 5),
YachtCategory.Sixes => SumOfValue(dice, 6),
YachtCategory.ThreeOfAKind => NOfAKind(dice, 3) ? Sum(dice) : 0,
YachtCategory.FourOfAKind => NOfAKind(dice, 4) ? Sum(dice) : 0,
YachtCategory.FullHouse => IsFullHouse(dice) ? 25 : 0,
YachtCategory.SmallStraight => HasStraightRun(dice, 4) ? 30 : 0,
YachtCategory.LargeStraight => HasStraightRun(dice, 5) ? 40 : 0,
YachtCategory.Yacht => NOfAKind(dice, 5) ? 50 : 0,
YachtCategory.Chance => Sum(dice),
_ => 0
};
}
if (dice == null || dice.Length != 5)
throw new ArgumentException("Exactly 5 dice values required.");
private static int SumOfValue(int[] dice, int target)
{
int sum = 0;
for (int i = 0; i < dice.Length; i++)
if (dice[i] == target) sum += target;
return sum;
}
private static int Sum(int[] dice)
{
int sum = 0;
for (int i = 0; i < dice.Length; i++) sum += dice[i];
return sum;
}
private static bool NOfAKind(int[] dice, int n)
{
int[] counts = new int[7];
for (int i = 0; i < dice.Length; i++) counts[dice[i]]++;
for (int v = 1; v <= 6; v++)
if (counts[v] >= n) return true;
return false;
}
private static bool IsFullHouse(int[] dice)
{
int[] counts = new int[7];
for (int i = 0; i < dice.Length; i++) counts[dice[i]]++;
bool hasTwo = false, hasThree = false;
for (int v = 1; v <= 6; v++)
{
if (counts[v] == 2) hasTwo = true;
if (counts[v] == 3) hasThree = true;
return category switch
{
YachtCategory.Ones => SumOfValue(dice, 1),
YachtCategory.Twos => SumOfValue(dice, 2),
YachtCategory.Threes => SumOfValue(dice, 3),
YachtCategory.Fours => SumOfValue(dice, 4),
YachtCategory.Fives => SumOfValue(dice, 5),
YachtCategory.Sixes => SumOfValue(dice, 6),
YachtCategory.ThreeOfAKind => NOfAKind(dice, 3) ? Sum(dice) : 0,
YachtCategory.FourOfAKind => NOfAKind(dice, 4) ? Sum(dice) : 0,
YachtCategory.FullHouse => IsFullHouse(dice) ? 25 : 0,
YachtCategory.SmallStraight => HasStraightRun(dice, 4) ? 30 : 0,
YachtCategory.LargeStraight => HasStraightRun(dice, 5) ? 40 : 0,
YachtCategory.Yacht => NOfAKind(dice, 5) ? 50 : 0,
YachtCategory.Chance => Sum(dice),
_ => 0
};
}
return hasTwo && hasThree;
}
private static bool HasStraightRun(int[] dice, int runLength)
{
bool[] present = new bool[7];
for (int i = 0; i < dice.Length; i++) present[dice[i]] = true;
int consecutive = 0;
for (int v = 1; v <= 6; v++)
private static int SumOfValue(int[] dice, int target)
{
consecutive = present[v] ? consecutive + 1 : 0;
if (consecutive >= runLength) return true;
int sum = 0;
for (int i = 0; i < dice.Length; i++)
if (dice[i] == target) sum += target;
return sum;
}
private static int Sum(int[] dice)
{
int sum = 0;
for (int i = 0; i < dice.Length; i++) sum += dice[i];
return sum;
}
private static bool NOfAKind(int[] dice, int n)
{
int[] counts = new int[7];
for (int i = 0; i < dice.Length; i++) counts[dice[i]]++;
for (int v = 1; v <= 6; v++)
if (counts[v] >= n) return true;
return false;
}
private static bool IsFullHouse(int[] dice)
{
int[] counts = new int[7];
for (int i = 0; i < dice.Length; i++) counts[dice[i]]++;
bool hasTwo = false, hasThree = false;
for (int v = 1; v <= 6; v++)
{
if (counts[v] == 2) hasTwo = true;
if (counts[v] == 3) hasThree = true;
}
return hasTwo && hasThree;
}
private static bool HasStraightRun(int[] dice, int runLength)
{
bool[] present = new bool[7];
for (int i = 0; i < dice.Length; i++) present[dice[i]] = true;
int consecutive = 0;
for (int v = 1; v <= 6; v++)
{
consecutive = present[v] ? consecutive + 1 : 0;
if (consecutive >= runLength) return true;
}
return false;
}
return false;
}
}
}
+20 -21
View File
@@ -3,28 +3,27 @@ using UnityEngine;
namespace YachtDice.Scoring
{
[Serializable]
public struct ScoreResult
{
public int BaseScore;
public int FlatBonus;
public float Multiplier;
public int[] DiceValues;
public YachtCategory Category;
public int FinalScore => Mathf.FloorToInt((BaseScore + FlatBonus) * Multiplier);
public static ScoreResult Create(int baseScore, int[] diceValues, YachtCategory category)
[Serializable]
public struct ScoreResult
{
return new ScoreResult
public int BaseScore;
public int FlatBonus;
public float Multiplier;
public int[] DiceValues;
public YachtCategory Category;
public int FinalScore => Mathf.FloorToInt((BaseScore + FlatBonus) * Multiplier);
public static ScoreResult Create(int baseScore, int[] diceValues, YachtCategory category)
{
BaseScore = baseScore,
FlatBonus = 0,
Multiplier = 1f,
DiceValues = diceValues,
Category = category
};
return new ScoreResult
{
BaseScore = baseScore,
FlatBonus = 0,
Multiplier = 1f,
DiceValues = diceValues,
Category = category
};
}
}
}
}
+75 -76
View File
@@ -5,85 +5,84 @@ using YachtDice.Modifiers;
namespace YachtDice.Scoring
{
public sealed class ScoringSystem : MonoBehaviour
{
public event Action<YachtCategory, int> OnCategoryScored;
public event Action<int> OnAllCategoriesScored;
public event Action<YachtCategory, ScoreResult> OnCategoryConfirmed;
private readonly Dictionary<YachtCategory, int> scorecard = new();
private readonly HashSet<YachtCategory> usedCategories = new();
private List<ModifierData> activeModifierData = new();
public bool IsCategoryUsed(YachtCategory category) => usedCategories.Contains(category);
public int GetCategoryScore(YachtCategory category)
public class ScoringSystem : MonoBehaviour
{
return scorecard.TryGetValue(category, out int score) ? score : -1;
}
public event Action<YachtCategory, int> OnCategoryScored;
public event Action<int> OnAllCategoriesScored;
public event Action<YachtCategory, ScoreResult> OnCategoryConfirmed;
public int TotalScore
{
get
private readonly Dictionary<YachtCategory, int> scorecard = new();
private readonly HashSet<YachtCategory> usedCategories = new();
private List<ModifierData> activeModifierData = new();
public bool IsCategoryUsed(YachtCategory category) => usedCategories.Contains(category);
public int GetCategoryScore(YachtCategory category)
{
int total = 0;
foreach (var kvp in scorecard) total += kvp.Value;
return total;
return scorecard.TryGetValue(category, out int score) ? score : -1;
}
public int TotalScore
{
get
{
int total = 0;
foreach (var kvp in scorecard) total += kvp.Value;
return total;
}
}
public int CategoriesFilledCount => usedCategories.Count;
public int TotalCategoryCount => Enum.GetValues(typeof(YachtCategory)).Length;
public bool IsComplete => CategoriesFilledCount >= TotalCategoryCount;
public void SetActiveModifiers(List<ModifierData> modifiers)
{
activeModifierData = modifiers ?? new List<ModifierData>();
}
public IReadOnlyList<ModifierData> ActiveModifiers => activeModifierData;
public ScoreResult PreviewScore(int[] diceValues, YachtCategory category)
{
int baseScore = CategoryScorer.Calculate(diceValues, category);
ScoreResult result = ScoreResult.Create(baseScore, diceValues, category);
ModifierPipeline.Apply(activeModifierData, ref result, ModifierScope.SelectedCategory);
return result;
}
public ScoreResult ScoreCategory(int[] diceValues, YachtCategory category)
{
if (usedCategories.Contains(category))
throw new InvalidOperationException($"Category {category} has already been scored.");
int baseScore = CategoryScorer.Calculate(diceValues, category);
ScoreResult result = ScoreResult.Create(baseScore, diceValues, category);
ModifierPipeline.Apply(activeModifierData, ref result, ModifierScope.SelectedCategory);
ModifierPipeline.Apply(activeModifierData, ref result, ModifierScope.AnyCategoryClosed);
int finalScore = result.FinalScore;
scorecard[category] = finalScore;
usedCategories.Add(category);
OnCategoryScored?.Invoke(category, finalScore);
OnCategoryConfirmed?.Invoke(category, result);
if (IsComplete)
OnAllCategoriesScored?.Invoke(TotalScore);
return result;
}
public void ResetScorecard()
{
scorecard.Clear();
usedCategories.Clear();
}
}
public int CategoriesFilledCount => usedCategories.Count;
public int TotalCategoryCount => Enum.GetValues(typeof(YachtCategory)).Length;
public bool IsComplete => CategoriesFilledCount >= TotalCategoryCount;
public void SetActiveModifiers(List<ModifierData> modifiers)
{
activeModifierData = modifiers ?? new List<ModifierData>();
}
public IReadOnlyList<ModifierData> ActiveModifiers => activeModifierData;
public ScoreResult PreviewScore(int[] diceValues, YachtCategory category)
{
int baseScore = CategoryScorer.Calculate(diceValues, category);
ScoreResult result = ScoreResult.Create(baseScore, diceValues, category);
ModifierPipeline.Apply(activeModifierData, ref result, ModifierScope.SelectedCategory);
return result;
}
public ScoreResult ScoreCategory(int[] diceValues, YachtCategory category)
{
if (usedCategories.Contains(category))
throw new InvalidOperationException($"Category {category} has already been scored.");
int baseScore = CategoryScorer.Calculate(diceValues, category);
ScoreResult result = ScoreResult.Create(baseScore, diceValues, category);
ModifierPipeline.Apply(activeModifierData, ref result, ModifierScope.SelectedCategory);
ModifierPipeline.Apply(activeModifierData, ref result, ModifierScope.AnyCategoryClosed);
int finalScore = result.FinalScore;
scorecard[category] = finalScore;
usedCategories.Add(category);
OnCategoryScored?.Invoke(category, finalScore);
OnCategoryConfirmed?.Invoke(category, result);
if (IsComplete)
OnAllCategoriesScored?.Invoke(TotalScore);
return result;
}
public void ResetScorecard()
{
scorecard.Clear();
usedCategories.Clear();
}
}
}
+18 -18
View File
@@ -1,22 +1,22 @@
namespace YachtDice.Scoring
{
public enum YachtCategory
{
// Upper Section
Ones,
Twos,
Threes,
Fours,
Fives,
Sixes,
public enum YachtCategory
{
// Upper Section
Ones,
Twos,
Threes,
Fours,
Fives,
Sixes,
// Lower Section
ThreeOfAKind,
FourOfAKind,
FullHouse,
SmallStraight,
LargeStraight,
Yacht,
Chance
}
// Lower Section
ThreeOfAKind,
FourOfAKind,
FullHouse,
SmallStraight,
LargeStraight,
Yacht,
Chance
}
}