Learn Programming in Java

(Chomp from Java)


<<Previous | ToC | Next >>

Lesson #2c: Controlling Chomp from Java

We began to learn how to control the computer by scripting the game I call "Chomp". Now you get to do exactly the same thing in a real programming language, in our case Java. First, some cookbook stuff to get you to that stage in BlueJ.

In the Hello program window that you get by double-clicking its icon in the main control window, we are going to add two words to the first line, so it reads "public class Hello extends Chomp {" Chomp is one of the other icons, which you could look at if you want to, but it's very big and probably confusing. After we learn the necessary Java language elements we can open it up and look at what's inside, but for now we'll just use it.

Next, in our  "REPLACE THIS LINE"  we replace it with this single line:

ChompJstart(5,new Hello());


Now when you run the program (choose "main()" from the popup menu you get when you right-click the Hello icon), it will open a new window with the game board you already recognize. There is no place to enter command lines, and no listing panel to display a script, because you will program the nibbler directly in Java. You can still click on the arrow buttons and the nibbler will immediately start moving. Or you can click on the Reset button to revert to the starting board. To terminate the game, just close its window.

But then this is about programming the computer, so that's what we'll do next.

In your Hello window, click in the empty space above the "public static void main" line and below the top line, and then (copy from here and paste it, or else) type in the following program:

public void UserMain() {
  DoLeft(6);
  DoUp(16);
  super.UserMain();}


You may have noticed in the console window the notice "[default UserMain, did not override]". One of the benefits of OOPS (Object Oriented Programming) languages like Java is that you can make changes to pre-written programs without actually changing the original code -- but only if the original programmer designed it that way (which I did ;-) When you wrote "extends Chomp" in the first line of Hello, you were telling the Java compiler that you would be making some invited changes, including a rewritten UserMain() which this is. Not to worry, we'll get to all that, much later.

For now we will concentrate on the two lines in the middle, "DoLeft(6);" and "DoUp(16);" which (you probably already guessed) are like the "left 6" and "up 16" commands in the web browser version of Chomp, except they are spelled a little differently.

Now when you run the program (choose "main()" from the popup menu you get when you right-click the Hello icon), and it opens its window with the game board, the nibbler will immediately start moving left 6, then up 16. If you click the Reset button, it will restart from the front of your code with a fresh maze.
 

Java Chomp Commands

Each of the Java Chomp commands is in the form of a subroutine call, which takes zero or more parameters -- usually the same as in the Chomp scripting language -- but always enclosed in parentheses, even if there are no parameters. These subroutines (in Java they are called "methods") all return an error code value, so you should treat them as if they were a variable or expression, and 3either assign them to an "int" variable, or else use them in a conditional as I did in the last line of my example. When we spell out for users a Java method, it is customary to give the data types of the parameters and the return value, here all "int". Each command listed here is linked to its Chomp description, because it is essentially the same:
int DoReset(int aNum) // (I recommend 0 as a parameter, but it is ignored)
int DoSkill(int aNum)
int DoRight(int aNum)
int DoDown(int aNum)
int DoLeft(int aNum)
int DoUp(int aNum)
int DoAhead(int aNum)
int DoCwise(int aNum)
int DoAcwise(int aNum)
int DoFast()
int DoSlow()
int DoWall(int top, int left, int aNum)
int DoPill(int top, int left, int aNum)
int DoStart(int top, int left, int face)

int DoWaitNoPend(int logn)

This synchronizes your code with the nibbler, which normally putters along at its own speed, while your Java code zooms ahead at machine speed. Each time you call this method, it will wait until the nibbler runs out of steps, or Something Bad Happens -- like maybe you clicked the Reset button. If you give it a parameter value ten or more, it will log once in the console window (and again if an error occurs).


There are eight possible error codes returned (besides 0=no error):

0: "No error"
1: "Game Over (ran off edge)"
2: "Game Over (score>999)"
3: "Nibbler started inside wall"
4: "Invalid maze param"
5: "Startup failed" -- (something went badly wrong)
6: "Startup out of sequence" -- (ditto)
7: "Game Over" -- (some other reason)
8: "Java asked for Reset"


If you are wondering why there are no commands for repeats and conditionals or variable assignment, it's because you now can do those things directly in Java. You already know about variables in Java. Read on for conditionals and (later) repeats...

Next: Conditionals & Input

<<Previous | ToC | Next >>

Rev. 2020 July 10