Computer Hope

Software => Computer programming => Topic started by: Dilbert on March 15, 2007, 01:16:40 AM

Title: I did it! Yay!
Post by: Dilbert on March 15, 2007, 01:16:40 AM
It's currently 12:08 at GMT-8 as I type this. :)

I needed to do a project for my Health class, and I was sick of the same ol' posters and crap. "Game" was on the list, and, well, the rest is history.

Long story short: I wrote a C++ command-line menu-driven game revolving around Suicide, the last topic of our unit. In it, the player attempts to talk some guy out of jumping off a tall building. I worried my parents greatly, because I've never finished a "real" C++ program to date.

How did I write a fully-fledged game (by 1990 standards, anyway) within a week, you ask? I'm almost ashamed to say it, but I nearly completely abandoned the concept of OOP. I wrote purely procedural code. I had a one-variable class out of habit, but actually, a global variable would have been easier for something of this small scale. I also stripped the program of all exceptions and error logic, since I'll be the one at the helm. :)

The game is actually simple enough for a one-page flow chart. Luckily, debugging is a small chore when all conceivable paths are marked. I'm proud to announce my first real program was a Gold-star program -- it compiled and ran to specifications perfectly the first time. (I'm not counting the delays in the couts I added later; I left them out at first intentionally for debugging purposes.)

^^^^I'm not sure whether to be proud or scared. What can I say, other than I'm done ahead of time and did a good job? Oh, maybe I should get to bed. :)
Title: Re: I did it! Yay!
Post by: Neil on March 15, 2007, 12:00:49 PM
Well done! You also learnt a valuable lesson: OOP is just one style of programming available. Each different situation requires a different style and with experiance you will be able to weigh up the pros and cons of each style. Sometimes it can be disastrous to force a particular style "just because".

For small simple projects, OOP can be more trouble than it's worth. Forcing OOP for them can be fine for learning purposes but when it comes to create a "real" (wasn't sure of the correct word to use) program, then consider the best style.

I'm still looking forward to your escape game! ;)
Title: Re: I did it! Yay!
Post by: Dilbert on March 22, 2007, 07:45:04 AM
I'd rather not use a "software creator" program to make programs. Not only is it "one step removed"* from actual programming, it's Visual Basic. There's nothing wrong with VB as a language, but it's extremely removed from Assembler, which causes unnecessary overhead.

Besides, anything that uses VB requires the .NET framework. Since we're talking 2006-07, it's probably the Framework 2.0. I'd rather not make other users download a 20-something MB thing just so my 400 KB EXE works. When you write in C/C++/Java/Assembler/alltheothers, no additional stuff is required -- it runs on its own.

*FrontPage is "one step removed" from writing HTML. The code it makes is a mess, and it usually causes only IE to render properly. Not my idea of fun.
Title: Re: I did it! Yay!
Post by: Neil on March 22, 2007, 11:15:45 AM
Learn how to use apostrophes.
Title: Re: I did it! Yay!
Post by: Dilbert on March 22, 2007, 04:55:40 PM
You mean DeltaSpider, right? If so, then add periods, commas and capital letters to the list. :)
Title: Re: I did it! Yay!
Post by: steelegbr on March 23, 2007, 06:50:39 AM
Quote
When you write in C/C++/Java/Assembler/alltheothers, no additional stuff is required -- it runs on its own.

I thought you needed at least the JRE to run Java programs / applets (~16-20Mb download from SUN). :)

Quote
*FrontPage is "one step removed" from writing HTML. The code it makes is a mess, and it usually causes only IE to render properly. Not my idea of fun.

100% accurate there. I'll take notepad over it any day. When you use any WYSIWYG system for programming or websites it usually makes it 100x harder to maintain (and understand the code) than if you went away and learned the HTML/Java/C(++)/etc...
Title: Re: I did it! Yay!
Post by: Dilbert on March 23, 2007, 08:26:19 AM
*Reads up on Java

Oops! Oh well, the rest of my statement stands. :)

As for WYSIWYG, Dreamweaver is fairly good at not totally botching up the code it makes. It's still hard to maintain, though. :) And I'd never use a C++ code generator; the mess of code it makes would be a nightmare.

Side-Note: Do you know about the old compilers that would try to correct programmer's mistakes at compile time, so stupid things like forgetting a ; at the end of a line or missing a ) or two wouldn't cause a program that takes 5 minutes to compile to spit back an error instead of linking.

It was a noble quest, but like most of mankind's projects it went way too far. One infamous example is a compiler that someone typed only one word into: "if". No includes, no "int main()", nothing. Would you believe that it created a 200-line command-line which successfully compiled and ran? ;D
Title: Re: I did it! Yay!
Post by: steelegbr on March 23, 2007, 08:59:47 AM
Quote
Side-Note: Do you know about the old compilers that would try to correct programmer's mistakes at compile time, so stupid things like forgetting a ; at the end of a line or missing a ) or two wouldn't cause a program that takes 5 minutes to compile to spit back an error instead of linking.

