| View previous topic :: View next topic |
| Author |
Message |
rikoshay2020
Joined: Sun Jan 24, 2010 6:17 pm Posts: 6
|
Posted: Sun Jan 24, 2010 7:27 pm Post subject: Coding Academy - WordScramble Homework |
|
|
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 |
|
 |
Iain
Joined: Wed Jul 13, 2005 3:03 pm Posts: 29
|
Posted: Sun Jan 24, 2010 8:46 pm Post subject: |
|
|
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 |
|
 |
rikoshay2020
Joined: Sun Jan 24, 2010 6:17 pm Posts: 6
|
Posted: Sun Jan 24, 2010 9:35 pm Post subject: |
|
|
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 |
|
 |
Iain
Joined: Wed Jul 13, 2005 3:03 pm Posts: 29
|
Posted: Sun Jan 24, 2010 9:37 pm Post subject: |
|
|
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 |
|
 |
rikoshay2020
Joined: Sun Jan 24, 2010 6:17 pm Posts: 6
|
Posted: Sun Jan 24, 2010 9:49 pm Post subject: |
|
|
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 previous topic :: View next topic |
|