Can you just write the documentation of this code? import java.io.IOException; i
ID: 3838989 • Letter: C
Question
Can you just write the documentation of this code?
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.IntStream;
import javafx.animation.PathTransition;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import javafx.stage.*;
import javafx.util.Duration;
public class Battle extends Application {
private final int ROUND = 3;
private final int SIZE = 6;
private Monster[] monsters = new Monster[SIZE];
private final int sceneWidth = 900;
private final int sceneHeight = 800;
private PathTransition path1;
private PathTransition path2;
private int HydronsScore = 0;
private int ZexorScore = 0;
private final Label HydronLabel = new Label("HYDRON SCORES: 0");
private final Label ZexorLabel = new Label("ZEXOR SCORES: 0");
private final HBox HydronsSide = new HBox();
private final HBox ZexorsSide = new HBox();
private final GridPane battlePane = new GridPane();
private Pane mainPane = new Pane();
private Button play = new Button("PLAY GAME");
private Label status = new Label();
@Override
public void start(Stage primaryStage) throws IOException, URISyntaxException {
Scene battleScene = new Scene(mainPane, sceneWidth, sceneHeight);
Colorcode pickColor = new Colorcode();
battlePane.setPadding(new Insets(40, 40, 40, 40));
play.setPrefWidth(200);
play.setMinWidth(play.getPrefWidth()/2);
LoginInfo LoginInfoInOut = new LoginInfo();
play.setStyle("-fx-font: 12 verdana; -fx-base: #FFD732; -fx-font-weight: bold;");
mainPane.setStyle("-fx-background-color:#4682B4;");
mainPane.getChildren().add(battlePane);
HydronLabel.setStyle("-fx-text-fill: white");
HydronLabel.setStyle("-fx-border-color: black;-fx-border-width: 2px");
ZexorLabel.setStyle("-fx-text-fill: white");
ZexorLabel.setStyle("-fx-border-color: black;-fx-border-width: 2px");
HydronsSide.setSpacing(10);
battlePane.setHgap(40);
battlePane.setVgap(40);
status.setWrapText(true);
status.setStyle("-fx-font: 13 verdana; -fx-text-fill: #FFB400; -fx-font-weight: bold; -fx-border-width: 2px");
monsters[0] = new Hydron("FRED");
monsters[1] = new Hydron("SAMO");
monsters[2] = new Hydron("MIGUEL");
monsters[3] = new Zexor("SALLY");
monsters[4] = new Zexor("MARK");
monsters[5] = new Zexor("NISTA");
if (LoginInfoInOut.LoginInfo()) {
if (pickColor.chooseColorPane(monsters)) {
play.setOnAction(e -> {
String statusString;
statusString = setPlayGame();
status.setText(statusString);
for (int i = 0; i < 6; ++i) {
if (monsters[i].getHealth() == 0) {
play.setDisable(true);
}
}
try {
LoginInfoInOut.SaveFile(statusString);
}
catch (IOException ex) {
Logger.getLogger(Monster.class.getName()).log(Level.SEVERE, null, ex);
}
});
primaryStage.setScene(battleScene);
primaryStage.show();
}
HydronsSide.setAlignment(Pos.CENTER);
HydronsSide.getChildren().addAll(monsters[0], monsters[1], monsters[2]);
ZexorsSide.setAlignment(Pos.CENTER);
ZexorsSide.getChildren().addAll(monsters[3], monsters[4], monsters[5]);
battlePane.add(ZexorsSide, 3, 0);
battlePane.add(HydronsSide, 3, 5);
battlePane.add(status, 3,6);
battlePane.add(play, 3,7 );
battlePane.add(ZexorLabel, 4, 0);
battlePane.add(HydronLabel, 4, 5);
}
}
private String playGame (int attacker, int defender) {
int[] HydronSide = null;
int[] ZexorSide = null;
int sumScoreHydron;
int sumScoreZexor;
int newHealth = 0;
String win = "", lose = "";
int winSum = 0, loseSum = 0;
String HydronName = "", ZexorName = "";
String status;
if (monsters[attacker] instanceof Hydron) {
HydronName = monsters[attacker].getName();
ZexorName = monsters[defender].getName();
HydronSide = monsters[attacker].rollDice();
ZexorSide = monsters[defender].rollDice();
}
else if (monsters[attacker] instanceof Zexor) {
HydronName = monsters[defender].getName();
ZexorName = monsters[attacker].getName();
HydronSide = monsters[defender].rollDice();
ZexorSide = monsters[attacker].rollDice();
}
sumScoreHydron = IntStream.of(HydronSide).sum();
sumScoreZexor = IntStream.of(ZexorSide).sum();
System.err.println(sumScoreHydron);
System.err.println(sumScoreZexor);
if (monsters[attacker] instanceof Hydron) {
if (sumScoreHydron > sumScoreZexor) {
//System.err.println("I'm Here Hydron");
win = monsters[attacker].getName();
winSum = sumScoreHydron;
lose = monsters[defender].getName();
loseSum = sumScoreZexor;
newHealth = sumScoreHydron - sumScoreZexor;
HydronsScore += newHealth;
HydronLabel.setText("HYDRON SCORE: " + HydronsScore);
setHealth(newHealth, monsters[defender]);
monsters[defender].sizeChange();
}}
else if (monsters[attacker] instanceof Zexor) {
if (sumScoreZexor > sumScoreHydron) {
//System.err.println("I'm here Zexor");
win = monsters[attacker].getName();
winSum = sumScoreZexor;
lose = monsters[defender].getName();
loseSum = sumScoreHydron;
newHealth = sumScoreZexor - sumScoreHydron;
ZexorScore += newHealth;
ZexorLabel.setText("ZEXOR SCORE: " + ZexorScore);
setHealth(newHealth, monsters[defender]);
monsters[defender].sizeChange();
}
}
if (win != "" && lose != "") {
status=monsters[attacker].getName() + " battles "
+ monsters[defender].getName() + ".... "
+ win + " wins this round with " + winSum + " to "
+ lose + "'s " + loseSum +". "
+ lose + " loses " + newHealth +". ";
}
else {
status = monsters[attacker].getName() + " Total Roll: " + winSum + " "
+ monsters[defender].getName() + " Total Roll:" + loseSum ;
}
for (int i = 0; i < 6; ++i) {
if (monsters[i].getHealth() == 0) {
status = "- THE GAME END -";
}
}
return status;
}
private void setHealth(int newHealth, Monster obj) {
obj.setHealth(obj.getHealth() - newHealth);
}
private String setPlayGame() {
int diceSide;
int diceRoll;
int attacker;
int defender;
String status;
diceSide = RandomDiceSide();
diceRoll = RandomDiceRound();
attacker = Attacker();
defender = Defender(attacker);
centerPoint(attacker, defender);
monsters[attacker].setDiceSide(diceSide);
monsters[attacker].setDiceRoll(diceRoll);
monsters[defender].setDiceSide(diceSide);
monsters[defender].setDiceRoll(diceRoll);
status = playGame(attacker, defender);
return status;
}
private int Attacker() {
Random number = new Random();
int temp;
temp = number.nextInt(5) + 0;
return temp;
}
private int Defender(int attacker) {
Random number = new Random();
int temp = 0;
if (attacker >= 3) {
temp = number.nextInt(2);
} else if (attacker < 3) {
temp = number.nextInt((5 - 3) + 1) + 3;
}
return temp;
}
private int RandomDiceSide() {
Random number = new Random();
int temp;
do {
temp = number.nextInt((20 - 2) + 1) + 2;
} while (temp % 2 != 0);
return temp;
}
private int RandomDiceRound() {
Random number = new Random();
int temp;
temp = number.nextInt(4) + 1;
return temp;
}
private void centerPoint(int attacker, int defender) {
if (attacker >= 3 && defender < 3) {
centerHyron(defender);
goBackHydron(defender);
startGameZexor(attacker);
goBackZexor(attacker);
} else if (attacker < 3 && defender >= 3) {
centerHyron(attacker);
goBackHydron(attacker);
startGameZexor(defender);
goBackZexor(defender);
}
}
private void centerHyron(int num) {
int startX = 50, startY = 80;
int endX = 50, endY = -80;
path1 = new PathTransition(Duration.millis(1000), new Line(startX, startY, endX, endY), monsters[num]);
path1.setCycleCount(1);
path1.play();
}
private void goBackHydron(int num) {
int endX = 50, endY = 80;
int startX = 50, startY = -80;
path1 = new PathTransition(Duration.millis(1000), new Line(startX, startY, endX, endY), monsters[num]);
path1.setCycleCount(1);
path1.setDelay(Duration.millis(1000));
path1.play();
}
private void goBackZexor(int num) {
int endX = 50, endY = 80;
int startX = 50, startY = 150;
path1 = new PathTransition(Duration.millis(1000), new Line(startX, startY, endX, endY), monsters[num]);
path1.setCycleCount(1);
path1.setDelay(Duration.millis(1000));
path1.play();
}
private void startGameZexor(int num) {
int startX = 50, startY = 80;
int endX = 50, endY = 150;
path2 = new PathTransition(Duration.millis(1000), new Line(startX, startY, endX, endY), monsters[num]);
path2.setCycleCount(1);
path2.play();
}
public static void main(String[] args) {
launch(args);
}
public int getRound() {
return 0;
}
public int getDiceSide() {
return 0;
}
interface Action {
public void sizeChange();
}
}
Explanation / Answer
// Package being imported
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.IntStream;
import javafx.animation.PathTransition;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import javafx.stage.*;
import javafx.util.Duration;
//Battle class inherits Application class
public class Battle extends Application {
// Round given is 3
private final int ROUND = 3;
// Size as 6 as army
private final int SIZE = 6;
// Monster as an array
private Monster[] monsters = new Monster[SIZE];
// Screen size and width
private final int sceneWidth = 900;
private final int sceneHeight = 800;
// Path declartion
private PathTransition path1;
private PathTransition path2;
// Initial score
private int HydronsScore = 0;
private int ZexorScore = 0;
// Labels declartion to show in game
private final Label HydronLabel = new Label("HYDRON SCORES: 0");
private final Label ZexorLabel = new Label("ZEXOR SCORES: 0");
// Hydron and zexrin as showing as box
private final HBox HydronsSide = new HBox();
private final HBox ZexorsSide = new HBox();
// setting up scene
private final GridPane battlePane = new GridPane();
// Setting up the bacnkground
private Pane mainPane = new Pane();
// Setting up Button
private Button play = new Button("PLAY GAME");
// setting up Label for status
private Label status = new Label();
@Override
public void start(Stage primaryStage) throws IOException, URISyntaxException {
// with exception hadling
// Scene created with specified constant
Scene battleScene = new Scene(mainPane, sceneWidth, sceneHeight);
// Color code to be used as class
Colorcode pickColor = new Colorcode();
// setting up padding away from sides
battlePane.setPadding(new Insets(40, 40, 40, 40));
// setting play width and minimum width
play.setPrefWidth(200);
play.setMinWidth(play.getPrefWidth()/2);
// Login into game
LoginInfo LoginInfoInOut = new LoginInfo();
//Setting up style as bold and verdana as font
play.setStyle("-fx-font: 12 verdana; -fx-base: #FFD732; -fx-font-weight: bold;");
// Background color
mainPane.setStyle("-fx-background-color:#4682B4;");
// adding this to main scene
mainPane.getChildren().add(battlePane);
// Hydron and zexron style setting up
HydronLabel.setStyle("-fx-text-fill: white");
HydronLabel.setStyle("-fx-border-color: black;-fx-border-width: 2px");
ZexorLabel.setStyle("-fx-text-fill: white");
ZexorLabel.setStyle("-fx-border-color: black;-fx-border-width: 2px");
// Setting up spacing and gap and Wrapping up the Text
HydronsSide.setSpacing(10);
battlePane.setHgap(40);
battlePane.setVgap(40);
// Show the status text and its style
status.setWrapText(true);
status.setStyle("-fx-font: 13 verdana; -fx-text-fill: #FFB400; -fx-font-weight: bold; -fx-border-width: 2px");
// Hydron name as monster
monsters[0] = new Hydron("FRED");
monsters[1] = new Hydron("SAMO");
monsters[2] = new Hydron("MIGUEL");
monsters[3] = new Zexor("SALLY");
monsters[4] = new Zexor("MARK");
monsters[5] = new Zexor("NISTA");
// login as choosing color for monster
if (LoginInfoInOut.LoginInfo()) {
if (pickColor.chooseColorPane(monsters)) {
// Setting up game
play.setOnAction(e -> {
String statusString;
statusString = setPlayGame();
status.setText(statusString);
// Initial health of all monsters
for (int i = 0; i < 6; ++i) {
if (monsters[i].getHealth() == 0) {
play.setDisable(true);
}
}
// saving info to a file of status
try {
LoginInfoInOut.SaveFile(statusString);
}
catch (IOException ex) {
Logger.getLogger(Monster.class.getName()).log(Level.SEVERE, null, ex);
}
});
// displaying battle scene
primaryStage.setScene(battleScene);
primaryStage.show();
}
// alignment as center and showing up monster
HydronsSide.setAlignment(Pos.CENTER);
HydronsSide.getChildren().addAll(monsters[0], monsters[1], monsters[2]);
ZexorsSide.setAlignment(Pos.CENTER);
ZexorsSide.getChildren().addAll(monsters[3], monsters[4], monsters[5]);
// adding all monster and information on battlepane
battlePane.add(ZexorsSide, 3, 0);
battlePane.add(HydronsSide, 3, 5);
battlePane.add(status, 3,6);
battlePane.add(play, 3,7 );
battlePane.add(ZexorLabel, 4, 0);
battlePane.add(HydronLabel, 4, 5);
}
}
// game class with attacker and defender
private String playGame (int attacker, int defender) {
// Initial values
int[] HydronSide = null;
int[] ZexorSide = null;
int sumScoreHydron;
int sumScoreZexor;
int newHealth = 0;
String win = "", lose = "";
int winSum = 0, loseSum = 0;
String HydronName = "", ZexorName = "";
String status;
// monster attack Hydron and it conditions
if (monsters[attacker] instanceof Hydron) {
HydronName = monsters[attacker].getName();
ZexorName = monsters[defender].getName();
HydronSide = monsters[attacker].rollDice();
ZexorSide = monsters[defender].rollDice();
}
// if attacking zexor get name and rolls dice
else if (monsters[attacker] instanceof Zexor) {
HydronName = monsters[defender].getName();
ZexorName = monsters[attacker].getName();
HydronSide = monsters[defender].rollDice();
ZexorSide = monsters[attacker].rollDice();
}
// calucalate their sum
sumScoreHydron = IntStream.of(HydronSide).sum();
sumScoreZexor = IntStream.of(ZexorSide).sum();
System.err.println(sumScoreHydron);
System.err.println(sumScoreZexor);
// Monster is instance of Hyderon
if (monsters[attacker] instanceof Hydron) {
if (sumScoreHydron > sumScoreZexor) {
//System.err.println("I'm Here Hydron");
// get the name of monster
win = monsters[attacker].getName();
// increarse score
winSum = sumScoreHydron;
lose = monsters[defender].getName();
// get loose name and score
loseSum = sumScoreZexor;
// change healths
newHealth = sumScoreHydron - sumScoreZexor;
HydronsScore += newHealth;
HydronLabel.setText("HYDRON SCORE: " + HydronsScore);
setHealth(newHealth, monsters[defender]);
monsters[defender].sizeChange();
}}
// if it is monster of zerox
else if (monsters[attacker] instanceof Zexor) {
if (sumScoreZexor > sumScoreHydron) {
//System.err.println("I'm here Zexor");
// get winner name
win = monsters[attacker].getName();
// get sum of win
winSum = sumScoreZexor;
lose = monsters[defender].getName();
// looser name and score
loseSum = sumScoreHydron;
newHealth = sumScoreZexor - sumScoreHydron;
// health changes recording
ZexorScore += newHealth;
ZexorLabel.setText("ZEXOR SCORE: " + ZexorScore);
setHealth(newHealth, monsters[defender]);
monsters[defender].sizeChange();
}
}
// if win and lose not empty
// get the name
// show the sum and their health as status
if (win != "" && lose != "") {
status=monsters[attacker].getName() + " battles "
+ monsters[defender].getName() + ".... "
+ win + " wins this round with " + winSum + " to "
+ lose + "'s " + loseSum +". "
+ lose + " loses " + newHealth +". ";
}
// els show monster name and total roll
else {
status = monsters[attacker].getName() + " Total Roll: " + winSum + " "
+ monsters[defender].getName() + " Total Roll:" + loseSum ;
}
// if health is zero of all game ends
for (int i = 0; i < 6; ++i) {
if (monsters[i].getHealth() == 0) {
status = "- THE GAME END -";
}
}
return status;
}
// health of monster as things changes
private void setHealth(int newHealth, Monster obj) {
obj.setHealth(obj.getHealth() - newHealth);
}
// setting up the stage
// getting status dice side and roll
// attacker and defender starting
//center them attacker and defender
private String setPlayGame() {
int diceSide;
int diceRoll;
int attacker;
int defender;
String status;
diceSide = RandomDiceSide();
diceRoll = RandomDiceRound();
attacker = Attacker();
defender = Defender(attacker);
centerPoint(attacker, defender);
monsters[attacker].setDiceSide(diceSide);
monsters[attacker].setDiceRoll(diceRoll);
monsters[defender].setDiceSide(diceSide);
monsters[defender].setDiceRoll(diceRoll);
status = playGame(attacker, defender);
return status;
}
// attacker number
private int Attacker() {
Random number = new Random();
int temp;
temp = number.nextInt(5) + 0;
return temp;
}
// defender attacker coming
private int Defender(int attacker) {
Random number = new Random();
int temp = 0;
if (attacker >= 3) {
temp = number.nextInt(2);
} else if (attacker < 3) {
temp = number.nextInt((5 - 3) + 1) + 3;
}
return temp;
}
// get the dice side
private int RandomDiceSide() {
Random number = new Random();
int temp;
do {
temp = number.nextInt((20 - 2) + 1) + 2;
} while (temp % 2 != 0);
return temp;
}
// generate random number for dic
private int RandomDiceRound() {
Random number = new Random();
int temp;
temp = number.nextInt(4) + 1;
return temp;
}
// at center palce if attacker is greater than 3 and defender less than 3
// bring Hydron defender as center
// Start Game as attacker and send back Zexor
private void centerPoint(int attacker, int defender) {
if (attacker >= 3 && defender < 3) {
centerHyron(defender);
goBackHydron(defender);
startGameZexor(attacker);
goBackZexor(attacker);
// else if is not happeing center hydron and start game with zexor as back
} else if (attacker < 3 && defender >= 3) {
centerHyron(attacker);
goBackHydron(attacker);
startGameZexor(defender);
goBackZexor(defender);
}
}
// startgame zexor and setting cooridnate as x and y and setting up path based upon coordinates
private void centerHyron(int num) {
int startX = 50, startY = 80;
int endX = 50, endY = -80;
path1 = new PathTransition(Duration.millis(1000), new Line(startX, startY, endX, endY), monsters[num]);
path1.setCycleCount(1);
path1.play();
}
// startgame zexor and setting cooridnate as x and y and setting up path based upon coordinates
private void goBackHydron(int num) {
int endX = 50, endY = 80;
int startX = 50, startY = -80;
path1 = new PathTransition(Duration.millis(1000), new Line(startX, startY, endX, endY), monsters[num]);
path1.setCycleCount(1);
path1.setDelay(Duration.millis(1000));
path1.play();
}
// startgame zexor and setting cooridnate as x and y and setting up path based upon coordinates
private void goBackZexor(int num) {
int endX = 50, endY = 80;
int startX = 50, startY = 150;
path1 = new PathTransition(Duration.millis(1000), new Line(startX, startY, endX, endY), monsters[num]);
path1.setCycleCount(1);
path1.setDelay(Duration.millis(1000));
path1.play();
}
// startgame zexor and setting cooridnate as x and y and setting up path based upon coordinates
private void startGameZexor(int num) {
int startX = 50, startY = 80;
int endX = 50, endY = 150;
path2 = new PathTransition(Duration.millis(1000), new Line(startX, startY, endX, endY), monsters[num]);
path2.setCycleCount(1);
path2.play();
}
// entry point
public static void main(String[] args) {
launch(args);
}
// get round as 0
public int getRound() {
return 0;
}
// get side of dice return 0
public int getDiceSide() {
return 0;
}
// interace for acton and function sizechanges
interface Action {
public void sizeChange();
}
}