The Meaning of Life

I’ve been known to give the meaning of life a bit of thought every so often. So I thought I’d take a stab at implementing it in Java.

This is a first cut.

public class Life {

  static final int ZERO = 0; // nothing is certain
  static final long ONE_THOUSAND_MINUS_ONE = 999; // omg
  static final long MAX_ATTEMPTS = ONE_THOUSAND_MINUS_ONE - 995;

  static int ATTEMPT_COUNT = 0; // nothing ventured
  static long SEED;

  boolean TRYING;
  boolean FAILED;
  boolean SUCCEEDED;
  boolean ALIVE;

  public static void main(String[] args) {

    Life life = new Life();

    jot(" THE MEANING OF LIFE\\n");

    //initialise the beginning of the beginning
    life.goRandom();

    //initialise the beginning of the life
    life.beBorn();

    // oh yes i gotta lotta livin' to do uh uh
    life.liveMacLongAndMacProsper();

    // finish him!
    life.die();

    jot("\\n --\\n");
}

  void beBorn() {
    goRandom();
    ALIVE = true;
  }

  void liveMacLongAndMacProsper() {

    // just a tiny subset of life here for now
    goRandom();
    while(event(ONE_THOUSAND_MINUS_ONE - 100)) {
      doStuff();
    }
  }

  public void doStuff() {
    goRandom();
    while(event(ONE_THOUSAND_MINUS_ONE - 100) &&
                ATTEMPT_COUNT < MAX_ATTEMPTS) {
      attempt();
    }
  }

  void attempt() {
    TRYING = true;
    ATTEMPT_COUNT++;
    jot(" we try ");
    if (ATTEMPT_COUNT < MAX_ATTEMPTS) {
      if(successful()) {
        SUCCEEDED = true;
        jot(" we succeed ");
      } else {
        SUCCEEDED = false;
        jot(" we fail ");
      }
    }
    TRYING = false;
  }

  void die() {
    if (ATTEMPT_COUNT == ZERO) {
      jot(" we didn't get a turn :( ");
    }
    ALIVE = false;

  }

  // this is where weird stuff happens
  // where a bee sting results in a symphony
  // where a tune triggers a memory
  // where the unrelated are related
  boolean event(long intervene) {
    goRandom();
    return SEED < intervene ? true: false;
  }

  boolean successful() {
    return SUCCEEDED = event(ONE_THOUSAND_MINUS_ONE - 800);
  }

  void goRandom() {
    SEED = Math.round(Math.random() * ONE_THOUSAND_MINUS_ONE);
    // jot("I=N " + SEED);
  }

  void goRandom(double intervene) {
    SEED = Math.round(Math.random() * intervene);
    // jot("I=Y " + SEED);
  }

  public static void jot(String string) {
    System.out.print(string);
  }

}
Subscribe Share
×

Subscribe

The latest articles sent straight to your inbox.