import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class TwoDBattleshipGame implements ActionListener
{
private JSplitPane compPane, humPane;
final private JPanel topCompPanel;
final private JPanel bottomCompPanel;
final private JPanel topHumPanel;
final private JPanel bottomHumPanel;
private HumanPlayer p1, p2;
private ComputerPlayer cp;
private JButton[][] humanGrid;
private JButton[][] humanGrid2;
private JButton[][] computerGrid;
private BattleshipHumanPlayerUI bhpui;
private int i, j;
private JFrame contentHuman;
private JDialog jf;
private JLabel shotsLabel;
private JTextField shots;
private JButton quit, reveal;
private JMenuBar p1MBar, p2MBar;
private JMenu p1File, p2File;
private JMenu p1Help, p2Help, p1Save2, p2Save2;
private JMenuItem p1Save, p1Load, p2Save, p1SaveAs, p2SaveAs, p2Load, p1Exit, p2Exit, p1HighScore, p2HighScore;
private JMenuItem p1Help2, p2Help2;
private JScrollPane p1ScrollPane;
private QuitListener ql;
private BattleshipPlayerUI bpui;
private BattleshipPlayerUI bpui2;
protected class BattleshipPlayerUI extends JFrame
{
protected JPanel topPanel;
protected JPanel bottomPanel;
protected JButton[][] grid;
protected JScrollPane scrollPane;
protected JLabel shotsLabel;
protected JTextField shots;
protected int size;
protected String name;
protected SecondListenerClass slc;
protected int shots2;
protected JSplitPane contentPane;
public BattleshipPlayerUI()
{
}
public BattleshipPlayerUI(String name, int size)
{
setName(name);
setTitle(getName());
setShots(0);
slc = new SecondListenerClass();
setGridSize(size);
grid = new JButton[size][size];
topPanel = new JPanel();
scrollPane = new JScrollPane(topPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//topPanel.setLayout(new GridLayout(size,size));
for (int i =0; i < grid.length; i++)
{
for (int j =0; j < grid[i].length; j++)
{
Integer iValue = i;
Integer jValue = j;
grid[i][j] = new JButton("?");
grid[i][j].putClientProperty("X value", (Integer) i);
grid[i][j].putClientProperty("Y value", (Integer) j);
grid[i][j].setToolTipText("(" + iValue.toString() + "," + jValue.toString() + ")");
grid[i][j].setBackground(Color.BLUE);
topPanel.add(grid[i][j]);
grid[i][j].addActionListener(slc);
}
}
shotsLabel = new JLabel("Shots:");
bottomPanel = new JPanel();
//topPanel.setLayout(new GridLayout(size,size));
contentPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPane, bottomPanel);
shots = new JTextField(11);
shots.setEditable(false);
shots.setText("0");
bottomPanel.add(shotsLabel);
bottomPanel.add(shots);
topPanel.setLayout(new GridLayout(size,size));
setContentPane(contentPane);
}
public JTextField getShotsField()
{
return shots;
}
public JSplitPane getSplitPane()
{
return contentPane;
}
public JPanel getTopPanel()
{
return topPanel;
}
public JPanel getBottomPanel()
{
return bottomPanel;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setGridSize(int size)
{
this.size = size;
}
public int getGridSize()
{
return size;
}
public JButton[][] getGrid()
{
return grid;
}
public void setShots(int shots2)
{
this.shots2 = shots2;
}
public int getShots()
{
return shots2;
}
}
private class BattleshipComputerPlayerUI extends JSplitPane
{
private JPanel topCompPanel;
private JPanel bottomCompPanel;
private JScrollPane scrollPane;
private JButton[][] computerGrid;
private JLabel shotsLabel;
public BattleshipComputerPlayerUI(String name, int size)
{
computerGrid = new JButton[size][size];
}
public JLabel getShotsLabel()
{
return shotsLabel;
}
}
private class BattleshipHumanPlayerUI extends BattleshipPlayerUI
{
private Integer playerNumber;
private JPanel topHumPanel;
private JButton reveal;
private JButton quit;
private QuitListener ql;
public BattleshipHumanPlayerUI(Integer playerNumber)
{
this.playerNumber = playerNumber;
topHumPanel = new JPanel();
reveal = new JButton("Reveal Battleship");
quit = new JButton("Quit");
ql = new QuitListener();
quit.addActionListener(ql);
if (playerNumber == 1)
getSplitPane().putClientProperty("Player 1", playerNumber);
else if (playerNumber == 2)
getSplitPane().putClientProperty("Player 2", playerNumber);
else
getSplitPane().putClientProperty("Invalid", playerNumber);
setTopPanel(topHumPanel);
}
public void setTopPanel(JPanel topHumPanel)
{
this.topHumPanel = topHumPanel;
}
public JPanel getTopPanel()
{
return topHumPanel;
}
}
public class SecondListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e)
{
JButton sjb = (JButton) e.getSource();
char value = p1.fire((Integer)(sjb.getClientProperty("X value")),(Integer)(sjb.getClientProperty("Y value")));
if (value == 'H')
{
System.out.println("X Value: " + sjb.getClientProperty("X value"));
System.out.println("Y Value: " + sjb.getClientProperty("Y value"));
sjb.setText("H");
JOptionPane.showMessageDialog(null, "You sunk my battleship!");
}
else if (value == 'M')
{
JOptionPane.showMessageDialog(null,"Miss");
humanGrid[(Integer)sjb.getClientProperty("X value")][(Integer)sjb.getClientProperty("Y value")].setText("M");
}
else if (value == 'A' && humanGrid[(Integer)sjb.getClientProperty("X value")][(Integer)sjb.getClientProperty("Y value")].getText().equals("M"))
{
System.out.println("Is it going here?");
JOptionPane.showMessageDialog(null, "Already shot there.");
humanGrid[(Integer)sjb.getClientProperty("X value")][(Integer)sjb.getClientProperty("Y value")].setText("A");
}
else
{
System.out.println("Or is it going here always?");
JOptionPane.showMessageDialog(null, "Already shot there.");
}
}
}
public class FirstListenerClass implements ActionListener {
private SecondListenerClass slc;
public FirstListenerClass()
{
slc = new SecondListenerClass();
}
public void actionPerformed(ActionEvent e)
{
jf.setVisible(false);
int gs = 0;
boolean isValid = false;
while (isValid == false)
{
try
{
gs = Integer.parseInt(JOptionPane.showInputDialog ("Enter grid size.", "Enter grid size."));
if (gs == 0 || gs > 30 || gs < -30)
throw new SizeOutOfRangeException();
isValid = true;
}
catch(NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null,"Enter an integer.", "Error!", JOptionPane.ERROR_MESSAGE);
}
catch(SizeOutOfRangeException soore)
{
JOptionPane.showMessageDialog(null, soore.toString(), "Error!", JOptionPane.ERROR_MESSAGE);
}
}
try
{
humanGrid = new JButton[gs][gs];
}
catch(NegativeArraySizeException nase)
{
gs = -gs;
humanGrid = new JButton[gs][gs];
}
System.out.println(humanGrid);
topHumPanel.setLayout(new GridLayout(gs,gs));
String name = JOptionPane.showInputDialog("Enter name of player.", "Enter name of player.");
p1 = new HumanPlayer(name, gs, 1);
bpui = new BattleshipPlayerUI(p1.getName(), p1.getPlayerNumber());
bpui.setVisible(true);
String cpName = JOptionPane.showInputDialog("Enter name of computer player.", "Enter name of computer player.");
cp = new ComputerPlayer(cpName, gs);
int xLoc = Integer.parseInt(JOptionPane.showInputDialog("Enter x location of battleship."));
int yLoc = Integer.parseInt(JOptionPane.showInputDialog("Enter y location of battleship."));
p1.placeBattleship(xLoc,yLoc);
cp.setOpposingX(xLoc);
cp.setOpposingY(yLoc);
cp.placeBattleship();
p1.setOpposingX(cp.getX());
p1.setOpposingY(cp.getY());
System.out.println("Placed battleship.");
for ( int i =0; i < humanGrid.length; i++)
{
for ( int j = 0; j < humanGrid[i].length; j++)
{
Integer iValue = i;
Integer jValue = j;
humanGrid[i][j] = new JButton("?");
humanGrid[i][j].putClientProperty("X value", (Integer) i);
humanGrid[i][j].putClientProperty("Y value", (Integer) j);
humanGrid[i][j].setToolTipText("(" + iValue.toString() + "," + jValue.toString() + ")");
humanGrid[i][j].setBackground(Color.BLUE);
topHumPanel.add(humanGrid[i][j]);
humanGrid[i][j].addActionListener(slc);
}
}
}
}
protected class Player
{
private int x;
private int y;
private int opposingX;
private int opposingY;
private String name;
private Integer shots;
protected char[][] grid;
protected int size;
public Player(String name,int size)
{
setName(name);
setGridSize(size);
shots = 0;
grid = new char[getGridSize()][getGridSize()];
for (int i = 0; i < grid.length; i++)
{
for (int j = 0; j < grid[i].length; j++)
grid[i][j] = '?';
}
}
public Player()
{
setName("John Doe");
shots = 0;
size = 10;
setGridSize(10);
grid = new char[getGridSize()][getGridSize()];
for (int i = 0; i < grid.length; i++)
{
for (int j = 0; j < grid[i].length; j++)
grid[i][j] = '?';
}
}
public void setShots(Integer shots)
{
this.shots = shots;
}
public Integer getShots()
{
return shots;
}
public void setX(int x)
{
this.x = x;
}
public int getX()
{
return x;
}
public void setY(int y)
{
this.y = y;
}
public int getY()
{
return y;
}
public void setOpposingX(int opposingX)
{
this.opposingX = opposingX;
}
protected void setGridSize(int size)
{
this.size = size;
}
protected int getGridSize()
{
return size;
}
public int getOpposingX()
{
return opposingX;
}
public void setOpposingY(int opposingY)
{
this.opposingY = opposingY;
}
public int getOpposingY()
{
return opposingY;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public char fire(int x, int y)
{
if (x == getOpposingX() && y == getOpposingY())
{
System.out.println("Oops I did it again!");
setShots(getShots() + 1);
return 'H';
}
else
{
if (((x != getOpposingX()&& y != getOpposingY()) || (x == getOpposingX() && y != getOpposingY()) || (x != getOpposingX() && y == getOpposingY())) && grid[x][y] == '?')
{
System.out.println("Miss!");
grid[x][y] = 'M';
setShots(getShots() + 1);
return 'M';
}
else if ( ((x != getOpposingX()&& y != getOpposingY()) || (x == getOpposingX() && y != getOpposingY()) || (x != getOpposingX() && y == getOpposingY())) && grid[x][y] == 'M')
{
System.out.println("First already shot there.");
grid[x][y] = 'A';
setShots(getShots() + 1);
return 'A';
}
else
{
System.out.println("It's going here.");
setShots(getShots() + 1);
return 'A';
}
}
}
protected char[][] getGrid()
{
return grid;
}
}
private class HumanPlayer extends Player
{
private int playerNumber;
//private int size;
public HumanPlayer(String name, int size, int playerNumber)
{
super(name,size);
setPlayerNumber(playerNumber);
}
public HumanPlayer()
{
super();
setPlayerNumber(1);
}
public void setPlayerNumber(int playerNumber)
{
this.playerNumber = playerNumber;
}
public int getPlayerNumber()
{
return playerNumber;
}
public void placeBattleship(int x, int y)
{
setX(x);
setY(y);
}
}
private class ComputerPlayer extends Player
{
//private int size;
public ComputerPlayer(String name, int size)
{
super(name,size);
}
public ComputerPlayer()
{
super();
}
public void placeBattleship()
{
int x = (int) (Math.random() * grid.length);
int y = (int) (Math.random() * grid.length);
setX(x);
setY(y);
}
}
public TwoDBattleshipGame()
{
ql = new QuitListener();
contentHuman = new JFrame("Human Player");
p1MBar = new JMenuBar();
FirstListenerClass flc = new FirstListenerClass();
jf = new JDialog();
jf.setTitle("Select an option.");
jf.setVisible(true);
ButtonGroup bg = new ButtonGroup();
JRadioButton option1 = new JRadioButton("Human vs. Computer");
JRadioButton option2 = new JRadioButton("Human vs. Human");
bg.add(option1);
bg.add(option2);
jf.add(option1);
jf.add(option2);
jf.getRootPane().setWindowDecorationStyle(JRootPane.INFORMATION_DIALOG);
JDialog.setDefaultLookAndFeelDecorated(true);
jf.add(option1);
jf.add(option2);
jf.setLayout(new GridLayout(2,1));
jf.setMinimumSize(new Dimension(200,200));
jf.setResizable(false);
jf.setBackground(Color.GREEN);
topHumPanel = new JPanel();
p1ScrollPane = new JScrollPane(topHumPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
topCompPanel = new JPanel();
bottomCompPanel = new JPanel();
bottomHumPanel = new JPanel();
shotsLabel = new JLabel("Shots:");
shots = new JTextField(11);
shots.setText("0");
humPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, p1ScrollPane, bottomHumPanel);
humPane.setVisible(true);
contentHuman.setContentPane(humPane);
contentHuman.setVisible(true);
contentHuman.setMinimumSize(new Dimension(300,300));
option1.addActionListener(flc);
reveal = new JButton("Reveal Battleship.");
quit = new JButton("Quit");
reveal.addActionListener(this);
quit.addActionListener(ql);
bottomHumPanel.add(shotsLabel);
shots.setEditable(false);
bottomHumPanel.add(shots);
bottomHumPanel.add(reveal);
bottomHumPanel.add(quit);
bottomHumPanel.setLayout(new GridLayout(2,2));
contentHuman.setJMenuBar(p1MBar);
p1File = new JMenu("File");
p1MBar.add(p1File);
p1Save2 = new JMenu("Save");
p1File.add(p1Save2);
p1Save = new JMenuItem("Save");
p1SaveAs = new JMenuItem("Save As");
p1Save2.add(p1SaveAs);
p1Save2.addSeparator();
p1Save2.add(p1Save);
p1File.addSeparator();
p1Load = new JMenuItem("Open");
p1File.add(p1Load);
p1File.addSeparator();
p1Exit = new JMenuItem("Exit");
p1File.add(p1Exit);
p1Exit.addActionListener(ql);
p1File.addSeparator();
option2.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}});
}
public int Human1ShotsToInt()
{
return (Integer.parseInt(shots.getText()));
}
public String Human1ShotsToString()
{
Integer h1Shots = p1.getShots();
return h1Shots.toString();
}
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "Welcome to Battleship Game.", "Welcome", JOptionPane.INFORMATION_MESSAGE);
new TwoDBattleshipGame();
}
public class QuitListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object options[] = new Object[2];
options[0] = "Yes";
options[1] = "No. Clicked this button by mistake.";
int m = JOptionPane.showOptionDialog(null, "Are you sure you want to quit?", "Quit?",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options , options[0]);
if (m == JOptionPane.YES_OPTION)
System.exit(0);
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Quit"))
{
Object options[] = new Object[2];
options[0] = "Yes";
options[1] = "No. Clicked thsi button by mistake.";
int m = JOptionPane.showOptionDialog(null, "Are you sure you wnat to quit?", "Quit?",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options , options[0]);
if (m == JOptionPane.YES_OPTION)
System.exit(0);
}
}
}