Coding Academy - WordScramble Homework

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Coding Academy - WordScramble Homework

Postby rikoshay2020 » Sun Jan 24, 2010 7:27 pm

I'm struggling with part 2 of the homework for project 2. I have managed to get the score displayed for the number of correct words by adding a new list string called ScoreList, adding any input that passes all conditions to this string and then using the .Count function to display the score.

In order get the score to count the number of letters rather than words in the string I reckon I need to use a foreach loop and a counter to go over the data in my ScoreList string. Every time and every way I try and do this I get an error telling me that I cannot convert strings into chars. I don't really understand what I'm missing because this seems to be what I've done in the GetLetters method. I realise however, I may completely barking up the wrong tree. Any help/pointers appreciated. Below is my code so far:

Code: Select all
using System;
using System.Collections.Generic;
using System.IO;

namespace WordScramble
{
   class MainClass
   {
      static List<string> Wordlist = new List<string>();
      static List<string> UsedList = new List<string>();
      static List<string> ScoreList = new List<string>();
      static string PromptLetters;
      static Random Rand = new Random();
      public static void Main(string[] args)
      {
         string[] lines = File.ReadAllLines("wordlist");
         
         foreach(string word in lines) {
            if (word.Length <3) continue;
            if (word.Contains("'")) continue;
            Wordlist.Add(word.ToLower());
         }
         Console.WriteLine("");
         Console.WriteLine("Welcome to WordScramble!");
         Console.WriteLine("Type !quit to exit, !letters for a reminder, or !next for a new word.");
         Console.WriteLine("");
         GetLetters();
         Console.WriteLine(PromptLetters);
         bool running = true;
         while (running) {
         string input = Console.ReadLine();
            switch (input) {
            case "!quit":
               running = false;
               break;
               
            case "!next":
               GetLetters ();
               Console.WriteLine(PromptLetters);
               break;
               
            case "!letters":
               Console.WriteLine(PromptLetters);
               break;
               
            default:
               if (!UsedList.Contains(input)){
                  if (WordIsPossible(input)){
               if (Wordlist.Contains(input)){
                     UsedList.Add(input);
                     ScoreList.Add(input);
                     Console.WriteLine("Good!");
                     Console.WriteLine("Your score is: " + ScoreList.Count);
               } else {
                  Console.WriteLine("That word doesn't exist!");
                     }
               } else {
                     Console.WriteLine("Did you forget your letters or something?");
                     Console.WriteLine("Reminder: " + PromptLetters);
                  }
               } else {
                  Console.WriteLine("You've had that word already!");
               }
               break;
            }
         }
      }
      static void GetLetters() {
      PromptLetters = "";
      UsedList.Clear();
         string word = "";
         while (word.Length < 8) {
         word = Wordlist[Rand.Next(Wordlist.Count)];
      }
      foreach (char letter in word) {
         PromptLetters = PromptLetters + letter + " ";
      }
      }
      static bool WordIsPossible(string word) {
         string letters = PromptLetters;
         
         foreach (char letter in word) {
            int pos = letters.IndexOf(letter);
            if (pos !=-1) {
               letters = letters.Remove(pos, 1);
            } else {
               return false;
            }
         }
         return true;
      }
   }
   }
rikoshay2020
 
Posts: 6
Joined: Sun Jan 24, 2010 6:17 pm

Postby Iain » Sun Jan 24, 2010 8:46 pm

I managed to do this by creating an int called score using:

static int score =0;

just under 'class MainClass' and then adding this line where you have ScoreList.Add(input):

score = score + input.Length;

I don't know if this is correct, but it seems to work.

Iain
Iain
 
Posts: 29
Joined: Wed Jul 13, 2005 2:03 pm

Postby rikoshay2020 » Sun Jan 24, 2010 9:35 pm

Thanks Iain! That makes much more sense than what I was trying to do. I've actually just used:

static int score;

under the class MainClass (missing off the =0 as I wasn't sure what that was doing) and it works fine.
rikoshay2020
 
Posts: 6
Joined: Sun Jan 24, 2010 6:17 pm

Postby Iain » Sun Jan 24, 2010 9:37 pm

You are probably right to be honest. I put it in so I knew the score started from 0. It probably doesn't need it.

I also wondered if the score should be reset when a new set of letters was chosen. I decided not to, but I wasn't sure if we were meant to.
Iain
 
Posts: 29
Joined: Wed Jul 13, 2005 2:03 pm

Postby rikoshay2020 » Sun Jan 24, 2010 9:49 pm

I left it out because I figured arrays start counting at zero unless you tell them otherwise. I guessed the counter's not meant to reset as well. Makes more sense that way.

Saw you posted about part 3. I'll have a look when I've had decent go trying to get it working myself. Thanks for the help.
rikoshay2020
 
Posts: 6
Joined: Sun Jan 24, 2010 6:17 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest