CTF Tools

General Support

In addition to the network and UI code, I have also included a logging class and some helpful enumerations.

Logging

The ctf::Log class provides synchronized printing methods such as print(String), println(String), logCommand(String) and logCommandResponse(String). It can print to System.out, the default, or any PrintStream you choose - including a Console.

Synchronization is important, as you will almost certainly have multiple threads running, and these threads could try to write at the same time. Using ctf::Log, you will see this:

This is a message from Thread 1.
Hello, I'm Thread 2!
There's trouble in Thread 3.
Everything is great in Thread 4.

Whereas four threads printing to System.out simultaneously might cause this mess:

This is a Hello, There's I'm trouble in message from Thread 1.
Everything Thread is great 2!
in Thread in Thread 3.
4.

The ctf::Log class follows the Singleton pattern, and the singleton instance can be accessed through the getInstance() method. Thus, using Log is as easy as:

import ctf.Log;

public class MyClass
{
    public void someMethod()
    {
        log.println("Hello, world!");
        // do stuff...
        String response = ...
        log.logCommandResponse(response);
    }

    private ctf.Log log = ctf.Log.getInstance();
}    

Miscellaneous Enumerations

In order to implement ctf.view::GameViewInterface, you will need to make use of the enumerations ctf.model::Side and ctf.model::TeamColor. The Side enum is very simple:

/** Sides of the field */
public enum Side { LEFT, RIGHT }

The TeamColor is a little more sophisticated. In addition to the usual enum stuff:

/** Valid team colors */
public enum TeamColor
{
    RED, GREEN, BLUE, ORANGE, PURPLE, BLACK;
    ...
}

Java allows enumerations to have methods. TeamColor gives two methods, whose signatures are:

public static TeamColor stringToColor(String str)
public java.awt.Color toAWTColor()

The first method, stringToColor(), allows you to convert a string like "red", "RED" or even "rEd" to the enum value ctf.model.TeamColor.RED. Such a conversion is as simple as:

import ctf.model.TeamColor;
import static ctf.model.TeamColor.stringToColor;

...

String s = "pUrPLe";
TeamColor color = stringToColor(s);

// the variable "color" is now PURPLE

The toAWTColor() method should help you in GUI development: it returns the java.awt.Color that corresponds to a given TeamColor (for instance, TeamColor.PURPLE corresponds to java.awt.Color.MAGENTA).