Skip to content

Commit

Permalink
hit detection works
Browse files Browse the repository at this point in the history
still need to implement board generation and sink detection
  • Loading branch information
aircooledjacob committed Jun 16, 2021
1 parent a40e8da commit 7ec3dd0
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 18 deletions.
Binary file modified Images/example_enemy_grid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions src/battleship/BattleShipGUI.form
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<Property name="horizontalTextPosition" type="int" value="0"/>
<Property name="verticalTextPosition" type="int" value="3"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton1ActionPerformed"/>
</Events>
</Component>
</SubComponents>
</Container>
Expand Down
35 changes: 29 additions & 6 deletions src/battleship/BattleShipGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -87,6 +96,20 @@ private void initComponents() {
setLocationRelativeTo(null);
}// </editor-fold>//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
*/
Expand Down Expand Up @@ -124,20 +147,20 @@ 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;
}
}

return grid;

}

static void logGrid(String[][] grid) {
static void logGrid(boolean[][] grid) {
System.out.println("----------------");
for (int x = 0; x < grid.length; x++) {
System.out.print("|");
Expand Down
18 changes: 16 additions & 2 deletions src/battleship/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,35 @@
*/
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));
}
}

this.revalidate();
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
Expand Down
48 changes: 48 additions & 0 deletions src/battleship/Team.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
23 changes: 15 additions & 8 deletions src/battleship/ship/Segment.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package battleship.ship;

import battleship.Team;
import battleship.ship.segment.Position;
import battleship.ship.segment.State;

Expand All @@ -16,6 +17,7 @@
public class Segment extends javax.swing.JPanel {

private State state;
private Team team;
private final Position pos;

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 7ec3dd0

Please sign in to comment.