wiki:ControlSystems/SoftwareTeam/Training/VirtualTraining/JuniorRobot

Junior Robot

In the last lesson, we created and tested a basic "Junior Robot". In this lesson, we'll examine the structure of that robot. This lesson will touch lightly on many topics that we will explore in detail in subsequent lessons. Don't expect to understand everything in detail yet; it will become clear over time.

Packages

package daa2537;

In Java, related things are grouped together into packages. Examples of related things include: all the robots written by Alice or all the robots written by team 2537 or a set of software tools to make robots (like robocode). Keeping related things in separate packages helps prevent "naming conflicts"; team2537 and team5945 could both create robots named RoboJojo and battle them without confusion or conflict; they would be referred to as team2537.RoboJojo and team5945.RoboJojo. You can read more about Java packages here

A large part of software development is focused on modularity: keeping related things together cohesion and avoiding unwanted interactions coupling. These concepts underpin much of modern software engineering, keep them in mind as you learn to program. Packages are an example of how Java helps encourage cohesion and reduce coupling.

Imports

To use a package in your program, you import it. Java lets you create collections of tools that you can use over and over. For example, the Junior Robot program imports the robocode package so it can use them to make a robot. The .* at the end indicates that you want to import all of the tools. In Java '*' is a wildcard.

import robocode.*;

Comments

Programs can get large and complicated. A critical aspect of writing good programs is to include comments that explain how they work and/or provide additional information that can help someone else reading your program. In Java, there are two ways to write a comment:

    // This is a single line comment

    /*
     * This is a multi-line
     * comment.
     */

In the sample program, a comment is provided to show you where to learn more about robocode and the JuniorRobot followed by a comment that provides the name of your robot, the author, and typically you'd provide a brief summary of what your robot does.

// API help : https://robocode.sourceforge.io/docs/robocode/robocode/JuniorRobot.html

/**
 * RoboJojo - a robot by (your name here)
 */

Classes

We're going to cover classes in much more detail later, but most things in Java programs are either a class or a part of a class. When you define a new type of robot, you create a class and give it a name (e.g. RoboJojo). That class can be based on (extend) another class (e.g. JuniorRobot). When a class is based on another class, it inherits all of the behaviors and attributes of that class and extends them with new behaviors and attributes. You must indicate whether your class can be imported and used in other packages (e.g. is public). Finally, everything that is part of your class must be placed inside a pair of curly braces: { }

public class RoboJojo extends JuniorRobot
{

You can read more about Java classes here

Methods

Methods are the verbs of a programming language; they describe the behaviors (actions) of your robot.

Every robocode robot must have a run method that defines what the robot will do when it enters the arena and the battle begins. It is good practice to always place a comment at the start of each method that explains what the method does. For now, just know that you invoke a verb by placing parentheses after it that may include relevant values; for example, to turn the robot 90-degrees to the right you would invoke the method: turnRight(90); Notice too that a semicolon finishes each invoked method.

	/**
	 * run: RoboJojo's default behavior
	 */
	public void run() {

Methods are important to modularity because they can form building blocks. One method can string together other methods to create complex behaviors: for example: turnRight(90); ahead(100); fire(); You can understand that sequence of verbs without having to understand exactly how each of them works (e.g. how the robot turns or drives forward). This allows you to construct increasingly complex behaviors from simple verbs. In the sample robot program, the run() method invokes the setColors() method to change the colors of the robot. Try modifying these colors (you can see the list of color choices in the JuniorRobot documentation).

		// Initialization of the robot should be put here

		// Some color codes: blue, yellow, black, white, red, pink, brown, grey, orange...
		// Sets these colors (robot parts): body, gun, radar, bullet, scan_arc
		setColors(orange, blue, white, yellow, black);

You can read more about Java methods here

Loops

One of the nice things about programs is that they can do things repetitively without you having to write the same instructions (methods) over and over. Once your robocode robot starts running in the battle arena, you want it to keep running until the battle is over so it should continue doing its behaviors indefinitely. In Java, one of the repetition methods is while which will repeatedly execute some set of methods (specified inside the curly braces) until some condition (specified inside the parentheses) is false. Since we want the robot to keep doing its thing indefinitely, we use the while(true) syntax which will repeat forever. In this case, the robot moves forward 100 spaces, turns in a full circle, then moves back 100 spaces and turns in a full circle. It will keep doing this forever.

		// Robot main loop
		while(true) {
			// Replace the next 4 lines with any behavior you would like
			ahead(100);
			turnGunRight(360);
			back(100);
			turnGunRight(360);
		}

You can read more about Java while loops here

Observers

The Junior Robot class defines several observer methods that are invoked automatically when something happens in the battle. (you'll sometimes hear observer methods referred to as "listeners" or "callbacks"). For example, as the robot scans its surroundings, if it sees another robot in front of it, the onScannedRobot() method is invoked. In this case, the robot will fire when it sees another robot in front of it.

	}

	/**
	 * onScannedRobot: What to do when you see another robot
	 */
	public void onScannedRobot() {
		// Replace the next line with any behavior you would like
		fire(1);
	}

Other observer methods will respond to being hit by a bullet or running into the wall

	/**
	 * onHitByBullet: What to do when you're hit by a bullet
	 */
	public void onHitByBullet() {
		// Replace the next line with any behavior you would like
		back(10);
	}
	
	/**
	 * onHitWall: What to do when you hit a wall
	 */
	public void onHitWall() {
		// Replace the next line with any behavior you would like
		back(20);
	}	
}

You can read more about Java Observers here

Exercises

  1. Run RoboJojo again and observe how the behaviors of the robot relate to the methods.
  2. Change the methods in the robot's main loop to make its behavior more interesting, try making the robot travel in a square and fire its gun at each corner. you can find the list of methods you can use in the JuniorRobot class documentation
Last modified 9 months ago Last modified on Aug 31, 2020, 11:51:31 PM