[Add] Yacht scoring system with Balatro-like modifier pipeline
Implements the core game loop for Yacht dice: 5-dice rolling with lock/unlock, 3 rolls per turn, 13 standard scoring categories, and an extensible ScriptableObject-based modifier system that applies additive then multiplicative bonuses (chips+mult pattern). Includes two test modifiers: BonusForOnes (+10 per 1) and MultiplierForSixes (x6 per 6). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
|
||||
public static class CategoryScorer
|
||||
{
|
||||
public static int Calculate(int[] dice, YachtCategory category)
|
||||
{
|
||||
if (dice == null || dice.Length != 5)
|
||||
throw new ArgumentException("Exactly 5 dice values required.");
|
||||
|
||||
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
|
||||
};
|
||||
}
|
||||
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
[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)
|
||||
{
|
||||
return new ScoreResult
|
||||
{
|
||||
BaseScore = baseScore,
|
||||
FlatBonus = 0,
|
||||
Multiplier = 1f,
|
||||
DiceValues = diceValues,
|
||||
Category = category
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public sealed class ScoringSystem : MonoBehaviour
|
||||
{
|
||||
[Header("Modifiers")]
|
||||
[SerializeField] private List<ScoreModifier> activeModifiers = new();
|
||||
|
||||
public event Action<YachtCategory, int> OnCategoryScored;
|
||||
public event Action<int> OnAllCategoriesScored;
|
||||
|
||||
private readonly Dictionary<YachtCategory, int> scorecard = new();
|
||||
private readonly HashSet<YachtCategory> usedCategories = new();
|
||||
|
||||
public bool IsCategoryUsed(YachtCategory category) => usedCategories.Contains(category);
|
||||
|
||||
public int GetCategoryScore(YachtCategory category)
|
||||
{
|
||||
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 AddModifier(ScoreModifier modifier)
|
||||
{
|
||||
if (modifier != null && !activeModifiers.Contains(modifier))
|
||||
activeModifiers.Add(modifier);
|
||||
}
|
||||
|
||||
public void RemoveModifier(ScoreModifier modifier)
|
||||
{
|
||||
activeModifiers.Remove(modifier);
|
||||
}
|
||||
|
||||
public IReadOnlyList<ScoreModifier> ActiveModifiers => activeModifiers;
|
||||
|
||||
public ScoreResult PreviewScore(int[] diceValues, YachtCategory category)
|
||||
{
|
||||
int baseScore = CategoryScorer.Calculate(diceValues, category);
|
||||
ScoreResult result = ScoreResult.Create(baseScore, diceValues, category);
|
||||
ApplyModifiers(ref result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public ScoreResult ScoreCategory(int[] diceValues, YachtCategory category)
|
||||
{
|
||||
if (usedCategories.Contains(category))
|
||||
throw new InvalidOperationException($"Category {category} has already been scored.");
|
||||
|
||||
ScoreResult result = PreviewScore(diceValues, category);
|
||||
|
||||
int finalScore = result.FinalScore;
|
||||
scorecard[category] = finalScore;
|
||||
usedCategories.Add(category);
|
||||
|
||||
OnCategoryScored?.Invoke(category, finalScore);
|
||||
|
||||
if (IsComplete)
|
||||
OnAllCategoriesScored?.Invoke(TotalScore);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void ApplyModifiers(ref ScoreResult result)
|
||||
{
|
||||
// Pass 1: Additive
|
||||
for (int i = 0; i < activeModifiers.Count; i++)
|
||||
{
|
||||
if (activeModifiers[i] == null) continue;
|
||||
if (activeModifiers[i].Phase == ScoreModifier.ModifierPhase.Additive)
|
||||
activeModifiers[i].Apply(ref result);
|
||||
}
|
||||
|
||||
// Pass 2: Multiplicative
|
||||
for (int i = 0; i < activeModifiers.Count; i++)
|
||||
{
|
||||
if (activeModifiers[i] == null) continue;
|
||||
if (activeModifiers[i].Phase == ScoreModifier.ModifierPhase.Multiplicative)
|
||||
activeModifiers[i].Apply(ref result);
|
||||
}
|
||||
}
|
||||
|
||||
public void ResetScorecard()
|
||||
{
|
||||
scorecard.Clear();
|
||||
usedCategories.Clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
public enum YachtCategory
|
||||
{
|
||||
// Upper Section
|
||||
Ones,
|
||||
Twos,
|
||||
Threes,
|
||||
Fours,
|
||||
Fives,
|
||||
Sixes,
|
||||
|
||||
// Lower Section
|
||||
ThreeOfAKind,
|
||||
FourOfAKind,
|
||||
FullHouse,
|
||||
SmallStraight,
|
||||
LargeStraight,
|
||||
Yacht,
|
||||
Chance
|
||||
}
|
||||
Reference in New Issue
Block a user