Mehmet Ethem SULAN :: Test Güdümlü Yazılım(Test Driven Development) :: Game Example in Java
//www.ethemsulan.com
package Robotlar;
public class Robot1 extends Robot1SuperSinif {
public boolean unit1Dagit(){
if(getEnergy1()>0){
	dagilimiYap=getEnergy1()/3;
setAttact1(degerDagit1(dagilimiYap,
	dagilimiYap-4>0 ? dagilimiYap-4:1));
dagilimiYap=getEnergy1()-dagilimiYap;
	dagilimiYap=dagilimiYap/2;
setDefense1(degerDagit1(dagilimiYap,
	dagilimiYap-4>0 ? dagilimiYap-4:1));	
setSpeed1(degerDagit1(dagilimiYap,
	dagilimiYap-4>0 ? dagilimiYap-4:1));
dagilimiYap=getEnergy1()-getAttact1()-getSpeed1()-getDefense1();
	int kalanEnerEkle=rastgele.nextInt(3);
if(kalanEnerEkle==0 && dagilimiYap>0){
	setAttact1(getAttact1()+dagilimiYap);
}else if(kalanEnerEkle==1 && dagilimiYap>0){
	setDefense1(getDefense1()+dagilimiYap);
}else if(kalanEnerEkle==2 && dagilimiYap>0){
	setSpeed1(getSpeed1()+dagilimiYap);
		}
		return true;
		}
		return false;
	} }
//-------------------------------------------------------------------
//www.ethemsulan.com
package Robotlar;
import java.util.Random;
public class Robot1SuperSinif {
	private int attact1;
	private int defense1;
	private int speed1 = 2;
	private int location1;
	protected static int energy1 = 100;
public static void setEnergy1(int energy1) {
		Robot1.energy1 -=energy1;
	}
	public void setAttact1(int attact1) {
		this.attact1 = attact1;
	}
public void setDefense1(int defense1) {
		this.defense1 = defense1;
	}
public void setSpeed1(int speed1) {
		this.speed1 = speed1;
	}
public int getAttact1() {
		return attact1;
	}
public int getDefense1() {
		return defense1;
	}
public int getSpeed1() {
		return speed1;
	}
public static int getEnergy1() {
		return energy1;
	}
public int getLocation1() {
		return location1;
	}
public void setLocation1(int location1) {
		this.location1 = location1;
	}
protected Random rastgele = new Random();
protected int dagilimiYap = 0;
public int degerDagit1(int max, int min) {
//rastgele.next(negatif olamaz) onun icin 1 donderiyoruz
	if(max<=0){
		return 1;
		}
return (rastgele.nextInt(max-min+1)+min);
		}
public Robot1SuperSinif() {
		super();
	} }
//--------------------------------------------------------------
//www.ethemsulan.com
package Robotlar;
public class Robot2 extends Robot2SuperSinif {
public boolean unit2Dagit(){
if(getEnergy2()>0){
	dagilimYap2=getEnergy2()/3;
setAttact2(degerDagit2(dagilimYap2,
	dagilimYap2-4>0 ?dagilimYap2-4:1));
dagilimYap2=getEnergy2()-dagilimYap2;
	dagilimYap2=dagilimYap2/2;
setDefense2(degerDagit2(dagilimYap2, 
	dagilimYap2-4>0 ?dagilimYap2-4:1));
setSpeed2(degerDagit2(dagilimYap2,
	dagilimYap2-4>0 ? dagilimYap2-4:1));
dagilimYap2=getEnergy2()-getAttact2()-getDefense2()-getSpeed2();
int kalaniEkle=rast.nextInt(3);
//random ile dagitimda gucten artan olursa rastgele(3 degerden) birisine ekliyoruz.
if(kalaniEkle==0 && dagilimYap2>0){
	setAttact2(getAttact2()+dagilimYap2);
}else if(kalaniEkle==1&& dagilimYap2>0){
	setDefense2(getDefense2()+dagilimYap2);
}else if(kalaniEkle==2 && dagilimYap2>0){
	setSpeed2(getSpeed2()+dagilimYap2);
	}
	return true;
	}
	return false;
	} }
//---------------------------------------------------------------------
//www.ethemsulan.com
package Robotlar;
import java.util.Random;
public class Robot2SuperSinif {
	private int attact2;
	private int defense2;
	private int speed2 = 10;
	private int location2;
protected static int energy2 = 100;
public static int getEnergy2() {
		return energy2;
	}
public int getLocation2() {
		return location2;
	}
public void setLocation2(int location2) {
		this.location2 = location2;
	}
public int getAttact2() {
		return attact2;
	}
public void setAttact2(int attact2) {
		this.attact2 = attact2;
	}
public int getDefense2() {
		return defense2;
	}
public void setDefense2(int defense2) {
		this.defense2 = defense2;
	}
public int getSpeed2() {
		return speed2;
	}
public void setSpeed2(int speed2) {
		this.speed2 = speed2;
	}
public static int getUnit2() {
		return energy2;
	}
public void setEnergy2(int energy2) {
		Robot2.energy2 -=energy2;
	}
public static void setUnit2(int unit2) {
		Robot2.energy2 -= unit2;
	}
protected Random rast = new Random();
protected int dagilimYap2;
public int degerDagit2(int max, int min) {
//random deger max<=0 oldugunda hata olmamasi icin 1 donderdik
	if(max<=0){
		return 1;
		}
return (rast.nextInt(max-min+1)+min);
	}
public Robot2SuperSinif() {
		super();
	} }

Ana sınıf: http://www.ethemsulan.com/2010/03/cevik-surec-agile-process-ile-javada.html

Write a Comment

Let me know what you think?