Results 1 to 15 of 15
  1. #1
    SilentCoder is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default Help with Computer Ai

    Hi, i am making Battle Ship game as School Array Project and i was wondering how would i randomize computer ships?

    For Example, how would i randomize this ship
    Java Code:
    comp [1] = "Resources/Images/Ships/Horizontal/BattleshipH/bsh1";
            comp [2] = "Resources/Images/Ships/Horizontal/BattleshipH/bsh2";
            comp [3] = "Resources/Images/Ships/Horizontal/BattleshipH/bsh3";
            comp [4] = "Resources/Images/Ships/Horizontal/BattleshipH/bsh4";
    Thank you.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your question as asked is unanswerable: how does one "randomize" a ship? Randomize what? Color? Position? Image? Name? With what constraints? etc...

    Care to give us the details? Please look at the link in my links below on how to ask smart questions so you'll see exactly the information we need.

    Luck

  3. #3
    SilentCoder is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Sorry forgot to mention that. by Randomize i mean put a ship on a random position on the Grid i have.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can use Collections.shuffle, or use a random object to index into the array

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Now that you clarified, it really depends on what the grid is like, a 2d array? If so, choose a blank space in the array randomly and then look the slot and surrounding slots and determine if they are empty, if you have enough room either fill horizontally, or vertically. Of course this depends on you using multiple spots per ship.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by SilentCoder View Post
    Sorry forgot to mention that. by Randomize i mean put a ship on a random position on the Grid i have.
    I'm guessing that it's more than that: I'm guessing that you want to randomize both the position and the orientation of the ship (either up-down or left-right). If so, then it may take more effort than simply calling shuffle on a collection. You could create a collection of all possible positions of a ship, shuffle it, and then on selecting one, make sure that the ship fits the site and that it doesn't already contain a ship/

  7. #7
    SilentCoder is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    i tried doing random position by using Math.random() out of 100 but sometimes i get out of bound expression or the ship won't show up!

    Below is my code for the game. Thanks

    Computer ship placement code is compShips ();

    [SPOILER]
    Java Code:
    import java.awt.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.border.*;
    
    public class PlayerVsComputer extends JFrame implements ActionListener
    {
        String ship = "";
        int shipsPlaced = 0;
        String tracker[] = new String [100];
        boolean mAccount = false;
        boolean uAccount = false;
        boolean PVP = false;
        boolean PVC = false;
        boolean soundz = true;
        String comp[] = new String [100];
        String user[] = new String [100];
        boolean doneTurn = false;
    
        int bshipz = '0';
        int cshipz = '0';
        int pboatz = '0';
        int swolfz = '0';
        int smarinez = '0';
        JButton battleShipsH;
        JButton carriersH;
        JButton patrolH;
        JButton seawolfH;
        JButton submarineH;
        JButton battleShipV;
        JButton carriersV;
        JButton patrolV;
        JButton seawolfV;
        JTextArea patcher;
        JTextField mssgz;
        JButton mssgDisPatch;
        JButton submarineV;
        JButton statz;
        JLabel turnz;
    
    
        JButton a[];
        JButton c[];
        int row = 10;
        int col = 10;
        int total = row * col;
        int current[] [];
        Image img;
        int attempt;
        Container z;
    
        int playerShots = 0;
        int computerShots = 0;
        int playerMiss = 0;
        int playerHits = 0;
        int computerMiss = 0;
        int computerHits = 0;
    
        int placeSHip = 0;
        int placeC = 0;
        int placePatrol = 0;
        int placeSW = 0;
        int placeSub = 0;
    
    
        public PlayerVsComputer ()
        {
            super ("BattleShip Player V.S Computer"); // Title of the program
            setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            Sound.themeSong.play ();
            for (int i = 0 ; i < 100 ; i++)
            {
    
                tracker [i] = "";
    
            }
            JPanel game = new JPanel ();
            game.setLocation (0, 0);
            game.setBounds (270, 50, 100, 40);
            game.setPreferredSize (new Dimension (1025, 720));
    
            //---- title ----
            JLabel title = new JLabel ("<html><center><font size=\"16\">BattleShip Player V.S Computer</font></center></html>");
            title.setFont (new Font ("Transformers", Font.PLAIN, 30));
            game.add (title);
            title.setBounds (65, 30, 495, title.getPreferredSize ().height);
    
            JLabel ptitle = new JLabel ("Player V.S Computer");
            ptitle.setFont (new Font ("Segoe UI", Font.PLAIN, 20));
            ptitle.setForeground (Color.red);
            //game.add (ptitle);
            ptitle.setBounds (190, 150, 165, 30);
    
    
            battleShipsH = new JButton (createImageIcon ("Resources/Images/Ships/Horizontal/battleship.gif"));
            battleShipsH.setBounds (180, 50, 120, 40);
            battleShipsH.addActionListener (this);
            battleShipsH.setActionCommand ("battleShipH");
            getContentPane ().add (battleShipsH);
    
            carriersH = new JButton (createImageIcon ("Resources/Images/Ships/Horizontal/carrier.gif"));
            carriersH.setBounds (305, 50, 140, 40);
            carriersH.addActionListener (this);
            carriersH.setActionCommand ("carrierH");
            getContentPane ().add (carriersH);
    
            patrolH = new JButton (createImageIcon ("Resources/Images/Ships/Horizontal/patrol.gif"));
            patrolH.setBounds (450, 50, 70, 40);
            patrolH.setActionCommand ("patrolH");
            patrolH.addActionListener (this);
            getContentPane ().add (patrolH);
    
            seawolfH = new JButton ((createImageIcon ("Resources/Images/Ships/Horizontal/seawolf.gif")));
            seawolfH.setBounds (525, 50, 110, 40);
            seawolfH.addActionListener (this);
            seawolfH.setActionCommand ("seawolfH");
            getContentPane ().add (seawolfH);
    
            submarineH = new JButton ((createImageIcon ("Resources/Images/Ships/Horizontal/submarine.gif")));
            submarineH.setBounds (640, 50, 100, 40);
            submarineH.addActionListener (this);
            submarineH.setActionCommand ("submarineH");
            getContentPane ().add (submarineH);
    
            battleShipV = new JButton (createImageIcon ("Resources/Images/Ships/Vertical/battleshipv.gif"));
            battleShipV.setBounds (50, 120, 40, 120);
            getContentPane ().add (battleShipV);
            battleShipV.addActionListener (this);
            battleShipV.setActionCommand ("battleShipV");
    
    
            carriersV = new JButton (createImageIcon ("Resources/Images/Ships/Vertical/carrierv.gif"));
            carriersV.setBounds (50, 250, 40, 150);
            carriersV.addActionListener (this);
            carriersV.setActionCommand ("carriersV");
            getContentPane ().add (carriersV);
    
            patrolV = new JButton (createImageIcon ("Resources/Images/Ships/Vertical/patrolv.gif"));
            patrolV.setBounds (50, 405, 40, 70);
            patrolV.addActionListener (this);
            patrolV.setActionCommand ("patrolV");
            getContentPane ().add (patrolV);
    
            seawolfV = new JButton (createImageIcon ("Resources/Images/Ships/Vertical/seawolfv.gif"));
            seawolfV.setBounds (50, 480, 40, 110);
            seawolfV.addActionListener (this);
            seawolfV.setActionCommand ("seawolfV");
            getContentPane ().add (seawolfV);
    
            submarineV = new JButton (createImageIcon ("Resources/Images/Ships/Vertical/submarinev.gif"));
            submarineV.setBounds (50, 600, 40, 110);
            submarineV.addActionListener (this);
            submarineV.setActionCommand ("submarineV");
            getContentPane ().add (submarineV);
    
            JPanel grid = new JPanel ();
            JPanel player = new JPanel (new GridLayout (row, col, 0, 0));
            TitledBorder titlez = BorderFactory.createTitledBorder ("         " + battleShip.PlayerName + "'s Ships        ");
            player.setBorder (titlez);
    
            current = new int [row] [col];
            for (int i = 0 ; i < col ; i++)
                for (int j = 0 ; j < col ; j++)
                    current [i] [j] = 0;
    
            a = new JButton [total];
    
            for (int i = 0 ; i < total ; i++)
            {
                a [i] = new JButton ();
                a [i].setBounds (10, 555, 400, 400);
                player.add (a [i]);
                a [i].setPreferredSize (new Dimension (30, 30));
                a [i].setBackground (Color.red);
                player.setBounds (190, 65, 165, 30);
                a [i].setBackground (Color.white);
                a [i].addActionListener (this);
                a [i].setActionCommand ("" + i);
            }
            player.setBounds (100, 100, 450, 450);
            getContentPane ().add (player);
    
            JPanel Computer = new JPanel (new GridLayout (row, col, 0, 0));
            TitledBorder ctitle = BorderFactory.createTitledBorder ("         Computer's Ships        ");
            Computer.setBorder (ctitle);
    
            current = new int [row] [col];
            for (int i = 0 ; i < col ; i++)
                for (int j = 0 ; j < col ; j++)
                    current [i] [j] = 0;
    
            c = new JButton [total];
    
            for (int i = 0 ; i < total ; i++)
            {
                c [i] = new JButton ("");
                Computer.add (c [i]);
                c [i].setPreferredSize (new Dimension (40, 40));
                Computer.setBounds (800, 65, 165, 30);
                c [i].setBackground (Color.white);
                c [i].addActionListener (this);
                c [i].setActionCommand ("" + i);
            }
            getContentPane ().add (Computer);
            Computer.setBounds (550, 100, 450, 450);
            //getContentPane ().add (grid);
    
            patcher = new JTextArea ();
            patcher.setEditable (false);
            JScrollPane patcherScroll = new JScrollPane (patcher);
            patcherScroll.setVerticalScrollBarPolicy (
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            patcherScroll.setPreferredSize (new Dimension (250, 145));
            patcherScroll.setMinimumSize (new Dimension (10, 10));
    
            patcher.setText ("[System] Please Place your Ships!");
            patcherScroll.setBounds (100, 550, 700, 100);
            getContentPane ().add (patcherScroll);
    
            mssgz = new JTextField ();
            mssgz.setBounds (100, 660, 550, 25);
            getContentPane ().add (mssgz);
    
            mssgDisPatch = new JButton ("Dispatch Message!");
            mssgDisPatch.setBounds (660, 661, 135, 25);
            mssgDisPatch.addActionListener (this);
            mssgDisPatch.setActionCommand ("mssgDisp");
            getContentPane ().add (mssgDisPatch);
    
            JPanel stats = new JPanel ();
    
            turnz = new JLabel ("Computer's Turn");
            turnz.setForeground (Color.white);
            stats.add (turnz);
    
            statz = new JButton ("Statistics");
            statz.setActionCommand ("statistics");
            statz.addActionListener (this);
            stats.add (statz);
    
            stats.setBounds (800, 550, 200, 300);
            getContentPane ().add (stats);
    
            getContentPane ().add (game);
    
            pack ();
            setVisible (true);
            compShips ();
        }
    
    
        public void actionPerformed (ActionEvent e)
        {
            if (e.getActionCommand ().equals ("mssgDisp"))
            {
                String mz = mssgz.getText ();
                if (mz.equals (""))
                {
                    patcher.append ("\n[System] Please Enter a message to Dispatch!");
                }
                else
                {
                    String MssgDisp = ("\n[" + battleShip.PlayerName + "] " + mssgz.getText () + ".");
                    patcher.append (MssgDisp);
                }
            }
            else if (e.getActionCommand ().equals ("statistics"))
            {
                statistics ();
            }
            else if (e.getActionCommand ().equals ("battleShipV"))
            {
                ship = "battleShipV";
            }
            else if (e.getActionCommand ().equals ("carriersV"))
            {
                ship = "carriersV";
            }
            else if (e.getActionCommand ().equals ("patrolV"))
            {
                ship = "patrolV";
            }
            else if (e.getActionCommand ().equals ("seawolfV"))
            {
                ship = "seawolfV";
            }
            else if (e.getActionCommand ().equals ("submarineV"))
            {
                ship = "submarineV";
            }
            else if (e.getActionCommand ().equals ("battleShipH"))
            {
                ship = "battleShipH";
            }
            else if (e.getActionCommand ().equals ("carrierH"))
            {
                ship = "carrierH";
            }
            else if (e.getActionCommand ().equals ("patrolH"))
            {
                ship = "patrolH";
            }
            else if (e.getActionCommand ().equals ("seawolfH"))
            {
                ship = "seawolfH";
            }
            else if (e.getActionCommand ().equals ("submarineH"))
            {
                ship = "submarineH";
            }
            else
            {
                int pos = Integer.parseInt (e.getActionCommand ());
                if (shipsPlaced < 5) //pre game
                {
                    placeShips (pos);
                }
                else //playing game
                {
                    patcher.setText ("");
                    if (comp [pos].equals (""))
                    {
                        c [pos].setIcon (createImageIcon ("Resources/Images/water.gif"));
                        patcher.append ("You missed");
                        doneTurn = true;
                        playerShots++;
                        playerMiss++;
                    }
                    else
                    {
                        c [pos].setIcon (createImageIcon ("Resources/Images/fire.gif"));
                        patcher.append ("You hit");
                        doneTurn = true;
                        Sound.explosion.play ();
                        playerShots++;
                        playerHits++;
                    }
                    //change tracking array
    
    
                    //random move comp
                    int rand = (int) (Math.random () * 100);
                    if (tracker [rand].equals (""))
                    {
                        a [rand].setIcon (createImageIcon ("Resources/Images/water.gif"));
    
                        patcher.append ("Computer missed");
                        doneTurn = true;
                        computerShots++;
                        computerMiss++;
                    }
                    else
                    {
                        a [rand].setIcon (createImageIcon ("Resources/Images/fire.gif"));
    
                        patcher.append ("Player hit");
                        doneTurn = true;
                        Sound.explosion.play ();
                        computerShots++;
                        computerHits++;
                    }
                    //check for win
                }
            }
            ship_mssg ();
        }
    
    
        public void placeShips (int pos)
        {
    
            if (ship.equals ("battleShipV"))
            {
                if ((pos + col) < 100 && (pos + col + col) < 100 && tracker [pos].equals ("") && tracker [pos + col].equals ("") && tracker [pos + col + col].equals (""))
                {
                    a [pos].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/BattleShipV/bsv1.gif"));
                    a [pos + col].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/BattleShipV/bsv2.gif"));
                    a [pos + col + col].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/BattleShipV/bsv3.gif"));
                    battleShipV.setVisible (false);
                    battleShipsH.setVisible (false);
                    bshipz = '1';
                    tracker [pos] = "Resources/Images/Ships/Vertical/BattleShipV/bsv1";
                    tracker [pos + col] = "Resources/Images/Ships/Vertical/BattleShipV/bsv2";
                    tracker [pos + col + col] = "Resources/Images/Ships/Vertical/BattleShipV/bsv3";
                    shipsPlaced++;
    
                }
                else
                {
                    patcher.append ("\n[System] Cannot place the ship there! Please place somewhere else.");
    
                }
            }
            else if (ship.equals ("carriersV"))
            {
                if ((pos + col) < 100 && (pos + col + col) < 100 && (pos + col + col + col) < 100 && tracker [pos].equals ("") && tracker [pos + col].equals ("") && tracker [pos + col + col].equals ("") && tracker [pos + col + col + col].equals (""))
                {
                    a [pos].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/CarrierV/cv1.gif"));
                    a [pos + col].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/CarrierV/cv2.gif"));
                    a [pos + col + col].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/CarrierV/cv3.gif"));
                    a [pos + col + col + col].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/CarrierV/cv4.gif"));
                    carriersV.setVisible (false);
                    carriersH.setVisible (false);
                    cshipz = '1';
                    tracker [pos] = "Resources/Images/Ships/Vertical/CarrierV/cv1";
                    tracker [pos + col] = "Resources/Images/Ships/Vertical/CarrierV/cv2";
                    tracker [pos + col + col] = "Resources/Images/Ships/Vertical/CarrierV/cv3";
                    tracker [pos + col + col + col] = "Resources/Images/Ships/Vertical/CarrierV/cv4";
                    shipsPlaced++;
                }
                else
                {
                    patcher.append ("\n[System] Cannot place the ship there! Please place somewhere else.");
    
                }
            }
            else if (ship.equals ("patrolV"))
            {
                if ((pos + col) < 100 && tracker [pos].equals ("") && tracker [pos + col].equals (""))
                {
                    a [pos].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/patrolV/pv1.gif"));
                    a [pos + col].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/patrolV/pv2.gif"));
                    patrolV.setVisible (false);
                    patrolH.setVisible (false);
                    pboatz = '1';
                    tracker [pos] = "Resources/Images/Ships/Vertical/patrolV/pv1";
                    tracker [pos + col] = "Resources/Images/Ships/Vertical/patrolV/pv2";
                    shipsPlaced++;
                }
                else
                {
                    patcher.append ("\n[System] Cannot place the ship there! Please place somewhere else.");
    
                }
            }
            else if (ship.equals ("seawolfV"))
            {
                if ((pos + col) < 100 && (pos + col + col) < 100 && tracker [pos].equals ("") && tracker [pos + col].equals ("") && tracker [pos + col + col].equals (""))
                {
                    a [pos].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/seawolfV/swv1.gif"));
                    a [pos + col].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/seawolfV/swv2.gif"));
                    a [pos + col + col].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/seawolfV/swv3.gif"));
                    seawolfV.setVisible (false);
                    seawolfH.setVisible (false);
                    swolfz = '1';
                    tracker [pos] = "Resources/Images/Ships/Vertical/seawolfV/swv1";
                    tracker [pos + col] = "Resources/Images/Ships/Vertical/seawolfV/swv2";
                    tracker [pos + col + col] = "Resources/Images/Ships/Vertical/seawolfV/swv3";
    
    
    
                    shipsPlaced++;
                }
                else
                {
                    patcher.append ("\n[System] Cannot place the ship there! Please place somewhere else.");
    
                }
            }
            else if (ship.equals ("submarineV"))
            {
                if ((pos + col) < 100 && (pos + col + col) < 100 && tracker [pos].equals ("") && tracker [pos + col].equals ("") && tracker [pos + col + col].equals (""))
                {
                    a [pos].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/submarineV/smv1.gif"));
                    a [pos + col].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/submarineV/smv2.gif"));
                    a [pos + col + col].setIcon (createImageIcon ("Resources/Images/Ships/Vertical/submarineV/smv3.gif"));
                    submarineV.setVisible (false);
                    submarineH.setVisible (false);
                    smarinez = '1';
                    tracker [pos] = "Resources/Images/Ships/Vertical/submarineV/smv1";
                    tracker [pos + col] = "Resources/Images/Ships/Vertical/submarineV/smv2";
                    tracker [pos + col + col] = "Resources/Images/Ships/Vertical/submarineV/smv3";
                    shipsPlaced++;
                }
                else
                {
                    patcher.append ("\n[System] Cannot place the ship there! Please place somewhere else.");
    
                }
            }
            else if (ship.equals ("battleShipH"))
            {
                if ((pos % 10) < 7 && (pos + 1) < 100 && (pos + 2) < 100 && (pos + 3) < 100 && tracker [pos].equals ("") && tracker [pos + 1].equals ("") && tracker [pos + 2].equals ("") && tracker [pos + 3].equals (""))
                {
                    a [pos].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/BattleShipH/bsh1.gif"));
                    a [pos + 1].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/BattleShipH/bsh2.gif"));
                    a [pos + 2].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/BattleShipH/bsh3.gif"));
                    a [pos + 3].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/BattleShipH/bsh4.gif"));
                    battleShipV.setVisible (false);
                    battleShipsH.setVisible (false);
                    bshipz = '1';
                    tracker [pos] = "Resources/Images/Ships/Horizontal/BattleShipH/bsh1";
                    tracker [pos + 1] = "Resources/Images/Ships/Horizontal/BattleShipH/bsh2";
                    tracker [pos + 2] = "Resources/Images/Ships/Horizontal/BattleShipH/bsh3";
                    tracker [pos + 3] = "Resources/Images/Ships/Horizontal/BattleShipH/bsh4";
                    shipsPlaced++;
                }
                else
                {
                    patcher.append ("\n[System] Cannot place the ship there! Please place somewhere else.");
    
                }
            }
            else if (ship.equals ("carrierH"))
            {
                if ((pos % 10) < 7 && (pos + 1) < 100 && (pos + 2) < 100 && (pos + 3) < 100 && tracker [pos].equals ("") && tracker [pos + 1].equals ("") && tracker [pos + 2].equals ("") && tracker [pos + 3].equals (""))
                {
                    a [pos].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/carrierH/ch1.gif"));
                    a [pos + 1].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/carrierH/ch2.gif"));
                    a [pos + 2].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/carrierH/ch3.gif"));
                    a [pos + 3].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/carrierH/ch4.gif"));
                    carriersV.setVisible (false);
                    carriersH.setVisible (false);
                    cshipz = '1';
                    tracker [pos] = "Resources/Images/Ships/Horizontal/carrierH/ch1";
                    tracker [pos + 1] = "Resources/Images/Ships/Horizontal/carrierH/ch2";
                    tracker [pos + 2] = "Resources/Images/Ships/Horizontal/carrierH/ch3";
                    tracker [pos + 3] = "Resources/Images/Ships/Horizontal/carrierH/ch4";
                    shipsPlaced++;
                }
                else
                {
                    patcher.append ("\n[System] Cannot place the ship there! Please place somewhere else.");
    
                }
            }
            else if (ship.equals ("patrolH"))
            {
                if ((pos % 10) < 9 && (pos + 1) < 100 && tracker [pos].equals ("") && tracker [pos + 1].equals (""))
                {
                    a [pos].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/patrolH/pbh1.gif"));
                    a [pos + 1].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/patrolH/pbh2.gif"));
                    patrolV.setVisible (false);
                    patrolH.setVisible (false);
                    pboatz = '1';
                    tracker [pos] = "Resources/Images/Ships/Horizontal/patrolH/pbh1";
                    tracker [pos + 1] = "Resources/Images/Ships/Horizontal/patrolH/pbh2";
                    shipsPlaced++;
                }
                else
                {
                    patcher.append ("\n[System] Cannot place the ship there! Please place somewhere else.");
    
                }
            }
            else if (ship.equals ("seawolfH"))
            {
                if ((pos % 10) < 8 && (pos + 1) < 100 && (pos + 2) < 100 && tracker [pos].equals ("") && tracker [pos + 1].equals ("") && tracker [pos + 2].equals (""))
                {
                    a [pos].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/seawolfH/swh1.gif"));
                    a [pos + 1].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/seawolfH/swh2.gif"));
                    a [pos + 2].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/seawolfH/swh3.gif"));
                    seawolfV.setVisible (false);
                    seawolfH.setVisible (false);
                    swolfz = '1';
                    tracker [pos] = "Resources/Images/Ships/Horizontal/seawolfH/swh1";
                    tracker [pos + 1] = "Resources/Images/Ships/Horizontal/seawolfH/swh2";
                    tracker [pos + 2] = "Resources/Images/Ships/Horizontal/seawolfH/swh3";
                    shipsPlaced++;
                }
                else
                {
                    patcher.append ("\n[System] Cannot place the ship there! Please place somewhere else.");
    
                }
            }
            else if (ship.equals ("submarineH"))
            {
                if ((pos % 10) < 9 && (pos + 1) < 100 && tracker [pos].equals ("") && tracker [pos + 1].equals (""))
                {
                    a [pos].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/submarineH/smh1.gif"));
                    a [pos + 1].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/submarineH/smh2.gif"));
                    seawolfV.setVisible (false);
                    seawolfH.setVisible (false);
                    smarinez = '1';
                    tracker [pos] = "Resources/Images/Ships/Horizontal/submarineH/smh1";
                    tracker [pos + 1] = "Resources/Images/Ships/Horizontal/submarineH/smh2";
                    shipsPlaced++;
                }
                else
                {
                    patcher.append ("\n[System] Cannot place the ship there! Please place somewhere else.");
    
                }
            }
            hits ();
        }
    
    
        public void ship_mssg ()
        {
            if (bshipz == '1')
            {
                Sound.dispatcherz.play ();
                patcher.append ("\n[System] BattleShip has been stationed to battle.");
                bshipz = '2';
            }
            else if (cshipz == '1')
            {
                Sound.dispatcherz.play ();
                patcher.append ("\n[System] CarrierShip has been stationed to battle.");
                cshipz = '2';
            }
            else if (pboatz == '1')
            {
                Sound.dispatcherz.play ();
                patcher.append ("\n[System] Patrol Boat has been stationed to battle.");
                pboatz = '2';
            }
            else if (swolfz == '1')
            {
                Sound.dispatcherz.play ();
                patcher.append ("\n[System] Seawolf has been stationed to battle.");
                swolfz = '2';
            }
            else if (smarinez == '1')
            {
                Sound.dispatcherz.play ();
                patcher.append ("\n[System] Submarine has been stationed to battle.");
                smarinez = '2';
            }
            if ((bshipz == '2') && (cshipz == '2') && (pboatz == '2') && (swolfz == '2') && (smarinez == '2'))
            {
                int pgame = JOptionPane.showConfirmDialog (null, "Ready to Deploy Ships?", "Ready?", JOptionPane.YES_NO_OPTION);
                if (pgame == 0)
                    playGame ();
    
                Sound.dispatcherz.play ();
                patcher.append ("\n[System] Ok! Get Ready To Battle!");
                compShips ();
                bshipz = '3';
                cshipz = '3';
                pboatz = '3';
                swolfz = '3';
                smarinez = '3';
            }
        }
    
    
        public void playGame ()
        {
            if (doneTurn)
            {
                // TURN
            }
        }
    
    
        public void compShips ()
        {
            // BUGS COMPUTER'S SHIPS CANNOT BE PLACED PROPERLY!
            /*int r = (int) ((Math.random () * 100));
            int r1 = (int) ((Math.random () * 100));
            int r2 = (int) ((Math.random () * 100));
            int r3 = (int) ((Math.random () * 100));
            int r4 = (int) ((Math.random () * 100));*/
    
            int r = 1;
            int r2 = 3;
            int r3 = 7;
            int r4 = 9;
    
            for (int i = 0 ; i < comp.length ; i++)
                comp [i] = "";
    
    
            // Random SHIP GLITCHES
            //if (battleShip.gmode.equals ("easy"))
            //{
    
            comp [1] = "Resources/Images/Ships/Horizontal/BattleshipH/bsh1";
            comp [2] = "Resources/Images/Ships/Horizontal/BattleshipH/bsh2";
            comp [3] = "Resources/Images/Ships/Horizontal/BattleshipH/bsh3";
            comp [4] = "Resources/Images/Ships/Horizontal/BattleshipH/bsh4";
            placeSHip = 1;
    
    
            comp [42] = "Resources/Images/Ships/Vertical/CarrierV/cv1";
            comp [43] = "Resources/Images/Ships/Vertical/CarrierV/cv1";
            comp [44] = "Resources/Images/Ships/Vertical/CarrierV/cv1";
            comp [45] = "Resources/Images/Ships/Vertical/CarrierV/cv1";
    
            placeC = 1;
    
            comp [r2] = "Resources/Images/Ships/Horizontal/patrolH/pbh1";
            comp [r2 + 1] = "Resources/Images/Ships/Horizontal/patrolH/pbh2";
    
            placePatrol = 1;
    
    
            comp [r3] = "Resources/Images/Ships/Horizontal/seawolfH/swh1";
            comp [r3 + row] = "Resources/Images/Ships/Horizontal/seawolfH/swh1";
            comp [r3 + row + row] = "Resources/Images/Ships/Horizontal/seawolfH/swh1";
    
            placeSW = 1;
    
            comp [r4] = "Resources/Images/Ships/Vertical/submarineV/smv1";
            comp [r4 + row] = "Resources/Images/Ships/Vertical/submarineV/smv1";
            comp [r4 + 1 + 1] = "Resources/Images/Ships/Vertical/submarineV/smv1";
    
            placeSub = 1;
            /* }
    
             else if (battleShip.gmode.equals ("medium"))
             {
    
             }
    
    
             else
             {
                 hardDiff ();
             }*/
        }
    
    
        //c [1].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/BattleshipH/bsh1.gif"));
        //comp [1] = "Resources/Images/Ships/Horizontal/BattleshipH/bsh1";
        //c [1 + 1].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/BattleshipH/bsh2.gif"));
        //c [1 + 1 + 1].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/BattleshipH/bsh3.gif"));
        //c [1 + 1 + 1 + 1].setIcon (createImageIcon ("Resources/Images/Ships/Horizontal/BattleshipH/bsh4.gif"));
    
    
    
        public void hardDiff ()
        {
    
        }
    
    
    
        public void hits ()
        {
    
    
        }
    
    
    
        public void statistics ()
        {
            // Accuracy // Glitches
            int pacc, aacc;
            if (playerHits == 0 && playerMiss == 0)
            {
                pacc = 0;
            }
    
    
            else
            {
                pacc = (playerHits / playerMiss) * 100;
            }
    
    
            if (computerHits == 0 && computerMiss == 0)
            {
                aacc = 0;
            }
    
    
            else
            {
                aacc = (computerHits / computerMiss) * 100;
            }
    
    
            // HMR (Hits 2 Miss Ratio) // Glitches
            double phmr, chmr;
            if (playerHits == 0 && playerMiss == 0)
            {
                phmr = 0;
            }
    
    
            else
            {
                phmr = (playerHits / playerMiss);
            }
    
    
            if (computerHits == 0 && computerMiss == 0)
            {
                chmr = 0;
            }
    
    
            else
            {
                chmr = (computerHits / computerMiss);
            }
    
    
            JOptionPane.showMessageDialog (null,
                    "Game Statistics            " + battleShip.gmode + " Mode\n\n" +
                    "                                                            Player's Name                    Computer\n" +
                    "                                                            " + battleShip.PlayerName + "                                        Computer" +
                    "\nTotal Shots Taken:                         " + playerShots + "                                             " + computerShots + "" +
                    "\nTotal Hits:                                         " + playerHits + "                                             " + computerHits + "" +
                    "\nTotal Misses:                                   " + playerMiss + "                                             " + computerMiss + "" +
                    "\nAccuracy:                                         " + pacc + "                                             " + aacc + "" +
                    "\nHMR (Hits 2 Miss Ratio):               " + phmr + "                                          " + chmr + "",
                    "BattleShip Statistics", JOptionPane.PLAIN_MESSAGE);
        }
    
    
    
        public static void main (String[] args)
        {
            try
            {
                // select Look and Feel
                UIManager.setLookAndFeel ("com.jtattoo.plaf.hifi.HiFiLookAndFeel");
                // start application
                new PlayerVsComputer ();
            }
    
    
            catch (Exception ex)
            {
                ex.printStackTrace ();
            }
    
    
            // new battleShip (); //Main creates a new instance of our BattleShip class
    
        }
    
    
        protected static ImageIcon createImageIcon (String path)
        { //change the red to your class name
            java.net.URL imgURL = battleShip.class.getResource (path);
            if (imgURL != null)
                return new ImageIcon (imgURL);
            else
            {
                System.err.println ("Couldn't find file: " + path);
                return null;
            }
        }
    }
    [/SPOILER]
    Attached Thumbnails Attached Thumbnails Help with Computer Ai-capture.jpg  
    Last edited by SilentCoder; 06-10-2011 at 12:08 AM.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How do you plan on placing the ship? One ship per space, or can the ship take up multiple spaces? How is the grid represented? I can't see all of your code, and don't have enough time to sort through large amounts of code right now.

  9. #9
    SilentCoder is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    i plan to place ships by using up multiple place horizontally or vertically. The ships would look like in he attachment picture except hidden.
    Attached Thumbnails Attached Thumbnails Help with Computer Ai-capture.jpg  

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How is the code representing the grid? A 2d array?

    If so, choose a random spot on the grid, then choose a random alignment(horizontal or vertical). Then check to see the adjacent spaced required and fill in if safe. If it isn't, choose another random space.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you have an NxN grid and your ship has a length of L, then your ship has 2 * N * (N - L + 1) potential locations and orientations for where it can be placed, and the 2 * part comes from the two different potential orientations possible for each ship if it's length is > 1. I'd randomly choose a number from this range and then place the ship in the corresponding position if it is not already occupied.

  12. #12
    SilentCoder is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Umm, Can you please give me an example? Thanks

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    While Fubars explanation may have been a bit hard to understand, further review will help you understand it. Re read it a few times and see what you can come up with. You will basically be randomly indexing into some range, then checkingthe validity of the space, if it's valid, make the placement, if invalid, retry.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by SilentCoder View Post
    Umm, Can you please give me an example? Thanks
    I think that you can figure this out yourself if you start small and simple. For instance, think through the logic with a small grid, say a 5 x 5 grid and if ships were all 3 cells big. Then the ship can only be placed in one of 3 horizontal positions in a row (N - L + 1) = 5 - 3 + 1 = 3 positions, and if you try this out on paper you'll see why it's true. And since there are 5 rows, that means that each ship can potentially be placed in 5 * 3 or 15 positions horizontally. The same is true for vertically, hence the 2 * in the equation above. So if you choose a random number between 0 and 29, then use that number to figure out which position it corresponds to, you can get a potential position for the ship. For instance if the number is 19, then 19 / 15 (or 11 / (N - L + 1) using int division) is 1, meaning orientation is vertical (assuming horizontal positions are represented by the first half of these numbers), and then work with 19 % 15 (11 % (N - L + 1)) which returns 4, meaning the 4th vertical location. Then 4 / 3 (4 / N) gives us the column number, here 1, and 4 % 3 (4 % N) gives us the row in the column. Piece of cake if you think through it logically.

  15. #15
    SilentCoder is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    While Fubars explanation may have been a bit hard to understand, further review will help you understand it. Re read it a few times and see what you can come up with. You will basically be randomly indexing into some range, then checkingthe validity of the space, if it's valid, make the placement, if invalid, retry.
    this was the code i tried it before. Would this work?

    Java Code:
    int r = (int) ((Math.random () * 100) + 1);
    
     if ((r % 10) < 8 && (r + 1) < 100 && (r + 1 + 1) < 100 && (r + 1 + 1 + 1)< 100 && (r + 1 + 1 + 1 + 1) < 100 && comp [r + 1].equals ("") && comp [r + 1 + 1].equals ("") && comp [r + 1 + 1 + 1].equals ("") && comp [r + 1 + 1 + 1 + 1].equals (""))
                {
                      comp [r + 1] = "Resources/Images/Ships/Horizontal/BattleshipH/bsh1";
                      comp [r + 1 + 1] = "Resources/Images/Ships/Horizontal/BattleshipH/bsh2";
                      comp [r + 1 + 1 + 1] = "Resources/Images/Ships/Horizontal/BattleshipH/bsh3";
                      comp [r + 1 + 1 + 1 + 1] = "Resources/Images/Ships/Horizontal/BattleshipH/bsh4";
    }

Similar Threads

  1. Simple Instruction Set Computer
    By RKO in forum New To Java
    Replies: 7
    Last Post: 12-13-2010, 04:21 PM
  2. How to access to a LAN computer
    By cjavierm in forum New To Java
    Replies: 5
    Last Post: 12-02-2010, 05:57 PM
  3. Replies: 28
    Last Post: 09-15-2010, 10:21 AM
  4. how to log data being sent to another computer
    By jdizon in forum Networking
    Replies: 4
    Last Post: 08-10-2010, 12:19 PM
  5. I am new, can anyone help me in Computer Vision?
    By howardL in forum Introductions
    Replies: 0
    Last Post: 04-23-2009, 09:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •