| View previous topic :: View next topic |
| Author |
Message |
Iain
Joined: Wed Jul 13, 2005 3:03 pm Posts: 29
|
Posted: Sun Jan 24, 2010 9:03 pm Post subject: Another Coding Academy query... |
|
|
I worked a way for doing part 3 of the homework on assignment 2. I have copied it below. What I wanted to know is this. Is there a better way of doing this, because I seemed to take a bit of a roundabout way to the solution.
Now on to homework part 4!!
Thanks,
Iain
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> WordFiles = new List<string>();
static string PromptLetters;
static Random Rand = new Random();
static int score = 0;
static string file;
static string directory;
public static void Main(string[] args)
{
directory = "/home/iain/Projects/WordScramble/WordScramble/bin/Debug";
string[] Word = Directory.GetFiles(directory);
if (args.Length ==0){
file = "wordlist";
}else{
string filelist = directory + "/" + args[0];
foreach(string dict in Word){
WordFiles.Add(dict);
if (!WordFiles.Contains(filelist)){
file="wordlist";
}else{
file = args[0];
}
}
}
string[] lines = File.ReadAllLines(file);
foreach(string word in lines){
if (word.Length < 3) continue;
if (word.Contains("'")) continue;
WordList.Add(word.ToLower());
}
Console.WriteLine("");
Console.WriteLine("Welcome to Word Scramble");
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 ("Current Score: "+score);
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);
score = score + input.Length;
Console.WriteLine ("Bravo You");
Console.WriteLine ("Score: " + score);
}else{
Console.WriteLine("That's no good. That word doesn't exist");
}
}else{
Console.WriteLine("Did you forget your letters or something?");
Console.WriteLine("Here is a reminder " + PromptLetters);
}
}else{
Console.WriteLine("You've done that one already!");
}
break;
}
}
}//this brace closes Main()
//Methods go here
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 |
|
 |
rikoshay2020
Joined: Sun Jan 24, 2010 6:17 pm Posts: 6
|
Posted: Sun Jan 24, 2010 11:02 pm Post subject: |
|
|
I interpreted the homework for this part differently. What I've done is far less complicated than what you've done. I thought you were just meant to get it to load an entirely different word list if an argument was specified, otherwise just default to wordlist.
Am I right in thinking what you've done is get the specified wordlist added to the default after checking that words contained in both aren't added twice?
Here's what I did which works:
| Code: | class MainClass
{
static List<string> Wordlist = new List<string>();
static List<string> UsedList = new List<string>();
static int score;
static string PromptLetters;
static Random Rand = new Random();
public static void Main(string[] args)
{
if (args.Length == 0) {
string[] lines = File.ReadAllLines("wordlist");
foreach(string word in lines) {
if (word.Length <3) continue;
if (word.Contains("'")) continue;
Wordlist.Add(word.ToLower());
}
} else {
string[] lines = File.ReadAllLines(args[0]);
foreach(string word in lines) {
if (word.Length <3) continue;
if (word.Contains("'")) continue;
Wordlist.Add(word.ToLower());
}
} |
|
|
| Back to top |
|
 |
Judda
Joined: Sun Jan 10, 2010 9:41 pm Posts: 9
|
Posted: Mon Jan 25, 2010 2:12 am Post subject: |
|
|
I've done mine the same as rikoshay2020.
Works fine.
What did have me pulling my hair out was on Homework 2:
score =+ input.Length;
For about an hour I was puzzled to why it didn't work.
That's right should be:
score += input.Length;
arghh!! |
|
| Back to top |
|
 |
Ram LXF regular

Joined: Thu Apr 07, 2005 10:44 pm Posts: 1569 Location: Guisborough
|
Posted: Mon Jan 25, 2010 10:57 am Post subject: Re: Another Coding Academy query... |
|
|
| Iain wrote: | | I worked a way for doing part 3 of the homework on assignment 2. |
You do mean Project 2 don't you.. _________________
Ubuntu LXDE 12.04 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
|
|
| Back to top |
|
 |
Iain
Joined: Wed Jul 13, 2005 3:03 pm Posts: 29
|
Posted: Mon Jan 25, 2010 9:44 pm Post subject: |
|
|
| Oh yes - sorry about the confusion. |
|
| Back to top |
|
 |
Iain
Joined: Wed Jul 13, 2005 3:03 pm Posts: 29
|
Posted: Fri Feb 05, 2010 6:37 pm Post subject: |
|
|
| What I wanted to do was when a file was specified as an argument it checked to see if the file existed and then used that as the word list, otherwise it would use 'Wordlist'. This seemed to work, but probably isn't the most elegant solution. |
|
| Back to top |
|
 |
smcvey4
Joined: Sun Mar 28, 2010 8:22 pm Posts: 1
|
Posted: Sun Mar 28, 2010 8:35 pm Post subject: Rikoshay2020 solution to Wordscramble Homework # 3 |
|
|
Rik,
are you sure your solution below works? I copied your code into my Wordscramble program and when I run it, the program works fine when I don't specify an argument and the default wordlist is loaded. But when I do specify an alternate file, the program runs but does not print out any letters nor do commands !quit, etc. work.
Any ideas???
Thanks,
Sean McVey
USA
| rikoshay2020 wrote: | I interpreted the homework for this part differently. What I've done is far less complicated than what you've done. I thought you were just meant to get it to load an entirely different word list if an argument was specified, otherwise just default to wordlist.
Am I right in thinking what you've done is get the specified wordlist added to the default after checking that words contained in both aren't added twice?
Here's what I did which works:
| Code: | class MainClass
{
static List<string> Wordlist = new List<string>();
static List<string> UsedList = new List<string>();
static int score;
static string PromptLetters;
static Random Rand = new Random();
public static void Main(string[] args)
{
if (args.Length == 0) {
string[] lines = File.ReadAllLines("wordlist");
foreach(string word in lines) {
if (word.Length <3) continue;
if (word.Contains("'")) continue;
Wordlist.Add(word.ToLower());
}
} else {
string[] lines = File.ReadAllLines(args[0]);
foreach(string word in lines) {
if (word.Length <3) continue;
if (word.Contains("'")) continue;
Wordlist.Add(word.ToLower());
}
} |
|
_________________ Sean |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|