Changes between Initial Version and Version 1 of ControlSystems/SoftwareTeam/Training/VirtualTraining/JuniorRobot


Ignore:
Timestamp:
Aug 31, 2020, 11:39:50 PM (5 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/VirtualTraining/JuniorRobot

    v1 v1  
     1== Junior Robot ==
     2In the last lesson, we tested a basic "Junior Robot" that was created for us.  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.
     3
     4=== Packages ===
     5{{{
     6package daa2537;
     7}}}
     8In Java, related things are grouped together into ''packages''. 
     9Examples 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 [https://www.tutorialspoint.com/java/java_packages.htm here]
     10
     11A 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.
     12
     13=== Imports ===
     14To 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.
     15{{{
     16import robocode.*;
     17}}}
     18
     19=== Comments ===
     20Programs 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:
     21{{{
     22    // This is a single line comment
     23
     24    /*
     25     * This is a multi-line
     26     * comment.
     27     */
     28}}}
     29In the sample program, a comment is provided to show you where to learn more about robocode and the [https://robocode.sourceforge.io/docs/robocode/robocode/JuniorRobot.html !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.
     30{{{
     31// API help : https://robocode.sourceforge.io/docs/robocode/robocode/JuniorRobot.html
     32
     33/**
     34 * RoboJojo - a robot by (your name here)
     35 */
     36}}}
     37
     38=== Classes ===
     39We'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 create a 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).   You must indicate whether your class can be imported and used in other packages (e.g. is public).  Finally, everything you want to be in  your class is placed inside a pair of curly braces: {   }
     40
     41{{{
     42public class RoboJojo extends JuniorRobot
     43{
     44}}}
     45
     46=== Methods ===
     47Methods are the verbs of a programming language; the describe the behaviors of your robot. 
     48
     49Every 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.
     50{{{
     51        /**
     52         * run: RoboJojo's default behavior
     53         */
     54        public void run() {
     55}}}
     56
     57Methods 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 [https://robocode.sourceforge.io/docs/robocode/robocode/JuniorRobot.html JuniorRobot documentation]).
     58{{{
     59                // Initialization of the robot should be put here
     60
     61                // Some color codes: blue, yellow, black, white, red, pink, brown, grey, orange...
     62                // Sets these colors (robot parts): body, gun, radar, bullet, scan_arc
     63                setColors(orange, blue, white, yellow, black);
     64}}}
     65
     66=== Loops ===
     67One 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.
     68{{{
     69                // Robot main loop
     70                while(true) {
     71                        // Replace the next 4 lines with any behavior you would like
     72                        ahead(100);
     73                        turnGunRight(360);
     74                        back(100);
     75                        turnGunRight(360);
     76                }
     77}}}
     78
     79=== Observers ===
     80The Junior Robot class defines several observer methods that are invoked automatically when something happens in the battle.  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.
     81{{{
     82        }
     83
     84        /**
     85         * onScannedRobot: What to do when you see another robot
     86         */
     87        public void onScannedRobot() {
     88                // Replace the next line with any behavior you would like
     89                fire(1);
     90        }
     91}}}
     92Other observer methods will respond to being hit by a bullet or running into the wall
     93{{{
     94
     95        /**
     96         * onHitByBullet: What to do when you're hit by a bullet
     97         */
     98        public void onHitByBullet() {
     99                // Replace the next line with any behavior you would like
     100                back(10);
     101        }
     102       
     103        /**
     104         * onHitWall: What to do when you hit a wall
     105         */
     106        public void onHitWall() {
     107                // Replace the next line with any behavior you would like
     108                back(20);
     109        }       
     110}
     111}}}