Commit a0fb468d authored by Darth Vernon's avatar Darth Vernon

Main Comments

parent 2b5e50d5
......@@ -24,7 +24,7 @@
<Button fx:id="fire" layoutX="532.0" layoutY="617.0" mnemonicParsing="false" onAction="#fireAction" prefHeight="28.0" prefWidth="184.0" text="*FIRE*" />
<Button fx:id="quit" layoutX="532.0" layoutY="646.0" mnemonicParsing="false" onAction="#quitAction" prefHeight="25.0" prefWidth="184.0" text="*QUIT*" />
<Button fx:id="battleStage" layoutX="716.0" layoutY="646.0" mnemonicParsing="false" onAction="#stageAction" prefHeight="25.0" prefWidth="184.0" text="*BATTLE STAGE*" />
<Label fx:id="status" layoutX="165.0" layoutY="673.0" prefHeight="25.0" prefWidth="455.0" text="Status : NO HITS" />
<Label fx:id="status" layoutX="165.0" layoutY="673.0" prefHeight="25.0" prefWidth="455.0" text="Ships left:" />
<GridPane gridLinesVisible="true" layoutX="8.0" layoutY="20.0" prefHeight="552.0" prefWidth="501.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
......@@ -283,8 +283,8 @@
</GridPane>
<Label fx:id="occupiedSpace" layoutX="165.0" layoutY="690.0" prefHeight="17.0" prefWidth="734.0" text="Your occupied coordinates are :" />
<Label fx:id="enemyHM" layoutX="532.0" layoutY="600.0" prefHeight="17.0" prefWidth="368.0" text="Computer: Prepare to die!" />
<Label fx:id="takenShips" layoutY="707.0" prefHeight="17.0" prefWidth="501.0" text="Taken Ships:" />
<Label fx:id="lostShips" layoutX="509.0" layoutY="707.0" prefHeight="17.0" prefWidth="540.0" text="Lost Ships:" />
<Label fx:id="takenShips" layoutX="54.0" layoutY="707.0" prefHeight="17.0" prefWidth="992.0" text="No ships taken yet" />
<Label fx:id="lostShips" layoutX="54.0" layoutY="727.0" prefHeight="17.0" prefWidth="992.0" text="No ships lost.. yet" />
<Label fx:id="hits" layoutX="165.0" layoutY="572.0" prefHeight="17.0" prefWidth="344.0" text="Hits:" />
</children>
</AnchorPane>
......
......@@ -26,55 +26,57 @@ import javafx.stage.Stage;
public class BSMain extends Application{
@FXML public ChoiceBox<String> DPx;
@FXML public ChoiceBox<String> DPy;
@FXML public ChoiceBox<String> shipSelection;
@FXML public Label occupiedSpace;
@FXML public Label enemyHM;
@FXML public ChoiceBox<String> DPx; // Choice box for the user to insert an X coordinate.
@FXML public ChoiceBox<String> DPy; // Choice box for the user to insert an Y coordinate.
@FXML public ChoiceBox<String> shipSelection; //Choice box for the user to select which ship to place.
@FXML public Label lostShips;
@FXML public Label takenShips;
@FXML public Label hits;
ArrayList<String> hitsList = new ArrayList<String>();
ArrayList<String> lostShipsList = new ArrayList<String>();
ArrayList<String> takenShipsList = new ArrayList<String>();
@FXML public Label occupiedSpace; // This label will display all current user occupied space, as an alternative to the grid display.
@FXML public Label enemyHM; //This label will display users last move as a hit or miss.
@FXML private Button place;
@FXML private Button fire;
@FXML private Button battleStage;
@FXML private Button quit;
@FXML public Label lostShips; // This label will inform the user of all ships sunk by the computer. As an alternative to the grid display.
@FXML public Label takenShips; // This label will inform the user of all ships they have sunk. As an alternative to the grid display.
@FXML public Label hits; // This label will inform the user of all the places they have had a hit on the computer grid, As an alternative to the grid display.
@FXML public Label stage;
@FXML public Label admiral;
@FXML public Label status;
ArrayList<String> hitsList = new ArrayList<String>(); //Holds the data for the corresponding label above.
ArrayList<String> lostShipsList = new ArrayList<String>(); //Holds the data for the corresponding label above.
ArrayList<String> takenShipsList = new ArrayList<String>(); //Holds the data for the corresponding label above.
@FXML private Button place; //Runs the code, in relation to placing a ship.
@FXML private Button fire; //Runs the code in relation to firing on the computer grid.
@FXML private Button battleStage; //Runs the code which furthers the game from the placing stage to the battle stage.
@FXML private Button quit; //Ends the game.
@FXML public Label stage; //This label will inform the user of which stage of the game they are in
@FXML public Label admiral; //This label will display: hits, misses, rejections, and general information for the user.
@FXML public Label status; //This label will display the number of occupied spaces left on player grid and computer grid.
ObservableList<String> numbrz = FXCollections.observableArrayList("0","1","2","3","4","5","6","7","8","9");
ObservableList<String> ltrz = FXCollections.observableArrayList("A","B","C","D","E","F","G","H","I","J");
ObservableList<String> numbrz = FXCollections.observableArrayList("0","1","2","3","4","5","6","7","8","9"); // Holds data for DPx.
ObservableList<String> ltrz = FXCollections.observableArrayList("A","B","C","D","E","F","G","H","I","J"); // Holds data for DPy.
static ObservableList<String> ships= FXCollections.observableArrayList("Aircraft", "BattleShip","BattleShip Two", "Destroyer One" , "Destroyer Two", "Patrol Boat One" ,
"Patrol Boat Two" , "Patrol Boat Three");
"Patrol Boat Two" , "Patrol Boat Three"); //Holds data for shipSelection.
String x;
String y;
String ship;
String x; //Holds entered X coordinate.
String y; //Holds entered y coordinate.
String ship; //Holds entered ship.
static Grid playerG = new Grid();
static Grid computerG = new Grid();
static ShipClass playerS = new ShipClass();
static ShipClass computerS = new ShipClass();
static BattleClass playerB = new BattleClass();
static BattleClass computerB = new BattleClass();
static Grid playerG = new Grid(); //Creates new grid, related variables and lists for player only.
static Grid computerG = new Grid(); //Creates new grid, related variables and lists for computer only.
static ShipClass playerS = new ShipClass(); //Creates new ships, related variables and lists for player only.
static ShipClass computerS = new ShipClass(); //Creates new ships, related variables and lists for computer only.
static BattleClass playerB = new BattleClass(); //Creates new battle related variables and lists for player only.
static BattleClass computerB = new BattleClass();//Creates new battle related variables and lists for player only.
Boolean stage0= true;
Boolean stage1=false;
Boolean stage0= true; //Activates placing stage upon the start of the programme.
Boolean stage1=false; //Sets the battle stage to false until the user chooses to change the stage.
@Override
public void start(Stage arg0) throws Exception {
// TODO Auto-generated method stub
Parent root = FXMLLoader.load(getClass().getResource("/BSGUI.fxml"));
Parent root = FXMLLoader.load(getClass().getResource("/BSGUI.fxml")); // Loads GUI created in scene builder.
......@@ -94,9 +96,9 @@ public class BSMain extends Application{
@FXML
public void initialize(){
DPx.getItems().addAll(numbrz);
DPy.getItems().addAll(ltrz);
shipSelection.getItems().addAll(ships);
DPx.getItems().addAll(numbrz); //Loads options of X coordinates into the choice box DPx
DPy.getItems().addAll(ltrz); //Loads options of Y coordinates into the choice box DPy
shipSelection.getItems().addAll(ships); //Loads options of ships into the choice box shipSelection.
......@@ -104,45 +106,48 @@ public class BSMain extends Application{
x=(newValue.toString());
System.out.println(x);
});
//This listener will update the X coordinate as the user changes it.
DPy.getSelectionModel().selectedItemProperty().addListener((ObservableValue, oldValue, newValue) -> {
y=(newValue.toString());
System.out.println(y);
});
//This listener will update the Y coordinate as the user changes it.
shipSelection.getSelectionModel().selectedItemProperty().addListener((ObservableValue, oldValue, newValue) -> {
ship=(newValue.toString());
System.out.println(ship);
});
//This listener will update the ship as the user changes it.
}
@FXML
void placeAction(ActionEvent event) {
if (stage0) {
if (stage0) { // if placement stage.
String response = "";
if (!playerS.ShipPlacer(playerS,ship, x, y).isEmpty()) {
if (!playerS.ShipPlacer(playerS,ship, x, y).isEmpty()) { //Tests for an empty list.
response = playerS.moves.toString();
admiral.setText("Admiral Nutsey: Your occupied coordinates are: " + response);
response = playerS.moves.toString(); // Response holds moves generated by shipPlacer.
admiral.setText("Admiral Nutsey: Your occupied coordinates are: " + response); // The "Admiral" will inform the user of placed ship.
}
else {
admiral.setText("Admiral: Ship type is already Placed");
//shipPlacer has returned an empty list, therefore ship must have been placed already.
}
if (!playerG.MoveValidCheck(playerG, playerS.moves, playerS, ship)){
admiral.setText("Admiral Nutsey: Space occupied");
//Attempted moves are rejected by MoveValidCheck, as one of the coordinates on placer grid is already taken.
}
}
} //if(stage0) END
else if (stage1) {
else if (stage1) { // if battle stage.
admiral.setText("Admiral Nutsey: Your in Battle, try *FIRE*");
//Player is in battle stage thus no more ships can be placed.
}
occupiedSpace.setText(playerS.allMoves.toString());
occupiedSpace.setText(playerS.allMoves.toString()); //Coordinates generated, passed all checks and can be added to players allMoves list.
}
......@@ -151,82 +156,86 @@ public class BSMain extends Application{
@FXML
void fireAction(ActionEvent event) {
if(playerS.allMoves.isEmpty()) {
Boolean win = true;
stage1 = false;
admiral.setText("Admiral Nutsey: You have lost :( ");
enemyHM.setText(ComputerInput.talk(win));
if(playerS.allMoves.isEmpty()) { //Checks for complete decimation of player ships.
Boolean win = true; //player has won.
stage1 = false; //both stages are now inactive because the game has finished.
admiral.setText("Admiral Nutsey: You have lost :( "); //"Admiral" informs user of loss.
enemyHM.setText(ComputerInput.talk(win)); //Computer responds with randomised taunt #Cool
stage.setText("Stage: GAME OVER");
stage.setText("Stage: GAME OVER"); //Updates stage label to current state.
}
else if(computerS.allMoves.isEmpty()) {
Boolean win = false;
stage1 = false;
admiral.setText("Admiral Nutsey: You have won!!!");
enemyHM.setText(ComputerInput.talk(win));
else if(computerS.allMoves.isEmpty()) { //Checks for complete decimation of computer ships.
Boolean win = false; //player has lost.
stage1 = false; //both stages are now inactive because the game has finished.
admiral.setText("Admiral Nutsey: You have won!!!"); //"Admiral" informs user of win.
enemyHM.setText(ComputerInput.talk(win)); //Computer responds with randomised response to loss #Cool
stage.setText("Stage: GAME OVER"); //Updates stage label to current state.
}
else if (stage1) {
String HmnMove = x + y;
String HmnMove = x + y; //Takes x y and creates the coordinate fired upon.
String CmptrMove = (String) ComputerInput.CoOrdRandomiser().get(0)
+ (String) ComputerInput.CoOrdRandomiser().get(1);
if (playerB.fire(computerS, HmnMove)) {
String response = BattleClass.ShipCheck(HmnMove, computerS);
admiral.setText("Player has " + response + HmnMove );
hitsList.add(HmnMove);
hits.setText("You have hit:" + hitsList.toString());
if (response != "Hit ") {
takenShipsList.add(response);
lostShips.setText("You have lost:" + takenShipsList.toString());
//Calls CoordRandomiser from computer input class to create the coordinate fired upon.
if (playerB.fire(computerS, HmnMove)) { //Calls fire method, which returns a boolean, based on if fire was successful.
String response = BattleClass.ShipCheck(HmnMove, computerS); //Checks for sunk ships and returns a response.
admiral.setText("Player has " + response + HmnMove ); //Admiral displays hit / sunk / miss.
hitsList.add(HmnMove); //Hit is recored.
hits.setText("You have hit:" + hitsList.toString()); //All hits player does is displayed through out.
if (response != "Hit ") { //Checks for a sunk response, anything other than just Hit.
takenShipsList.add(response); //Sunk ship recorded.
takenShips.setText("You have taken:" + takenShipsList.toString()); //Sunk ships displayed throughout.
}
}
else if (!playerB.fire(computerS, HmnMove)) {
admiral.setText("Player has missed at " + HmnMove);
else if (!playerB.fire(computerS, HmnMove)) { // Fire returned false, thus player has missed.
admiral.setText("Player has missed at " + HmnMove); //User informed of miss.
}
if (computerB.fire(playerS, CmptrMove)) {
if (computerB.fire(playerS, CmptrMove)) { // All methods and variables work in same way as above, modifications / additions explained.
String response = BattleClass.ShipCheck(CmptrMove, playerS);
enemyHM.setText("Computer: I have hit " + response + CmptrMove );
if (response != "Hit ") {
lostShipsList.add(response);
lostShips.setText("You have lost:" + lostShipsList.toString());
}
}
} // if (computer fire) END
else if (!computerB.fire(playerS, CmptrMove)) {
enemyHM.setText("Computer: I have missed at " + CmptrMove);
}
status.setText("Computer status: " + computerS.allMoves.size() + " Player Status: " + playerS.allMoves.size());
status.setText("Ships left: " + computerS.allMoves.size() + " Player Status: " + playerS.allMoves.size()); // Number left of both players ships updated and displayed.
}
else if (stage0) {
admiral.setText("Place all ships before firing");
admiral.setText("Place all ships before firing"); //Placement stage not completed.
}
occupiedSpace.setText(playerS.allMoves.toString());
occupiedSpace.setText(playerS.allMoves.toString()); // Label updated due to possible loss of ships.
}
@FXML
void stageAction(ActionEvent event) {
void stageAction(ActionEvent event) { // Activates battle stage and disables placing stage
if (playerS.allMoves.size() == 25) {
stage0 = false;
stage1 = true;
admiral.setText("Admiral Nutsey: Fire on the enemy");
stage.setText("Stage: Battle");
}
}
@FXML
void quitAction(ActionEvent event) {
//Add save function
System.exit(0);
System.exit(0); //END OF GAME
}
......@@ -236,13 +245,13 @@ public class BSMain extends Application{
playerG.CoOrdGen(playerG);
playerG.CoOrdGen(playerG); //Generates Coordinates for both player and computer grids.
computerG.CoOrdGen(computerG);
ComputerInput.cmptrMoveGen();
ComputerInput.cmptrMoveGen(); //Computer places its ships
launch(args);
launch(args); //Application launch
}
......
public class Players {
Grid GC = new Grid();
ShipClass SC = new ShipClass();
BattleClass BC = new BattleClass();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment