Sunday, April 27, 2008

Project 3: Dictofunk


My newest project had the objective of loading a word list from a text file and converting the words into a tree.

In other words:

act

arc
art

would become

a->c->t
.->r->c
....->t

This is one of those sort of important, school-y types of things that people should learn, and I wanted to do it for the Boggle Solver program (given a Boggle board and a list of valid words, find all possible words within the rules of Boggle) I had talked with somebody about a couple of months ago.

The basic process involved is this:
1. Read a word from a file.
2. Starting out with an empty tree, search the top level of the tree for the first letter in the word.
3a. If you find it, make that spot the top of the new tree.
3b. If you don't, add the letter to the tree, make that letter the top of the new tree.
4. Chop off the first letter of the word and start over at 2 with your new subtree.

Finally, when the word is done, it sticks a "." at the end so it knows it's a complete word (so that you know, for instance, while "act" and "actual" are words, "actu" and "actua" aren't--they won't have a "." in their subtrees).

Here's the same algorithm in C# form:

public void insertWord(TreeNode thisNode, Char[] chars)
{
bool found = false;
String charString = new String(chars);

if (charString.Length > 0)
{
charString = charString.Substring(1);

foreach (TreeNode branch in thisNode.Nodes)
{
if (branch.Text == chars[0].ToString())
{
found = true;
insertWord(branch, charString.ToCharArray());
}
}
if (!found)
{
insertWord(thisNode.Nodes.Add(chars[0].ToString()), charString.ToCharArray());
}
}
}


And that's pretty much that. The actual algorithm is fast (as seen in the screen shot, with the test dictionary of over 70,000 words it was going through about 13,000 words per second). Loading the tree into the Windows Form, however, takes a good 60-90 seconds. That's not a major concern, however, since the graphical display (yet another great thing that C# does that makes life so easy) is essentially pointless except for visually verifying that the algorithm works. I was kind of shocked that this was even included as a basic display type. Oh, the future.

Oh, also, C# in general was a huge help in doing this in that it I didn't have to worry about constructing a tree class of my own. I was able to spend my time actually figuring out the algorithm I needed, which, while I've certainly read about tree insertions before, I did last night and this morning on my own. Once again, this is totally freshman programming class stuff, but it feels good to know I did it more or less on my own.

Source
Dictofunk.cs

Saturday, April 26, 2008

Update: Time Sheet Calculator v. 0.2

I recently updated the Time Sheet Calculator program to incorporate a few additions I wanted to have after using it to do the payroll this past week. For the record, it saved me at least 90 minutes of work this week alone, and it will save me about that much time once every two weeks in the future.

I needed to add a button to reset all the fields to 0, and I labeled it "TARE" because I felt like labeling it "TARE". I also fixed the formatting of the total time display so that it displayed it in HH:MM rather than the default format of d.hh:mm. Finally, I changed the format of the individual DateTimePickers to be hh:mm am/pm, because the seconds were unused and leading to too many mistyped numbers. If I think of anything else to add, I'll post it. Right now I'm satisfied.

Source:
TSCv0_2.cs
TSCv0_2.Designer.cs
TSCv0_2.resx

Wednesday, April 23, 2008

Project 2: Swarm and Gravitate

In this program, I started learning the basic flow of the Windows Forms program. I did some event handling and some graphics, learned some more stuff that C# makes retarded easy (the List container, primarily), and did some really simple math (that took a little too long to figure out--it's been too long since I've taken math. Or physics.)

Swarm() calculates the center position of the collection of bodies and everybody accelerates towards that position. Gravitate() attracts the bodies to one another by (something resembling) gravity. This is all, of course, 2D.

Things I'd like to add:

  • Hitting tab to center on a body doesn't properly work. I need to fix that.
  • Once that works, I'd like to be able to double click on a position to center on that point.
  • There is no consequence of collisons--gravity simply turns off when the bodies are about to touch (to keep them both from rocketing off into space).
  • It'd be nice to put in a menu to add / delete planetary bodies.
  • It'd be nice to put in controls to drive around one of the planets.
If I can think of anything else to add, I'll add it. I'm planning on moving on to a simple game next. Don't think I'm going to mess with DirectX yet, think I'll do something hands-on and 2D before I start messing around with the XNA toolkit.

Here's the source file.

Friday, April 18, 2008

Project 1: Time Sheet Calculator

Okay, so this whole thing started the other day when I got sick of doing payroll. At my workplace, the staff members clock in and clock out with and old-fashioned time card system, once which prints the time on a piece of card stock. Every other Thursday, I have to go through every single card, calculate the shift times in my head, and then add them all up on my own. It's a tedious and pointless process. I could spend a couple hundred bucks to buy an electronic system that would make it automatic. That, OR--!!!

I downloaded Visual C# Express, since I heard every retard on the planet is using C# these days. I learned what I needed to know from the in-program documentation, and start to finish spent about 30 or 40 minutes. Here's what I ended up with:


It's ugly, right? But it gets the job done. I spend about 2 hours per payroll adding all that up--with this, I'll probably spend 15 minutes.

Here's the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PayrollMcGee
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void TotalLabel_Update()
{
TotalLabel.Text = ((dateTimePicker2.Value - dateTimePicker1.Value) +
(dateTimePicker4.Value - dateTimePicker3.Value) +
(dateTimePicker6.Value - dateTimePicker5.Value) +
(dateTimePicker8.Value - dateTimePicker7.Value) +
(dateTimePicker10.Value - dateTimePicker9.Value) +
(dateTimePicker12.Value - dateTimePicker11.Value) +
(dateTimePicker14.Value - dateTimePicker13.Value) +
(dateTimePicker16.Value - dateTimePicker15.Value) +
(dateTimePicker18.Value - dateTimePicker17.Value) +
(dateTimePicker20.Value - dateTimePicker19.Value) +
(dateTimePicker22.Value - dateTimePicker21.Value) +
(dateTimePicker24.Value - dateTimePicker23.Value) +
(dateTimePicker26.Value - dateTimePicker25.Value) +
(dateTimePicker28.Value - dateTimePicker27.Value)).ToString();
}

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
TotalLabel_Update();
}

