BBC (Brian's Boot Camp)

Not to be confused with the British Broadcasting Corporation, the BBC will focus on Brian's fast paced journey of the mastery of the Java programming language.

Saturday, March 05, 2005

Assignment #2 Answer

Oh man, oh man. I can't believe I ate the whole thing.

After doing a little Google search to help me out on how to use SimpleDateFormat I spent a while scratching my head wondering why you wanted to me to use GregorianCalendar or Calendar. Now I see.

import java.text.*; 
import java.util.*;

public class DateParse {
public static void main(String[] arguments) {

int month = 2;
int day = 3;
int year = 1976;

if (2 < arguments.length) {
month = Integer.parseInt(arguments[0]);
day = Integer.parseInt(arguments[1]);
year = Integer.parseInt(arguments[2]);
}
GregorianCalendar gregCal = new GregorianCalendar(year, (month - 1), day);

SimpleDateFormat sdf = new SimpleDateFormat("MMMM d, yyyy");
System.out.println(sdf.format(gregCal.getTime()));
}
}

Thursday, March 03, 2005

Assignment #5 The Odds are Stacktropolised Against You

Alright, after that forced title, here's an exercise that will primarily teach you how to read/trace through/modify someone else's code. You'll also get exposure with managing projects with more than one source file (Stacktropolis has 15).

First things first: Stacktropolis: An Overview:
Stacktropolis is a Tetris clone; the fundamental rules of Tetris apply. The object is to rotate the falling blocks in order to fit them in and clear lines. When a line is cleared the remaining blocks will fall accordingly.

Stacktropolis' twist is that it introduces the idea of the reward gauge. I loved the reward system in the original Monkey Target from Super Monkey Ball (Turning off wind, Magnet Ball, etc). I like the idea that as you do better, your reward is progressively better.

So at the bottom of the game is the reward gauge. To fill up the gauge you need to clear lines with green balls in them. Each green ball that is cleared raises the gauge by one unit. Watch out though, if you clear a line with a red ball, your reward gauge drops to zero. The idea then becomes balancing between trying to get the best reward possible and "caching out" the gauge before clearing a line with a red ball.

At any point you can cach out your current reward (by pressing the 'A' key), or take the equivalent point value (by pressing the 'Z' key). The rewards, in order of least to best, are:

  • A single white block is put in your queue as the next block. This is useful for "filling in the cracks".
  • The drill-down block. This block will essentially clear a column -- it will continue to fall until it hits the bottom, removing any blocks in its path.
  • The delete line reward. This starts a moving line from top to bottom. When the player hits "D" the current line is removed.
  • Hazards to Rewards. This reward changes all the red balls into green.
  • Gravity Well (my favorite). This invokes gravity on the blocks by having all the blocks fall as far as they can, filling in the gaps, clearing lines as they go.
  • Clear Blocks Reward. The highest reward simply clears all the existing blocks on the screen.
The Assignment:
  • I emailed you the codebase for Stacktropolis a while back, so you already have everything you need from me. To build Stacktropolis you'll need to download Ant, which is a build tool similar to Make. Once you've downloaded Ant and extracted it, you'll need to set two environment variables, ANT_HOME and JAVA_HOME, and add ANT_HOME\bin to your path. The installation instructions should have details on that. Once Ant is configured, go to the st directory (of Stacktropolis) and type "ant" to build it.
  • Once built, play around with the game. Get a feel for it. Try out all the rewards, etc.
  • Once you feel comfortable with the game itself, try reading through some of the source. Trace through the startup process, insert a System.out.println() statement here and there and rebuild it to see if it did what you thought it was going to.
  • Once you're relatively comfortable with all that, make the following modification: Change the first and second rewards (the single block and the drill down) to have the option to "detonate" at any point during their trip down if the player hits the "D" key. The detonation should be a clearing of blocks within a certain radius of the current position of the reward block. You can play around with what that radius should be (whatever seems to serve the game balance). Blocks left hanging in space should drop accordingly.
  • You're in someone else's kitchen now, follow the coding style and comment what you do accordingly. Stacktropolis is very light on the comments since I never intended anyone else to see it, but you can get an indication of the style of comments (Javadoc).
So there you go. Good luck.

Wednesday, March 02, 2005

Assignment #4: The Ubiquitous Bouncing Balls

Ah, the bouncing balls app. A program Paul had me write in the early days, and then in turn that I asked Dave to write a year later, and now 7 years after that, you get to take a wack at this timeless classic (hmmm, wacking at balls?). This assignment covers GUI, Threads, animation, drawing, creating your own classes, and events; it's kind of a non-interactive game. Just in time for your solo weekend too:

Write a program that displays a frame. The top part of the frame should have a black background, this is the ball cage. The bottom of the frame contains a button labeled "Add Ball". When pressed, it will add a "ball" (a circle) to the cage that will head off in some direction and "bounce" off the borders of the cage indefinitely. Subsequent presses of the button will continue to add balls.

Notes:
1) It's up to you of course, but now is a good time to start doing some Object Oriented design. Consider writing a Ball class. You could even go crazy and write a BallCage class too...

2)Some extra options: make each ball a random color, and give it an initial random direction and velocity (a bunch of balls all the same color going the same direction and speed isn't very interesting).

3) Don't worry about collision detection between balls -- just the walls.

This will be your most ambitious effort yet. But for your reference, I wrote this from stratch in real-time, in front of a high school class in a 50 minute class period.

Assignment #3 Answer

This morning, I put chapter 12 and Assignment 3 behind me. At the rist of invoking the Sin of Pride, I'm pretty proud of myself. Huzzah indeed.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class StringSwapper extends JFrame implements ActionListener {
JTextField inputField = new JTextField("String", 5);
JButton pressMe = new JButton("Press Me");
JTextField outputField = new JTextField(5);

public StringSwapper() {
super("Convert a String");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
pressMe.addActionListener(this);

getContentPane().setLayout(flow);
getContentPane().add(inputField);
getContentPane().add(pressMe);
getContentPane().add(outputField);
pack();
setVisible(true);
}

public void actionPerformed(ActionEvent evt) {
String input = new String(inputField.getText());
int len = input.length();
char[] gnirts = new char[len];

for (int i = len - 1, c = 0; i > -1; i--, c++) {
gnirts[c] = input.charAt(i);
}

String output = new String(gnirts);
outputField.setText(output);
setTitle("Huzzah!");
}

public static void main(String[] args) {
StringSwapper ss = new StringSwapper();
}
}