9 Replies - 1846 Views - Last Post: 17 May 2013 - 07:14 AM Rate Topic: -----

#1 jlm55  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-May 13

Battleship program

Posted 09 May 2013 - 07:19 AM

I am having some problems with this program. Our class has not worked with matrices yet this year so I checked the API and still am stuck. Handler 1 is supposed to detect a hit or miss and change the background to red or white respectively. I can do the torpedoes alright but am having trouble with the detection part and am also unsure if the other parts are correct. Any tips are appreciated.


import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   public class Battleship extends JPanel
   {
      private JButton[][] board;
      private int[][] matrix;
      private int hits, torpedoes;
      private JLabel label;
      private JButton reset;
      public Battleship()
      {
         setLayout(new BorderLayout());
         hits = 0;
         torpedoes = 20;
      
         JPanel north = new JPanel();
         north.setLayout(new FlowLayout());
         add(north, BorderLayout.NORTH);
         label = new JLabel("Welcome to Battleship -- You have 20 torpedoes.");
         north.add(label);
      
         JPanel center = new JPanel();
         center.setLayout(new GridLayout(10,10));
         add(center, BorderLayout.CENTER);
      
         board = new JButton[10][10];
         matrix = new int[10][10];
         for(int r = 0; r < 10; r++)
            for(int c = 0; c < 10; c++)
            {
               board[r][c] = new JButton();
               board[r][c].setBackground(Color.blue);
               board[r][c].addActionListener( new Handler1(r, c) );
               center.add(board[r][c]);
            }
      
         reset = new JButton("Reset");
         reset.addActionListener( new Handler2() );
         reset.setEnabled(false);
         add(reset, BorderLayout.SOUTH);
      
         placeShip();
      }
      private void placeShip()
      {
int col = (int)((Math.random()*10)+1);
int row = (int)((Math.random()*10)+1);



int coin = (int)(Math.random() * 2);

for(int i = 0; i<4; i++)
{
if(coin==1)
{
col = col -1;
}
else
{
row = row +1;
}
int[][]battleship = new int[row][col];
}




		}

   
      private class Handler1 implements ActionListener
      {
         private int myRow, myCol;
         public Handler1(int r, int c)
         {
            myRow = r;
            myCol = c;
         }
         public void actionPerformed(ActionEvent e)
         {



         }
      }
      private class Handler2 implements ActionListener
      {
         public void actionPerformed(ActionEvent e)
         {
board[10][10].setBackground(null);
for(int r = 0; r < matrix.length; r++)
   matrix[r][0] = 0;				
for(int c = 0; c < matrix[0].length; c++)
   matrix[1][c] = 0;


         }
      }
   
	}


This post has been edited by modi123_1: 09 May 2013 - 07:33 AM
Reason for edit:: fixed botched code tag


Is This A Good Question/Topic? 1
  • +

Replies To: Battleship program

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Battleship program

Posted 09 May 2013 - 07:51 AM

Well first thing I noticed is that your placeship() function creates some random values and adjusts the col/row values but you never actually put the ship on the board. You won't be able to make any detection if there is a hit or miss until you place the ships on the board. In the placeship() function you will need to be setting the values in board[][].

Also you will need to be detecting if the place where you are going to set a ship isn't already part of another ship. That way you don't set one ship on top of another.

I offer some ship placement code for battleship over on my website. The code is in PHP but you can at least get an idea of how I am detecting where to place a ship and how to place it on the board. May be of some use.

http://www.coderslexicon.com/code/23/

You will need to work on this part before you can get to the hit/miss detection. :)
Was This Post Helpful? 1
  • +
  • -

#3 jlm55  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-May 13

Re: Battleship program

Posted 09 May 2013 - 09:09 AM

Thanks for the quick reply. The program I am creating only has one ship that is four "blocks" long so I will just have to make sure it doesn't extend off of the board.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1423
  • View blog
  • Posts: 3,165
  • Joined: 05-April 11

Re: Battleship program

Posted 09 May 2013 - 09:35 AM

Start by decoupling your code and separate GUI and logic
You should be able to get all the logic down without writing any GUI code
public class Ship {
	//...
}

public class Board {
	private Ship[][] board;
	//...
	
	public void placeShip(int startX, int startY, boolean vertical, Ship ship) {
		//...
	}
	
	public boolean hasShip(int x, int y) {
		return getShip(x, y) != null;
	}
	
	public Ship getShip(int x, int y) {
		return board[y][x];
	}
}


Was This Post Helpful? 1
  • +
  • -

#5 jlm55  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-May 13

Re: Battleship program

Posted 15 May 2013 - 05:43 AM

Thanks for the help, I finally finished the program!!
Was This Post Helpful? 0
  • +
  • -

#6 jlm55  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-May 13

Re: Battleship program

Posted 15 May 2013 - 06:51 AM

I have a problem. I'm not sure how to disable the board once I am finished.
Was This Post Helpful? 0
  • +
  • -

#7 jlm55  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-May 13

Re: Battleship program

Posted 15 May 2013 - 07:29 AM

This part works, but after I get the four hits, or run out of torpedoes, I still can click the buttons and the torpedoes just go to negative numbers. Any suggestions on how to freeze the board!! :donatello:/>
  public void actionPerformed(ActionEvent e)
         {
         
            if(matrix[myRow][myCol]==1)
            {
               board[myRow][myCol].setBackground(Color.RED);
            }
            else
            {
               board[myRow][myCol].setBackground(Color.WHITE);
            }
         
            if(matrix[myRow][myCol]==1)
            {
               torpedoes = torpedoes -1;
               hits=hits+1;
               label.setText("Hit! You have "  + torpedoes +  " torpedoes.");
            }
            else
            {
               torpedoes = torpedoes -1;
               label.setText("Miss! You have "  + torpedoes +  " torpedoes.");
            }


Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1423
  • View blog
  • Posts: 3,165
  • Joined: 05-April 11

Re: Battleship program

Posted 15 May 2013 - 08:27 AM

Maybe add a game state? Only handle the event if the game is live or somthing
You could also show some kind of defeat view or remove the action listener
..there are so many ways - even checking if the player has any torpedoes left would be an option
Was This Post Helpful? 1
  • +
  • -

#9 jlm55  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-May 13

Re: Battleship program

Posted 15 May 2013 - 09:10 AM

I'm going to freeze the board when there are no torpedoes left, or if you sink the ship, but I'm not sure how to freeze the board. I've tried setEnabled(false); but I got an error.
Was This Post Helpful? 0
  • +
  • -

#10 jlm55  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-May 13

Re: Battleship program

Posted 17 May 2013 - 07:14 AM

Never mind, I finally solved my problem. Thanks for the help!! :bananaman:/>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1