Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Coding Academy - WordScramble Homework

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
rikoshay2020



Joined: Sun Jan 24, 2010 6:17 pm
Posts: 6

PostPosted: Sun Jan 24, 2010 7:27 pm    Post subject: Coding Academy - WordScramble Homework Reply with quote

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:
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;
      }
   }
   }
Back to top
View user's profile Send private message
Iain



Joined: Wed Jul 13, 2005 3:03 pm
Posts: 29

PostPosted: Sun Jan 24, 2010 8:46 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
rikoshay2020



Joined: Sun Jan 24, 2010 6:17 pm
Posts: 6

PostPosted: Sun Jan 24, 2010 9:35 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Iain



Joined: Wed Jul 13, 2005 3:03 pm
Posts: 29

PostPosted: Sun Jan 24, 2010 9:37 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
rikoshay2020



Joined: Sun Jan 24, 2010 6:17 pm
Posts: 6

PostPosted: Sun Jan 24, 2010 9:49 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast