Equal time for players, Part One
Tuesday, May 26, 2009
I'm coaching my son's U-9 (under 9) soccer team. We have 11 players, and I need to have 7 players on the field at all times: we have a goalie and 6 in-field players.
In the division Gabriel is playing, we are not looking for position specialization yet. The main goal of the program is to get all the players interested in soccer and to help them learn a bit about the game. Therefore, everyone playing equal amounts of time is advisable and preferred. (The club does have a rep league that kids can try out for, though; at that level, I think they have them playing specific positions.)
Having to spread equal time among 11 players is tricky. It's a combinatorial problem that is not hard, but it's not trivial if you don't think about it first. The issue is that I have a large number of combinations: 7 players in the field out of 11--that's a lot of combinations.
To get all the players playing all positions, I have a 1-3-3 lineup (a goalie, 3 defenders, 3 forwards). This means that I now have 2 sub-problems: 3 out of 5 players for the defenders, and 3 out of 5 players for the forwards (the goalie is not a problem because one he's is in that position, I can't sub him for the rest of the half).
In the first 2 games, I thought I wing it and do subs every 5 minutes by rotating the players around. It worked all right, but I had to do too many calculations in my head figuring out who had been on the field the longest and who hadn't. This, of course, was error prone and I think I got a couple of players playing too long, because they asked to be substituted.
This is the kind of problem I like to solve, and the kind of problems, I guess, I paid so much money to learn how to solve. I sat down for 5 minutes and I have a solution that equally divides playing time among the kids. I should say, more or less equally, as some players will always play a bit more because of the rotations. Nevertheless, my head calculation time has now shrunk to 0, because I have everything automated.
Actually, I have the algorithm worked out. I will code it over the next couple of days and post it here. Check back for my next entry
Equal time for players, Part Two.
Java JTextField Field Validator
Thursday, May 07, 2009
There are different ways to validate form fields in Java Swing applications, but the one I found the easiest to work with is extending
javax.swing.text.PlainDocument
.
If you use this method, field validation is accomplished in 2 easy steps.
Step 1Create a validator class, as follows:
public class OnlyNumberValidator extends javax.swing.text.PlainDocument {
@Override
public void insertString(int offs, String str,
javax.swing.text.AttributeSet a)
throws javax.swing.text.BadLocationException {
StringBuffer buf = new StringBuffer(str);
int size = buf.length();
char c;
for (int i = 0; i < size; i++) {
c = buf.charAt(i);
if (!Character.isDigit(c)) {
buf.deleteCharAt(i);
}
}
super.insertString(offs, buf.toString(), a);
}
}
The idea here is that every key stroke is accounted for. If the key stroke is a digit, we add it to the buffer and update the view; if it's not, we remove it from the buffer and update the view. The result is a field that doesn't allow unwanted characters.
Step 2Set a
Document
object to the field that needs validation, as follows:
// Assume you have the following JTextField object
javax.swing.JTextField jTextFieldOnlyNumber = new JTextField();
// We now set the validation
jTextFieldOnlyNumber.setDocument(new OnlyNumberValidator());
Simple as that. Now our
jTextFieldOnlyNumber
object only accepts numbers.
You can create more sophisticated validator classes by adding error checking and by limiting the type and number of characters per field. But the idea is the same: extend
javax.swing.text.PlainDocument
and override
insertString()
.
Outrunning the thunderstorm
Tuesday, May 05, 2009
The following story has nothing to do with software development. Read at your own peril.
Thunderstorms are powerful. I remember when I was around 11 years old I got caught in a thunderstorm at the park. My friends and I were playing basketball when the storm began to envelop the park, the city, the country, the whole world, it appeared. We moved the playing to a
covered area to wait for the rain to stop. There was no point in going home if we were already safe and dry.
As the sky began to turn dark gray, my heart rate began to keep up with each darker gradient: the darker the sky got, the faster my heart beat. When the smell of wet dirt hit us, so did the thunder. And then the lightning followed. I first saw our shelter lit as if the sun peeked through the clouds. I then heard the big loud boom and crackling of electrons flying from the clouds to the ground and from the ground to the clouds. I was scared and humbled: the power of nature in living color.
Today, I experienced a similar thunderstorm. I'm obviously older, but my mind recalled, my body remembered. This time, however, I wasn't scared. I went outside to my balcony to smell the rain, touch the wind, and admire the world with its dark gray, angry sky. As it began to rain harder and harder, two kids came running from the park. They were maybe 5 and 9. I could hear the rain, but I wasn't wet yet. The wall of rain kept encroaching towards them as they ran by my balcony. The sky rumbled again. I'm sure they were scared.
The compact, red car stopped and I only saw the older kid standing on the other side of the street. I couldn't see the younger brother, only his shoes thrown across the road. Where was he? They were holding hands a second ago.
I said, "Oh shit...the car hit the 2 kids." I thought, "Oh, shit...she killed him."
The woman driving got out of the car screaming with her hands holding her head or pulling her hair--I couldn't really tell.
The rain didn't stop, and the wind blew stronger still. And then...the little boy got up crying and began to run. Where? Home? Then his brother followed running after him. Then the driver followed running after
them. Then a neighbor followed running after
them.
I screamed, "Wait!" But nobody waited...everybody just ran.
No one caught up to the kids. They both looked all right. They were running, after all.
The driver returned to her car, still hysterical. I couldn't see the kids any more. The rain continued. I went inside and remembered the day when I was caught in a thunderstorm while playing basketball at the park,
Parque Toluca...