Now if only you could do that with javac.... Then I wouldn't get pages of console text telling me I missed a couple of ;s. It ticks you off enough to check what you type before sending it to the "compiler". Especially when you have to ssh it to the server first as it's running a different version of Java.
Title: Re: I did it! Yay!
Post by: CBMatt on March 28, 2007, 03:57:51 AM
Sheesh, a suicide negotiator game?  I think I lose by default.
Title: Re: I did it! Yay!
Post by: Dilbert on March 28, 2007, 10:29:58 AM
Nah, a monkey could beat it. You see, as long as the Suicide Meter is below 50% after five moves, you win a technical victory. It's really not hard at all, but it does include a lot of the things we covered in the Suicide unit at school, and it's playable and, for about a minute or so, interesting to play. Which is why I received an A+ stamped on the printed source code. (It's kinda hard to write the grade on a file...)

For anyone interested, here's the program and the source code. Note that I didn't comment a single line, which is completely out of character for me, I assure you. The only reason for that is that I didn't ever... EVER... want to return to this program and modify it further.

Only other thing to mention is that the dialog is rather cheesy. But hey, a week deadline is an hour or so of creativity. ;D

*grumble grumble* stupid 128KB attach limit... you'll have to compile the source code with Dev-C++ (http://www.bloodshed.net/devcpp.html) or a similar C++ compiler. But the compiled game is 130 KB -- 2 over the limit. :-/

[old attachment deleted by admin]
Title: Re: I did it! Yay!
Post by: Neil on March 28, 2007, 12:12:46 PM
You need this:

http://upx.sourceforge.net/

It's so amazing. I got a game, with graphics included in the exe, to 114kb. And the guy on the other end doesn't need to do anything or have anything. He just runs like normal.

Edit: On my compiler it is 68kb, and 16kb after upx.
Title: Re: I did it! Yay!
Post by: Dilbert on March 28, 2007, 12:22:27 PM
I wonder if I can attach a .7z file...

Sweet! 104 KB! ;D

That's amazing... the uncompressed is 400+ KB... (pre-UPX and 7-zip) :o

[old attachment deleted by admin]
Title: Re: I did it! Yay!
Post by: Calum on March 28, 2007, 12:26:06 PM
I like that game.
Nice job there.
Title: Re: I did it! Yay!
Post by: Dilbert on March 28, 2007, 12:26:42 PM
Thank you! ;D
Title: Re: I did it! Yay!
Post by: Neil on March 30, 2007, 06:34:29 AM
Again, I got it down to 16kb so I still win. :P
Title: Re: I did it! Yay!
Post by: Dilbert on March 30, 2007, 10:49:38 AM
What compiler?
Title: Re: I did it! Yay!
Post by: Neil on March 30, 2007, 12:23:08 PM
C++ 2005 Express Edition. But I sent it to my friend without a compiler installed, and it would not run, so I might not have made a "full" compile or something. Check the link I sent you. It is an excellent tool to greatly shrink the size of your compiled exes and does not require anything special for people receiving your program.
Title: Re: I did it! Yay!
Post by: Dilbert on March 30, 2007, 12:31:40 PM
I did use it. Before UPX: 481 KB. After the best UPX had: 283 KB. Packing that in a .7z file with Ultra compression: 105 KB. That's it; it's compressed and compressed again.
Title: Re: I did it! Yay!
Post by: Dilbert on March 30, 2007, 10:57:07 PM
I'm still looking forward to your escape game! ;)

I've begun it again, the right way. I went to a C++ forum and got some pointers. I then realized how stupid my mindset was. I am writing OOP -- true OOP -- and *censored* it, it feels good!

Here's what I did in earlier attempts:

I created a class timsBedroom class. All usable objects were variables, BOOL for if they were in possession, and possibly an int or two. All, mind you, in a timsBedroom class. Imagine how silly this looks for a moment. In a timsBedroom object, I'm wondering about whether the reading light from that room is in player's possession, and this BOOL is next to the BOOL that asks whether the door is closed or not!

I was on the right track, but it wasn't it. Then, I learned about member classes. It clicked. Now, I've got a bedroom object (of which timsBedroom will be an instance) with a desk object with computer and reading light child objects... just like a forum. Main boards(classes), in which are some threads (variables) and child boards (child classes) with threads (variables) of their own. Just found the term in my book: I'm referring to aggregation, the has-a relationship. :)
Title: Re: I did it! Yay!
Post by: Neil on March 31, 2007, 05:25:54 AM
Hee hee well done! I find it's a bit odd you can nest classes and not functions but oh well! Once you've finished, consider redesigned so your game has its own script read from a text file. Then your game will be truely generic, rather than a hardcoded story. Like I noticed in your suicide game, you've basically hardcoded all the conversation into functions. I would suggest some kind of "event\room\scene" class, containing an id number, string for the room's description and choices, and then a list of choices which lead to other ids (for moving) of affect variables, for picking up and stuff.
Title: Re: I did it! Yay!
Post by: Dilbert on March 31, 2007, 11:09:27 AM
You mean, allow users to edit the story file, by having the program read from text? That'd be good. Heck, it'd save on space in the actual .cpp file. *Starts looking up text file parsing tutorials

As for the aggregation, I'm not nesting classes, I'm doing something like this:

Code: [Select]
class A
{
public:
    A();
    ~A();
    //Other declarations
};

class B
{
public:
    //Stuff
private:
    A theA;
};

This way, I can define a class Door, and have all rooms in the house have doors:

Code: [Select]
class LargeBedroom
{
    //Other
private:
    door toLivingRoom, toMasterBath, toCloset;
};

Now, I can use these classes in multiple areas, multiple times. :)