Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-26-2013, 11:13 PM   PM User | #1
coderhelp
New to the CF scene

 
Join Date: Jan 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
coderhelp is an unknown quantity at this point
Battleship help

Can anyone help me out?
this code is not placing the ship on the JFrame correctly and the Gameover is not working. it is due Monday morning.thanks

Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Random;
import javax.swing.JOptionPane;

/*
 
 */
public class GRID extends javax.swing.JFrame {

    int MISS = 1;
    int ii;
    int i;
    int SHIP = 6;
    private static Image ACpicture;
    private static Image Bpicture;
    private static Image Dpicture;
    private static Image Spicture;
    private static Image PBpicture;
    int r;
    int c;
    int row;
    int column;
    //  Initializes Board
    private int[][] Player = {
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
    private int[][] Computer = {
        {0, 6, 0, 0, 3, 3, 3, 0, 0, 0},
        {0, 6, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 6, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 6, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 6, 0, 0, 0, 4, 4, 4, 4, 0},
        {0, 6, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
        {0, 0, 0, 5, 5, 5, 5, 5, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tu...feel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GRID.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GRID.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GRID.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GRID.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GRID().setVisible(true);
            }
        });
    }

    public GRID() {

        super("BATTLESHIP");
        initComponents();
        setBounds(0, 0, 1200, 750);

        // Aircraft Carrier JPG
        ClassLoader cloader = GRID.class.getClassLoader();
        ACpicture = Toolkit.getDefaultToolkit().getImage(cloader.getResource("Aircraft Carrier.jpg"));
        prepareImage(ACpicture, this);

        // Battleship JPG
        Bpicture = Toolkit.getDefaultToolkit().getImage(cloader.getResource("Battleship.jpg"));
        prepareImage(Bpicture, this);

        // Destroyer JPG
        Dpicture = Toolkit.getDefaultToolkit().getImage(cloader.getResource("Destroyer.jpg"));
        prepareImage(Dpicture, this);

        // Submarine JPG
        Spicture = Toolkit.getDefaultToolkit().getImage(cloader.getResource("Submarine.jpg"));
        prepareImage(Spicture, this);

        // PatrolBoat JPG
        PBpicture = Toolkit.getDefaultToolkit().getImage(cloader.getResource("PatrolBoat.jpg"));
        prepareImage(PBpicture, this);

        int instructions = JOptionPane.showConfirmDialog(null, "Welcome to the "
                + " game of Battleship!  \n Would you like to read the "
                + "instructions first?");
        if (instructions == JOptionPane.YES_OPTION) {
            JOptionPane.showMessageDialog(null, "First, you will input the "
                    + "coordinate of one side of an indicated ship and then the"
                    + " other end point of the ship.  "
                    + "\n (A-L) from left to right, and (1-10) from top to bottom"
                    + "\n You will do this with all five of your ships. "
                    + "\n Second, you will input the"
                    + " coordinate in which you would like to fire at. \n You will"
                    + " be told whether you have hit or missed the computer's"
                    + " ship. \n Third, it is the computer's turn so it will fire"
                    + " at you. \n You will be told whether it has hit or missed"
                    + " you. \n You will repeat this until you or the comouter"
                    + " has sunk all of each other's ships.");
        }

        // Coordinates and Orientation of ALL 6 SHIPS                
        do {
            int DIR;
            do {
            // Ask user for coordinates of SHIP
            column = (Integer.parseInt(JOptionPane.showInputDialog("Insert the 'x' coordinate in which you \n"
                    + " would like to place your ship #" + SHIP))) - 1;
            row = (Integer.parseInt(JOptionPane.showInputDialog("Insert the 'y' coordinate in which you \n"
                    + " would like to place your ship" + SHIP))) - 1;

            // Orientation: Horizontal or Vertical?
            Object[] options = {"Horizontal",
                "Vertical"};
            DIR = JOptionPane.showOptionDialog(null,
                    "Which way would you \n like to place your ship?",
                    "Orientation",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null, //do not use a custom Icon
                    options, //the titles of buttons
                    options[0]); //default button title
           } while (!isValid(row, column, DIR, SHIP));
            // PLACES SHIP

            // SHIP == 6
            if (SHIP == 6) {
                if (DIR == (JOptionPane.YES_OPTION)) { // YES_OPTION means Horizontal
                    for (c = column; c < column + 6; c++) {
                        Player[r][c] = 6;
                    }
                } else {
                    for (r = row; r <= row + 6; r++) {
                        Player[r][c] = 6;
                    }
                }
                // SHIP == 5
            } else if (SHIP == 5) {
                if (DIR == (JOptionPane.YES_OPTION)) { // YES_OPTION means Horizontal
                    for (c = column; c < column + 5; c++) {
                        Player[r][c] = 5;
                    }
                } else {
                    for (r = row; r <= row + 5; r++) {
                        Player[r][c] = 5;
                    }
                }
                // SHIP == 4
            } else if (SHIP == 4) {
                if (DIR == (JOptionPane.YES_OPTION)) { // YES_OPTION means Horizontal
                    for (c = column; c < column + 4; c++) {
                        Player[r][c] = 4;
                    }
                } else {
                    for (r = row; r <= row + 4; r++) {
                        Player[r][c] = 4;
                    }
                }
                // SHIP == 3
            } else if (SHIP == 3) {
                if (DIR == (JOptionPane.YES_OPTION)) { // YES_OPTION means Horizontal
                    for (c = column; c < column + 3; c++) {
                        Player[r][c] = 3;
                    }
                } else {
                    for (r = row; r <= row + 3; r++) {
                        Player[r][c] = 3;
                    }
                }
                // SHIP == 2
            } else {
                if (DIR == (JOptionPane.YES_OPTION)) { // YES_OPTION means Horizontal
                    for (c = column; c < column + 2; c++) {
                        Player[r][c] = 2;
                    }
                } else {
                    for (r = row; r <= row + 2; r++) {
                        Player[r][c] = 2;
                    }
                }
            }
            SHIP--;

        } while (SHIP != 1);
        repaint();

        // DO WHILE NOT GAMEOVER
        do {

            // Aks User For Coordinate to Fire At
            int fireC = Integer.parseInt(JOptionPane.showInputDialog("Input the 'X'"
                    + "Coordinate in which you choose"
                    + "to fire at. \n "
                    + "(1-0) from Left to Right")) - 1;
            int fireR = Integer.parseInt(JOptionPane.showInputDialog("Input the 'Y'"
                    + "Coordinate in which you choose"
                    + "to fire at. \n "
                    + "(1-10) from top to bottom")) - 1;

            c = fireR;
            r = fireC;
            if (Computer[r][c] >= 2) {
                JOptionPane.showMessageDialog(null, "YOU HIT THE COMPUTER'S SHIP");
                Computer[r][c] *= -1;
            } else if (Computer[r][c] == 0) {
                JOptionPane.showMessageDialog(null, "YOU MISSED THE COMPUTER'S SHIP");
                Computer[r][c] = MISS;
            } else {
                JOptionPane.showMessageDialog(null, "YOU ALREADY FIRED THERE");
            }
            repaint();

            if (GAMEOVER(Player)) {
                JOptionPane.showMessageDialog(null, "YOU WON");
            } else {

                // COMPUTER ENTERS COORDINATES AND FIRES
                Random generator = new Random();
                int cfirec = generator.nextInt(10);
                int cfirer = generator.nextInt(10);
                if (Player[r][c] >= 2) {
                    Player[r][c] *= -1;
                } else if (Player[r][c] == 0) {
                } else {
                }


            }
        } while (!GAMEOVER(Computer));
    }

    // CHECKING SHIPs
    private boolean isValid(int r, int c, int DIR, int SHIP) {
        if (DIR == JOptionPane.YES_NO_OPTION) {
            if (r < 0 || (r + SHIP >= 9)) {
                return false;
            } else {
                return true;
            }
        } else {                           // (Vert)
            if (c < 0 || (c + SHIP >= 9)) {
                return false;
            } else {
                return true;
            }
        }
    }
    // CHECKS FOR GAMEOVER
    private boolean GAMEOVER(int[][] G) {
        for (int rows = 0; rows < 10; rows++) {
            for (int columns = 0; columns < 10; columns++) {
                if (G[r][c] > 1) {
                    return false;
                }
            }
        }
        return true;
    }

    public void paint(Graphics g) {
        super.paint(g);       // call the jframe paint
        PaintBoard(g);        // call the tictactoe paintboard
    }
    // Variables declaration - do not modify                     
    // End of variables declaration                   

    private void PaintBoard(Graphics g) {

        // Left Box
        g.drawLine(40, 100, 550, 100);
        g.drawLine(40, 100, 40, 610);
        g.drawLine(550, 100, 550, 610);
        g.drawLine(40, 610, 550, 610);

        // Right Box
        g.drawLine(650, 100, 1160, 100);
        g.drawLine(650, 100, 650, 610);
        g.drawLine(650, 610, 1160, 610);
        g.drawLine(1160, 100, 1160, 610);

        // Left Box Grid Up/Down
        g.drawLine(91, 100, 91, 610);
        g.drawLine(142, 100, 142, 610);
        g.drawLine(193, 100, 193, 610);
        g.drawLine(244, 100, 244, 610);
        g.drawLine(295, 100, 295, 610);
        g.drawLine(346, 100, 346, 610);
        g.drawLine(397, 100, 397, 610);
        g.drawLine(448, 100, 448, 610);
        g.drawLine(499, 100, 499, 610);

        // Right Box Grid Up/Down
        g.drawLine(701, 100, 701, 610);
        g.drawLine(752, 100, 752, 610);
        g.drawLine(803, 100, 803, 610);
        g.drawLine(854, 100, 854, 610);
        g.drawLine(905, 100, 905, 610);
        g.drawLine(956, 100, 956, 610);
        g.drawLine(1007, 100, 1007, 610);
        g.drawLine(1058, 100, 1058, 610);
        g.drawLine(1109, 100, 1109, 610);

        // Left Box Left/Right
        g.drawLine(40, 151, 550, 151);
        g.drawLine(40, 202, 550, 202);
        g.drawLine(40, 253, 550, 253);
        g.drawLine(40, 304, 550, 304);
        g.drawLine(40, 355, 550, 355);
        g.drawLine(40, 406, 550, 406);
        g.drawLine(40, 457, 550, 457);
        g.drawLine(40, 508, 550, 508);
        g.drawLine(40, 559, 550, 559);

        // Right Box Left/Right
        g.drawLine(650, 151, 1160, 151);
        g.drawLine(650, 202, 1160, 202);
        g.drawLine(650, 253, 1160, 253);
        g.drawLine(650, 304, 1160, 304);
        g.drawLine(650, 355, 1160, 355);
        g.drawLine(650, 406, 1160, 406);
        g.drawLine(650, 457, 1160, 457);
        g.drawLine(650, 508, 1160, 508);
        g.drawLine(650, 559, 1160, 559);

        // Rectangle @ Bottom
        g.drawRect(141, 630, 919, 100);

        // Letters For Coordinates Left
        g.drawString("A", 62, 95);
        g.drawString("B", 113, 95);
        g.drawString("C", 164, 95);
        g.drawString("D", 215, 95);
        g.drawString("E", 266, 95);
        g.drawString("F", 317, 95);
        g.drawString("G", 368, 95);
        g.drawString("H", 419, 95);
        g.drawString("I", 470, 95);
        g.drawString("J", 521, 95);

        // Letter For Coordinates Right
        g.drawString("A", 672, 95);
        g.drawString("B", 733, 95);
        g.drawString("C", 774, 95);
        g.drawString("D", 825, 95);
        g.drawString("E", 876, 95);
        g.drawString("F", 927, 95);
        g.drawString("G", 978, 95);
        g.drawString("H", 1029, 95);
        g.drawString("I", 1080, 95);
        g.drawString("J", 1131, 95);

        // Number For Coordinate Lefy
        g.drawString("1", 30, 130);
        g.drawString("2", 30, 181);
        g.drawString("3", 30, 232);
        g.drawString("4", 30, 283);
        g.drawString("5", 30, 334);
        g.drawString("6", 30, 385);
        g.drawString("7", 30, 436);
        g.drawString("8", 30, 487);
        g.drawString("9", 30, 538);
        g.drawString("10", 23, 589);

        //Number For Coordinate Right
        g.drawString("1", 640, 130);
        g.drawString("2", 640, 181);
        g.drawString("3", 640, 232);
        g.drawString("4", 640, 283);
        g.drawString("5", 640, 334);
        g.drawString("6", 640, 385);
        g.drawString("7", 640, 436);
        g.drawString("8", 640, 487);
        g.drawString("9", 640, 538);
        g.drawString("10", 633, 589);

        // "BATTLESHIP
        g.drawString("BATTLESHIP", 563, 50);
        g.drawString("YOUR SHIPS", 259, 75);
        g.drawString("YOUR SHOTS", 867, 75);

        // Insert Image

        // Inset # Hits to Sink
        // Aircraft Carrier
        g.drawImage(ACpicture, 185, 637, 100, 40, null);
        g.drawString("AIRCRAFT CARRIER", 185, 645);
        g.setColor(Color.red);
        g.fillOval(350, 650, 10, 10);
        g.fillOval(370, 650, 10, 10);
        g.fillOval(390, 650, 10, 10);
        g.fillOval(410, 650, 10, 10);
        g.fillOval(430, 650, 10, 10);
        g.fillOval(450, 650, 10, 10);

        // Battleship
        g.setColor(Color.black);
        g.drawImage(Bpicture, 725, 632, 150, 47, null);
        g.drawString("BATTLESHIP", 700, 645);
        g.setColor(Color.red);
        g.fillOval(900, 650, 10, 10);
        g.fillOval(920, 650, 10, 10);
        g.fillOval(940, 650, 10, 10);
        g.fillOval(960, 650, 10, 10);
        g.fillOval(980, 650, 10, 10);


        // Destroyer
        g.setColor(Color.black);
        g.drawImage(Dpicture, 185, 680, 100, 50, null);
        g.drawString("DESTROYER", 185, 685);
        g.setColor(Color.red);
        g.fillOval(350, 705, 10, 10);
        g.fillOval(370, 705, 10, 10);
        g.fillOval(390, 705, 10, 10);
        g.fillOval(410, 705, 10, 10);


        // Submarine
        g.setColor(Color.black);
        g.drawString("SUBMARINE", 500, 680);
        g.setColor(Color.red);
        g.fillOval(625, 705, 10, 10);
        g.fillOval(645, 705, 10, 10);
        g.fillOval(665, 705, 10, 10);
        g.drawImage(Spicture, 485, 680, 100, 50, null);

        // Patrol Boat
        g.setColor(Color.black);
        g.drawImage(PBpicture, 725, 685, 100, 45, null);
        g.drawString("PATROL BOAT", 700, 685);
        g.setColor(Color.red);
        g.fillOval(900, 705, 10, 10);
        g.fillOval(920, 705, 10, 10);



        // Places A BLACK DOT Where There Is A SHIP

        // COLORS LEFT GRID FOR SHIPS
        for (r = 0; r < 10; r++) {
            for (c = 0; c < 10; c++) {
                if (Player[r][c] > 0) {
                    g.setColor(Color.black);
                    g.fillOval(r * 51 + 49, c * 51 - 11 + 120, 36, 36);
                } else if (Player[r][c] > 0) {
                    g.setColor(Color.red);
                    g.fillOval(r * 51 + 49, c * 51 - 11 + 120, 36, 36);
                }
            }

            // COLORS GRID ON RIGHT FOR HIT OR MISS
            for (r = 0; r < 10; r++) {
                for (c = 0; c < 10; c++) {
                    if (Computer[r][c] < 0) {
                        g.setColor(Color.red);
                        g.fillOval(r * 51 + 655, c * 51 - 11 + 120, 36, 36);
                    } else if (Computer[r][c] == 1) {
                        g.setColor(Color.white);
                        g.fillOval(r * 51 + 655, c * 51 - 11 + 120, 36, 36);
                    }

                }
            }
        }
    }
}

Last edited by VIPStephan; 01-26-2013 at 11:27 PM.. Reason: added code BB tags
coderhelp is offline   Reply With Quote
Old 01-26-2013, 11:27 PM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,596
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
If you post any code please put it in between [CODE][/CODE] tags. It makes scanning your posts much easier. You can do this by clicking the small ‘#’ icon above the reply field.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 01-27-2013, 04:17 AM   PM User | #3
coderhelp
New to the CF scene

 
Join Date: Jan 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
coderhelp is an unknown quantity at this point
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Random;
import javax.swing.JOptionPane;

/*
 * GRID BY AMBER BISHOP, PERIOD 5, MR. FORSTER, DECEMBER 2012
 */
public class GRID extends javax.swing.JFrame {

    int MISS = 1;
    int ii;
    int i;
    int SHIP = 6;
    private static Image ACpicture;
    private static Image Bpicture;
    private static Image Dpicture;
    private static Image Spicture;
    private static Image PBpicture;
    int r;
    int c;
    int row;
    int column;
    //  Initializes Board
    private int[][] Player = {
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
    private int[][] Computer = {
        {0, 6, 0, 0, 3, 3, 3, 0, 0, 0},
        {0, 6, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 6, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 6, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 6, 0, 0, 0, 4, 4, 4, 4, 0},
        {0, 6, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
        {0, 0, 0, 5, 5, 5, 5, 5, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GRID.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GRID.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GRID.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GRID.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GRID().setVisible(true);
            }
        });
    }

    public GRID() {

        super("BATTLESHIP");
        initComponents();
        setBounds(0, 0, 1200, 750);

        // Aircraft Carrier JPG
        ClassLoader cloader = GRID.class.getClassLoader();
        ACpicture = Toolkit.getDefaultToolkit().getImage(cloader.getResource("Aircraft Carrier.jpg"));
        prepareImage(ACpicture, this);

        // Battleship JPG
        Bpicture = Toolkit.getDefaultToolkit().getImage(cloader.getResource("Battleship.jpg"));
        prepareImage(Bpicture, this);

        // Destroyer JPG
        Dpicture = Toolkit.getDefaultToolkit().getImage(cloader.getResource("Destroyer.jpg"));
        prepareImage(Dpicture, this);

        // Submarine JPG
        Spicture = Toolkit.getDefaultToolkit().getImage(cloader.getResource("Submarine.jpg"));
        prepareImage(Spicture, this);

        // PatrolBoat JPG
        PBpicture = Toolkit.getDefaultToolkit().getImage(cloader.getResource("PatrolBoat.jpg"));
        prepareImage(PBpicture, this);

        int instructions = JOptionPane.showConfirmDialog(null, "Welcome to the "
                + " game of Battleship!  \n Would you like to read the "
                + "instructions first?");
        if (instructions == JOptionPane.YES_OPTION) {
            JOptionPane.showMessageDialog(null, "First, you will input the "
                    + "coordinate of one side of an indicated ship and then the"
                    + " other end point of the ship.  "
                    + "\n (A-L) from left to right, and (1-10) from top to bottom"
                    + "\n You will do this with all five of your ships. "
                    + "\n Second, you will input the"
                    + " coordinate in which you would like to fire at. \n You will"
                    + " be told whether you have hit or missed the computer's"
                    + " ship. \n Third, it is the computer's turn so it will fire"
                    + " at you. \n You will be told whether it has hit or missed"
                    + " you. \n You will repeat this until you or the comouter"
                    + " has sunk all of each other's ships.");
        }

        // Coordinates and Orientation of ALL 6 SHIPS                
        do {
            int DIR;
            do {
            // Ask user for coordinates of SHIP
            column = (Integer.parseInt(JOptionPane.showInputDialog("Insert the 'x' coordinate in which you \n"
                    + " would like to place your ship #" + SHIP))) - 1;
            row = (Integer.parseInt(JOptionPane.showInputDialog("Insert the 'y' coordinate in which you \n"
                    + " would like to place your ship" + SHIP))) - 1;

            // Orientation: Horizontal or Vertical?
            Object[] options = {"Horizontal",
                "Vertical"};
            DIR = JOptionPane.showOptionDialog(null,
                    "Which way would you \n like to place your ship?",
                    "Orientation",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null, //do not use a custom Icon
                    options, //the titles of buttons
                    options[0]); //default button title
           } while (!isValid(row, column, DIR, SHIP));
            // PLACES SHIP

            // SHIP == 6
            if (SHIP == 6) {
                if (DIR == (JOptionPane.YES_OPTION)) { // YES_OPTION means Horizontal
                    for (c = column; c < column + 6; c++) {
                        Player[r][c] = 6;
                    }
                } else {
                    for (r = row; r <= row + 6; r++) {
                        Player[r][c] = 6;
                    }
                }
                // SHIP == 5
            } else if (SHIP == 5) {
                if (DIR == (JOptionPane.YES_OPTION)) { // YES_OPTION means Horizontal
                    for (c = column; c < column + 5; c++) {
                        Player[r][c] = 5;
                    }
                } else {
                    for (r = row; r <= row + 5; r++) {
                        Player[r][c] = 5;
                    }
                }
                // SHIP == 4
            } else if (SHIP == 4) {
                if (DIR == (JOptionPane.YES_OPTION)) { // YES_OPTION means Horizontal
                    for (c = column; c < column + 4; c++) {
                        Player[r][c] = 4;
                    }
                } else {
                    for (r = row; r <= row + 4; r++) {
                        Player[r][c] = 4;
                    }
                }
                // SHIP == 3
            } else if (SHIP == 3) {
                if (DIR == (JOptionPane.YES_OPTION)) { // YES_OPTION means Horizontal
                    for (c = column; c < column + 3; c++) {
                        Player[r][c] = 3;
                    }
                } else {
                    for (r = row; r <= row + 3; r++) {
                        Player[r][c] = 3;
                    }
                }
                // SHIP == 2
            } else {
                if (DIR == (JOptionPane.YES_OPTION)) { // YES_OPTION means Horizontal
                    for (c = column; c < column + 2; c++) {
                        Player[r][c] = 2;
                    }
                } else {
                    for (r = row; r <= row + 2; r++) {
                        Player[r][c] = 2;
                    }
                }
            }
            SHIP--;

        } while (SHIP != 1);
        repaint();

        // DO WHILE NOT GAMEOVER
        do {

            // Aks User For Coordinate to Fire At
            int fireC = Integer.parseInt(JOptionPane.showInputDialog("Input the 'X'"
                    + "Coordinate in which you choose"
                    + "to fire at. \n "
                    + "(1-0) from Left to Right")) - 1;
            int fireR = Integer.parseInt(JOptionPane.showInputDialog("Input the 'Y'"
                    + "Coordinate in which you choose"
                    + "to fire at. \n "
                    + "(1-10) from top to bottom")) - 1;

            c = fireR;
            r = fireC;
            if (Computer[r][c] >= 2) {
                JOptionPane.showMessageDialog(null, "YOU HIT THE COMPUTER'S SHIP");
                Computer[r][c] *= -1;
            } else if (Computer[r][c] == 0) {
                JOptionPane.showMessageDialog(null, "YOU MISSED THE COMPUTER'S SHIP");
                Computer[r][c] = MISS;
            } else {
                JOptionPane.showMessageDialog(null, "YOU ALREADY FIRED THERE");
            }
            repaint();

            if (GAMEOVER(Player)) {
                JOptionPane.showMessageDialog(null, "YOU WON");
            } else {

                // COMPUTER ENTERS COORDINATES AND FIRES
                Random generator = new Random();
                int cfirec = generator.nextInt(10);
                int cfirer = generator.nextInt(10);
                if (Player[r][c] >= 2) {
                    Player[r][c] *= -1;
                } else if (Player[r][c] == 0) {
                } else {
                }


            }
        } while (!GAMEOVER(Computer));
    }

    // CHECKING SHIPs
    private boolean isValid(int r, int c, int DIR, int SHIP) {
        if (DIR == JOptionPane.YES_NO_OPTION) {
            if (r < 0 || (r + SHIP >= 9)) {
                return false;
            } else {
                return true;
            }
        } else {                           // (Vert)
            if (c < 0 || (c + SHIP >= 9)) {
                return false;
            } else {
                return true;
            }
        }
    }
    // CHECKS FOR GAMEOVER
    private boolean GAMEOVER(int[][] G) {
        for (int rows = 0; rows < 10; rows++) {
            for (int columns = 0; columns < 10; columns++) {
                if (G[r][c] > 1) {
                    return false;
                }
            }
        }
        return true;
    }

    public void paint(Graphics g) {
        super.paint(g);       // call the jframe paint
        PaintBoard(g);        // call the tictactoe paintboard
    }
    // Variables declaration - do not modify                     
    // End of variables declaration                   

    private void PaintBoard(Graphics g) {

        // Left Box
        g.drawLine(40, 100, 550, 100);
        g.drawLine(40, 100, 40, 610);
        g.drawLine(550, 100, 550, 610);
        g.drawLine(40, 610, 550, 610);

        // Right Box
        g.drawLine(650, 100, 1160, 100);
        g.drawLine(650, 100, 650, 610);
        g.drawLine(650, 610, 1160, 610);
        g.drawLine(1160, 100, 1160, 610);

        // Left Box Grid Up/Down
        g.drawLine(91, 100, 91, 610);
        g.drawLine(142, 100, 142, 610);
        g.drawLine(193, 100, 193, 610);
        g.drawLine(244, 100, 244, 610);
        g.drawLine(295, 100, 295, 610);
        g.drawLine(346, 100, 346, 610);
        g.drawLine(397, 100, 397, 610);
        g.drawLine(448, 100, 448, 610);
        g.drawLine(499, 100, 499, 610);

        // Right Box Grid Up/Down
        g.drawLine(701, 100, 701, 610);
        g.drawLine(752, 100, 752, 610);
        g.drawLine(803, 100, 803, 610);
        g.drawLine(854, 100, 854, 610);
        g.drawLine(905, 100, 905, 610);
        g.drawLine(956, 100, 956, 610);
        g.drawLine(1007, 100, 1007, 610);
        g.drawLine(1058, 100, 1058, 610);
        g.drawLine(1109, 100, 1109, 610);

        // Left Box Left/Right
        g.drawLine(40, 151, 550, 151);
        g.drawLine(40, 202, 550, 202);
        g.drawLine(40, 253, 550, 253);
        g.drawLine(40, 304, 550, 304);
        g.drawLine(40, 355, 550, 355);
        g.drawLine(40, 406, 550, 406);
        g.drawLine(40, 457, 550, 457);
        g.drawLine(40, 508, 550, 508);
        g.drawLine(40, 559, 550, 559);

        // Right Box Left/Right
        g.drawLine(650, 151, 1160, 151);
        g.drawLine(650, 202, 1160, 202);
        g.drawLine(650, 253, 1160, 253);
        g.drawLine(650, 304, 1160, 304);
        g.drawLine(650, 355, 1160, 355);
        g.drawLine(650, 406, 1160, 406);
        g.drawLine(650, 457, 1160, 457);
        g.drawLine(650, 508, 1160, 508);
        g.drawLine(650, 559, 1160, 559);

        // Rectangle @ Bottom
        g.drawRect(141, 630, 919, 100);

        // Letters For Coordinates Left
        g.drawString("A", 62, 95);
        g.drawString("B", 113, 95);
        g.drawString("C", 164, 95);
        g.drawString("D", 215, 95);
        g.drawString("E", 266, 95);
        g.drawString("F", 317, 95);
        g.drawString("G", 368, 95);
        g.drawString("H", 419, 95);
        g.drawString("I", 470, 95);
        g.drawString("J", 521, 95);

        // Letter For Coordinates Right
        g.drawString("A", 672, 95);
        g.drawString("B", 733, 95);
        g.drawString("C", 774, 95);
        g.drawString("D", 825, 95);
        g.drawString("E", 876, 95);
        g.drawString("F", 927, 95);
        g.drawString("G", 978, 95);
        g.drawString("H", 1029, 95);
        g.drawString("I", 1080, 95);
        g.drawString("J", 1131, 95);

        // Number For Coordinate Lefy
        g.drawString("1", 30, 130);
        g.drawString("2", 30, 181);
        g.drawString("3", 30, 232);
        g.drawString("4", 30, 283);
        g.drawString("5", 30, 334);
        g.drawString("6", 30, 385);
        g.drawString("7", 30, 436);
        g.drawString("8", 30, 487);
        g.drawString("9", 30, 538);
        g.drawString("10", 23, 589);

        //Number For Coordinate Right
        g.drawString("1", 640, 130);
        g.drawString("2", 640, 181);
        g.drawString("3", 640, 232);
        g.drawString("4", 640, 283);
        g.drawString("5", 640, 334);
        g.drawString("6", 640, 385);
        g.drawString("7", 640, 436);
        g.drawString("8", 640, 487);
        g.drawString("9", 640, 538);
        g.drawString("10", 633, 589);

        // "BATTLESHIP
        g.drawString("BATTLESHIP", 563, 50);
        g.drawString("YOUR SHIPS", 259, 75);
        g.drawString("YOUR SHOTS", 867, 75);

        // Insert Image

        // Inset # Hits to Sink
        // Aircraft Carrier
        g.drawImage(ACpicture, 185, 637, 100, 40, null);
        g.drawString("AIRCRAFT CARRIER", 185, 645);
        g.setColor(Color.red);
        g.fillOval(350, 650, 10, 10);
        g.fillOval(370, 650, 10, 10);
        g.fillOval(390, 650, 10, 10);
        g.fillOval(410, 650, 10, 10);
        g.fillOval(430, 650, 10, 10);
        g.fillOval(450, 650, 10, 10);

        // Battleship
        g.setColor(Color.black);
        g.drawImage(Bpicture, 725, 632, 150, 47, null);
        g.drawString("BATTLESHIP", 700, 645);
        g.setColor(Color.red);
        g.fillOval(900, 650, 10, 10);
        g.fillOval(920, 650, 10, 10);
        g.fillOval(940, 650, 10, 10);
        g.fillOval(960, 650, 10, 10);
        g.fillOval(980, 650, 10, 10);


        // Destroyer
        g.setColor(Color.black);
        g.drawImage(Dpicture, 185, 680, 100, 50, null);
        g.drawString("DESTROYER", 185, 685);
        g.setColor(Color.red);
        g.fillOval(350, 705, 10, 10);
        g.fillOval(370, 705, 10, 10);
        g.fillOval(390, 705, 10, 10);
        g.fillOval(410, 705, 10, 10);


        // Submarine
        g.setColor(Color.black);
        g.drawString("SUBMARINE", 500, 680);
        g.setColor(Color.red);
        g.fillOval(625, 705, 10, 10);
        g.fillOval(645, 705, 10, 10);
        g.fillOval(665, 705, 10, 10);
        g.drawImage(Spicture, 485, 680, 100, 50, null);

        // Patrol Boat
        g.setColor(Color.black);
        g.drawImage(PBpicture, 725, 685, 100, 45, null);
        g.drawString("PATROL BOAT", 700, 685);
        g.setColor(Color.red);
        g.fillOval(900, 705, 10, 10);
        g.fillOval(920, 705, 10, 10);



        // Places A BLACK DOT Where There Is A SHIP

        // COLORS LEFT GRID FOR SHIPS
        for (r = 0; r < 10; r++) {
            for (c = 0; c < 10; c++) {
                if (Player[r][c] > 0) {
                    g.setColor(Color.black);
                    g.fillOval(r * 51 + 49, c * 51 - 11 + 120, 36, 36);
                } else if (Player[r][c] > 0) {
                    g.setColor(Color.red);
                    g.fillOval(r * 51 + 49, c * 51 - 11 + 120, 36, 36);
                }
            }

            // COLORS GRID ON RIGHT FOR HIT OR MISS
            for (r = 0; r < 10; r++) {
                for (c = 0; c < 10; c++) {
                    if (Computer[r][c] < 0) {
                        g.setColor(Color.red);
                        g.fillOval(r * 51 + 655, c * 51 - 11 + 120, 36, 36);
                    } else if (Computer[r][c] == 1) {
                        g.setColor(Color.white);
                        g.fillOval(r * 51 + 655, c * 51 - 11 + 120, 36, 36);
                    }

                }
            }
        }
    }
}
coderhelp is offline   Reply With Quote
Old 01-27-2013, 10:29 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 03-06-2013, 08:16 PM   PM User | #5
Taro
Regular Coder

 
Taro's Avatar
 
Join Date: Oct 2011
Location: Geraldton, Ontario
Posts: 155
Thanks: 1
Thanked 1 Time in 1 Post
Taro is an unknown quantity at this point
Well, there can be problems where the ships are placed along the map. I suggest trying to make a checking function/ if-statement to make sure that the user input follows the rules that you specified. For example,

PHP Code:
public void checkPlacement()
{
if(
/**/)
for(
int f=xf>xf--;)//assuming this is a 100 by 100 board
{
Spicture Toolkit.getDefaultToolkit().getImage(cloader.getResource("Submarine.jpg"));
        
prepareImage(Spicturethis);
}

An alternative would be to set a Boolean type, but its important that any external classes would be able to access it, hence the public statement.
__________________
Element ID

Webs Support Helper

Your friendly neighborhood Taroman.
Taro is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:18 PM.


Advertisement
Log in to turn off these ads.