Advertisement
somniumIncendia

Battleship

Feb 12th, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.47 KB | None | 0 0
  1. package battleship;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import java.awt.image.BufferedImage;
  9. import javax.swing.GroupLayout;
  10. import javax.swing.LayoutStyle;
  11.  
  12. public class Battleship extends JFrame{
  13.    
  14.     public Battleship()
  15.     {
  16.         super("Battleship");
  17.         setLayout (new FlowLayout());
  18.         setTitle("Battleship");
  19.         setSize(1440, 870);
  20.         setVisible(true);
  21.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  22.     }
  23.    
  24.     static Scanner console = new Scanner (System.in);
  25.    
  26.     //Generation of the battlespace
  27.     static JButton [][] OneHome = new JButton [10][10];
  28.     static JButton [][] OneField = new JButton [10][10];
  29.     static JButton [][] TwoHome = new JButton [10][10];
  30.     static JButton [][] TwoField = new JButton [10][10];
  31.    
  32.     public static Battleship board = new Battleship();
  33.     public static Battleship board2 = new Battleship();
  34.    
  35.     static JButton NextTurn = new JButton("Next Turn");
  36.     static JButton NextTurn2 = new JButton("Next Turn");
  37.    
  38.     static JButton PlayerOne = new JButton("Player One");
  39.     static JButton PlayerTwo = new JButton("Player Two");
  40.    
  41.     static int turn = 0;
  42.    
  43.     public static void NextTurnActionPerformed(ActionEvent e)
  44.     {
  45.         board.setVisible(false);
  46.         board2.setVisible(true);
  47.        
  48.         for (int x = 0; x < 10; x++)
  49.             {
  50.                 for (int y = 0; y < 10; y++)
  51.                 {
  52.                     //TwoHome
  53.                     TwoHome [x][y].setSize(50, 50);
  54.                     TwoHome [x][y].setLocation(((x + 1) * 50) + 50, ((y + 1) * 50) + 50);
  55.                     TwoHome [x][y].setVisible(true);
  56.                
  57.                     //TwoField
  58.                     TwoField [x][y].setSize(50, 50);
  59.                     TwoField [x][y].setLocation(((x + 1) * 50) + 750, ((y + 1) * 50) + 50);
  60.                     TwoField [x][y].setVisible(true);
  61.                 }
  62.             }
  63.            
  64.             NextTurn2.setSize(100, 50);
  65.             NextTurn2.setLocation(1200, 750);
  66.            
  67.             PlayerTwo.setSize(100, 50);
  68.             PlayerTwo.setLocation(650, 50);
  69.     }
  70.    
  71.     public static void NextTurn2ActionPerformed(ActionEvent e)
  72.     {
  73.         board.setVisible(true);
  74.         board2.setVisible(false);
  75.        
  76.         for (int x = 0; x < 10; x++)
  77.             {
  78.                 for (int y = 0; y < 10; y++)
  79.                 {
  80.                     //OneHome
  81.                     OneHome [x][y].setSize(50, 50);
  82.                     OneHome [x][y].setLocation(((x + 1) * 50) + 50, ((y + 1) * 50) + 50);
  83.                     OneHome [x][y].setVisible(true);
  84.                
  85.                     //OneField
  86.                     OneField [x][y].setSize(50, 50);
  87.                     OneField [x][y].setLocation(((x + 1) * 50) + 750, ((y + 1) * 50) + 50);
  88.                     OneField [x][y].setVisible(true);
  89.                 }
  90.             }
  91.            
  92.             NextTurn.setSize(100, 50);
  93.             NextTurn.setLocation(1200, 750);
  94.            
  95.             PlayerOne.setSize(100, 50);
  96.             PlayerOne.setLocation(650, 50);
  97.            
  98.             turn++;
  99.     }
  100.    
  101.     public static void OneHomeActionPerformed(ActionEvent e)
  102.     {
  103.         if (turn == 0)
  104.         {
  105.             OneHome [0][0].setText("F");
  106.         }
  107.        
  108.         else
  109.         {
  110.            
  111.         }
  112.     }
  113.    
  114.     public static void OneFieldActionPerformed(ActionEvent e)
  115.     {
  116.         if ()
  117.         {
  118.            
  119.         }
  120.        
  121.         else
  122.         {
  123.            
  124.         }
  125.     }
  126.    
  127.     public static void TwoHomeActionPerformed(ActionEvent e)
  128.     {
  129.         if (turn == 0)
  130.         {
  131.            
  132.         }
  133.        
  134.         else
  135.         {
  136.            
  137.         }
  138.     }
  139.    
  140.     public static void TwoFieldActionPerformed(ActionEvent e)
  141.     {
  142.         if ()
  143.         {
  144.            
  145.         }
  146.        
  147.         else
  148.         {
  149.            
  150.         }
  151.     }
  152.    
  153.     public static void main(String[] args) {
  154.        
  155.         board2.setVisible(false);
  156.        
  157.         try
  158.         {
  159.             Robot ai = new Robot ();
  160.        
  161.             ai.delay(100);
  162.        
  163.             //Create buttons, place in window
  164.             for (int x = 0; x < 10; x++)
  165.             {
  166.                 for (int y = 0; y < 10; y++)
  167.                 {
  168.                     //OneHome
  169.                     OneHome [x][y] = new JButton();
  170.                     OneHome [x][y].setSize(50, 50);
  171.                     OneHome [x][y].setLocation(((x + 1) * 50) + 50, ((y + 1) * 50) + 50);
  172.                     OneHome [x][y].setVisible(true);
  173.                     OneHome [x][y].setText("");
  174.                     board.add(OneHome [x][y]);
  175.                    
  176.                     //OneHome ActionListener
  177.                     OneHome [x][y].addActionListener(new ActionListener() {
  178.                     @Override
  179.                     public void actionPerformed(ActionEvent e) {
  180.                             OneHomeActionPerformed(e);
  181.                         }
  182.                     });
  183.                
  184.                     //OneField
  185.                     OneField [x][y] = new JButton();
  186.                     OneField [x][y].setSize(50, 50);
  187.                     OneField [x][y].setLocation(((x + 1) * 50) + 750, ((y + 1) * 50) + 50);
  188.                     OneField [x][y].setVisible(true);
  189.                     OneField [x][y].setText("");
  190.                     board.add(OneField [x][y]);
  191.                    
  192.                     //OneField ActionListener
  193.                     OneField [x][y].addActionListener(new ActionListener() {
  194.                     @Override
  195.                     public void actionPerformed(ActionEvent e) {
  196.                             OneFieldActionPerformed(e);
  197.                         }
  198.                     });
  199.                
  200.                     //TwoHome
  201.                     TwoHome [x][y] = new JButton();
  202.                     TwoHome [x][y].setSize(50, 50);
  203.                     TwoHome [x][y].setLocation(((x + 1) * 50) + 50, ((y + 1) * 50) + 50);
  204.                     TwoHome [x][y].setVisible(true);
  205.                     TwoHome [x][y].setText("");
  206.                     board2.add(TwoHome [x][y]);
  207.                    
  208.                     //TwoHome ActionListener
  209.                     TwoHome [x][y].addActionListener(new ActionListener() {
  210.                     @Override
  211.                     public void actionPerformed(ActionEvent e) {
  212.                             TwoHomeActionPerformed(e);
  213.                         }
  214.                     });
  215.                
  216.                     //TwoField
  217.                     TwoField [x][y] = new JButton();
  218.                     TwoField [x][y].setSize(50, 50);
  219.                     TwoField [x][y].setLocation(((x + 1) * 50) + 750, ((y + 1) * 50) + 50);
  220.                     TwoField [x][y].setVisible(true);
  221.                     TwoField [x][y].setText("");
  222.                     board2.add(TwoField [x][y]);
  223.                    
  224.                     //TwoField ActionListener
  225.                     TwoField [x][y].addActionListener(new ActionListener() {
  226.                     @Override
  227.                     public void actionPerformed(ActionEvent e) {
  228.                             TwoFieldActionPerformed(e);
  229.                         }
  230.                     });
  231.                 }
  232.             }
  233.        
  234.             //Reveal
  235.             for (int x = 0; x < 10; x++)
  236.             {
  237.                 for (int y = 0; y < 10; y++)
  238.                 {
  239.                     ai.mouseMove(((x + 1) * 50) + 100, ((y + 1) * 50) + 100);
  240.                     ai.delay(1);
  241.                 }
  242.             }
  243.            
  244.             for (int x = 0; x < 10; x++)
  245.             {
  246.                 for (int y = 0; y < 10; y++)
  247.                 {
  248.                     ai.mouseMove(((x + 1) * 50) + 800, ((y + 1) * 50) + 100);
  249.                     ai.delay(1);
  250.                 }
  251.             }
  252.            
  253.             //Make NextTurn Button
  254.             NextTurn.setSize(100, 50);
  255.             NextTurn.setLocation(1200, 750);
  256.             board.add(NextTurn);
  257.             ai.delay(1);
  258.             ai.mouseMove(1250, 800);
  259.            
  260.             PlayerOne.setSize(100, 50);
  261.             PlayerOne.setLocation(650, 50);
  262.             board.add(PlayerOne);
  263.             ai.delay(1);
  264.             ai.mouseMove(700, 100);
  265.            
  266.             //NextTurn
  267.             NextTurn.addActionListener(new ActionListener() {
  268.                     @Override
  269.                     public void actionPerformed(ActionEvent e) {
  270.                             NextTurnActionPerformed(e);
  271.                     }
  272.             });
  273.            
  274.             //Make NextTurn2 Button
  275.             NextTurn2.setSize(100, 50);
  276.             NextTurn2.setLocation(1200, 750);
  277.             board2.add(NextTurn2);
  278.             ai.delay(1);
  279.             ai.mouseMove(1250, 800);
  280.            
  281.             PlayerTwo.setSize(100, 50);
  282.             PlayerTwo.setLocation(650, 50);
  283.             board2.add(PlayerTwo);
  284.             ai.delay(1);
  285.             ai.mouseMove(700, 100);
  286.            
  287.             //NextTurn2
  288.             NextTurn2.addActionListener(new ActionListener() {
  289.                     @Override
  290.                     public void actionPerformed(ActionEvent e) {
  291.                             NextTurn2ActionPerformed(e);
  292.                     }
  293.             });
  294.            
  295.            
  296.         }
  297.        
  298.         catch (AWTException e)
  299.         {
  300.            
  301.         }
  302.     }
  303. }
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement