robotfindskitten port

Post your NXJ projects, project ideas, etc here!

Moderators: imaqine, 99jonathan, roger

New User
Posts: 7
Joined: Wed May 19, 2010 11:23 pm

robotfindskitten port

Postby xpansive » Thu May 20, 2010 10:57 pm

I've been working on porting robotfindskitten for a couple days now.

This will probably be the final version.

Here's the full code listing (it's very big :D):

Code: Select all

Port of robotfindskitten to the Lego Mindstorms NXT using leJOS by XPANSiVE.

Compile with:
nxj -r rfk

import lejos.nxt.*;
import javax.microedition.lcdui.*;
import lejos.util.Delay;

class rfk

   static final String messages[] =
   { "\"I pity the fool who mistakes me for kitten!\", sez Mr. T.", "That's just an old tin can.",
         "It's an altar to the horse god.", "A box of dancing mechanical pencils. They dance! They sing!",
         "It's an old Duke Ellington record.", "A box of fumigation pellets.", "A digital clock. It's stuck at 2:17 PM.",
         "That's just a charred human corpse.", "I don't know what that is, but it's not kitten.",
         "An empty shopping bag. Paper or plastic?", "Could it be... a big ugly bowling trophy?",
         "A coat hanger hovers in thin air. Odd.", "Not kitten, just a packet of Kool-Aid(tm).",
         "A shameless plug for Rockbox:", "A freshly-baked pumpkin pie.",
         "A lone, forgotten comma, sits here, sobbing.", "ONE HUNDRED THOUSAND CARPET FIBERS!!!!!",
         "It's Richard Nixon's nose!", "It's Lucy Ricardo. \"Aaaah, Ricky!\", she says.",
         "You stumble upon Bill Gates' stand-up act.", "Just an autographed copy of the Kama Sutra.",
         "It's the Will Rogers Highway. Who was Will Rogers, anyway?",
         "It's another robot, more advanced in design than you but strangely immobile.",
         "Leonard Richardson is here, asking people to lick him.", "It's a stupid mask, fashioned after a beagle.",
         "Your State Farm Insurance(tm) representative!", "It's the local draft board.",
         "Seven 1/4\" screws and a piece of plastic.", "An 80286 machine.",
         "One of those stupid \"Homes of the Stars\" maps.",
         "A signpost saying \"TO KITTEN\". It points in no particular direction.",
         "A hammock stretched between a tree and a volleyball pole.", "A Texas Instruments of Destruction calculator.",
         "It's a dark, amphorous blob of matter.", "Just a pincushion.", "Oh hey, you're not Llorean!",
         "It's a mighty zombie talking about some love and prosperity.",
         "\"Dear robot, you may have already won our 10 MILLION DOLLAR prize...\"", "It's just an object.",
         "A mere collection of pixels.", "A badly dented high-hat cymbal lies on its side here.", "A marijuana brownie.",
         "A plush Chewbacca.", "Daily hunger conditioner from Australasia", "Just some stuff.",
         "Why are you touching this when you should be finding kitten?", "A glorious fan of peacock feathers.",
         "It's some compromising photos of Babar the Elephant.",
         "A copy of the Weekly World News. Watch out for the chambered nautilus!", "It's the proverbial wet blanket.",
         "A \"Get Out of Jail Free\" card.", "An incredibly expensive \"Mad About You\" collector plate.",
         "Paul Moyer's necktie.", "A haircut and a real job. Now you know where to get one!",
         "An automated robot-hater. It frowns disapprovingly at you.", "An automated robot-liker. It smiles at you.",
         "It's a black hole. Don't fall in!", "It's a Toshiba MK8022GAA hard drive, untested, as-is.",
         "Just a big brick wall.", "You found kitten! No, just kidding.", "Heart of Darkness brand pistachio nuts.",
         "A smoking branding iron shaped like a 24-pin connector.", "It's a Java applet.", "An abandoned used-car lot.",
         "A shameless plug for Crummy:",
         "A shameless plug for the UCLA Linux Users Group:", "A can of Spam Lite.",
         "This is another fine mess you've gotten us into, Stanley.", "It's scenery for \"Waiting for Godot\".",
         "This grain elevator towers high above you.", "A Mentos wrapper.", "It's the constellation Pisces.",
         "It's a fly on the wall. Hi, fly!", "This kind of looks like kitten, but it's not.", "It's a banana! Oh, joy!",
         "A helicopter has crashed here.", "Carlos Tarango stands here, doing his best impression of Pat Smear.",
         "A patch of mushrooms grows here.", "It's a song by Kraftwerk, all about robots.",
         "A patch of grape jelly grows here.", "A spindle, and a grindle, and a bucka-wacka-woom!",
         "A geyser sprays water high into the air.", "A toenail? What good is a toenail?",
         "You've found the fish! Not that it does you much good in this game.", "A Buttertonsils bar.",
         "One of the few remaining discoes.", "Ah, the uniform of a Revolutionary-era minuteman.",
         "A punch bowl, filled with punch and lemon slices.", "It's nothing but a G-thang, baby.",
         "IT'S ALIVE! AH HA HA HA HA!", "This was no boating accident!",
         "Wait! This isn't the poker chip! You've been tricked! DAMN YOU, MENDEZ!", "A livery stable! Get your livery!",
         "It's a perpetual immobility machine.", "\"On this spot in 1962, Henry Winkler was sick.\"",
         "There's nothing here; it's just an optical illusion.", "The World's Biggest Motzah Ball!",
         "A tribe of cannibals lives here. They eat Malt-O-Meal for breakfast, you know.",
         "This appears to be a rather large stack of trashy romance novels.", "Look out! Exclamation points!",
         "A herd of wild coffee mugs slumbers here.", "It's a limbo bar! How low can you go?",
         "It's the horizon. Now THAT'S weird.", "A vase full of artificial flowers is stuck to the floor here.",
         "A large snake bars your way.", "A pair of saloon-style doors swing slowly back and forth here.",
         "It's an ordinary bust of Beethoven... but why is it painted green?",
         "It's TV's lovable wisecracking Crow! \"Bite me!\", he says.",
         "Hey, look, it's war. What is it good for? Absolutely nothing. Say it again.",
         "It's the amazing self-referential thing that's not kitten.",
         "A flamboyant feather boa. Now you can dress up like Carol Channing!",
         "\"Sure hope we get some rain soon,\" says Farmer Joe.",
         "\"How in heck can I wash my neck if it ain't gonna rain no more?\" asks Farmer Al.",
         "\"Topsoil's all gone, ma,\" weeps Lil' Greg.",
         "This is a large brown bear. Oddly enough, it's currently peeing in the woods.",
         "A team of arctic explorers is camped here.", "This object here appears to be Louis Farrakhan's bow tie.",
         "This is the world-famous Chain of Jockstraps.", "A trash compactor, compacting away.",
         "This toaster strudel is riddled with bullet holes!", "A capsaicin molecule.",
         "It's a hologram of a crashed helicopter.",
         "This is a television. On screen you see a robot strangely similar to yourself.",
         "This balogna has a first name, it's R-A-N-C-I-D.", "A salmon hatchery? Look again. It's merely a single salmon.",
         "It's a rim shot. Ba-da-boom!", "It's creepy and it's kooky, mysterious and spooky. It's also somewhat ooky.",
         "This is an anagram.", "This object is like an analogy.",
         "It's a symbol. You see in it a model for all symbols everywhere.", "The object pushes back at you.",
         "A traffic signal. It appears to have been recently vandalized.",
         "\"There is no kitten!\" cackles the old crone. You are shocked by her blasphemy.",
         "This is a Lagrange point. Don't come too close now.", "The dirty old tramp bemoans the loss of his harmonica.",
         "Look, it's Fanny the Irishman!", "What in blazes is this?",
         "It's the instruction manual for a previous version of this game.",
         "A brain cell. Oddly enough, it seems to be functioning.", "Tea and/or crumpets.",
         "This jukebox has nothing but Cliff Richards albums in it.", "It's a Quaker Oatmeal tube, converted into a drum.",
         "This is a remote control. Being a robot, you keep a wide berth.", "It's a roll of industrial-strength copper wire.",
         "Oh boy! Grub! Er, grubs.", "A puddle of mud, where the mudskippers play.", "Plenty of nothing.",
         "Look at that, it's the Crudmobile.", "Just Walter Mattheau and Jack Lemmon.", "Two crepes, two crepes in a box.",
         "An autographed copy of \"Primary Colors\", by Anonymous.", "Another rabbit? That's three today!",
         "It's a segmentation fault. Core dumped, by the way.",
         "A historical marker showing the actual location of /dev/null.", "Thar's Mobius Dick, the convoluted whale. Arrr!",
         "It's a charcoal briquette, smoking away.", "A pizza, melting in the sun.",
         "It's a \"HOME ALONE 2: Lost in New York\" novelty cup.", "A stack of 7 inch floppies wobbles precariously.",
         "It's nothing but a corrupted floppy. Coaster anyone?",
         "A section of glowing phosphor cells sings a song of radiation to you.", "This TRS-80 III is eerily silent.",
         "A toilet bowl occupies this space.", "This peg-leg is stuck in a knothole!", "It's a solitary vacuum tube.",
         "This corroded robot is clutching a mitten.", "\"Hi, I'm Anson Williams, TV's 'Potsy'.\"",
         "This subwoofer was blown out in 1974.", "Three half-pennies and a wooden nickel.",
         "It's the missing chapter to \"A Clockwork Orange\".", "It's a burrito stand flyer. \"Taqueria El Ranchito\".",
         "This smiling family is happy because they eat LARD.", "Roger Avery, persona un famoso de los Estados Unidos.",
         "Ne'er but a potted plant.", "A parrot, kipping on its back.", "A forgotten telephone switchboard.",
         "It's a trio of Swedish hackers. They seem to be busy with that MP3 player.",
         "A forgotten telephone switchboard operator.", "It's an automated robot-disdainer. It pretends you're not there.",
         "It's a portable hole. A sign reads: \"Closed for the winter\".", "Just a moldy loaf of bread.",
         "A little glass tub of Carmex. ($.89) Too bad you have no lips.",
         "A Swiss-Army knife. All of its appendages are out. (toothpick lost)",
         "It's a zen simulation, trapped within an ASCII character.", "It's a copy of \"The Rubaiyat of Spike Schudy\".",
         "It's \"War and Peace\" (unabridged, very small print).",
         "A willing, ripe tomato bemoans your inability to digest fruit.", "A robot comedian. You feel amused.",
         "It's KITT, the talking car.", "Here's Pete Peterson. His batteries seem to have long gone dead.",
         "\"Blup, blup, blup\", says the mud pot.", "More grist for the mill.",
         "Cory Doctorow's hot air balloon is tethered here.", "Grind 'em up, spit 'em out, they're twigs.",
         "The boom box cranks out an old Ethel Merman tune.",
         "It's \"Finding kitten\", published by O'Reilly and Associates.", "Pumpkin pie spice.",
         "It's the Bass-Matic '76! Mmm, that's good bass!", "\"Lend us a fiver 'til Thursday\", pleas Andy Capp.",
         "It's a tape of '70s rock. All original hits! All original artists!",
         "You've found the fabled America Online disk graveyard!", "Empty jewelboxes litter the landscape.",
         "It's the astounding meta-object.", "Ed McMahon stands here, lost in thought. Seeing you, he bellows, \"YES SIR!\"",
         "...thingy???", "It's 1000 secrets the government doesn't want you to know!", "The letters O and R.",
         "A magical... magic thing.", "It's a moment of silence.", "It's Sirhan-Sirhan, looking guilty.",
         "It's \"Chicken Soup for the Kitten-seeking Soulless Robot.\"", "It is a set of wind-up chatter teeth.",
         "It is a cloud shaped like an ox.", "You see a snowflake here, melting slowly.", "It's 91 yards of twine.",
         "It's a big block of ice. Something seems to be frozen inside it.", "Vladimir Lenin's casket rests here.",
         "It's a copy of \"Zen and The Art of Robot Maintenance\".", "This invisible box contains a pantomime horse.",
         "A mason jar lies here open. It's label reads: \"do not open!\".", "A train of thought chugs through here.",
         "This jar of pickles expired in 1957.", "Someone's identity disk lies here.", "\"Yes!\" says the bit.",
         "\"No!\" says the bit.", "A dodecahedron bars your way.", "Mr. Hooper is here, surfing.", "It's a big smoking fish.",
         "You have new mail in /var/spool/robot", "Just a monitor with the blue element burnt out.",
         "A pile of coaxial plumbing lies here.", "It's a rotten old shoe.", "It's a hundred-dollar bill.",
         "It's a Dvorak keyboard.", "It's Bender, the loveable kleptomaniac robot! \"Bite my shiny metal ass!\"",
         "It's a cardboard box full of 8-tracks.", "Just a broken hard drive containg the archives of Nerth Pork.",
         "A broken metronome sits here, it's needle off to one side.", "A sign reads: \"Go home!\"",
         "A sign reads: \"No robots allowed!\"", "It's the handheld robotfindskitten game, by Tiger.",
         "This particular monstrosity appears to be ENIAC.", "This is a tasty-looking banana creme pie.",
         "A wireframe model of a hot dog rotates in space here.", "Just the empty husk of a locust.",
         "You disturb a murder of crows.", "It's a copy of the robotfindskitten EULA.", "It's Death.",
         "It's an autographed copy of \"Secondary Colors,\" by Bob Ross.",
         "It is a marzipan dreadnought that appears to have melted and stuck.",
         "It's a DVD of \"Crouching Monkey, Hidden Kitten\", region encoded for the moon.",
         "It's Kieran Hervold.  Damn dyslexia!", "A non-descript box of crackers.",
         "Carbonated Water, High Fructose Corn Syrup, Color, Phosphoric Acid, Flavors, Caffeine.",
         "\"Move along! Nothing to see here!\"", "It's the embalmed corpse of Vladimir Lenin.",
         "A coupon for one free steak-fish at your local family diner.", "A set of keys to a 2001 Rolls Royce. Worthless.",
         "A gravestone stands here.  \"Izchak Miller, ascended.\"", "Someone has written \"ad aerarium\" on the ground here.",
         "A large blue eye floats in midair.", "This appears to be a statue of Perseus.", "There is an opulent throne here.",
         "It's a squad of Keystone Kops.", "This seems to be junk mail addressed to the finder of the Eye of Larn.",
         "A wondrous and intricate golden amulet.  Too bad you have no neck.",
         "The swampy ground around you seems to stink with disease.",
         "An animate blob of acid.  Being metallic, you keep well away.",
         "It's a copy of Knuth with the chapter on kitten-search algorithms torn out.",
         "A crowd of people, and at the center, a popular misconception.",
         "It's a blind man. When you touch, he exclaims \"It's a kitten prospecting robot!\"",
         "It's a lost wallet. It's owner didn't have pets, so you discard it.",
         "This place is called Antarctica. There is no kitten here.", "It's a mousetrap, baited with soap.",
         "A book with \"Don't Panic\" in large friendly letters across the cover.", "A compendium of haiku about metals.",
         "A discredited cosmology, relic of a bygone era.", "A hollow voice says \"Plugh\".",
         "A knight who says \"Either I am an insane knave, or you will find kitten.\"",
         "The secret meeting place of the Knights of the Lambda Calculus.",
         "A neural net -- maybe it's trying to recognize kitten.", "A screwdriver.",
         "A statue of a girl holding a goose like the one in Gottingen, Germany.", "A tetradrachm dated \"42 B.C.\"",
         "A voice booms out \"Onward, kitten soldiers...\"", "An eminently forgettable zahir.",
         "Apparently, it's Edmund Burke.", "For a moment, you feel something in your hands, but it disappears!",
         "Here is a book about Robert Kennedy.", "Hey, robot, leave those lists alone.",
         "Ho hum.  Another synthetic a posteriori.", "It's Asimov's Laws of Robotics.  You feel a strange affinity for them.",
         "It's Bach's Mass in B-minor!", "It's a bug.", "It's a synthetic a priori truth!  Immanuel would be so pleased!",
         "It's the Tiki Room.", "Just some old play by a Czech playwright, and you can't read Czech.",
         "Kitten is the letter 'Q'.  Oh, wait, maybe not.", "Quidquid Latine dictum sit, kitten non est.",
         "Sutro Tower is visible at some distance through the fog.", "The Digital Millennium Copyright Act of 1998.",
         "The United States Court of Appeals for the Federal Circuit.",
         "The non-kitten item like this but with \"false\" and \"true\" switched is true.",
         "The non-kitten item like this but with \"true\" and \"false\" switched is false.",
         "This is the chapter called \"A Map of the Cat?\" from Feynman's autobiography.", "This is the forest primeval.",
         "Werner's \"Pocket Field Guide to Things That Are Not Kitten\".", "You found nettik, but that's backwards.",
         "You have found some zinc, but you must not stop here, for you must find kitten.",
         "\"50 Years Among the Non-Kitten Items\", by Ann Droyd.",
         "\"Robot may not injure kitten, or, through inaction, ...\"",
         "\"Address Allocation for Private Internets\" by Yakov Rekhter et al.",
         "\"Mail Routing and the Domain System\" by Craig Partridge.",
         "\"The Theory and Practice of Oligarchical Collectivism\" by Emmanuel Goldstein.",
         "\"201 Kitten Verbs, Fully Conjugated\".  You look for \"find\".",
         "A card shark sits here, practicing his Faro shuffle.  He ignores you.",
         "A copy of DeCSS.  They're a dime a dozen these days.",
         "A demonic voice proclaims \"There is no kitten, only Zuul\".  You flee.", "A lotus.  You make an interesting pair.",
         "It's the missing 24 seconds of \"Monty Python and the Holy Grail\".",
         "A milk carton, with a black and white picture of kitten on the side.",
         "Any ordinary robot could see from a mile away that this wasn't kitten.",
         "A stegosaurus, escaped from the stegosaurusfindsrobot game.  It finds you.", "Baling wire and chewing gum.",
         "Chewing gum and baling wire.", "Here is no kitten but only rock, rock and no kitten and the sandy road.",
         "Hey, I bet you thought this was kitten.", "It is an ancient mariner, and he stoppeth one of three.",
         "It pleases you to be kind to what appears to be kitten -- but it's not!",
         "It's a blatant plug for Ogg Vorbis,", "It's a business plan for a new startup,",
         "It's a revised business plan for a new startup,", "It's a square.",
         "It seems to be a copy of \"A Tail of Two Kitties\".", "It's the Donation of Constantine!",
         "It's this message, nothing more.", "Lysine, an essential amino acid.  Well, maybe not for robots.",
         "No kitten here.", "The score for a Czech composer's \"Kitten-Finding Symphony in C\".",
         "This looks like Bradley's \"Appearance and Reality\", but it's really not.", "This non-kitten item no verb.",
         "You feel strangely unfulfilled.", "You hit the non-kitten item.  The non-kitten item fails to yowl.",
         "You suddenly yearn for your distant homeland.",
         "You've found the snows of yesteryear!  So that's where they all went to.",
         "Approaching.  One car.  J.  Followed by.  Two car.  M, M.  In five. Minutes.", "Free Jon Johansen!",
         "Free Dmitry Sklyarov!", "One person shouts \"What do we want?\" The crowd answers \"Free Dmitry!\"",
         "Judith Platt insults librarians.", "This map is not the territory.",
         "\"Go back to Libraria!\", says Pat Schroeder.", "This is a porcelain kitten-counter.  0, 0, 0, 0, 0...",
         "An old bootable business card, unfortunately cracked down the middle.",
         "A kitten sink, for washing kitten (if only kitten liked water).", "A kitten source (to match the kitten sink).",
         "If it's one thing, it's not another.", "If it's not one thing, it's another.", "A caboodle.", "A grin.",
         "A hedgehog.  It looks like it knows something important.", "You've found... Oh wait, that's just a cat.",
         "Robot should not be touching that.", "Air Guitar!!!  NA na NA na!!",
         "An aromatherapy candle burns with healing light.", "You find a bright shiny penny.", "It's a free Jon Johansen!",
         "It's a free Dmitry Sklyarov!", "The rothe hits!  The rothe hits!",
         "It's an Internet chain letter about sodium laureth sulfate.", "Ed Witten sits here, pondering string theory.",
         "Something is written here in the dust.  You read: \"rJbotf ndQkttten\".",
         "We wish you a merry kitten, and a happy New Year!", "Run away!  Run away!",
         "You can see right through this copy of Brin\'s \"Transparent Society\".",
         "This copy of \"Steal This Book\" has been stolen from a bookstore.", "It's Roya Naini.",
         "This kit is the fourteenth in a series of kits named with Roman letters.",
         "This is the tenth key you've found so far.",
         "You find a fraud scheme in which loans are used as security for other loans.",
         "It's the phrase \"and her\", written in ancient Greek.",
         "It's the author of \"Randomness and Mathematical Proof\".", "It's the crusty exoskeleton of an arthropod!",
         "It's Emporer Shaddam the 4th's planet!",
         "It's the triangle leg adjacent to an angle divided by the leg opposite it.",
         "It's a bottle of nail polish remover.", "You found netkit! Way to go, robot!",
         "It's the ASCII Floating Head of Seth David Schoen!", "A frosted pink party-cake, half eaten.",
         "A bitchin' homemade tesla coil.", "Conan O'Brian, sans jawbone.",
         "It's either a mirror, or another soulless kitten-seeking robot.",
         "Preoccupation with finding kitten prevents you from investigating further.",
         "Fonzie sits here, mumbling incoherently about a shark and a pair of waterskis.",
         "The ghost of your dance instructor, his face a paper-white mask of evil.",
         "A bag of groceries taken off the shelf before the expiration date.",
         "A book: Feng Shui, Zen: the art of randomly arranging items that are not kitten.",
         "This might be the fountain of youth, but you'll never know.", "Tigerbot Hesh.", "Stimutacs.",
         "A canister of pressurized whipped cream, sans whipped cream.", "The non-kitten item bites!",
         "A chain hanging from two posts reminds you of the Gateway Arch.",
         "A mathematician calculates the halting probability of a Turing machine.",
         "A number of short theatrical productions are indexed 1, 2, 3, ... n.", "A technical university in Australia.",
         "It is -- I just feel something wonderful is about to happen.", "It's a Cat 5 cable.", "It's a U.S. president.",
         "It's a piece of cloth used to cover a stage in between performances.", "The ionosphere seems charged with meaning.",
         "This tomography is like, hella axial, man!", "It's your favorite game -- robotfindscatan!",
         "Just a man selling an albatross.", "The intermission from a 1930s silent movie.", "It's an inverted billiard ball!",
         "The spectre of Sherlock Holmes wills you onwards.", };

   // Constants for our internal representation of the screen.
   static final int EMPTY = -1;
   static final int ROBOT = 0;
   static final int KITTEN = 1;

   // Screen dimensions
   static final int X_MIN = 0;
   static final int X_MAX = ((LCD.SCREEN_WIDTH / LCD.CELL_WIDTH) - 1);
   static final int Y_MIN = 2;
   static final int Y_MAX = ((LCD.SCREEN_HEIGHT / LCD.CELL_HEIGHT) - 1);

   // Kitten and Robot
   static screen_object kitten = new screen_object();
   static screen_object robot = new screen_object();

   // Bogus object related stuff
   static final int NUM_BOGUS = 15;
   static screen_object[] bogus = new screen_object[NUM_BOGUS];
   static int[] bogus_messages = new int[NUM_BOGUS];
   static boolean[] used_messages = new boolean[messages.length];

   // Our internal representation of the screen
   static int[][] screen = new int[X_MAX + 1][Y_MAX + 1];

   static final int INPUT_NONE = -1;
   static boolean exit_rfk;

   // scrolling text thread stuff
   static String message;
   static boolean needUpdate = false;

   // Row constants for the animation
   static final int ADV_ROW = 4;
   static final int ANIMATION_MEET = X_MAX / 2 + 1;
   static final int ANIMATION_LENGTH = X_MAX / 2;

   static void draw(screen_object o)
      LCD.drawChar(o.character, o.x * LCD.CELL_WIDTH, o.y * LCD.CELL_HEIGHT, false);

   static void message(String s)
      needUpdate = true;
      message = s;

   static void play_game()
      int old_x = robot.x;
      int old_y = robot.y;

      int input = INPUT_NONE;

      while (!exit_rfk)

         // Process that input!

         if (!(old_x == robot.x && old_y == robot.y))
            // Erase the old robot
            LCD.drawChar(' ', old_x * LCD.CELL_WIDTH, old_y * LCD.CELL_HEIGHT, false);
            screen[old_x][old_y] = EMPTY;

            // Draw the new one
            screen[robot.x][robot.y] = ROBOT;

            old_x = robot.x;
            old_y = robot.y;

         input = Button.waitForPress();

   // Process input
   static void process_input(int input)
      int check_x = robot.x;
      int check_y = robot.y;

      switch (input)
      case Button.ID_ENTER:

      case Button.ID_ESCAPE:

      case Button.ID_LEFT:

      case Button.ID_RIGHT:

      // check for things not on the screen
      if (check_y < Y_MIN || check_y > Y_MAX || check_x < X_MIN || check_x > X_MAX)
         return; // Nothing at all!

      // Collision checking!
      if (screen[check_x][check_y] != EMPTY)
         switch (screen[check_x][check_y])
         case ROBOT:
            // Something strange happened.

         case KITTEN: // KITTEN!!!!!!!!!!!!11

         default: // Bogus object; print its message.
            message(messages[bogus_messages[screen[check_x][check_y] - 2]]);

      // Otherwise, move the robot.
      robot.x = check_x;
      robot.y = check_y;

   static char randchar()
      return (char) rand(33, 126);

   static int rand(int start, int end)
      return start + (int) (Math.random() * ((end - start) + 1));

   static boolean validchar(char a)
      switch (a)
      case '#':
      case ' ':
      case 127:
         return false;
      return true;

   static void init_arrays()
      int counter, counter2;
      screen_object empty = new screen_object();

      // Initialise the empty object
      empty.x = -1;
      empty.y = -1;
      empty.character = ' ';

      for (counter = 0; counter <= X_MAX; counter++)
         for (counter2 = 0; counter2 <= Y_MAX; counter2++)
            screen[counter][counter2] = EMPTY;

      // Initialise bogus related arrays
      for (counter = 0; counter < messages.length; counter++)
         used_messages[counter] = false;
      for (counter = 0; counter < NUM_BOGUS; counter++)
         bogus[counter] = empty;
         bogus_messages[counter] = 0;

   // I wonder what this does...
   static void init_robot()
      // Assign a random position to the player.
      robot.x = rand(X_MIN, X_MAX);
      robot.y = rand(Y_MIN, Y_MAX);

      robot.character = '#';
      screen[robot.x][robot.y] = ROBOT;

   static void init_kitten()
      // Assign kitten a random position
         kitten.x = rand(X_MIN, X_MAX);
         kitten.y = rand(Y_MIN, Y_MAX);
      while (screen[kitten.x][kitten.y] != EMPTY);

         kitten.character = randchar();
      while (!(validchar(kitten.character)));
      screen[kitten.x][kitten.y] = KITTEN;

   static void init_bogus()
      int counter, index;
      for (counter = 0; counter < NUM_BOGUS; counter++)
         // Initialise the object
         bogus[counter] = new screen_object();

         // Give it a character.
            bogus[counter].character = randchar();
         while (!(validchar(bogus[counter].character)));

         // Give it a position.
            bogus[counter].x = rand(X_MIN, X_MAX);
            bogus[counter].y = rand(Y_MIN, Y_MAX);
         while (screen[bogus[counter].x][bogus[counter].y] != EMPTY);

         screen[bogus[counter].x][bogus[counter].y] = counter + 2;

         // Find a message for this object.
            index = rand(0, messages.length);
         while (used_messages[index] != false);
         bogus_messages[counter] = index;
         used_messages[index] = true;

   static void init_screen()
      int counter;

      // Draw a line across the screen.
      Graphics g = new Graphics();

      // Draw all the objects on the playing field.
      for (counter = 0; counter < NUM_BOGUS; ++counter)


   static void play_animation(int input)
      int counter;
      screen_object left = new screen_object();
      screen_object right = new screen_object();

      // The grand cinema scene.

      if (input == Button.ID_RIGHT || input == Button.ID_ESCAPE)
         left = robot;
         right = kitten;
         left = kitten;
         right = robot;
      left.y = ADV_ROW;
      right.y = ADV_ROW;

      for (counter = ANIMATION_LENGTH; counter > 0; counter--)
         // Clear the previous position (empty the first time)
         LCD.drawChar(' ', (left.x - 1) * LCD.CELL_WIDTH, (left.y) * LCD.CELL_HEIGHT, false);
         LCD.drawChar(' ', (right.x + 1) * LCD.CELL_WIDTH, (right.y) * LCD.CELL_HEIGHT, false);
      message("You found kitten! Way to go, robot!");

   public static void main(String[] args)
      // Set the button sounds
      Button.setKeyClickTone(Button.ID_ENTER, 300);
      Button.setKeyClickTone(Button.ID_ESCAPE, 200);
      Button.setKeyClickTone(Button.ID_LEFT, 250);
      Button.setKeyClickTone(Button.ID_RIGHT, 250);

      // Show the title screen




      // Start the thread that will show the message, scrolling
      MessageDisplay m = new MessageDisplay();

      // Show the instructions
      message("In this game, you are robot (#). Your job is to find kitten. This task is complicated by the existance of various things which are not kitten. Robot must touch items to determine if they are kitten or not. The game ends when robotfindskitten.");

      // Play the game!

   private static void title()
      Graphics g = new Graphics();

      // show a logo
      g.drawImage(new Image(69, 48, new byte[]
      { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0,
            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x3f, (byte) 0x3f, (byte) 0x3f, (byte) 0x3f, (byte) 0x3f,
            (byte) 0x3f, (byte) 0x3f, (byte) 0x3f, (byte) 0x3f, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x3f,
            (byte) 0x3f, (byte) 0x3f, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0,
            (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0,
            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xf0,
            (byte) 0xf0, (byte) 0xf0, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
            (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff,
            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0,
            (byte) 0xf0, (byte) 0x03, (byte) 0x03, (byte) 0x03, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
            (byte) 0xff, (byte) 0xff, (byte) 0x03, (byte) 0x03, (byte) 0x03, (byte) 0x03, (byte) 0x03, (byte) 0x03,
            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xfc, (byte) 0xfc, (byte) 0xfc, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x03, (byte) 0x03, (byte) 0xff,
            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x03, (byte) 0x03, (byte) 0x03,
            (byte) 0x03, (byte) 0x03, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
            (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xfc, (byte) 0xfc, (byte) 0xfc,
            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x03, (byte) 0x03, (byte) 0x03, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
            (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x3f,
            (byte) 0x3f, (byte) 0x3f, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0xff,
            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0, (byte) 0xff, (byte) 0xff,
            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0f,
            (byte) 0x0f, (byte) 0x0f, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xf0, (byte) 0xf0, (byte) 0xf0,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x03, (byte) 0x03,
            (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0xff,
            (byte) 0xff, (byte) 0x03, (byte) 0x03, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x03, (byte) 0x03, (byte) 0x03, (byte) 0x03, (byte) 0x03, (byte) 0x03, (byte) 0x03, (byte) 0x03,
            (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03,
            (byte) 0x03, (byte) 0x03, (byte) 0xff, (byte) 0xff, (byte) 0xff, }), (LCD.SCREEN_WIDTH / 2) - (69 / 2), 0, false);

      // Play some cool sounds
      for (int i = 100; i < 500; i++)
         Sound.playTone(i, 20);
      Sound.playTone(501, 500);

      char[][] foo = new char[][]
      { 'P', 'r', 'e', 's', 's', ' ', 'a', 'n', 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o' },
      { ' ', ' ', ' ', ' ', ' ', 's', 't', 'a', 'r', 't', ' ', ' ', ' ', ' ', ' ', ' ' } };

      // Write the "Press any key to start message" one character at a time to
      // make it more awesome
      for (int y = 0; y < foo.length; y++)
         for (int x = 0; x < foo[y].length; x++)
            g.drawChar(foo[y][x], x * LCD.CELL_WIDTH, (y + 6) * LCD.CELL_HEIGHT, false);

            // If they pressed enter before its done printing, start the
            // game anyway
            if (Button.readButtons() == Button.ID_ENTER)

      // If they pressed abort, exit.
      if (Button.waitForPress() == Button.ID_ESCAPE)

      // clear the display

class MessageDisplay extends Thread
   public MessageDisplay()

   public void run()
      int exitTestInput = 0, oldExitTestInput = 0, numExitInput = 0;
      while (true)
         for (int i = 16; i > -rfk.message.length(); i--)
            if (rfk.needUpdate)
               rfk.needUpdate = false;
            // clear the screen
            LCD.drawString("                ", 0, 0);

            LCD.drawString(rfk.message, i, 0);


            // This tests to see if you've held the abort button for 3
            // seconds. I put it here because its simpler than making
            // another thread.
            oldExitTestInput = exitTestInput;
            exitTestInput = Button.readButtons();
            if (exitTestInput == Button.ID_ESCAPE && oldExitTestInput == Button.ID_ESCAPE)
            if (numExitInput > 10) // Two seconds (it sleeps for 200 every loop)
               rfk.exit_rfk = true;

//An object on the screen
class screen_object
   int x;
   int y;
   char character;

Should compile and run fine - but if you find any bugs let me know.

Thanks to gloomyandy for pointing out a previous error.

This will probably be the final version - all the features are there and everything works fine.
Last edited by xpansive on Sun May 23, 2010 11:05 pm, edited 2 times in total.

User avatar
leJOS Team Member
Posts: 5971
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby gloomyandy » Thu May 20, 2010 11:59 pm

What are you expecting to happen? You seem to be trying to display the instructions on how to play after you have played the game (which seems a little strange to me)....

As far as I can see nothing ever sets exit_rfk to true, so the loop in play_game will never exit (and if it did then your call to NXT.exit() will terminate the program). So your message display code will never get to run. The only code that seems to end the program is when the game is complete and you call NXT.exit() which will terminate the program.


New User
Posts: 7
Joined: Wed May 19, 2010 11:23 pm

Postby xpansive » Fri May 21, 2010 2:22 am


Well, that was stupid.

I just need to put

MessageDisplay m = new MessageDisplay();

before play_game();.


Thanks for pointing that out - I'll update the first post with the working code when I test it out.

Return to “NXJ Projects”

Who is online

Users browsing this forum: No registered users and 4 guests