Doug's Freecell Goal Setter: Source Code

Doug's Freecell Goal Setter is "open source."  That means that you get to look at the source code.  It also means that Doug is now "cool" in the "geek" "community."  Yesterday I was the merest of mere math professors.  Today I am an open source pioneer, a programming outlaw who makes his own rules and doesn't even necessarily follow them.  You don't want to get mixed up with me; I'm dangerous.

import java.awt.*;

public class freecell extends java.applet.Applet {

TextField wins,losses;
Button go;
float average,average2;
float desired[]=new float[7];
float newwins[]=new float[7];

public void init(){
	int i;
	wins=new TextField("90",5);
	losses=new TextField("10",5);	
	go=new Button("Compute!");	

	add(new Label("Your wins"));
	add(wins);
	add(new Label("Your losses"));
	add(losses);
	add(go);
}
public float rounddd(double victim){
	victim = (victim*100+0.5);
	victim = Math.floor(victim)/100;
	return (float) victim;
}
public float roundddd(double victim){
	victim = (victim*1000+0.5);
	victim = Math.floor(victim)/1000;
	return (float) victim;
}
public void setarrays(float center,float w,float l){
	int i;
	float left = rounddd(center-.015);
	for(i=0;i<=6;i++){
		desired[i]=(float)(left+i*0.005);
		newwins[i]=(desired[i]*(w+l)-w)/(1-desired[i]);
		if(newwins[i]< 0){
			newwins[i]=-(desired[i]*(w+l)-w)/(0-desired[i]);
		}
	}
}
public boolean action(Event evt, Object arg) {
	if(evt.target instanceof Button){
		float w = Float.valueOf(wins.getText().trim()).floatValue(); 
		float l = Float.valueOf(losses.getText().trim()).floatValue(); 
		average=rounddd(w/(w+l));
		average2=(w/(w+l));
		setarrays(average,w,l);
		repaint();
		return true;
	}
	else
		return false;
}
public void paint(Graphics g) {
	int i;
	int x=2,y=go.location().y+60;
	String temp;
	temp = "  Your average is ";
	temp = temp + average2;
	g.drawString(temp,x,y);
	y+=50;


	for(i=0;i<=6;i++){
		if(newwins[i]>=0){
			temp = "  If you want your percent to be ";
			temp = temp + roundddd(desired[i]);
			temp = temp + " you need to win ";
			temp = temp + (int)Math.ceil(newwins[i]) + " games in a row.";
			g.drawString(temp,x,y);
			y+=25;
			}
		else{
			temp = "  If you want your percent to remain above ";
			temp = temp + roundddd(desired[i]);
			temp = temp + " you cannot lose ";
			temp = temp + (int)Math.abs(Math.floor(newwins[i])) + " games in a row.";
			g.drawString(temp,x,y);
			y+=25;
			}


		}
	}
}

widget.gif (1386 bytes) Back to Doug's Freecell Goal Setter
widget.gif (1386 bytes) Back to Main