[ad_1]
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 coin = (int)(Math.random() * 2);
int r = (int)(Math.random() * 7);
int c = (int)(Math.random() * 7);
if(coin == 1)
for(int rows = r; rows <= r + 3; rows++)
matrix[rows][c] = 1;
else
for(int col = c; col <= c + 3; col++)
matrix[r][col] = 1;
private class Handler1 implements ActionListener
private int myRow, myCol;
public Handler1(int r, int c)
myRow = r;
myCol = c;
public void actionPerformed(ActionEvent e)
torpedoes--;
board[myRow][myCol].setEnabled(false);
if(matrix[myRow][myCol] == 1)
board[myRow][myCol].setBackground(Color.red);
label.setText("Hit! " + "" + torpedoes + " torpedoes remaining.");
hits++;
else
board[myRow][myCol].setBackground(Color.white);
label.setText("Miss! " + "" + torpedoes + " torpedoes remaining.");
if(hits == 4)
label.setText("You sunk my battleship!");
reset.setEnabled(true);
for(int x = 0; x < 10; x++)
for(int z = 0; z < 10; z++)
board[x][z].setEnabled(false);
if(torpedoes == 0)
label.setText("Miss! 0 torpedoes remaining.");
reset.setEnabled(true);
for(int q = 0; q < 10; q++)
for(int w = 0; w < 10; w++)
board[q][w].setEnabled(false);
if(matrix[q][w] == 1)
board[q][w].setBackground(Color.black);
private class Handler2 implements ActionListener
public void actionPerformed(ActionEvent e)
torpedoes = 20;
hits = 0;
placeShip();
for(int r = 0; r < 10; r++)
for(int c = 0; c < 10; c++)
board[r][c].setBackground(Color.blue);
board[r][c].setEnabled(true);
label.setText("You have 20 torpedoes remaining.");
What I'm trying to do is randomly choose whether the battleship is vertically or horizontally placed using an integer matrix, and then every time the player hits the battleship, the corresponding button on the board turns red. If the player misses it turns white. At the end, if the player wins, s/he can reset the board. If s/he loses, the location of the battleship is shown in black. Everything works how it should, but at the end, when the player loses (no torpedoes left), the battleship appears in two places. Can anyone explain how to fix this? I am a beginner in java so please keep that in mind!
[ad_2]
لینک منبع