Trying to make a scrabble program on c#, individual letters work but a whole word doesnt

86 Views Asked by At

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.

2

There are 2 best solutions below

2
Morten Bork On BEST ANSWER

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:

string Word = "";
            int score = 0;

            Console.WriteLine("Enter a word");
            Word = Console.ReadLine();
            Word = Word.ToUpper();




            List<char> OnePointLetters = new List<char>(){ 'A', 'O', 'I', 'U', 'R', 'S', 'E', 'T', 'N', 'L' };
            List<char> TwoPointLetters = new List<char>() { 'D', 'G' };
            List<char> ThreePointLetters = new List<char>() { 'B', 'C', 'M', 'P' };
            List<char> FourPointLetters = new List<char>() { 'F', 'H', 'W', 'Y', 'V' };
            List<char> FivePointLetters = new List<char>() { 'K' };
            List<char> EightPointLetters = new List<char>() { 'J', 'X' };
            List<char> TenPointLetters = new List<char>() { 'Q', 'Z' };

            for (int i = 0; i < Word.Length; i++)
            {
                char letter = Word[i];
                if (OnePointLetters.Contains(letter))
                {
                    score = score + 1;
                }
                else if (TwoPointLetters.Contains(letter))
                {
                    score = score + 2;
                }

                else if (ThreePointLetters.Contains(letter))
                {
                    score = score + 3;
                }

                else if (FourPointLetters.Contains(letter))
                {
                    score = score + 4;
                }

                else if (FivePointLetters.Contains(letter))
                {
                    score = score + 5;
                }

                else if (EightPointLetters.Contains(letter))
                {
                    score = score + 8;
                }

                else if (TenPointLetters.Contains(letter))

                {
                    score = score + 10;
                }
            }

            Console.WriteLine("The word is worth: " + score);

EDIT: CLEANER CODE EXAMPLE:

class Program
    {
        public static void Main(String[] args)
        {
            string Word = "";
            int score = 0;

            Console.WriteLine("Enter a word");
            Word = Console.ReadLine();
            Word = Word.ToUpper();

            List<IPointMapper> pointsMappers = new List<IPointMapper>();
            pointsMappers.Add(new PointMapper(new List<char>() { 'A', 'O', 'I', 'U', 'R', 'S', 'E', 'T', 'N', 'L' }, 1));
            pointsMappers.Add(new PointMapper(new List<char>() { 'D', 'G' }, 2));
            pointsMappers.Add(new PointMapper(new List<char>() { 'B', 'C', 'M', 'P' }, 3));
            pointsMappers.Add(new PointMapper(new List<char>() { 'F', 'H', 'W', 'Y', 'V' }, 4));
            pointsMappers.Add(new PointMapper(new List<char>() { 'K' }, 5));
            pointsMappers.Add(new PointMapper(new List<char>() { 'J', 'X' }, 8));
            pointsMappers.Add(new PointMapper(new List<char>() { 'Q', 'Z' }, 10));


            foreach (char c in Word)

            {
                foreach (IPointMapper pointMapper in pointsMappers)
                {
                    score += pointMapper.MapLetterToPoint(c);
                }
            }

            Debug.WriteLine("Your word scored: " + score);

            Console.WriteLine("The word is worth: " + score);
        }
    }

    public interface IPointMapper
    {
        public int MapLetterToPoint(char letter);

    }

    public class PointMapper : IPointMapper
    {
        private const int NoPoints = 0;
        private List<char> _lettersThatGrantPoints;
        private int _points;
        
        public PointMapper(List<char> lettersThatGrantPoints, int points)
        {
            _lettersThatGrantPoints = lettersThatGrantPoints;
            _points = points;
        }

        public int MapLetterToPoint(char letter)
        {
            if (_lettersThatGrantPoints.Contains(letter))
            {
                return _points;
            }
            return NoPoints;
        }
    }
2
Dmitry Ivanov On

In CalcVal you need replace in for loops i on letter and compare if (letter == arrOneP[i]):

    //in main method        
    int score = 0;
    for (int i = 0; i < Word.Length; i++)
       {
          score =+ CalcVal(Word[i]);
       } 

    // change in CallVal
    for (int i = 0; i < arrOneP.Length; i++)
        {
            if (letter == arrOneP[i])
            {
                return 1;
            }
        }