private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
TotalLabel_Update();
}

...

private void dateTimePicker28_ValueChanged(object sender, EventArgs e)
{
TotalLabel_Update();
}
}
}

Tedious, right? Just like always. It's super ghett0, completely taped together, and probably looks like something somebody would do if they'd just discovered visual programming interfaces.

So, um, yeah. That's me.

Chapter 1: Starts and beginnings

The goal of this blog is to document my work while I'm learning to code again, and to show to other people so that they might be able to a) learn from it, or, more likely, b) yell at me because I've been slacking.

Let me begin by saying that I started programming when I was about 14 in C and C++ for my Macintosh running System 7. I think I was using Metrowerks Codewarrior, a trial copy that came with a book I bought from the closeout bin at Books-A-Million. Either because of my lack of experience, my lack of years, or the fact that, if I recall correctly, writing for Macs back then was impossible, I had a hard time of it. It went okay, though, and over the years I wrote a couple of little games that never really worked quite right from beginning to end. But whatever, I was learning.

I worked a lot in Java as well, still mainly in high school, and again wrote some games, wrote some simple programs to do this and that, and learned a bit along the way.

Fast forward five or ten years, and here I am. I've forgotten most of what I knew, and that might be a good thing. I'm going to try really hard over the next couple of months to see what I can do, and I'll try to document it here.


The primary point of this post, though, is to list my goals, which are--at this time--the following:

1. To work in Visual C# Express to learn the basics of contemporary Windows programming. A big sub-goal in this heading is to better understand the program flow inside the Windows Forms framework--something which is kind of weird to me right now. A major reason for working within this framework is that it makes the UI end of things so easy that it will free my mind to focus on the actual code, the actual algorithms rather than making sure the event handler or menu system is working.

1(a). To work with the XNA toolkit, once task 1 is well underway, to write a non-trivial game that doesn't look awful, will take a fair amount of time and effort on my part such that it will feel like an accomplishment, and hopefully something that is actually fun (or at least interesting) to play. More on this later.

2. To learn (or relearn) the fundamentals, be it basic searching and sorting, data handling, or really whatever. I have not decided the best source of information to go about learning this, although I do have several related textbooks, two Knuth books, and the whole wide interwebs to assist me in this matter. This is going to be the least fun and most important item to work on, so I really need to do it.

3. To learn (for the heck of it) to work with Lisp, possibly working from Structure and Interpretation of Computer Programs. This task is inspired by another blog, and I do not in any way expect to complete this task in nearly the extensive fashion that the linked person has done. This task, as you may notice, seems fairly unrelated to the other two. It may be, it may not. I think it will stretch my mind in a thoroughly different direction, and while this task will receive the least immediate attention of the three, I'd feel very accomplished to get somewhere with it.

That's enough for now. I'll be posting what I have of the programs I've written so far in the coming days.

OH MY GOD DOG, IT'S A PROG BLOG

Okay, I've decided to (re)learn how to program. This will document my journey. HERE WE GO.