Commit a0fb468d authored by Darth Vernon's avatar Darth Vernon

Main Comments

parent 2b5e50d5
...@@ -24,7 +24,7 @@ ...@@ -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="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="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*" /> <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"> <GridPane gridLinesVisible="true" layoutX="8.0" layoutY="20.0" prefHeight="552.0" prefWidth="501.0">
<columnConstraints> <columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
...@@ -283,8 +283,8 @@ ...@@ -283,8 +283,8 @@
</GridPane> </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="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="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="takenShips" layoutX="54.0" layoutY="707.0" prefHeight="17.0" prefWidth="992.0" text="No ships taken yet" />
<Label fx:id="lostShips" layoutX="509.0" layoutY="707.0" prefHeight="17.0" prefWidth="540.0" text="Lost Ships:" /> <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:" /> <Label fx:id="hits" layoutX="165.0" layoutY="572.0" prefHeight="17.0" prefWidth="344.0" text="Hits:" />
</children> </children>
</AnchorPane> </AnchorPane>
......
...@@ -26,55 +26,57 @@ import javafx.stage.Stage; ...@@ -26,55 +26,57 @@ import javafx.stage.Stage;
public class BSMain extends Application{ public class BSMain extends Application{
@FXML public ChoiceBox<String> DPx; @FXML public ChoiceBox<String> DPx; // Choice box for the user to insert an X coordinate.
@FXML public ChoiceBox<String> DPy; @FXML public ChoiceBox<String> DPy; // Choice box for the user to insert an Y coordinate.
@FXML public ChoiceBox<String> shipSelection; @FXML public ChoiceBox<String> shipSelection; //Choice box for the user to select which ship to place.
@FXML public Label occupiedSpace;
@FXML public Label enemyHM;
@FXML public Label lostShips; @FXML public Label occupiedSpace; // This label will display all current user occupied space, as an alternative to the grid display.
@FXML public Label takenShips; @FXML public Label enemyHM; //This label will display users last move as a hit or miss.
@FXML public Label hits;
ArrayList<String> hitsList = new ArrayList<String>();
ArrayList<String> lostShipsList = new ArrayList<String>();
ArrayList<String> takenShipsList = new ArrayList<String>();
@FXML private Button place; @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 private Button fire; @FXML public Label takenShips; // This label will inform the user of all ships they have sunk. As an alternative to the grid display.
@FXML private Button battleStage; @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 private Button quit;
@FXML public Label stage; ArrayList<String> hitsList = new ArrayList<String>(); //Holds the data for the corresponding label above.
@FXML public Label admiral; ArrayList<String> lostShipsList = new ArrayList<String>(); //Holds the data for the corresponding label above.
@FXML public Label status; 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> 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"); 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" , 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 x; //Holds entered X coordinate.
String y; String y; //Holds entered y coordinate.
String ship; String ship; //Holds entered ship.
static Grid playerG = new Grid(); static Grid playerG = new Grid(); //Creates new grid, related variables and lists for player only.
static Grid computerG = new Grid(); static Grid computerG = new Grid(); //Creates new grid, related variables and lists for computer only.
static ShipClass playerS = new ShipClass(); static ShipClass playerS = new ShipClass(); //Creates new ships, related variables and lists for player only.
static ShipClass computerS = new ShipClass(); static ShipClass computerS = new ShipClass(); //Creates new ships, related variables and lists for computer only.
static BattleClass playerB = new BattleClass(); static BattleClass playerB = new BattleClass(); //Creates new battle related variables and lists for player only.
static BattleClass computerB = new BattleClass(); static BattleClass computerB = new BattleClass();//Creates new battle related variables and lists for player only.
Boolean stage0= true; Boolean stage0= true; //Activates placing stage upon the start of the programme.
Boolean stage1=false; Boolean stage1=false; //Sets the battle stage to false until the user chooses to change the stage.
@Override @Override
public void start(Stage arg0) throws Exception { public void start(Stage arg0) throws Exception {
// TODO Auto-generated method stub // 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{ ...@@ -94,9 +96,9 @@ public class BSMain extends Application{
@FXML @FXML
public void initialize(){ public void initialize(){
DPx.getItems().addAll(numbrz); DPx.getItems().addAll(numbrz); //Loads options of X coordinates into the choice box DPx
DPy.getItems().addAll(ltrz); DPy.getItems().addAll(ltrz); //Loads options of Y coordinates into the choice box DPy
shipSelection.getItems().addAll(ships); shipSelection.getItems().addAll(ships); //Loads options of ships into the choice box shipSelection.
...@@ -104,45 +106,48 @@ public class BSMain extends Application{ ...@@ -104,45 +106,48 @@ public class BSMain extends Application{
x=(newValue.toString()); x=(newValue.toString());
System.out.println(x); System.out.println(x);
}); });
//This listener will update the X coordinate as the user changes it.
DPy.getSelectionModel().selectedItemProperty().addListener((ObservableValue, oldValue, newValue) -> { DPy.getSelectionModel().selectedItemProperty().addListener((ObservableValue, oldValue, newValue) -> {
y=(newValue.toString()); y=(newValue.toString());
System.out.println(y); System.out.println(y);
}); });
//This listener will update the Y coordinate as the user changes it.
shipSelection.getSelectionModel().selectedItemProperty().addListener((ObservableValue, oldValue, newValue) -> { shipSelection.getSelectionModel().selectedItemProperty().addListener((ObservableValue, oldValue, newValue) -> {
ship=(newValue.toString()); ship=(newValue.toString());
System.out.println(ship);
}); });
//This listener will update the ship as the user changes it.
} }
@FXML @FXML
void placeAction(ActionEvent event) { void placeAction(ActionEvent event) {
if (stage0) { if (stage0) { // if placement stage.
String response = ""; 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(); response = playerS.moves.toString(); // Response holds moves generated by shipPlacer.
admiral.setText("Admiral Nutsey: Your occupied coordinates are: " + response); admiral.setText("Admiral Nutsey: Your occupied coordinates are: " + response); // The "Admiral" will inform the user of placed ship.
} }
else { else {
admiral.setText("Admiral: Ship type is already Placed"); 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)){ if (!playerG.MoveValidCheck(playerG, playerS.moves, playerS, ship)){
admiral.setText("Admiral Nutsey: Space occupied"); 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*"); 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{ ...@@ -151,82 +156,86 @@ public class BSMain extends Application{
@FXML @FXML
void fireAction(ActionEvent event) { void fireAction(ActionEvent event) {
if(playerS.allMoves.isEmpty()) { if(playerS.allMoves.isEmpty()) { //Checks for complete decimation of player ships.
Boolean win = true; Boolean win = true; //player has won.
stage1 = false; stage1 = false; //both stages are now inactive because the game has finished.
admiral.setText("Admiral Nutsey: You have lost :( "); admiral.setText("Admiral Nutsey: You have lost :( "); //"Admiral" informs user of loss.
enemyHM.setText(ComputerInput.talk(win)); 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()) { else if(computerS.allMoves.isEmpty()) { //Checks for complete decimation of computer ships.
Boolean win = false; Boolean win = false; //player has lost.
stage1 = false; stage1 = false; //both stages are now inactive because the game has finished.
admiral.setText("Admiral Nutsey: You have won!!!"); admiral.setText("Admiral Nutsey: You have won!!!"); //"Admiral" informs user of win.
enemyHM.setText(ComputerInput.talk(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) { 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 CmptrMove = (String) ComputerInput.CoOrdRandomiser().get(0)
+ (String) ComputerInput.CoOrdRandomiser().get(1); + (String) ComputerInput.CoOrdRandomiser().get(1);
//Calls CoordRandomiser from computer input class to create the coordinate fired upon.
if (playerB.fire(computerS, HmnMove)) { if (playerB.fire(computerS, HmnMove)) { //Calls fire method, which returns a boolean, based on if fire was successful.
String response = BattleClass.ShipCheck(HmnMove, computerS); String response = BattleClass.ShipCheck(HmnMove, computerS); //Checks for sunk ships and returns a response.
admiral.setText("Player has " + response + HmnMove ); admiral.setText("Player has " + response + HmnMove ); //Admiral displays hit / sunk / miss.
hitsList.add(HmnMove); hitsList.add(HmnMove); //Hit is recored.
hits.setText("You have hit:" + hitsList.toString()); hits.setText("You have hit:" + hitsList.toString()); //All hits player does is displayed through out.
if (response != "Hit ") { if (response != "Hit ") { //Checks for a sunk response, anything other than just Hit.
takenShipsList.add(response); takenShipsList.add(response); //Sunk ship recorded.
lostShips.setText("You have lost:" + takenShipsList.toString()); takenShips.setText("You have taken:" + takenShipsList.toString()); //Sunk ships displayed throughout.
} }
} }
else if (!playerB.fire(computerS, HmnMove)) { else if (!playerB.fire(computerS, HmnMove)) { // Fire returned false, thus player has missed.
admiral.setText("Player has missed at " + HmnMove); 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); String response = BattleClass.ShipCheck(CmptrMove, playerS);
enemyHM.setText("Computer: I have hit " + response + CmptrMove ); enemyHM.setText("Computer: I have hit " + response + CmptrMove );
if (response != "Hit ") { if (response != "Hit ") {
lostShipsList.add(response); lostShipsList.add(response);
lostShips.setText("You have lost:" + lostShipsList.toString()); lostShips.setText("You have lost:" + lostShipsList.toString());
} }
} } // if (computer fire) END
else if (!computerB.fire(playerS, CmptrMove)) { else if (!computerB.fire(playerS, CmptrMove)) {
enemyHM.setText("Computer: I have missed at " + 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) { 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 @FXML
void stageAction(ActionEvent event) { void stageAction(ActionEvent event) { // Activates battle stage and disables placing stage
if (playerS.allMoves.size() == 25) {
stage0 = false; stage0 = false;
stage1 = true; stage1 = true;
admiral.setText("Admiral Nutsey: Fire on the enemy"); admiral.setText("Admiral Nutsey: Fire on the enemy");
stage.setText("Stage: Battle"); stage.setText("Stage: Battle");
}
} }
@FXML @FXML
void quitAction(ActionEvent event) { void quitAction(ActionEvent event) {
//Add save function //Add save function
System.exit(0); System.exit(0); //END OF GAME
} }
...@@ -236,13 +245,13 @@ public class BSMain extends Application{ ...@@ -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); 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