Jump to content
  • Advertisement
  • entries
    743
  • comments
    1924
  • views
    582794

C# is weird

Sign in to follow this  
Aardvajk

312 views

Very weird. But quite easy.

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.IsWhiteSpace(Text[Pos])) ++Pos;

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.
Sign in to follow this  


6 Comments


Recommended Comments

Quote:
Original post by EasilyConfused
I'm going to post all my files so far in case anyone expert in the language feels like offering some pointers.


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)

Share this comment


Link to comment
*groan*. Thank you for the pointers.

Yeah, who knows re the old .NET thing. I try to just learn things that interest me but I do think there is more scope for employment as a C# than a C++ programmer these days.

I could really do with learning ASP but don't you need to be running a web-server to play around with that? Loads of books in the library but I need to be able to sit at home and tinker to learn anything really.

Actually, when I've got a bit of C# under my belt, I reckon I'm going to learn javascript next. I know you can invoke javascript functions from ASP so I figure learn javascript, learn how to invoke a javascript function from ASP on the way to the interview and Bingo, one ASP programmer.

That's the theory anyway. I'm just sick and tired of sales to be honest.

Share this comment


Link to comment
Everything looks fine to me, but you might want to look into generics and the foreach loop structure. For example (vocab.cs):

using System;
using System.Collections;
using System.Collections.Generic;

public 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; }
}

public class WordRecord
{
public Word Info;
public string Text;

public WordRecord(int ID,Word.Types Type,string Tx){ Info=new Word(ID,Type); Text=Tx; }
}

public class Vocab
{
//private static ArrayList Records;
// Works just like templates.
static List<WordRecord> Records = new List<WordRecord>();

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<Records.Count;++I)
{
WordRecord W=(WordRecord)Records[I];
if(W.Text==S) return W.Info;
}*/


// foreach(Type LoopVariable in ListVariable)
foreach(WordRecord W in 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<Records.Count;++I)
{
WordRecord W=(WordRecord)Records[I];
if(W.Info.ID==ID) return W.Text;
}*/


foreach(WordRecord W in Records)
{
if(W.Info.ID == ID)
return W.Text;
}

return "(unknown)";
}
}



Just to make sure, class can now have access modifiers, so if you don't specify one classes will be private.

HTH!

Share this comment


Link to comment
Fantastic. That is exactly the kind of feedback I was looking for. Thanks for taking the time and I'll put all that into practise tonight.

Question: If I have created an array like:

int[] x=new int[20];

Can I then do like:

int[] old=x;
x=new int[30];
for(int i=0;i<20;++i) x[i]=old[i];

and then assume that the garbage collector will clean up old[]?

Just curious really.

Share this comment


Link to comment
Quote:
Original post by EasilyConfused
Question: If I have created an array like:

int[] x=new int[20];

Can I then do like:

int[] old=x;
x=new int[30];
for(int i=0;i<20;++i) x[i]=old[i];

and then assume that the garbage collector will clean up old[]?


Yes, the GC will start on it once 'old' leaves the scope of whereever you initialized it.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!