Another Coding Academy query...

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Another Coding Academy query...

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

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 <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;
}
}
}
Iain
 
Posts: 29
Joined: Wed Jul 13, 2005 2:03 pm

Postby rikoshay2020 » Sun Jan 24, 2010 11:02 pm

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: Select all
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());
            }   
         }
rikoshay2020
 
Posts: 6
Joined: Sun Jan 24, 2010 6:17 pm

Postby Judda » Mon Jan 25, 2010 2:12 am

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!!
Judda
 
Posts: 9
Joined: Sun Jan 10, 2010 9:41 pm

Re: Another Coding Academy query...

Postby Ram » Mon Jan 25, 2010 10:57 am

Iain wrote:I worked a way for doing part 3 of the homework on assignment 2.


You do mean Project 2 don't you..

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1676
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Iain » Mon Jan 25, 2010 9:44 pm

Oh yes - sorry about the confusion.
Iain
 
Posts: 29
Joined: Wed Jul 13, 2005 2:03 pm

Postby Iain » Fri Feb 05, 2010 6:37 pm

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.
Iain
 
Posts: 29
Joined: Wed Jul 13, 2005 2:03 pm

Rikoshay2020 solution to Wordscramble Homework # 3

Postby smcvey4 » Sun Mar 28, 2010 7:35 pm

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: Select all
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
smcvey4
 
Posts: 1
Joined: Sun Mar 28, 2010 7:22 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest