 |
Linux Format forums Help, discussion, magazine feedback and more
|
| View previous topic :: View next topic |
| Author |
Message |
branmask
Joined: Thu Feb 18, 2010 1:19 am Posts: 14
|
Posted: Wed Mar 03, 2010 11:21 pm Post subject: Coding Academy - WordScramble Help |
|
|
Hello all,
I have the code done for wordscramble(not including the homework), but for some reason all it prints in the terminal is: You've had that word already!
Even when I know I've typed a good word. ie:
cassandre
case
You've had that word already!
read
You've had that word already!
I'm thinking something is out of place or incorrect in my code, but I can't seem to find it. If anyone can see where I went wrong, help would be appreciated. Thanks. Here's my 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 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);
Console.WriteLine("Good!");
}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 < {
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 |
|
 |
branmask
Joined: Thu Feb 18, 2010 1:19 am Posts: 14
|
Posted: Wed Mar 03, 2010 11:29 pm Post subject: |
|
|
nevermind! I figured it out. in the " if (!UsedList.Contains(input)){ " line I didn't put the "!" before UsedList.
Thanks anyways... |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|
|
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
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|