package battleship; import java.io.*; import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import javax.swing.GroupLayout; import javax.swing.LayoutStyle; public class Battleship extends JFrame{ public Battleship() { super("Battleship"); setLayout (new FlowLayout()); setTitle("Battleship"); setSize(1440, 870); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } static Scanner console = new Scanner (System.in); //Generation of the battlespace static JButton [][] OneHome = new JButton [10][10]; static JButton [][] OneField = new JButton [10][10]; static JButton [][] TwoHome = new JButton [10][10]; static JButton [][] TwoField = new JButton [10][10]; public static Battleship board = new Battleship(); public static Battleship board2 = new Battleship(); static JButton NextTurn = new JButton("Next Turn"); static JButton NextTurn2 = new JButton("Next Turn"); static JButton PlayerOne = new JButton("Player One"); static JButton PlayerTwo = new JButton("Player Two"); static int turn = 0; public static void NextTurnActionPerformed(ActionEvent e) { board.setVisible(false); board2.setVisible(true); for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { //TwoHome TwoHome [x][y].setSize(50, 50); TwoHome [x][y].setLocation(((x + 1) * 50) + 50, ((y + 1) * 50) + 50); TwoHome [x][y].setVisible(true); //TwoField TwoField [x][y].setSize(50, 50); TwoField [x][y].setLocation(((x + 1) * 50) + 750, ((y + 1) * 50) + 50); TwoField [x][y].setVisible(true); } } NextTurn2.setSize(100, 50); NextTurn2.setLocation(1200, 750); PlayerTwo.setSize(100, 50); PlayerTwo.setLocation(650, 50); } public static void NextTurn2ActionPerformed(ActionEvent e) { board.setVisible(true); board2.setVisible(false); for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { //OneHome OneHome [x][y].setSize(50, 50); OneHome [x][y].setLocation(((x + 1) * 50) + 50, ((y + 1) * 50) + 50); OneHome [x][y].setVisible(true); //OneField OneField [x][y].setSize(50, 50); OneField [x][y].setLocation(((x + 1) * 50) + 750, ((y + 1) * 50) + 50); OneField [x][y].setVisible(true); } } NextTurn.setSize(100, 50); NextTurn.setLocation(1200, 750); PlayerOne.setSize(100, 50); PlayerOne.setLocation(650, 50); turn++; } public static void OneHomeActionPerformed(ActionEvent e) { if (turn == 0) { OneHome [0][0].setText("F"); } else { } } public static void OneFieldActionPerformed(ActionEvent e) { if () { } else { } } public static void TwoHomeActionPerformed(ActionEvent e) { if (turn == 0) { } else { } } public static void TwoFieldActionPerformed(ActionEvent e) { if () { } else { } } public static void main(String[] args) { board2.setVisible(false); try { Robot ai = new Robot (); ai.delay(100); //Create buttons, place in window for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { //OneHome OneHome [x][y] = new JButton(); OneHome [x][y].setSize(50, 50); OneHome [x][y].setLocation(((x + 1) * 50) + 50, ((y + 1) * 50) + 50); OneHome [x][y].setVisible(true); OneHome [x][y].setText(""); board.add(OneHome [x][y]); //OneHome ActionListener OneHome [x][y].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { OneHomeActionPerformed(e); } }); //OneField OneField [x][y] = new JButton(); OneField [x][y].setSize(50, 50); OneField [x][y].setLocation(((x + 1) * 50) + 750, ((y + 1) * 50) + 50); OneField [x][y].setVisible(true); OneField [x][y].setText(""); board.add(OneField [x][y]); //OneField ActionListener OneField [x][y].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { OneFieldActionPerformed(e); } }); //TwoHome TwoHome [x][y] = new JButton(); TwoHome [x][y].setSize(50, 50); TwoHome [x][y].setLocation(((x + 1) * 50) + 50, ((y + 1) * 50) + 50); TwoHome [x][y].setVisible(true); TwoHome [x][y].setText(""); board2.add(TwoHome [x][y]); //TwoHome ActionListener TwoHome [x][y].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { TwoHomeActionPerformed(e); } }); //TwoField TwoField [x][y] = new JButton(); TwoField [x][y].setSize(50, 50); TwoField [x][y].setLocation(((x + 1) * 50) + 750, ((y + 1) * 50) + 50); TwoField [x][y].setVisible(true); TwoField [x][y].setText(""); board2.add(TwoField [x][y]); //TwoField ActionListener TwoField [x][y].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { TwoFieldActionPerformed(e); } }); } } //Reveal for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { ai.mouseMove(((x + 1) * 50) + 100, ((y + 1) * 50) + 100); ai.delay(1); } } for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { ai.mouseMove(((x + 1) * 50) + 800, ((y + 1) * 50) + 100); ai.delay(1); } } //Make NextTurn Button NextTurn.setSize(100, 50); NextTurn.setLocation(1200, 750); board.add(NextTurn); ai.delay(1); ai.mouseMove(1250, 800); PlayerOne.setSize(100, 50); PlayerOne.setLocation(650, 50); board.add(PlayerOne); ai.delay(1); ai.mouseMove(700, 100); //NextTurn NextTurn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { NextTurnActionPerformed(e); } }); //Make NextTurn2 Button NextTurn2.setSize(100, 50); NextTurn2.setLocation(1200, 750); board2.add(NextTurn2); ai.delay(1); ai.mouseMove(1250, 800); PlayerTwo.setSize(100, 50); PlayerTwo.setLocation(650, 50); board2.add(PlayerTwo); ai.delay(1); ai.mouseMove(700, 100); //NextTurn2 NextTurn2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { NextTurn2ActionPerformed(e); } }); } catch (AWTException e) { } } } }