Commit 3893bb5e authored by Darth Vernon's avatar Darth Vernon

placement issues corrected, sunk() needs re-working.

parent ae50d988
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
<classpath> <classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="/Prog2Ass"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>
...@@ -3,21 +3,26 @@ ...@@ -3,21 +3,26 @@
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?> <?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?> <?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?> <?import javafx.scene.layout.Pane?>
<Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="749.0" prefWidth="1054.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="BSMain"> <Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="749.0" prefWidth="1054.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="BSMain">
<children> <children>
<Pane accessibleRole="COMBO_BOX" layoutY="-14.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="761.0" prefWidth="1054.0"> <Pane accessibleRole="COMBO_BOX" layoutY="-6.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="761.0" prefWidth="1054.0">
<children> <children>
<ChoiceBox fx:id="DPx" layoutX="30.0" layoutY="651.0" prefHeight="28.0" prefWidth="184.0" /> <AnchorPane layoutX="91.0" prefHeight="593.0" prefWidth="940.0">
<ChoiceBox fx:id="DPy" layoutX="214.0" layoutY="651.0" prefHeight="28.0" prefWidth="184.0" /> <children>
<Button fx:id="place" layoutX="398.0" layoutY="651.0" mnemonicParsing="false" onAction="#placeAction" prefHeight="28.0" prefWidth="184.0" text="*PLACE A SHIP*" textAlignment="CENTER" /> <ChoiceBox fx:id="DPx" layoutX="50.0" layoutY="495.0" prefHeight="28.0" prefWidth="184.0" />
<ChoiceBox fx:id="shipSelection" layoutX="30.0" layoutY="679.0" prefHeight="28.0" prefWidth="184.0" /> <ChoiceBox fx:id="shipSelection" layoutX="50.0" layoutY="523.0" prefHeight="28.0" prefWidth="184.0" />
<Label fx:id="label" alignment="CENTER" contentDisplay="CENTER" layoutX="214.0" layoutY="680.0" prefHeight="28.0" prefWidth="184.0" text="Stage : Preperation" textAlignment="JUSTIFY" wrapText="true" /> <Label fx:id="admiral" layoutX="50.0" layoutY="478.0" prefHeight="17.0" prefWidth="734.0" text="Admiral Nutsey : &quot;Place your ships Captain!!!&quot;" />
<Button fx:id="fire" layoutX="582.0" layoutY="651.0" mnemonicParsing="false" onAction="#fireAction" prefHeight="28.0" prefWidth="184.0" text="*FIRE*" /> <ChoiceBox fx:id="DPy" layoutX="234.0" layoutY="495.0" prefHeight="28.0" prefWidth="184.0" />
<Button fx:id="quit" layoutX="398.0" layoutY="681.0" mnemonicParsing="false" onAction="#quitAction" prefHeight="25.0" prefWidth="184.0" text="*QUIT*" /> <Label fx:id="error" alignment="CENTER" contentDisplay="CENTER" layoutX="234.0" layoutY="523.0" prefHeight="28.0" prefWidth="184.0" text="Stage : Preperation" textAlignment="JUSTIFY" wrapText="true" />
<Button fx:id="battleStage" layoutX="582.0" layoutY="681.0" mnemonicParsing="false" onAction="#stageAction" prefHeight="25.0" prefWidth="184.0" text="*BATTLE STAGE*" /> <Button fx:id="place" layoutX="417.0" layoutY="495.0" mnemonicParsing="false" onAction="#placeAction" prefHeight="28.0" prefWidth="184.0" text="*PLACE A SHIP*" textAlignment="CENTER" />
<Label fx:id="admiral" layoutX="30.0" layoutY="634.0" prefHeight="17.0" prefWidth="734.0" text="Admiral Nutsey : &quot;Place your ships Captain!!!&quot;" /> <Button fx:id="fire" layoutX="601.0" layoutY="495.0" mnemonicParsing="false" onAction="#fireAction" prefHeight="28.0" prefWidth="184.0" text="*FIRE*" />
<Button fx:id="quit" layoutX="417.0" layoutY="519.0" mnemonicParsing="false" onAction="#quitAction" prefHeight="25.0" prefWidth="184.0" text="*QUIT*" />
<Button fx:id="battleStage" layoutX="601.0" layoutY="519.0" mnemonicParsing="false" onAction="#stageAction" prefHeight="25.0" prefWidth="184.0" text="*BATTLE STAGE*" />
</children>
</AnchorPane>
</children> </children>
</Pane> </Pane>
</children> </children>
......
...@@ -30,18 +30,17 @@ public class BSMain extends Application{ ...@@ -30,18 +30,17 @@ public class BSMain extends Application{
@FXML private Button battleStage; @FXML private Button battleStage;
@FXML private Button quit; @FXML private Button quit;
@FXML private Label label; @FXML public Label error;
@FXML @FXML public Label admiral;
static Label admiral;
static 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");
static 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");
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");
static String x; String x;
static String y; String y;
static String ship; String ship;
static Grid playerG = new Grid(); static Grid playerG = new Grid();
static Grid computerG = new Grid(); static Grid computerG = new Grid();
...@@ -96,22 +95,23 @@ public class BSMain extends Application{ ...@@ -96,22 +95,23 @@ public class BSMain extends Application{
@FXML @FXML
void placeAction(ActionEvent event) { void placeAction(ActionEvent event) {
if (!playerG.MoveValidCheck(playerG, playerS.moves)){
admiral.setText("Cordinates are taken. not added to grid");
}
else if (stage0) {
if (stage0) { playerS.ShipPlacer(playerS,ship, x, y);
playerS.ShipPlacer(ship, x, y);
admiral.setText("Admiral Nutsey: Ship placed at " + x + y); admiral.setText("Admiral Nutsey: Ship placed at " + x + y);
if (!playerG.MoveValidCheck(playerG, playerS.moves)){
label.setText("Cordinates are taken. not added to grid");
} }
else {
else if (stage1) {
admiral.setText("Your in Battle, try *FIRE*"); admiral.setText("Your in Battle, try *FIRE*");
} }
} }
}
@FXML @FXML
void fireAction(ActionEvent event) { void fireAction(ActionEvent event) {
...@@ -156,8 +156,9 @@ public class BSMain extends Application{ ...@@ -156,8 +156,9 @@ public class BSMain extends Application{
playerG.CoOrdGen(playerG); playerG.CoOrdGen(playerG);
computerG.CoOrdGen(computerG); computerG.CoOrdGen(computerG);
ComputerInput.cmptrMoveGen(); ComputerInput.cmptrMoveGen();
playerS.name = "PLAYER";
computerS.name = "COMPUTER";
launch(args); launch(args);
......
public class BattleClass { public class BattleClass {
Boolean win = false;
int playerScore;
int computerScore;
public static void fire (ShipClass opponent, String Cordinates) { public static void fire (ShipClass opponent, String Cordinates) {
// loop through shipsHealth array and
// use .contains to see if any ships contain the fired upon coordinates
// then use Grid.sunk() to check if the the entire ship has gone.
for(int i=0; i< opponent.shipsHealth.size(); i++ ) {
for(int j=0; j< opponent.shipsHealth.get(i).size(); j++ ) { if (!opponent.ScoreKepper(opponent)) {
if(opponent.allMoves.contains(Cordinates)) {
System.out.println("Hit on " + Cordinates);
opponent.allMoves.remove(Cordinates);
if(opponent.shipsHealth.get(i).contains(Cordinates)) {
BSMain.admiral.setText("Hit on " + Cordinates);
opponent.shipsHealth.get(i).remove(Cordinates);
opponent.SunkCheck(opponent);
} }
else { else {
BSMain.admiral.setText("Miss at " + Cordinates ); System.out.println("Miss at " + Cordinates );
} }
} }
else {
System.out.println("Game Finshed");
} }
} }
public static void sunkCheck() {
}
} }
...@@ -20,7 +20,7 @@ public class ComputerInput { ...@@ -20,7 +20,7 @@ public class ComputerInput {
public static void cmptrMoveGen() { public static void cmptrMoveGen() {
//not a comment erhuaerouherbguipfsdf
for (int i = 0 ; i < BSMain.ships.size(); i++) { for (int i = 0 ; i < BSMain.ships.size(); i++) {
...@@ -28,7 +28,7 @@ public class ComputerInput { ...@@ -28,7 +28,7 @@ public class ComputerInput {
String y = (String) CoOrdRandomiser().get(1); String y = (String) CoOrdRandomiser().get(1);
String coOrd = x + y ; String coOrd = x + y ;
BSMain.computerS.ShipPlacer(BSMain.ships.get(i),x,y); BSMain.computerS.ShipPlacer(BSMain.computerS,BSMain.ships.get(i),x,y);
if (!BSMain.computerG.MoveValidCheck(BSMain.computerG, BSMain.computerS.moves) ) { if (!BSMain.computerG.MoveValidCheck(BSMain.computerG, BSMain.computerS.moves) ) {
......
...@@ -32,7 +32,7 @@ public class Grid { ...@@ -32,7 +32,7 @@ public class Grid {
return z.Avblcords; return z.Avblcords;
} }
public static boolean MoveValidCheck(Grid inputer, ArrayList moves ) { public boolean MoveValidCheck(Grid inputer, ArrayList moves ) {
boolean valid = true; boolean valid = true;
ArrayList validList = new ArrayList(); ArrayList validList = new ArrayList();
......
...@@ -6,160 +6,172 @@ import javafx.collections.ObservableList; ...@@ -6,160 +6,172 @@ import javafx.collections.ObservableList;
public class ShipClass { public class ShipClass {
ArrayList aC = new ArrayList(); ArrayList<String> aC = new ArrayList<String>();
ArrayList dS = new ArrayList(); ArrayList<String> dS = new ArrayList<String>();
ArrayList dS2 = new ArrayList(); ArrayList<String> dS2 = new ArrayList<String>();
ArrayList bS = new ArrayList(); ArrayList<String> bS = new ArrayList<String>();
ArrayList bS2 = new ArrayList(); ArrayList<String> bS2 = new ArrayList<String>();
ArrayList pB = new ArrayList(); ArrayList<String> pB = new ArrayList<String>();
ArrayList pB2 = new ArrayList(); ArrayList<String> pB2 = new ArrayList<String>();
ArrayList pB3 = new ArrayList(); ArrayList<String> pB3 = new ArrayList<String>();
ObservableList<ArrayList> shipsHealth = FXCollections.observableArrayList(aC,bS,bS2,dS,dS2,pB,pB2,pB3);
ArrayList moves = new ArrayList(); ArrayList<ArrayList<String>> shipsHealth = new ArrayList<ArrayList<String>>();
ArrayList<String> moves = new ArrayList<String>();
ArrayList<String> allMoves = new ArrayList<String>();
int score;
Boolean win;
String name;
public ArrayList ShipPlacer (String shipType, String x, String y) { public Boolean ScoreKepper(ShipClass plyr) {
plyr.score ++;
if (plyr.score == 10) {
win = true;
System.out.println(name + " has won!");
}
return win;
}
public void ShipPlacer (ShipClass plyr ,String shipType, String x, String y) {
System.out.println("Ships are" + BSMain.computerS.shipsHealth);
int xIndex = Grid.xCordL.indexOf(x); int xIndex = Grid.xCordL.indexOf(x);
int yIndex = Grid.yCordL.indexOf(y); int yIndex = Grid.yCordL.indexOf(y);
moves.clear(); plyr.moves.clear();
System.out.println(shipType + " "+ x + " " + y); System.out.println(shipType + " "+ x + " " + y);
System.out.println(allMoves.toString());
if (shipType == BSMain.ships.get(0) ) { if (shipType == BSMain.ships.get(0) ) {
aC.add(Grid.xCordL.get(xIndex) + y); for (int i = 0; i < 5 ; i++) {
aC.add(Grid.xCordL.get(xIndex+1) + y); String newCord = Grid.xCordL.get(xIndex + i) + y;
aC.add(Grid.xCordL.get(xIndex+2)+ y); plyr.aC.add(newCord);
aC.add(Grid.xCordL.get(xIndex+3)+ y); plyr.allMoves.add(newCord);
aC.add(Grid.xCordL.get(xIndex+4)+ y);
}
moves = aC;
System.out.println(aC.toString()); plyr.moves = plyr.aC;
plyr.shipsHealth.add(plyr.aC);
System.out.println(plyr.aC.toString());
} }
else if (shipType == BSMain.ships.get(1) ) { else if (shipType == BSMain.ships.get(1) ) {
bS.add(Grid.xCordL.get(xIndex)+ y); for (int i = 0; i < 4 ; i++) {
bS.add(Grid.xCordL.get(xIndex+1)+ y); String newCord = Grid.xCordL.get(xIndex + i) + y;
bS.add(Grid.xCordL.get(xIndex+2)+ y); plyr.bS.add(newCord);
bS.add(Grid.xCordL.get(xIndex+3)+ y); plyr.allMoves.add(newCord);
moves =bS; }
System.out.println(bS.toString()); plyr.moves =plyr.bS;
plyr.shipsHealth.add(plyr.bS);
System.out.println(plyr.bS.toString());
} }
else if (shipType == BSMain.ships.get(2) ) { else if (shipType == BSMain.ships.get(2) ) {
bS2.add(Grid.xCordL.get(xIndex)+ y); for (int i = 0; i < 4 ; i++) {
bS2.add(Grid.xCordL.get(xIndex+1)+ y); String newCord = Grid.xCordL.get(xIndex + i) + y;
bS2.add(Grid.xCordL.get(xIndex+2)+ y); plyr.bS2.add(newCord);
bS2.add(Grid.xCordL.get(xIndex+3)+ y); plyr.allMoves.add(newCord);
moves = bS2; }
System.out.println(bS2.toString()); plyr.moves = plyr.bS2;
plyr.shipsHealth.add(plyr.bS2);
System.out.println(plyr.bS2.toString());
} }
else if (shipType == BSMain.ships.get(3) ) { else if (shipType == BSMain.ships.get(3) ) {
dS.add(Grid.xCordL.get(xIndex) + y); for (int i = 0; i < 3 ; i++) {
dS.add(Grid.xCordL.get(xIndex+1)+ y); String newCord = Grid.xCordL.get(xIndex + i) + y;
dS.add(Grid.xCordL.get(xIndex+2)+ y); plyr.dS.add(newCord);
plyr.allMoves.add(newCord);
moves = dS; }
System.out.println(dS.toString()); plyr.moves = plyr.dS;
plyr.shipsHealth.add(plyr.dS);
System.out.println(plyr.dS.toString());
} }
else if (shipType == BSMain.ships.get(4) ) { else if (shipType == BSMain.ships.get(4) ) {
dS2.add(Grid.xCordL.get(xIndex)+ y); for (int i = 0; i < 3 ; i++) {
dS2.add(Grid.xCordL.get(xIndex+1)+ y); String newCord = Grid.xCordL.get(xIndex + i) + y;
dS2.add(Grid.xCordL.get(xIndex+2)+ y); plyr.dS2.add(newCord);
plyr.allMoves.add(newCord);
moves = dS2;
System.out.println(dS2.toString());
} }
else if (shipType == BSMain.ships.get(5) ) {
pB.add(Grid.xCordL.get(xIndex)+ y);
pB.add(Grid.xCordL.get(xIndex+1)+ y);
moves = pB; plyr.moves = plyr.dS2;
plyr.shipsHealth.add(plyr.dS2);
System.out.println(plyr.dS2.toString());
System.out.println(pB.toString());
} }
else if (shipType == BSMain.ships.get(6) ) { else if (shipType == BSMain.ships.get(5) ) {
pB2.add(Grid.xCordL.get(xIndex)+ y);
pB2.add(Grid.xCordL.get(xIndex+1)+ y);
moves = pB2 ;
System.out.println(pB2.toString()); for (int i = 0; i < 2 ; i++) {
String newCord = Grid.xCordL.get(xIndex + i) + y;
plyr.pB.add(newCord);
plyr.allMoves.add(newCord);
} }
else if (shipType == BSMain.ships.get(7) ) {
pB3.add(Grid.xCordL.get(xIndex)+ y);
pB3.add(Grid.xCordL.get(xIndex+1)+ y);
moves = pB3;
System.out.println(pB3.toString());
System.out.println(plyr.pB.toString());
} }
else { else if (shipType == BSMain.ships.get(6) ) {
System.out.println("Type of ship not valid"); for (int i = 0; i < 2 ; i++) {
String newCord = Grid.xCordL.get(xIndex + i) + y;
plyr.pB2.add(newCord);
plyr.allMoves.add(newCord);
} }
return null;
plyr.moves = plyr.pB2 ;
plyr.shipsHealth.add(plyr.pB2);
System.out.println(plyr.pB2.toString());
} }
else if (shipType == BSMain.ships.get(7) ) {
for (int i = 0; i < 2 ; i++) {
String newCord = Grid.xCordL.get(xIndex + i) + y;
plyr.pB3.add(newCord);
plyr.allMoves.add(newCord);
public void SunkCheck(ShipClass opponent) {
for (int i =0 ; i < opponent.shipsHealth.size() ; i++) {
if (opponent.shipsHealth.isEmpty()) {
System.out.println("All Ships Sunk");
} }
else if (opponent.shipsHealth.get(i).isEmpty()) { plyr.moves = plyr.pB3;
System.out.println("Target is sunk"); plyr.shipsHealth.add(plyr.pB3);
opponent.shipsHealth.remove(i); System.out.println(plyr.pB3.toString());
}
} }
else {
System.out.println("Type of ship not valid");
} }
}
} }
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