Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need a Java GUI layout for battleship game with 0-9 across and 0-9 downwards. Ne

ID: 3927692 • Letter: N

Question

Need a Java GUI layout for battleship game with 0-9 across and 0-9 downwards.
Need each box to be a button and to start with a ocean picture to be mouse listener so when I click on it it will change picture when it is a hit and if it misses it will show picture a splash as a missed. Need a Java GUI layout for battleship game with 0-9 across and 0-9 downwards.
Need each box to be a button and to start with a ocean picture to be mouse listener so when I click on it it will change picture when it is a hit and if it misses it will show picture a splash as a missed.
Need each box to be a button and to start with a ocean picture to be mouse listener so when I click on it it will change picture when it is a hit and if it misses it will show picture a splash as a missed.

Explanation / Answer

package battleships;

import java.awt.Font;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JTextArea;

public class Battleships {

   

    private char hitSymbol, missSymbol, emptySymbol;

    private char[][] board;

    private Ship[] ships;

    private JFrame frame;

   

    public Battleships(char hit, char miss, char empty) {

        hitSymbol = hit;

        missSymbol = miss;

        emptySymbol = empty;

       

        board = new char[9][9];

    }

    public static void showError() {

        JOptionPane.showMessageDialog(null, "Error, invalid input, please try again!");

    }

    public Ship askForShip(char type, Ship[] _ships, int count, StringBuilder massage) {

        Ship newShip = new Ship(type);

        String sms = "";

        String poss;

        char first;

        char second;

        char third;

       

        if (count != 0) {

            sms += massage + "--- ";

        }

       

        while(true) {

           

            String s = JOptionPane.showInputDialog(sms + "Commander, Please input the location for "+ newShip.getName() +"? Enter row[A-J], column[0-9] and position[H or V], e.g. [A1H]");

       

            if (s.length() != 3) {

                showError();

                continue;

            }

            first = s.charAt(0);

            second = s.charAt(1);

            third = s.charAt(2);

       

            if (first < 65 || first > 74) {

                //JOptionPane.showMessageDialog(null, "111Error, invalid input, please try again!");

              showError();

                continue;

            }

            if (second < 48 || second > 57) {

                //JOptionPane.showMessageDialog(null, "222Error, invalid input, please try again!");

                showError();

                continue;

            }

            if (third != 72 && third != 86) {

                //JOptionPane.showMessageDialog(null, "333Error, invalid input, please try again!");

                showError();

                continue;

            }

           

            newShip.setLocation(first, second, third);

            if (!newShip.isInBoundary()) {

                 showError();

                continue;

            }

           

            boolean ifOverlap;

            do {

            int i;

                ifOverlap = false;

                for (i=0; i < count; i++) {

                    if (newShip.isOverlap(_ships[i])) {

                      showError();

                       ifOverlap = true;

                    }

                }

                

                if (!ifOverlap) {

                       if (third == 'H')

                poss="Horizontal";

            else

                poss="Vertical";

           

                   int count1 = count + 1;

        massage.append(count1 +". "+ newShip.getName() +" at row: " + first +", column: "+second+", position: "+poss+" ");

       

        return newShip;

                }

            } while(!ifOverlap);

        } //end of while loop

    }

    public String getRadar(int shotsLeft) {

        String s = "Shots Left:" + shotsLeft + " Radar Screen: ";

        for (int i = 0; i < 9; i++) {

            s += i + " ";

        }

        s += " ";

        for (int i = 0; i < 9; i++) {

            s += ((char) (65 + i)) + " ";//print A-J

           

            for (int j = 0; j < 9; j++) {

                s += this.board[i][j] + " ";

            }

            s += " ";

        }

        s += "--------------------- ";

        return s;

    }

    public void play(int numOfShots) {

        int shotCount = 0;

        int dead = 0;

        for (int j = 0; j < 9; j++) {

            for (int k = 0; k < 9; k++) {

                this.board[j][k] = this.emptySymbol;

            }

        }

        while (shotCount < numOfShots && dead != this.ships.length) {

      

            JTextArea textarea = new JTextArea(getRadar(numOfShots - shotCount)

                    + "Captain, please call your shot? Enter row [A-J] and column[0-9] For example:D6");

            textarea.setFont(new Font("Monospaced", Font.PLAIN, 14));

            String s = (String) JOptionPane.showInputDialog( null, textarea, "Input", JOptionPane.QUESTION_MESSAGE, null, null, "");

            if (s.length() == 2 && (int) s.charAt(0) >= 65 && (int) s.charAt(0) <= 74

                    && (int) s.charAt(1) >= 48 && (int) s.charAt(1) <= 57

                    && this.board[(int) s.charAt(0) - 65][s.charAt(1)-48] == this.emptySymbol) {

               

                for (int j = 0; j < this.ships.length; j++) {

                    if (!this.ships[j].isSunk()) {

                        if (this.ships[j].getShot(s.charAt(0), s.charAt(1))) {

                            this.board[(int) s.charAt(0) - 65][s.charAt(1)-48] = this.hitSymbol;

                            if (this.ships[j].isSunk()) {

                            JOptionPane.showMessageDialog(null, this.ships[j].getName() + " Sunk!");

                            dead += 1;

                        }

                            break;

                        } else {

                            this.board[(int) s.charAt(0) - 65][s.charAt(1)-48] = this.missSymbol;

                        }                

                    }

                }

                shotCount++;

            } else {

                showError();

            }

        }// end of shotting while loop

       

        if (this.ships.length == dead) {

            JOptionPane.showMessageDialog(null, "Game Over, You Win!");

       } else {

            JOptionPane.showMessageDialog(null, "Game Over, You Lose!");

        }

    }

   

   

    public void prepareForBattle(char[] fleet) {

        this.ships = new Ship[fleet.length];

        StringBuilder s = new StringBuilder("Your Fleet at Service: ");

       

        for (int i=0; i < fleet.length; i++) {

           ships[i] = askForShip(fleet[i], ships, i, s);

        }//end of fleet fall in

       

        JOptionPane.showMessageDialog(null, s);

       

       

    }

   

   

    public static void main(String[] args) {

       

        char[] fleetOfShips = {'A', 'B', 'D', 'S', 'P' };

        Battleships game = new Battleships('X', 'O', '.');

        game.prepareForBattle(fleetOfShips);

        game.play(30);

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote