I tried using a function which wasn't working for me, so I restarted. I can get individual letters to give points when inputting a word it gives an error. How can i fix it or should I just try use a function again.
string Word = "";
int score = 0;
Console.WriteLine("Enter a word");
Word = Console.ReadLine();
Word.ToUpper();
string[] arrOneP = new string[] { "A", "O", "I", "U", "R", "S", "E", "T", "N", "L" };
string[] arrTwoP = new string[] { "D", "G" };
string[] arrThreeP = new string[] { "B", "C", "M", "P" };
string[] arrFourP = new string[] { "F", "H", "W", "Y", "V" };
string[] arrFiveP = new string[] { "K" };
string[] arrEightP = new string[] { "J", "X" };
string[] arrTenP = new string[] { "Q", "Z" };
for (int i = 0; i < Word.Length; i++)
{
if (Word == arrOneP[i])
{
score = score + 1;
}
else if (Word == arrTwoP[i])
{
score = score + 2;
}
else if(Word == arrThreeP[i])
{
score = score + 3;
}
else if (Word == arrFourP[i])
{
score = score + 4;
}
else if (Word == arrFiveP[i])
{
score = score + 5;
}
else if (Word == arrEightP[i])
{
score = score + 8;
}
else if (Word == arrTenP[i])
{
score = score + 10;
}
}
Console.WriteLine(score);
OLD CODE namespace Scrabble { internal class Program { static void Main(string[] args) { string Word = ""; int score = 0; int WordValue = 0;
Console.WriteLine("Enter a word");
Word = Console.ReadLine();
static int CalcVal(char letter)
{
string[] arrOneP = new string[] { "A", "O", "I", "U", "R", "S", "E", "T", "N", "L" };
string[] arrTwoP = new string[] { "D", "G" };
string[] arrThreeP = new string[] { "B", "C", "M", "P" };
string[] arrFourP = new string[] { "F", "H", "W", "Y", "V" };
string[] arrFiveP = new string[] { "K" };
string[] arrEightP = new string[] { "J", "X" };
string[] arrTenP = new string[] { "Q", "Z" };
for (int i = 0; i < arrOneP.Length; i++)
{
if (i == arrOneP.Length)
{
return 1;
}
}
for (int i = 0; i < arrTwoP.Length; i++)
{
if (i == arrTwoP.Length)
{
return 2;
}
}
for (int i = 0; i < arrThreeP.Length; i++)
{
if (i == arrThreeP.Length)
{
return 3;
}
}
for (int i = 0; i < arrFourP.Length; i++)
{
if (i == arrFourP.Length)
{
return 4;
}
}
for (int i = 0; i < arrFiveP.Length; i++)
{
if (i == arrFiveP.Length)
{
return 5;
}
}
for (int i = 0; i < arrEightP.Length; i++)
{
if (i == arrEightP.Length)
{
return 8;
}
}
for (int i = 0; i < arrTenP.Length; i++)
{
if (i == arrTenP.Length)
{
return 10;
}
}
Console.WriteLine("Your score is {0}", CalcVal);
}
}
}
}
This was my old code. in my new code, i wanted to be able to input a word and it return a value for each letter, instead it can only return values per letter.
I would rewrite your code slightly to make it a little more readable.
You have a few problems with your code:
1, Word.ToUpper(), needs to overwrite the original value, or you are simply discarding the return from the ToUpper method
2, you aren't traversing what you think you are traversing. The string "Word" is a set of chars, but stirng doesn't automattically convert to an array of characters, and then does a compare on each charater to the single character on the word.
You are comparing a single character and asking if it is equal to an entire word. Had you typed "Help" it is asking if arrOneP[i] ('A') == "help". It isn't. No letters in your comparison will be.
Try this instead:
EDIT: CLEANER CODE EXAMPLE: