2D Array Background

These are some key skills for 2D arrays.

  • Nested for loops, perhaps enhanced for loops.
  • Zero based counting.
  • Access of x and y coordinates. Find properties at coordinates.
    • sim.evaluateLight(0, 3);
    • sim.evaluateLight(6, 0);
    • sim.evaluateLight(4, 1);
  • Perform counts on values in columns or rows.

Ideation

Sahill class of '23 discussion... For 2019 FRQ4, one idea I had was maybe turning each light into an object, since right now each element of the 2D array is simply a Boolean. Each light can have Boolean property for off/on, but also additional method to turn on the light (this would be the same as a setter, but it is more abstracted and cleaner in my opinion). An interesting idea could be to have a luminosity property and a quality property. We can start off each light object with a certain luminosity, say 100, and then decrement this luminosity based on quality value. For example, if quality is 1, then the luminosity decreases by 1 every minute (if the light is on). We can then have an API endpoint that tracks the luminosity for each light.

Code ideation does not always follow conversation. As you can see, the light board started out looking more like a color palette. This was because conversation(s) were ideas, but real purpose is yet to be found.

Color Board

import java.util.HashMap;
import java.util.Map;

public class Light {
    boolean on;
    short red;
    short green;
    short blue;
    short effect;

    /*  ANSI effects
        n	Name	Note
        0	Reset or normal	All attributes off
        1	Bold or increased intensity	As with faint, the color change is a PC (SCO / CGA) invention.[38][better source needed]
        2	Faint, decreased intensity, or dim	May be implemented as a light font weight like bold.[39]
        3	Italic	Not widely supported. Sometimes treated as inverse or blink.[38]
        4	Underline	Style extensions exist for Kitty, VTE, mintty and iTerm2.[40][41]
        5	Slow blink	Sets blinking to less than 150 times per minute
        6	Rapid blink	MS-DOS ANSI.SYS, 150+ per minute; not widely supported
        7	Reverse video or invert	Swap foreground and background colors; inconsistent emulation[42]
        8	Conceal or hide	Not widely supported.
        9	Crossed-out, or strike	Characters legible but marked as if for deletion. Not supported in Terminal.app
     */
    private final Map<Short, String> EFFECT = new HashMap<>();
    {
        // Map<"separator", not_used>
        EFFECT.put((short) 0, "Normal");
        EFFECT.put((short) 1, "Bold");
        EFFECT.put((short) 2, "Faint");
        EFFECT.put((short) 3, "Italic");
        EFFECT.put((short) 4, "Underline");
        EFFECT.put((short) 5, "Slow Blink");
        EFFECT.put((short) 6, "Fast Blink");
        EFFECT.put((short) 7, "Reverse");
        EFFECT.put((short) 8, "Conceal");
        EFFECT.put((short) 9, "Crossed_out");
    }

    /* Assign random colors and effects */
    public Light() {
        int maxColor = 255;
        int effect = 9;
        this.red = (short) (Math.random()*(maxColor+1));
        this.green = (short) (Math.random()*(maxColor+1));
        this.blue = (short) (Math.random()*(maxColor+1));
        this.effect = (short) (Math.random()*(effect+1));
    }

    public String getEffectTitle() {
        return EFFECT.get(this.effect);
    }

    public String getRGB() {
        return ( "#" +
         String.format("%02X", this.red) +
         String.format("%02X", this.green) + 
         String.format("%02X", this.blue) 
         );
    }

    /* toString output as key/values */
    public String toString() {
        return( "{" + 
            "\"red\": " + red + "," +
            "\"green\": " +  green + "," + 
            "\"blue\": " + blue + "," +
            "\"effect\": " + "\"" + EFFECT.get(effect) + "\"" +
            "}" );
    }

    public boolean isOn() {
        return on;
    }

    public void setOn(boolean on) {
        this.on = on;
    }

    public short getRed() {
        return red;
    }

    public short getGreen() {
        return green;
    }

    public short getBlue() {
        return blue;
    }

    public short getEffect() {
        return effect;
    }

    static public void main(String[] args) {
        // create and display LightBoard
        Light light = new Light();
        System.out.println(light);  // use toString() method
    }
    

}
Light.main(null);
{"red": 117,"green": 65,"blue": 98,"effect": "Fast Blink"}
public class LightBoard {
    private Light[][] lights;

    /* Initialize LightBoard and Lights */
    public LightBoard(int numRows, int numCols) {
        this.lights = new Light[numRows][numCols];
        // 2D array nested loops, used for initialization
        for (int row = 0; row < numRows; row++) {
            for (int col = 0; col < numCols; col++) {
                lights[row][col] = new Light();  // each cell needs to be constructed
            }
        }
    }

    /* Output is intended for API key/values */
    public String toString() { 
        String outString = "[";
        // 2D array nested loops, used for reference
        for (int row = 0; row < lights.length; row++) {
            for (int col = 0; col < lights[row].length; col++) {
                outString += 
                // data
                "{" + 
                "\"row\": " + row + "," +
                "\"column\": " + col + "," +
                "\"light\": " + lights[row][col] +   // extract toString data
                "}," ;
            }
        }
        // remove last comma, newline, add square bracket, reset color
        outString = outString.substring(0,outString.length() - 1) + "]";
		return outString;
    }

    /* Output is intended for Terminal, effects added to output */
    public String toTerminal() { 
        String outString = "[";
        // 2D array nested loops, used for reference
        for (int row = 0; row < lights.length; row++) {
            for (int col = 0; col < lights[row].length; col++) {
                outString += 
                // reset
                "\033[m" +
                
                // color
                "\033[38;2;" + 
                lights[row][col].getRed() + ";" +  // set color using getters
                lights[row][col].getGreen() + ";" +
                lights[row][col].getBlue() + ";" +
                lights[row][col].getEffect() + "m" +
                // data, extract custom getters
                "{" +
                "\"" + "RGB\": " + "\"" + lights[row][col].getRGB() + "\"" +
                "," +
                "\"" + "Effect\": " + "\"" + lights[row][col].getEffectTitle() + "\"" +
                "}," +
                // newline
                "\n" ;
            }
        }
        // remove last comma, newline, add square bracket, reset color
        outString = outString.substring(0,outString.length() - 2) + "\033[m" + "]";
		return outString;
    }

    /* Output is intended for Terminal, draws color palette */
    public String toColorPalette() {
        // block sizes
        final int ROWS = 5;
        final int COLS = 10;

        // Build large string for entire color palette
        String outString = "";
        // find each row
        for (int row = 0; row < lights.length; row++) {
            // repeat each row for block size
            for (int i = 0; i < ROWS; i++) {
                // find each column
                for (int col = 0; col < lights[row].length; col++) {
                    // repeat each column for block size
                    for (int j = 0; j < COLS; j++) {
                        // print single character, except at midpoint print color code
                        String c = (i == (int) (ROWS / 2) && j == (int) (COLS / 2) ) 
                            ? lights[row][col].getRGB()
                            : (j == (int) (COLS / 2))  // nested ternary
                            ? " ".repeat(lights[row][col].getRGB().length())
                            : " ";

                        outString += 
                        // reset
                        "\033[m" +
                        
                        // color
                        "\033[38;2;" + 
                        lights[row][col].getRed() + ";" +
                        lights[row][col].getGreen() + ";" +
                        lights[row][col].getBlue() + ";" +
                        "7m" +

                        // color code or blank character
                        c +

                        // reset
                        "\033[m";
                    }
                }
                outString += "\n";
            }
        }
        // remove last comma, newline, add square bracket, reset color
        outString += "\033[m";
		return outString;
    }
    
    static public void main(String[] args) {
        // create and display LightBoard
        LightBoard lightBoard = new LightBoard(5, 5);
        System.out.println(lightBoard);  // use toString() method
        System.out.println(lightBoard.toTerminal());
        System.out.println(lightBoard.toColorPalette());
    }
}

Form toString() like JSON

LightBoard lightBoard = new LightBoard(5, 5);
System.out.println(lightBoard);  // use toString() method
[{"row": 0,"column": 0,"light": {"red": 200,"green": 253,"blue": 43,"effect": "Crossed_out"}},{"row": 0,"column": 1,"light": {"red": 116,"green": 244,"blue": 189,"effect": "Italic"}},{"row": 0,"column": 2,"light": {"red": 99,"green": 216,"blue": 210,"effect": "Reverse"}},{"row": 0,"column": 3,"light": {"red": 154,"green": 168,"blue": 36,"effect": "Italic"}},{"row": 0,"column": 4,"light": {"red": 210,"green": 250,"blue": 191,"effect": "Slow Blink"}},{"row": 1,"column": 0,"light": {"red": 40,"green": 63,"blue": 155,"effect": "Slow Blink"}},{"row": 1,"column": 1,"light": {"red": 176,"green": 80,"blue": 138,"effect": "Fast Blink"}},{"row": 1,"column": 2,"light": {"red": 14,"green": 241,"blue": 234,"effect": "Faint"}},{"row": 1,"column": 3,"light": {"red": 240,"green": 111,"blue": 72,"effect": "Crossed_out"}},{"row": 1,"column": 4,"light": {"red": 71,"green": 2,"blue": 93,"effect": "Underline"}},{"row": 2,"column": 0,"light": {"red": 177,"green": 122,"blue": 71,"effect": "Reverse"}},{"row": 2,"column": 1,"light": {"red": 109,"green": 149,"blue": 252,"effect": "Reverse"}},{"row": 2,"column": 2,"light": {"red": 121,"green": 125,"blue": 78,"effect": "Normal"}},{"row": 2,"column": 3,"light": {"red": 57,"green": 29,"blue": 155,"effect": "Slow Blink"}},{"row": 2,"column": 4,"light": {"red": 54,"green": 104,"blue": 135,"effect": "Italic"}},{"row": 3,"column": 0,"light": {"red": 231,"green": 54,"blue": 182,"effect": "Reverse"}},{"row": 3,"column": 1,"light": {"red": 157,"green": 6,"blue": 130,"effect": "Crossed_out"}},{"row": 3,"column": 2,"light": {"red": 200,"green": 226,"blue": 15,"effect": "Crossed_out"}},{"row": 3,"column": 3,"light": {"red": 202,"green": 146,"blue": 4,"effect": "Crossed_out"}},{"row": 3,"column": 4,"light": {"red": 119,"green": 121,"blue": 96,"effect": "Crossed_out"}},{"row": 4,"column": 0,"light": {"red": 55,"green": 224,"blue": 132,"effect": "Slow Blink"}},{"row": 4,"column": 1,"light": {"red": 108,"green": 200,"blue": 179,"effect": "Reverse"}},{"row": 4,"column": 2,"light": {"red": 111,"green": 43,"blue": 141,"effect": "Underline"}},{"row": 4,"column": 3,"light": {"red": 242,"green": 47,"blue": 40,"effect": "Underline"}},{"row": 4,"column": 4,"light": {"red": 19,"green": 251,"blue": 108,"effect": "Conceal"}}]

Color Codes and Effect

LightBoard lightBoard = new LightBoard(5, 5);
System.out.println(lightBoard.toTerminal());
[{"RGB": "#B5B63A","Effect": "Underline"},
{"RGB": "#0FC0DC","Effect": "Fast Blink"},
{"RGB": "#6866C8","Effect": "Slow Blink"},
{"RGB": "#260B97","Effect": "Normal"},
{"RGB": "#456D79","Effect": "Bold"},
{"RGB": "#208BFD","Effect": "Underline"},
{"RGB": "#59CD46","Effect": "Crossed_out"},
{"RGB": "#442D3D","Effect": "Faint"},
{"RGB": "#E4952D","Effect": "Bold"},
{"RGB": "#FE09AF","Effect": "Crossed_out"},
{"RGB": "#43FF66","Effect": "Faint"},
{"RGB": "#BE0547","Effect": "Slow Blink"},
{"RGB": "#23DBA7","Effect": "Underline"},
{"RGB": "#7E4553","Effect": "Conceal"},
{"RGB": "#002435","Effect": "Normal"},
{"RGB": "#80DFEE","Effect": "Reverse"},
{"RGB": "#1AB6E3","Effect": "Slow Blink"},
{"RGB": "#2B68D9","Effect": "Italic"},
{"RGB": "#7D713F","Effect": "Underline"},
{"RGB": "#1D4A9E","Effect": "Reverse"},
{"RGB": "#B6477D","Effect": "Slow Blink"},
{"RGB": "#C90437","Effect": "Underline"},
{"RGB": "#4705EC","Effect": "Crossed_out"},
{"RGB": "#56B29C","Effect": "Bold"},
{"RGB": "#1DD10D","Effect": "Underline"}]

Color Code Palette

LightBoard lightBoard = new LightBoard(5, 5);
System.out.println(lightBoard.toColorPalette());
                                                                                
                                                                                
     #7B98E3         #66A57E         #9272F2         #AD556C         #2BD6CF    
                                                                                
                                                                                
                                                                                
                                                                                
     #14D232         #CAE823         #06C556         #A02372         #C7BA0F    
                                                                                
                                                                                
                                                                                
                                                                                
     #6D051F         #B05AFD         #CED54B         #0D90CF         #B8DA02    
                                                                                
                                                                                
                                                                                
                                                                                
     #D680F0         #D5AF74         #C6B475         #049F0E         #8FE4F8    
                                                                                
                                                                                
                                                                                
                                                                                
     #3C18B2         #234106         #85BA77         #482052         #D98D91    
                                                                                
                                                                                

Hacks

There are many ideas and possibilities with 2D arrays.

  • Java skill. Establish key concepts and code examples on 2D arrays using FRQ4 and code above. Build vocab and add a new terminal concept.

    • Examples:

      • add usability for on/off or effects
      • edit size of 2d array
      • change size of each color palette cell
      • Input color values instead of setting values using Math.random
Extra (+++):
  • Create API endpoints similar to previous FRQ's, use postman or curl to test
Advanced:
  • Full stack skill. Think about Frontend, Backend use case to make the existing color board into more of a light board show. Perhaps on a timer to change Frontend color board to be a light show, with all the CSS and HTML possibilities. Ideate something of your own, break away from board.