You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何根据用户输入字符串中的字符为totalScore整数累加分数?

Scrabble-Style Score Calculation for Your Game

Hey there! Let's get that score calculation sorted exactly like you want it—just like Scrabble. I'll walk you through a practical, step-by-step solution tailored to your game:

1. First, Define Scrabble Letter Values

We need a quick way to look up how much each letter is worth. A Dictionary<char, int> is perfect here because it lets us grab a letter's value in no time:

// Scrabble's official letter values (case-insensitive)
var letterValues = new Dictionary<char, int>
{
    {'A', 1}, {'E', 1}, {'I', 1}, {'O', 1}, {'U', 1}, {'L', 1}, {'N', 1}, {'S', 1}, {'T', 1}, {'R', 1},
    {'D', 2}, {'G', 2},
    {'B', 3}, {'C', 3}, {'M', 3}, {'P', 3},
    {'F', 4}, {'H', 4}, {'V', 4}, {'W', 4}, {'Y', 4},
    {'K', 5},
    {'J', 8}, {'X', 8},
    {'Q', 10}, {'Z', 10}
};

2. Prep Your Available Letters & User Input

Your spacedLetters string has spaces between letters, so first we'll clean that up. We'll also normalize everything to uppercase (or lowercase) to avoid case mismatches (like if the user types "room" but your letters are uppercase "R O O M"):

string spacedLetters = "R O O M B A T"; // Replace with your dynamic 7 random letters
int totalScore = 0;

// Clean up spacedLetters: remove spaces and convert to uppercase
var availableLetters = spacedLetters.Replace(" ", "").ToUpper();
// Track how many times each letter is available (critical for Scrabble rules!)
var letterCount = new Dictionary<char, int>();
foreach (char c in availableLetters)
{
    if (letterCount.ContainsKey(c))
        letterCount[c]++;
    else
        letterCount[c] = 1;
}

// Get and normalize user input
Console.WriteLine("Please Enter A Word:");
string userInput = Console.ReadLine()?.ToUpper() ?? "";

3. Calculate the Score (With Rule Checks)

Now we'll loop through each character in the user's input. For every letter:

  • We'll check if it's a valid Scrabble letter
  • We'll make sure the user isn't using more copies of the letter than are available in spacedLetters
  • If all checks pass, we'll add the letter's value to totalScore
bool isValidWord = true;

foreach (char c in userInput)
{
    // Check if the character is a valid Scrabble letter
    if (!letterValues.ContainsKey(c))
    {
        Console.WriteLine($"Oops, '{c}' isn't a valid letter!");
        isValidWord = false;
        break;
    }

    // Check if we have enough of this letter available
    if (!letterCount.ContainsKey(c) || letterCount[c] == 0)
    {
        Console.WriteLine($"You don't have enough '{c}' letters to use here!");
        isValidWord = false;
        break;
    }

    // Add the score and reduce the available count for that letter
    totalScore += letterValues[c];
    letterCount[c]--;
}

// Show the final result
if (isValidWord)
{
    Console.WriteLine($"Great job! Your total score is: {totalScore}");
}
else
{
    Console.WriteLine("Sorry, that word isn't valid—no score awarded.");
}

Quick Notes to Tweak as Needed

  • Skip Validation? If you don't need to enforce using only the spacedLetters (e.g., you just want to calculate score based on the word itself), you can remove the letterCount checks entirely. Just loop through each character and add its value directly to totalScore.
  • Case Flexibility: We used uppercase here, but you could easily switch to lowercase—just make sure both input and letter values use the same case.
  • Edge Cases: The code handles null input (if the user enters nothing) and invalid characters gracefully, so your game won't crash unexpectedly.

内容的提问来源于stack exchange,提问作者Joe Podmore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:09:08