05.23.06

Sanity Program

Posted in ALPHA, Computer Science, Programming at 10:50 pm by Nick

Time: 1 hour
Total: 77 hours
————————–

With a tad bit of spurring from Wade I turned the code in my AIM info into a real working program. This inspired a friend of mine to attempt to write a “sanity program” in Ruby. The code itself seems almost poetic, though it accomplishes nothing. Interestingly enough the program actually reflects moods, and when I lower the homework/academic levels to lower (and currently unrealistic) numbers the program stays sane longer.

The original code is still within my info, though since it looks bad I will not post it here.

You can find the sanity program (executable and code) in its entirety at http://www.nick-cash.com/download/sanity

[edit]
After reviewing the code, there is one thing that I think definitely needs changing. I am referring to these two lines:

short percent_to_help_sanity = (NICKS_CARING_FRIENDS*10) > 100 ? 100 : (NICKS_CARING_FRIENDS*10);
short percent_to_hurt_sanity = 100-(percent_to_help_sanity/2);

According to the way it is written, the higher the help_sanity, the higher the hurt_sanity as well. That doesnt really make sense, though it worked before because it checks the sanity range first. A more accurate line for hurt_sanity would be:

short percent_to_hurt_sanity = 100-(100/(percent_to_help_sanity > 0 ? percent_to_help_sanity : 5)*2);

So, best case scenario, percent_to_hurt_sanity would be 98+. Worst case, 60+ would be hurt_sanity and helpping it would be impossible.

9 Comments »

  1. Nicholas E. May said,

    May 26, 2006 at 12:28 am

    *laugh* Awesome program, well, the messages in the source are funny. (OK, I suppose I should say “print” not “messages” since that’s the command in C++…I’m used to LISP, EMACS LISP. Free, not propriatary!)

    What I find interesting is the fact that the executable file is so much bigger than the source code file—it’s been my understanding that you compiled programs (or, in some cases, byte-compiled them) so that they would be smaller and would be faster…they’d be in machine-language, no longer human-readable and therefore optimized for the machines and, thus, one would think, smaller on the filesize. Rather long sentence there, what?

  2. Nicholas E. May said,

    May 26, 2006 at 12:32 am

    Oh, also, how would one go about compiling that for a *nix? Yipes, I feel so computer-illiterate right now!

  3. Odis said,

    May 26, 2006 at 10:16 am

    There is more that gets into an executable than just the code alone. The reason the print messages, the main function, and the other standard functions work is because the C library is built into it (which is rather large). Normally I use various ways to compress the exe’s, but UPX refused to compress the exe so I decided to post it with just compiler-side optimizations.

    As for compiling, you would need to copy the code from the page listed (don’t view source, its exported to support color) and paste it into an editor (pico/nano, vi, emacs, notepad/wordpad..). If you are on *nix, save it as a *.c file. Then hit the command “gcc filenamehere.c”. It should compile and produce an exe for you to run (probably named “a”).

    If you get errors about the getche() function you can change it to getchar() and it should work just fine. Once you get it compiled and working feel free to screw with it. Change the variables to fit your life and see how long your life can be kept sane in the program!

  4. Tim Treichel said,

    May 27, 2006 at 10:39 am

    My sanity has reached an all-time low and I’m not even in college right now? Genetics research must be more taxing than I thought :P

  5. Nicholas E. May said,

    June 2, 2006 at 8:53 pm

    You were right about getche(), but it worked fine once I did a query replace. I guess I thought the program was interactive, so it was’n as much fun as I thought it would be. Except now I get to go mucking about through source code! Hehehe.

  6. Odis said,

    June 3, 2006 at 12:40 pm

    You could make it fully autonomous and remove the getche()/getchar() calls. In general I made it so it would run quickly (rather than infinitely).

    One thing you might consider adding is a counter to see how many “Woot, sanity” prints you get.

  7. Nicholas E. May said,

    July 18, 2006 at 4:51 pm

    This inspired a friend of mine to attempt to write a “sanity program” in Ruby.

    Weeird. I had that idea last night and was just looking this post (and the code) over and tripped across that line—which I missed in my past readings (well, skimmings). I think it’ll be another week or two before I give it a go, though. Maybe you could include some license information with the code?

  8. Odis said,

    July 18, 2006 at 8:16 pm

    All code posted here is public domain unless otherwise noted.

  9. Odis said,

    July 19, 2006 at 6:14 pm

    Last night I converted the Sanity program (in C) to Ruby. I will post the code when I get a chance. I didn’t change too much, but you might be able to see how small-scale translating between languages works.

    Ruby shaved some 30 lines off the code, though the C version could be that much smaller depending on programmer preferences (I like my curly’s to have their own lines).

    I should also note that the Sanity program is an example of relatively bad programming. It uses a lot of defined constants, many of which could be avoided in favor of user input or file input.

Leave a Comment