Focus mode
Java Sınıflar ile boks maçını simüle eden programı yazıyoruz.
Projeye ilk kimin dövüşe başlayacağını %50 olasılık ile hesaplayan sistemi entegre ediniz.
public class Fighter {
String name;
int damage;
int health;
int weight;
double dodge;
public Fighter(String name, int damage, int health, int weight, double dodge) {
this.name = name;
this.damage = damage;
this.health = health;
this.weight = weight;
this.dodge = dodge;
}
public int hit(Fighter foe) {
System.out.println("------------");
System.out.println(this.name + " => " + foe.name + " " + this.damage + " hasar vurdu.");
if (foe.dodge()) {
System.out.println(foe.name + " gelen hasarı savurdu.");
return foe.health;
}
if (foe.health - this.damage < 0)
return 0;
return foe.health - this.damage;
}
public boolean dodge() {
double randomValue = Math.random() * 100; //0.0 to 99.9
return randomValue <= this.dodge;
}
}
public class Ring {
Fighter f1;
Fighter f2;
int minWeight;
int maxWeight;
public Ring(Fighter f1, Fighter f2, int minWeight, int maxWeight) {
this.f1 = f1;
this.f2 = f2;
this.minWeight = minWeight;
this.maxWeight = maxWeight;
}
public void run() {
if (checkWeight()) {
while (f1.health > 0 && f2.health > 0) {
System.out.println("======== YENİ ROUND ===========");
f2.health = f1.hit(f2);
if (isWin()){
break;
}
f1.health = f2.hit(f1);
if (isWin()){
break;
}
printScore();
}
} else {
System.out.println("Sporcuların ağırlıkları uyuşmuyor.");
}
}
public boolean checkWeight() {
return (f1.weight >= minWeight && f1.weight <= maxWeight) && (f2.weight >= minWeight && f2.weight <= maxWeight);
}
public boolean isWin() {
if (f1.health == 0) {
System.out.println("Maçı Kazanan : " + f2.name);
return true;
} else if (f2.health == 0){
System.out.println("Maçı Kazanan : " + f2.name);
return true;
}
return false;
}
public void printScore() {
System.out.println("------------");
System.out.println(f1.name + " Kalan Can \t:" + f1.health);
System.out.println(f2.name + " Kalan Can \t:" + f2.health);
}
}
public class Main {
public static void main(String[] args) {
Fighter marc = new Fighter("Marc" , 15 , 100, 90, 0);
Fighter alex = new Fighter("Alex" , 10 , 95, 100, 0);
Ring r = new Ring(marc,alex , 90 , 100);
r.run();
}
}
Programs to Accelerate Your Progress in a Software Career
Join our 4-8 month intensive Patika+ bootcamps, start with the fundamentals and gain comprehensive knowledge to kickstart your software career!
You need to enroll in the course to be able to comment!