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 

Another Coding Academy query...

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



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

PostPosted: Sun Jan 24, 2010 9:03 pm    Post subject: Another Coding Academy query... Reply with quote

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 <Cool {
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
rikoshay2020



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

PostPosted: Sun Jan 24, 2010 11:02 pm    Post subject: Reply with quote

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
View user's profile Send private message
Judda



Joined: Sun Jan 10, 2010 9:41 pm
Posts: 9

PostPosted: Mon Jan 25, 2010 2:12 am    Post subject: Reply with quote

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
View user's profile Send private message
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1668
Location: Guisborough

PostPosted: Mon Jan 25, 2010 10:57 am    Post subject: Re: Another Coding Academy query... Reply with quote

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



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

PostPosted: Mon Jan 25, 2010 9:44 pm    Post subject: Reply with quote

Oh yes - sorry about the confusion.
Back to top
View user's profile Send private message
Iain



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

PostPosted: Fri Feb 05, 2010 6:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
smcvey4



Joined: Sun Mar 28, 2010 8:22 pm
Posts: 1

PostPosted: Sun Mar 28, 2010 8:35 pm    Post subject: Rikoshay2020 solution to Wordscramble Homework # 3 Reply with quote

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 user's profile Send private message Send e-mail
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