From 7ec3dd00b1601d8d922ebd81ded070677cba2aa9 Mon Sep 17 00:00:00 2001 From: Jacob Neave <35272149+AggrievedBubble@users.noreply.github.com> Date: Wed, 16 Jun 2021 15:53:35 +0100 Subject: [PATCH] hit detection works still need to implement board generation and sink detection --- Images/example_enemy_grid.png | Bin 3191 -> 3191 bytes README.md | 26 ++++++++++++++-- src/battleship/BattleShipGUI.form | 3 ++ src/battleship/BattleShipGUI.java | 35 ++++++++++++++++++---- src/battleship/Grid.java | 18 +++++++++-- src/battleship/Team.java | 48 ++++++++++++++++++++++++++++++ src/battleship/ship/Segment.java | 23 +++++++++----- 7 files changed, 135 insertions(+), 18 deletions(-) create mode 100644 src/battleship/Team.java diff --git a/Images/example_enemy_grid.png b/Images/example_enemy_grid.png index 1d4aa242bb4b7f72c17d587dd2d2cb5779ca8f4f..5f41a9b536ed4bb74ed8f530a09c51faf2aeaecc 100644 GIT binary patch delta 1710 zcmew^@m)f(Gr-TCmrII^fq{Y7)59f*fq^j;h#fiDfFj4eZ5B^dG^uA(_jGX#sfc@f z^We(QcSIcmEj1N-T{b(M3glhovUWwUL)tIyiCi3fHYtC2Z8qNIPl?E0^x4GfM%`@g z@7v!0u8qB(IBVHMi$2DN@3vOIwtvoNVAA(rb$ZhuoAoL4o)=gBnr6E_*LwMDiQ^yV z%zN{5XL$9m+PZhr`^(is8{!pK)iWptvT}54Ie3IHIxXT75DIOWu!5<@MN~m$74@YS z-ro2AdueQY=B&2I7U!-r?0H;&F%akwgp~`Yi-#gP{`jZ&#k=~F|DCtAz4^I#{kJ>Y z-p*NGCaJ!_Wt|VyZA1f@E$Lmv!1cy_~ONWD1oJ~ zziM0V@5;E77JbjZq?|v0b93tXYpZRir&=z5J>&V$IWZxOf4CsQUQd^B2gUK-Z?*f^ zzqazZY$9{Ka39+P+5SGOl}zKCe`LY%%Zh zfw8dv|JVEA^tAtJ_5W%Am8&EK4UdfwXnJC|KW59kXZ z=W;}l)YGcaxsZGR`|n-r4x9KME|htm&2a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)kOeA@F z*|E=Gis{A3-$$DcAuL!wI;YdxJR>uu)^oOK^(>PFWYOQggm;wU$@*eGS$w2Kch1n3 ov-&)x>7Y%p?dd~~N4GWCgS%oeo~vsPSTyR|-9lE~^Xu^a2Q493-~a#s diff --git a/README.md b/README.md index 6a9a29b..de32a19 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,29 @@ # BattleShip -This is a simple **_BattleShip_** game created in ~~JavaScript~~ **Java** for a college project that allows you to play against ~~another player~~ a computer. +This is a simple **BattleShip** game created in ~~JavaScript~~ **Java** for a college project that allows you to play against ~~another player~~ a computer. (note: would have liked to develop in javascript but college systems don't allow the use of npm or yarn) +--- + +## How the game works +**Battleship** is a game played by two players in which each player has a grid on which they can place 5 **ships** of varying lengths: + +- 5 spaces - Carrier +- 4 spaces - Battleship +- 3 spaces - Submarine +- 3 spaces - Cruiser +- 2 spaces - Destroyer + +The ships can **not** be placed next to each other or at diagonals, and **must** all be placed on the board. + +Players can **not** see each others grids and take turns firing shots at each other by calling out grid references, the other player then reveals whether the shot hit or missed. + +If a player hits a shot they get another go. + + + +--- The board state is to be saved in a 2d array that can be created with this function: ```java @@ -34,7 +54,9 @@ static void logGrid(String[][] grid) { ``` The above function would be a useful template debugging function to quickly log the board state when programming. -I plan to store the board as a 2d array that contains panels which the user can interact with, suich as the background color of the panel changes color as shown +--- + +I plan to store the board as a 2d array that contains panels which the user can interact with, suich as the background color of the panel changes color as shown: ## Example enemy grid ![Example enemy grid](./Images/example_enemy_grid.png "Enemy Grid") diff --git a/src/battleship/BattleShipGUI.form b/src/battleship/BattleShipGUI.form index 6c8de34..a4b358b 100644 --- a/src/battleship/BattleShipGUI.form +++ b/src/battleship/BattleShipGUI.form @@ -44,6 +44,9 @@ + + + diff --git a/src/battleship/BattleShipGUI.java b/src/battleship/BattleShipGUI.java index 98b02fd..acea584 100644 --- a/src/battleship/BattleShipGUI.java +++ b/src/battleship/BattleShipGUI.java @@ -16,10 +16,14 @@ public class BattleShipGUI extends javax.swing.JFrame { */ public BattleShipGUI() { initComponents(); - friendlyPanel.add(new Grid()); + Grid grid = new Grid(Team.FRIENDLY); + Team.FRIENDLY.setGrid(grid); + friendlyPanel.add(grid); friendlyPanel.revalidate(); friendlyPanel.repaint(); - enemyPanel.add(new Grid()); + grid = new Grid(Team.ENEMY); + Team.ENEMY.setGrid(grid); + enemyPanel.add(grid); enemyPanel.revalidate(); enemyPanel.repaint(); } @@ -53,6 +57,11 @@ private void initComponents() { jButton1.setFocusable(false); jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); toolBar.add(jButton1); gridBagConstraints = new java.awt.GridBagConstraints(); @@ -87,6 +96,20 @@ private void initComponents() { setLocationRelativeTo(null); }// //GEN-END:initComponents + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + // TODO add your handling code here: + boolean[][] grid = generateGrid(); + grid[1][1] = true; + grid[1][2] = true; + grid[1][3] = true; + grid[1][4] = true; + + grid[3][2] = true; + grid[3][3] = true; + Team.ENEMY.setTeamShipGrid(grid); + Team.FRIENDLY.setTeamShipGrid(grid); + }//GEN-LAST:event_jButton1ActionPerformed + /** * @param args the command line arguments */ @@ -124,12 +147,12 @@ public void run() { }); } - static String[][] createGrid() { - String[][] grid = new String[7][7]; + static boolean[][] generateGrid() { + boolean[][] grid = new boolean[8][8]; for (int x = 0; x < grid.length; x++) { for (int y = 0; y< grid.length; y++) { - grid[x][y] = " "; + grid[x][y] = false; } } @@ -137,7 +160,7 @@ static String[][] createGrid() { } - static void logGrid(String[][] grid) { + static void logGrid(boolean[][] grid) { System.out.println("----------------"); for (int x = 0; x < grid.length; x++) { System.out.print("|"); diff --git a/src/battleship/Grid.java b/src/battleship/Grid.java index 60c9425..fbf5577 100644 --- a/src/battleship/Grid.java +++ b/src/battleship/Grid.java @@ -14,14 +14,20 @@ */ public class Grid extends javax.swing.JPanel { + private final Team team; + /** * Creates new form Grid + * @param team */ - public Grid() { + public Grid(Team team) { initComponents(); + + this.team = team; + for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { - this.add(new Segment(x, y, State.UNKNOWN)); + this.add(new Segment(x, y, State.UNKNOWN, this.team)); } } @@ -29,6 +35,14 @@ public Grid() { this.repaint(); } + public void refresh() { + for (int x = 0; x < 8; x++) { + for (int y = 0; y < 8; y++) { + + } + } + } + /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always diff --git a/src/battleship/Team.java b/src/battleship/Team.java new file mode 100644 index 0000000..0123f4b --- /dev/null +++ b/src/battleship/Team.java @@ -0,0 +1,48 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package battleship; + +import static battleship.BattleShipGUI.generateGrid; + +/** + * + * @author j9neave + */ +public enum Team { + FRIENDLY { + + @Override + public void setTeamShipGrid(boolean[][] shipGrid) { + this.shipGrid = shipGrid; + this.getGrid().refresh(); + } + }, + ENEMY; + + boolean[][] shipGrid; + Grid grid; + + public void setTeamShipGrid(boolean[][] shipGrid) { + this.shipGrid = shipGrid; + } + + public boolean[][] getTeamShipGrid() { + try { + return this.shipGrid; + } catch (Exception e) { + return generateGrid(); + } + } + + public void setGrid(Grid grid) { + this.grid = grid; + } + + public Grid getGrid() { + return this.grid; + } + +} diff --git a/src/battleship/ship/Segment.java b/src/battleship/ship/Segment.java index 376a916..3e8de53 100644 --- a/src/battleship/ship/Segment.java +++ b/src/battleship/ship/Segment.java @@ -1,5 +1,6 @@ package battleship.ship; +import battleship.Team; import battleship.ship.segment.Position; import battleship.ship.segment.State; @@ -16,6 +17,7 @@ public class Segment extends javax.swing.JPanel { private State state; + private Team team; private final Position pos; /** @@ -24,12 +26,11 @@ public class Segment extends javax.swing.JPanel { * @param ypos * @param defaultState */ - - - public Segment(int xpos, int ypos, State defaultState) { + public Segment(int xpos, int ypos, State defaultState, Team team) { initComponents(); + this.team = team; this.state = defaultState; this.updateColor(); this.pos = new Position(xpos, ypos); @@ -92,12 +93,18 @@ public void mousePressed(java.awt.event.MouseEvent evt) { private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed // TODO add your handling code here: switch(this.state) { - case UNKNOWN -> this.setState(State.ALIVE); + case UNKNOWN -> { + if (this.team.getTeamShipGrid()[this.getPos().getX()][this.getPos().getY()]) { + this.setState(State.HIT); + } else if (!this.team.getTeamShipGrid()[this.getPos().getX()][this.getPos().getY()]) { + this.setState(State.MISS); + } + } case ALIVE -> this.setState(State.DEAD); - case DEAD -> this.setState(State.MISS); - case MISS -> this.setState(State.HIT); - case HIT -> this.setState(State.UNKNOWN); - default -> this.setState(State.UNKNOWN); +// case DEAD -> this.setState(State.MISS); +// case MISS -> this.setState(State.HIT); + case HIT -> this.setState(State.DEAD); +// default -> this.setState(State.UNKNOWN); } }//GEN-LAST:event_formMousePressed