Friday, May 30, 2008

Top coder competition

www.topcoder.com

Basically an outsourcing site where you can (supposedly) win money by competing to build software components on demand, or do algorithm contests for fun and some money.

I did a competition the other day, even though I knew they'd be hard. I mean, you know, worst case scenario is I had no idea how to solve it and I just go and move on to something else. But it turns out the problem wasn't all that hard (except for the third one), and they were all related to eachother.

So once I figure out problem 1 I just copy some code over for problem 2, make some changes, get in the top 7 after the coding phase is done and am feeling pretty good about myself. Well, the challenge phase comes along and my second program fails.

The reason? Time. TopCoder wants their programs to run, but run fast. Any algorithm that will take longer than 2 seconds will fail. I think the weakness here, for me at least, is my lack of math skill. I looked at source of some of the competitors and it seemed that the ones that did well were based in math.

Oh well. Lesson learned, plus, it was fun.

Tuesday, April 29, 2008

Words cannot describe the atrocites I've committed

So, I'm working on making Mastermind as a way to learn more about programming with graphical user interfaces in mind. The core logic for checking this and that took maybe 2-3 hours of programming, tops, to build. Plus a day or two to think of how to solve a really tough part (Which, to my knowledge, works correctly now, but I suck at Mastermind so I can't tell 100%. but it seems to be correct).

While I developed the game from the console first with the concept of switching over to a graphical user interface later, I quickly found that I didn't structure my code well enough for the changeover and, well, what started as a project that should take a few days and be somewhat elegant (as I try to be... well, elegant isn't the right word... I just mean like... not using more lines than needed) into something that's bogged me down and forced me to give up a feature I had planned (Just as well... I can't manipulate AutoCad like I used to anyway).

Once frustration set in I stopped caring about code elegance and just started bulldozing whatever wasn't finished (It's a decision based on time spent on coming up with a good solution vs. time spent forcing the solution into place which a computer can still solve pretty fast... whichever saves me more time I pick) because I'm just getting frustrated with it all.

Oh, don't get me wrong. It's almost finished (I just need to refactor a few things). Well, single player anyway.

Then comes two-player support and computer play. Yeah, making an AI will be real fun.

I think after this I'll go make Battleship.

Sunday, January 20, 2008

Opening really large text files

So I have this problem. I have a 500MB text file filled with prime numbers.

I'm not entirely sure how it got to be so big or how it was even made without the JVM crashing in protest (I guess file access for programs other than the end-user don't use RAM) like it did when I tried holding stuff in arrays, bt suffice it to say I'm not going to be able to open this file with any basic program (I know because opening a 4MB program in notepad takes about 1-2 hrs). Yet my programming teacher keeps talking about how stuff like this is always handled as a string (Primitive data types are so limited in that they can go up to 64 bit at most), so I got to thinking and googled "opening really large text files", thinking that there was probably some top-secret technology that only the super computer gurus had.

Turns out that I'm not the only one with this problem, and the solution seems to be a little program called TextPad (www.textpad.com). Even the evaluation copy is powerful enough to open files larger than 500MB in a little over ten seconds. OpenOffice exited after it tried, and wordpad struggled (it opened part of it then froze... well not froze, it was still working but was unresponsive).

I'm not sure why it works so fast while others struggle or even just crash outright. I wish I knew, because I think it'd be nice to write a text editor one day (among a whole slew of other things, of course), and knowing that this is a problem that can crop up from time to time (usually when trying to read files from mySQL servers, apparently)

Wednesday, December 26, 2007

Calculating Big (really really really really really MONSTEROUSLY BIG) numbers

The curious thing about programming and mathematics is that I'm not at all a good mathematician (C in Algebra 1, B in Geometry, D in Algebra 2), except when it comes to the computer, and the thing that has intrigued me most so far is the concept of computers being able to calculate extremely large numbers. Pi has been found to some odd billion digits, prime numbers (as far as we know) have been found using computers up to and beyond the 10 millionth digit (www.mersenne.org), and computers are used for other mathematical purposes as well (Seti, folding@Home, protein formations, etc).

The list is quite endless, I'm sure.

So as a novice programmer I also wanted to do something with higher math. Simply squaring numbers and all that is a little boring, yet the fibonacci sequence fascinated me, so I wrote a program to calculate that.

But it died after runs through the sequence. The 64 bits of data become clogged after about 19 digits, and fibonacci has been calculated to 10 million with some god-awful C++ code that was several lines long. So I asked the gods at www.javaranch.com, and they helped. The program no longer died after 91 runs through.

But then I realized that with an output to screen I couldn't retrieve the information, at all (what's the use of calculating a really really really big number if you can't get to it), which meant I had to solve another problem.

So I went searching, went on a journey, tried a bunch of things, looked at the hard-to-decipher-if-you-try-to-read-deep-in java docs, and then realized that the solution was right under my nose. The BigInteger class used to represent numbers larger than 64 bit (that is, 19 digits long or so) is an object. Every object has a toString method (returns it as a series of text). Text is savable to a file. My teacher went on about how these really big ass numbers are just strings of text..............

CLICK!

The only problem now is processing. The only solution for that is time.

Guess I'll be leaving something on overnight. Hey, fibonacci is already to the 100,000th digit :)

Tuesday, December 25, 2007

Welcome to Code Hell

Why the hell did I do my combat system that way?



With the way it was before, I was using about three different methods to do things I could use with just if-then logic. I dunno why I was doing it that way other than the idea that I was maybe only thinking one step at a time.

Building a game is a precarious thing. It's already 1500 some-odd lines (a lot are printouts, and stuff like that, but there's also some stuff that goes into the guts of the program) and I really don't feel like I've done that much at all with it, yet.

It doesn't help that my first draft of the game was something that meant one little change required a lot of stuff. The applications we write in CS are just quick and dirty little things that teach a lot about one little aspect of the language, but not necessarily how it all meshes too well. I had to learn that the hard way when I started on the game. The way I had learned coding up to that point would make anything semi-big unmaintainable.

Don't get me wrong, I'm not knocking my computer science course. It's nothing too advanced because its a first-year program but its nothing to laugh about either because it is an AP course. I'm just saying that my limited experience, trying to work on a big project has actually been a good thing. I've gotten my head around classes and objects a little before the need to do so was required (something I'm glad happened before I got my Gridworld assignment, or i'd have been lost) and am looking at applying new concepts I learn to the game.

The irony of it is its a text-based RPG. Right now its just singleplayer but i might make it into a MUD if its any good. The ironic thing is I don't play MUDS, really. Because they're just so dang complex that its hard to understand, or i feel lost, not having a map, and stuff.

However I think its largely due to the fact that I never could quite immerse yself into them, not that MUDs were bad. But with my game I can make it how I want, and strip away the complex learning curve (all those bajillion commands) and hopefully add depth.

Maybe.

All else fails I'll just work on it to learn CS.

But I have so many ideas for the thing, where I want to take it... And it's just a baby right now. Maybe when it's matured a bit it'll be something awesome.

Thursday, December 20, 2007

Winter Break

WINTER BREAK IS HERE!!!!!!!!!!!!

Whew.

I'm exhausted.

And I have no idea why.

I mean I have an idea asto why, and I think it pertains to the fact that I've pretty much worked my ass off the past few months (gaming, school, work, we all know that gaming is actually hard work) but was kept in a perpetual state of work until now.

And the week has winded down, and its now catching up to me. It's the whole time dilation effect, you know. When you don't have time to think until its all over. And then it hits you. HITS you.

So, yeah.

I have nothing over winter break save a lot of programming to do. Which means it'll be an awesome two weeks, sans work.

Thursday, December 13, 2007

Perfect Simulation

Computer Science, at its core, takes a model of a real-world situation and simulates it, perfectly. If two programmers utilize this concept to a perfect degree their code will look the same, raising an eerie question of "how do you judge who's a better programmer?"

Of course, always holding onto such an ideal is difficult, if not impossible in practice, because it entails not only solving a problem to get from point A to point B, but solving it in such a way that the underlying logic FITS in with how the world works.

This raises quite a few interesting questions.

1. What is a real-world situation? Do we interact with the world using windows user interfaces on a daily basis? Are games "real-world" situations? They could be an extension of fiction, but, how do you quantify the underlying code?

2. How do you quantify a programmer's skills, if both of them code in the same manner? Time spent?

3. Does this concept include the idea of getting it right, the first time?

4. Does this include variable names? XP :)

Of course, before a developer could even begin to do this, he would have to have a solid command of the language, and, of course, as humans we are not perfect, so do to this on a regular basis would be impossible.

However, I think it does say that there is always improvement in some aspects of code, which, when studied long enough, will allow for programs to run better if the necessary changes are made.

These are just my thoughts of course. We haven't explored the concept too much.