The document provides code for a JavaFX-based Warhammer 40k game. It includes a unit class with methods like shoot() and a proposed charge() method. It asks to implement the charge() method, which would control a unit charging when hit by another unit's bullet, handling processes like declaring a charge, resolving overwatch, rolling charge distance, and repeating charges. It provides the unit and D6 classes to help with implementation.
Here is my javafx code to create a basic warehammer 40k game. I want.pdf
1. Here is my javafx code to create a basic warehammer 40k game. I want you to write the charge
method in javafx which will charge the character when a bullet hit by the other unit team like
have to do who can charge , declare charge , resolve overwatch ,roll distance or roll charge
range, charge move and repeat if desired. my unit class and shoot method are available. please
please if you can do it accept the ticket. (Nothing is better than bad and unrunnable method)
Thanks.
Here is the method to be implement which is already is in unit class.
public void charge(turnManager tM, int distance) {
}
}
Here is the all class.
public abstract class unit {
String name;
int WS;//unit's weaponskill
int BS;//unit's ballistic skill
int S;//unit's strength
int T;//unit's toughness
int W;//unit's wounds
int I;//Unit's initiave
int A;//unit's number of close combat attacks
int Ld;//unit's leadership
int Sv;//unit's ARMOR save
Weapon gun;
Weapon melee;
boolean shot;
boolean moved;
boolean charged;
boolean team;
Circle rep;
boolean alive
public void shoot(turnManager tM, int Distance,D6 d6,Tables t,Pane root) {
if(tM.selected.shot){
System.out.println("Unit has allready shot");
return;
2. }
tM.selected.shot=true;
if((tM.selected.gun.Range*30)>=Distance){
int Tw=t.getToWound(gun.S, tM.targeted.T);
int Th=tM.selected.BS;
int shots=tM.selected.gun.Shots;
if(tM.selected.gun.isRapidFire && ((tM.selected.gun.Range*15)>=Distance)){
shots=2;
}
int hits=d6.roll(shots, Th);//roll to hit.
int wounds=d6.roll(hits, Tw);;//roll to wound.
while(wounds>0){
if(d6.roll(1,Sv)==1){//wound was saved
System.out.println("Wound saved");
}
else{//wound was not saved
tM.targeted.W--;
if (tM.targeted.W==0){
tM.targeted.die(root);
return;
}
}
wounds--;//
}
}
}
/**
*TO DO
* @param tM
* @param distance
*/
public void charge(turnManager tM, int distance, Pane root) {
// To do method
}
public void setPos(double x, double y) {
3. rep.setCenterX(x);
rep.setCenterY(y);
}
public double getPosX() {
return rep.getCenterX();
}
public double getPosY() {
return rep.getCenterY();
}
public void die(Pane root) {
alive = false;
root.getChildren().remove(rep);
}
public void handleMouseClick(MouseEvent e, turnManager tM, Circle movementAssist, Pane
root, Line targetingAssist) {
if (tM.getTurn() == team) {
if (tM.isMove()) {
//select the unit and have the circle move it
tM.selected = this;
if (!root.getChildren().contains(movementAssist)){
root.getChildren().add(movementAssist);
}
movementAssist.setCenterX(rep.getCenterX());
movementAssist.setCenterY(rep.getCenterY());
}
if (tM.isShoot()) {
//select the unit that is shooting
tM.selected = this;
targetingAssist.setStartX(rep.getCenterX());
targetingAssist.setStartY(rep.getCenterY());
}
if (tM.isAssualt()) {
//select the unit to charge
tM.selected = this;
4. targetingAssist.setStartX(rep.getCenterX());
targetingAssist.setStartY(rep.getCenterY());
}
} else {
if (tM.isMove()) {
return;
}
if (tM.isShoot()) {
tM.targeted = this;
targetingAssist.setEndX(rep.getCenterX());
targetingAssist.setEndY(rep.getCenterY());
Point2D p1 = new Point2D(targetingAssist.getStartX(), targetingAssist.getStartY());
Point2D p2 = new Point2D(targetingAssist.getEndX(), targetingAssist.getEndY());
int distance = (int) p1.distance(p2);
if (!tM.selected.shot) {//see if that unit had shot yet
D6 d6=new D6();
Tables table=new Tables();
shoot(tM, distance, d6, table, root);//resolve shooting attack
}
}
if (tM.isAssualt()) {
tM.targeted = this;
targetingAssist.setEndX(rep.getCenterX());
targetingAssist.setEndY(rep.getCenterY());
Point2D p1 = new Point2D(targetingAssist.getStartX(), targetingAssist.getStartY());
Point2D p2 = new Point2D(targetingAssist.getEndX(), targetingAssist.getEndY());
int distance = (int) p1.distance(p2);
if (!tM.selected.charged) {
charge(tM, distance);
}
}
}
}
public String toString() {
return ("WS, " + WS + " BS, " + BS + " S, " + S + " T," + T + " W, " + W + " I, " + I +
" A, " + A
5. + " LD, " + Ld + " SV, " + Sv);
}
}
public class D6 {
public int roll(int num, int success) {
int sum = 0;
for (int i = 0; i < num; i++) {
if ((((int) (Math.random() * 6)) + 1) >= success) {
sum += 1;
}
}
return sum;
}
public int charge(){ return ((((int)Math.random()*6)+1)+(((int)Math.random()*6)+1));}
}
Solution
public void charge(turnManager tM, int distance) {
}
}
Here is the all class.
public abstract class unit {
String name;
int WS;//unit's weaponskill
int BS;//unit's ballistic skill
int S;//unit's strength
int T;//unit's toughness
int W;//unit's wounds
int I;//Unit's initiave
int A;//unit's number of close combat attacks
int Ld;//unit's leadership
int Sv;//unit's ARMOR save
Weapon gun;
Weapon melee;
6. boolean shot;
boolean moved;
boolean charged;
boolean team;
Circle rep;
boolean alive
public void shoot(turnManager tM, int Distance,D6 d6,Tables t,Pane root) {
if(tM.selected.shot){
System.out.println("Unit has allready shot");
return;
}
tM.selected.shot=true;
if((tM.selected.gun.Range*30)>=Distance){
int Tw=t.getToWound(gun.S, tM.targeted.T);
int Th=tM.selected.BS;
int shots=tM.selected.gun.Shots;
if(tM.selected.gun.isRapidFire && ((tM.selected.gun.Range*15)>=Distance)){
shots=2;
}
int hits=d6.roll(shots, Th);//roll to hit.
int wounds=d6.roll(hits, Tw);;//roll to wound.
while(wounds>0){
if(d6.roll(1,Sv)==1){//wound was saved
System.out.println("Wound saved");
}
else{//wound was not saved
tM.targeted.W--;
if (tM.targeted.W==0){
tM.targeted.die(root);
return;
}
}
wounds--;//
}
}
}
7. /**
*TO DO
* @param tM
* @param distance
*/
public void charge(turnManager tM, int distance, Pane root) {
// To do method
}
public void setPos(double x, double y) {
rep.setCenterX(x);
rep.setCenterY(y);
}
public double getPosX() {
return rep.getCenterX();
}
public double getPosY() {
return rep.getCenterY();
}
public void die(Pane root) {
alive = false;
root.getChildren().remove(rep);
}
public void handleMouseClick(MouseEvent e, turnManager tM, Circle movementAssist, Pane
root, Line targetingAssist) {
if (tM.getTurn() == team) {
if (tM.isMove()) {
//select the unit and have the circle move it
tM.selected = this;
if (!root.getChildren().contains(movementAssist)){
root.getChildren().add(movementAssist);
}
movementAssist.setCenterX(rep.getCenterX());
movementAssist.setCenterY(rep.getCenterY());
8. }
if (tM.isShoot()) {
//select the unit that is shooting
tM.selected = this;
targetingAssist.setStartX(rep.getCenterX());
targetingAssist.setStartY(rep.getCenterY());
}
if (tM.isAssualt()) {
//select the unit to charge
tM.selected = this;
targetingAssist.setStartX(rep.getCenterX());
targetingAssist.setStartY(rep.getCenterY());
}
} else {
if (tM.isMove()) {
return;
}
if (tM.isShoot()) {
tM.targeted = this;
targetingAssist.setEndX(rep.getCenterX());
targetingAssist.setEndY(rep.getCenterY());
Point2D p1 = new Point2D(targetingAssist.getStartX(), targetingAssist.getStartY());
Point2D p2 = new Point2D(targetingAssist.getEndX(), targetingAssist.getEndY());
int distance = (int) p1.distance(p2);
if (!tM.selected.shot) {//see if that unit had shot yet
D6 d6=new D6();
Tables table=new Tables();
shoot(tM, distance, d6, table, root);//resolve shooting attack
}
}
if (tM.isAssualt()) {
tM.targeted = this;
targetingAssist.setEndX(rep.getCenterX());
targetingAssist.setEndY(rep.getCenterY());
Point2D p1 = new Point2D(targetingAssist.getStartX(), targetingAssist.getStartY());
Point2D p2 = new Point2D(targetingAssist.getEndX(), targetingAssist.getEndY());
9. int distance = (int) p1.distance(p2);
if (!tM.selected.charged) {
charge(tM, distance);
}
}
}
}
public String toString() {
return ("WS, " + WS + " BS, " + BS + " S, " + S + " T," + T + " W, " + W + " I, " + I +
" A, " + A
+ " LD, " + Ld + " SV, " + Sv);
}
}
public class D6 {
public int roll(int num, int success) {
int sum = 0;
for (int i = 0; i < num; i++) {
if ((((int) (Math.random() * 6)) + 1) >= success) {
sum += 1;
}
}
return sum;
}
public int charge(){ return ((((int)Math.random()*6)+1)+(((int)Math.random()*6)+1));}
}