I'm writing a text adventure to try to get a bit more comfortable with the language. I'm going to post all my files so far in case anyone expert in the language feels like offering some pointers.
cmd.cs
using System;class Cmd{ public string Verb,Noun,Prep,Noun2; public Cmd(){ Verb=Noun=Prep=Noun2=""; } public Cmd(string V,string N){ Verb=V; Noun=N; Prep=""; Noun2=""; } public Cmd(string V,string N,string P,string N2){ Verb=V; Noun=N; Prep=P; Noun2=N2; }}
vocab.cs
using System;using System.Collections;class Word{ public enum Types { Null,Verb,Noun,Prep }; public int ID; public Types Type; public Word(int I,Types T){ ID=I; Type=T; }}class WordRecord{ public Word Info; public string Text; public WordRecord(int ID,Word.Types Type,string Tx){ Info=new Word(ID,Type); Text=Tx; }}class Vocab{ private static ArrayList Records; public static void Create() { Records=new ArrayList(); Records.Add(new WordRecord(1,Word.Types.Verb,"quit")); Records.Add(new WordRecord(2,Word.Types.Verb,"get")); Records.Add(new WordRecord(2,Word.Types.Verb,"take")); Records.Add(new WordRecord(3,Word.Types.Noun,"book")); Records.Add(new WordRecord(4,Word.Types.Noun,"apple")); Records.Add(new WordRecord(5,Word.Types.Prep,"in")); Records.Add(new WordRecord(5,Word.Types.Prep,"into")); Records.Add(new WordRecord(6,Word.Types.Prep,"under")); } public static Word LookUp(string S) { for(int I=0;I { WordRecord W=(WordRecord)Records; if(W.Text==S) return W.Info; } return new Word(0,Word.Types.Null); } public static string Text(int ID) { for(int I=0;I { WordRecord W=(WordRecord)Records; if(W.Info.ID==ID) return W.Text; } return "(unknown)"; }}
parser.cs
using System;using System.Collections;class Parser{ private enum Types { wtWord,wtPunc,wtNull }; private Types Next(ref string W) { W=""; while(PosChar
if(Pos==Text.Length) return Types.wtNull;
if(Char.IsPunctuation(Text[Pos]))
{
while(PosChar.IsPunctuation(Text[Pos]) || Char.IsWhiteSpace(Text[Pos]))) ++Pos;
W=",";
return Types.wtPunc;
}
while(PosChar.IsWhiteSpace(Text[Pos]) && !Char.IsPunctuation(Text[Pos])) W+=Text[Pos++];
W=W.ToLower();
if(W=="and" || W=="then" || W=="but") return Types.wtPunc;
return Types.wtWord;
}
private string Text;
private int Pos;
public Parser(string S,Queue Qu)
{
Text=S; Pos=0;
while(Next(Qu));
}
private void Consider(string W,Cmd C)
{
Word R=Vocab.LookUp(W); if(R.Type==Word.Types.Null) return;
switch(R.Type)
{
case Word.Types.Verb: if(C.Verb=="") C.Verb=Vocab.Text(R.ID); break;
case Word.Types.Prep: if(C.Prep=="") C.Prep=Vocab.Text(R.ID); break;
case Word.Types.Noun: if(C.Noun=="") C.Noun=Vocab.Text(R.ID);
else if(C.Noun2=="") C.Noun2=Vocab.Text(R.ID);
break;
}
}
private bool Next(Queue Qu)
{
Cmd C=new Cmd();
string W="";
Types T=Next(ref W); while(T==Types.wtPunc) T=Next(ref W);
if(T==Types.wtNull) return false;
while(T==Types.wtWord)
{
Consider(W,C); T=Next(ref W);
}
Qu.Enqueue(C);
return true;
}
}
main.cs
using System;using System.Collections;class Imp{ static void Main() { Queue CmdQu=new Queue(); Vocab.Create(); while(true) { FillQueue(CmdQu); while(CmdQu.Count!=0) { Cmd C=(Cmd)CmdQu.Dequeue(); Console.WriteLine("{0} {1} {2} {3}",C.Verb,C.Noun,C.Prep,C.Noun2); } } } private static void FillQueue(Queue Qu) { while(Qu.Count==0) { Console.Write("> "); string S=Console.ReadLine(); Parser P=new Parser(S,Qu); } }}
Obviously all it does at the moment is parse user input and convert it into a Cmd structure containing predefined words in the format verb-noun1-preposition-noun2. Synonyms are supported by giving two WordRecord entries in the Vocab class the same numeric ID.
The reason for having Parser as an instance-based rather than static class was in case two parsers needed to run, one inside the other. Doubtful but this is more an exercise than a serious project.
So come on. Don't hold back. If I'm doing anything really dumbass, please don't pull punches. I really need to learn C# as quickly as possible.
-----------------------------------------------------------------------
On a side note, I broke the news to my boss that I can't afford to continue to work for the salary my current job offers any more and am now actively seeking other employment.
I was talking to a guy from another company in my building the other day and programming came up. He (like many people) was of the opinion that I was mad to not be seeking a career in programming and all my usual excuses:
a) I don't have a formal qualification
b) I'm over thirty and no-one would touch me as a trainee
c) My particular programming skills are not well suited for a normal programming job
were answered by:
a) Shut up
b) Shut up you decrepid old bastard
c) Learn some new bloody skills then
Hence, as the more astute among you will have gathered, my sudden U-turn with regard to C#. Everyone seems to be telling me to learn .NET and earn GBP20K a year in minutes. I assume learning C# counts as learning .NET.
It made me realise though. Guy I was speaking to said "If you learned .NET, I'd offer you a programming job." Now, who knows what would really happen, but if I'd picked up C# six months ago, we MIGHT have had a much more interesting conversation.
Still, whatever. I enjoy learning new stuff anyway so I'm just going to press on for the time being and see what happens. Even if I end up in another sales job I should still be able to stick about 30% on top of what my current job pays. That's the trouble with working for a small company. Lots of freedom and variety but not much money.
Shame really because I like my job but the missus gets broodier every day and it is only so long before the inevitable happens. I really need to be earning a less laughable salary before we go there.
Sorry, can't resist:
0x0a544f
0xaaffe1
0x512b3c
[grin]
As for the serious stuff, I really can't comment on anything, since a) although I am fluent with C#, I'm in no position to be throwing critique at you, and b) I don't have the 'real world' experience stuff down yet. I doubt it'll be as nice as people throwing money at us just for knowing .NET, but who knows what the market is like? (I'll be able to blab with you on this topic perhaps this summer when I have my first co-op term :P)