Advertisement
icorrelate

BattleShip Game

Dec 31st, 2016
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.83 KB | None | 0 0
  1.  
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JPanel;
  13.  
  14. import java.util.Random;
  15.  
  16. public class Game extends JFrame implements ActionListener{
  17.     JLabel lblplayer = new JLabel("Player Score: ");
  18.     JLabel lblai = new JLabel("Artificial Intelligence: ");
  19.     JLabel lblplayers = new JLabel("0");
  20.     JLabel lblais = new JLabel("0");
  21.    
  22.     int style = Font.BOLD | Font.ITALIC;
  23.  
  24.     Font font = new Font ("Garamond", style , 30);
  25.     JButton[][]boxes = new JButton[12][12];
  26.     static Random rand = new Random();
  27.     JPanel board = new JPanel();
  28.     JPanel Scorecard = new JPanel();
  29.     static int aiboard[][] = new int[12][12];
  30.     int userBoard[][] = new int[12][12];
  31.     int playerScore = 0;
  32.     int aiScore = 0;
  33.     int CarrierCount = 0;
  34.     int CruiserCount = 0;
  35.     int DestroyerCount =0;
  36.     int SubmarineCount = 0;
  37.     boolean isplaying = false;
  38.     boolean isplacingCarrier = false;
  39.     boolean isplacingCruiser = false;
  40.     boolean isplacingDestroyer = false;
  41.     boolean isplacingSubmarine = true;
  42.     int aiturns[][] = new int[12][12];
  43.     int plturns[][] = new int[12][12];
  44.    
  45.     final int subID = 50; //just to tel that its a submarine
  46.     final int desID = 51; //just to tel that its a destroyer
  47.     final int cruiserID = 52; //just to tel that its a cruiser
  48.     final int carriersubID = 53; //just to tel that its a carrier
  49.            
  50.     public Game(){
  51.         super("Battleship Game");
  52.         setSize(1200,700);
  53.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54.         setResizable(false);
  55.         setLocationRelativeTo(null);
  56.        
  57.         lblplayer.setFont(font);
  58.         lblplayers.setFont(font);
  59.         lblai.setFont(font);
  60.         lblais.setFont(font);
  61.         board.setLayout(new GridLayout(12,12));
  62.         for(int row = 0; row<12 ; row++){
  63.             for (int col = 0; col < 12; col++){
  64.                 boxes[row][col] = new JButton("");
  65.                 board.add(boxes[row][col]);
  66.                 boxes[row][col].setBackground(Color.CYAN);
  67.                 boxes[row][col].addActionListener(this);
  68.             }
  69.         }
  70.         board.setBackground(Color.BLUE);
  71.        
  72.         Scorecard.setLayout(new GridLayout(1,4,5,5));
  73.         Scorecard.add(lblplayer);
  74.         Scorecard.add(lblplayers);
  75.         Scorecard.add(lblai);
  76.         Scorecard.add(lblais);
  77.         add("North", Scorecard);
  78.         add("Center", board);
  79.         setVisible(true);
  80.        
  81.     }
  82.    
  83.     public static void main(String[] args) {
  84.         new Game();
  85.         createAi();
  86.         JOptionPane.showMessageDialog(null, "Welcome to BAttleship Game XD");
  87.         JOptionPane.showMessageDialog(null, "Place your three one-decker Submarines!");
  88.        
  89.        
  90.     }
  91.  
  92.     @Override
  93.     public void actionPerformed(ActionEvent e) {
  94.         if(isplaying == false){
  95.             for(int row = 0; row<12 ; row++){
  96.                 for (int col = 0; col < 12; col++){
  97.                     if(e.getSource() == boxes[row][col]){
  98.                         if(isplacingSubmarine == true && SubmarineCount<3){
  99.                             placeShip(row, col, 1, true, subID);
  100.                             SubmarineCount++;
  101.                         }else{
  102.                             isplacingSubmarine = false;
  103.                             isplacingDestroyer = true;
  104.                             if(isplacingDestroyer == true && DestroyerCount<2){
  105.                                 Object[] options = {"Vertical","Horizontal"};
  106.                                     int n = JOptionPane.showOptionDialog(this,
  107.                                         "Please choose Orientation for Destroyer",
  108.                                         "Battle Ship Game",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options, options[0]);
  109.                                     if(n == 0){
  110.                                         placeShip(row, col, 2, true, desID);
  111.                                     }else{
  112.                                         placeShip(row, col, 2, false, desID);
  113.                                     }
  114.                                     DestroyerCount++;
  115.                             }else{
  116.                                 isplacingDestroyer = false;
  117.                                 isplacingCruiser = true;
  118.                                 if(isplacingCruiser == true && CruiserCount<1){
  119.                                     Object[] options = {"Vertical","Horizontal"};
  120.                                         int n = JOptionPane.showOptionDialog(this,
  121.                                             "Please choose Orientation for Cruiser",
  122.                                             "Battle Ship Game",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options, options[0]);
  123.                                         if(n == 0){
  124.                                             placeShip(row, col, 3, true, cruiserID);
  125.                                         }else{
  126.                                             placeShip(row, col, 3, false, cruiserID);
  127.                                         }
  128.                                         CruiserCount++;
  129.                                 }else{
  130.                                     isplacingCruiser = false;
  131.                                     isplacingCarrier = true;
  132.                                     if(isplacingCarrier == true && CarrierCount<1){
  133.                                         Object[] options = {"Vertical","Horizontal"};
  134.                                             int n = JOptionPane.showOptionDialog(this,
  135.                                                 "Please choose Orientation for Carrier",
  136.                                                 "Battle Ship Game",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options, options[0]);
  137.                                             if(n == 0){
  138.                                                 placeShip(row, col, 5, true, carriersubID);
  139.                                             }else{
  140.                                                 placeShip(row, col, 5, false, carriersubID);
  141.                                             }
  142.                                             CarrierCount++;
  143.                                     }else{
  144.                                         Object[] options = {"Lets Get it On!","Wait a minute!"};
  145.                                         int n = JOptionPane.showOptionDialog(this,
  146.                                             "Do you want to start the game ?",
  147.                                             "Battle Ship Game",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options, options[0]);
  148.                                         if(n == 0){
  149.                                             isplaying = true;
  150.                                         }
  151.                                     }
  152.                                 }
  153.                             }
  154.                         }
  155.                         break;
  156.                     }
  157.                 }
  158.             }
  159.            
  160.            
  161.            
  162.         }else if(isplaying == true){
  163.             for(int row = 0; row<12 ; row++){
  164.                 for (int col = 0; col < 12; col++){
  165.                     if(e.getSource() == boxes[row][col]){
  166.                         if(plturns[row][col] == 0){
  167.                             boxes[row][col].setBackground(Color.ORANGE);
  168.                             boxes[row][col].setText(boxes[row][col].getText() + "(PL)");
  169.                             if(aiboard[row][col] != 0){
  170.                                 playerScore++;
  171.                             }
  172.                             plturns[row][col] = 1;
  173.                             AiMove();
  174.                             lblplayers.setText(""+playerScore);
  175.                             lblais.setText(""+aiScore);
  176.                         }else{
  177.                             JOptionPane.showMessageDialog(null, "Please Choose another Block!");
  178.                         }
  179.                     }
  180.                 }
  181.             }
  182.  
  183.         }
  184.        
  185.         if(playerScore == 15){
  186.             Object[] options = {"Restart the Game!","Exit"};
  187.             int n = JOptionPane.showOptionDialog(this,
  188.                     "You win, Do yah wanna Restart the Game?",
  189.                     "Battle Ship Game",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options, options[0]);
  190.                 if(n == 0){
  191.                     restartGame();
  192.                 }else{
  193.                     JOptionPane.showMessageDialog(null, "Thank You for Playing");
  194.                     System.exit(1);
  195.                 }
  196.         }else if(aiScore == 15){
  197.             Object[] options = {"Restart the Game!","Exit"};
  198.             int n = JOptionPane.showOptionDialog(this,
  199.                     "You Lose, Do yah wanna Restart the Game?",
  200.                     "Battle Ship Game",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options, options[0]);
  201.                 if(n == 0){
  202.                     restartGame();
  203.                 }else{
  204.                     JOptionPane.showMessageDialog(null, "Thank You for Playing");
  205.                     System.exit(1);
  206.                 }
  207.         }
  208.     }
  209.    
  210.     private void restartGame(){
  211.         aiboard = new int[12][12];
  212.         userBoard = new int[12][12];
  213.         plturns = new int[12][12];
  214.         aiturns = new int[12][12];
  215.        
  216.          playerScore = 0;
  217.          aiScore = 0;
  218.          CarrierCount = 0;
  219.          CruiserCount = 0;
  220.          DestroyerCount =0;
  221.          SubmarineCount = 0;
  222.          isplaying = false;
  223.          isplacingCarrier = false;
  224.          isplacingCruiser = false;
  225.          isplacingDestroyer = false;
  226.          isplacingSubmarine = true;
  227.          
  228.          for(int row = 0; row<12 ; row++){
  229.                 for (int col = 0; col < 12; col++){
  230.                     boxes[row][col].setBackground(Color.CYAN);
  231.                     boxes[row][col].setText("");
  232.                 }
  233.          }
  234.     }
  235.     private void AiMove() {
  236.        
  237.         boolean valid = false;
  238.         do{
  239.             int row = rand.nextInt(12);
  240.             int col = rand.nextInt(12);
  241.             if(aiturns[row][col] == 0){
  242.                 boxes[row][col].setBackground(Color.MAGENTA);
  243.                 boxes[row][col].setText(boxes[row][col].getText() + "(AI)");
  244.                 if(userBoard[row][col] != 0){
  245.                     aiScore++;
  246.                 }
  247.                 valid = true;
  248.             }
  249.         }while(valid == false);
  250.     }
  251.  
  252.     private static void createAi(){
  253.         createAiSubmarine();
  254.         createAiShips(11, 12, 2, 2, 51);
  255.         createAiShips(10, 12, 3, 1, 52);
  256.         createAiShips(8, 12, 5, 1, 53);
  257.         for(int r = 0; r<12 ; r++){
  258.             for (int c = 0; c < 12; c++){
  259.                 System.out.print(aiboard[r][c] +"\t" );
  260.             }
  261.             System.out.println();
  262.         }
  263.     }
  264.     private static void createAiSubmarine(){
  265.         int row;
  266.         int col;
  267.         for(int i = 0 ; i <3 ;i++){ // generates AI's submarines XD
  268.             row = +rand.nextInt(12);
  269.             col = +rand.nextInt(12);
  270.            
  271.             if(aiboard[row][col] !=0 ){
  272.                 i--;
  273.             }else{
  274.                 aiboard[row][col] = 50;
  275.             }
  276.         }
  277.     }
  278.     private static void createAiShips(int lrow, int lcol, int noOfDeck,int noOfship, int id){
  279.         boolean isVertical;
  280.         int row;
  281.         int col;
  282.         for(int i = 0 ; i <noOfship ;i++){
  283.             isVertical = rand.nextBoolean();
  284.             boolean valid = true;
  285.             if(isVertical){
  286.                 row = rand.nextInt(lrow);
  287.                 col = rand.nextInt(lcol);
  288.                 for(int i1 = 0; i1 < noOfDeck; i1++){
  289.                     if(aiboard[row + i1][col] !=0){
  290.                         valid = false;
  291.                         break;
  292.                     }
  293.                 }
  294.                 if(valid){
  295.                     for(int i1 = 0; i1 < noOfDeck; i1++){
  296.                         aiboard[row + i1][col] = id;
  297.                     }
  298.                 }else{
  299.                     i--;
  300.                 }
  301.             }else{
  302.                 row = rand.nextInt(lcol);
  303.                 col = rand.nextInt(lrow);
  304.                 for(int i1 = 0; i1 < noOfDeck; i1++){
  305.                     if(aiboard[row][col + i1] !=0){
  306.                         valid = false;
  307.                         break;
  308.                     }
  309.                 }
  310.                 if(valid){
  311.                     for(int i1 = 0; i1 < noOfDeck; i1++){
  312.                         aiboard[row][col + i1] = id;
  313.                     }
  314.                 }else{
  315.                     i--;
  316.                 }
  317.             }
  318.         }
  319.     }
  320.    
  321.    
  322.     private void placeShip(int row, int col, int noOFDecks, boolean isVertical, int boatID){
  323.         if(isVertical){
  324.             if(boatID == subID){
  325.                 if(userBoard[row][col] != 0){
  326.                     JOptionPane.showMessageDialog(null, "Cant Place there!");
  327.                     SubmarineCount--;
  328.                 }else{
  329.                     boxes[row][col].setText("Submarine");
  330.                     boxes[row][col].setBackground(Color.RED);
  331.                     userBoard[row][col] = subID;
  332.                 }
  333.  
  334.             }else if(boatID == carriersubID){
  335.                 boolean valid = true;
  336.                 if(col < 12 && row < 8){
  337.                    
  338.                     for(int i = 0; i < noOFDecks; i++){
  339.                         if(userBoard[row + i][col] !=0){
  340.                             valid = false;
  341.                             break;
  342.                         }
  343.  
  344.                     }
  345.                     if(valid){
  346.                         for(int i = 0; i < noOFDecks; i++){
  347.                             boxes[row + i][col].setText("Carrier");
  348.                             boxes[row + i][col].setBackground(Color.GRAY);
  349.                             userBoard[row + i][col] = carriersubID;
  350.                         }
  351.                     }else{
  352.                         JOptionPane.showMessageDialog(null, "Cant Place there!");
  353.                         CarrierCount--;
  354.                     }
  355.  
  356.                 }else{
  357.                     JOptionPane.showMessageDialog(null, "Cant Place there!");
  358.                     CarrierCount--;
  359.                 }
  360.             }else if(boatID == desID){
  361.                 boolean valid = true;
  362.                 if(col < 12 && row < 11){
  363.                    
  364.                     for(int i = 0; i < noOFDecks; i++){
  365.                         if(userBoard[row + i][col] !=0){
  366.                             valid = false;
  367.                             break;
  368.                         }
  369.                     }
  370.                     if(valid){
  371.                         for(int i = 0; i < noOFDecks; i++){
  372.                             boxes[row + i][col].setText("Destroyer");
  373.                             boxes[row + i][col].setBackground(Color.PINK);
  374.                             userBoard[row + i][col] = desID;
  375.                         }
  376.                     }else{
  377.                         JOptionPane.showMessageDialog(null, "Cant Place there!");
  378.                         DestroyerCount--;
  379.                     }
  380.  
  381.                 }else{
  382.                     JOptionPane.showMessageDialog(null, "Cant Place there!");
  383.                     DestroyerCount--;
  384.                 }
  385.             }else if(boatID == cruiserID){
  386.                 boolean valid = true;
  387.                 if(col < 12 && row < 10){
  388.                    
  389.                     for(int i = 0; i < noOFDecks; i++){
  390.                         if(userBoard[row + i][col] !=0){
  391.                             valid = false;
  392.                             break;
  393.                         }
  394.  
  395.                     }
  396.                     if(valid){
  397.                         for(int i = 0; i < noOFDecks; i++){
  398.                             boxes[row + i][col].setText("Cruiser");
  399.                             boxes[row + i][col].setBackground(Color.GREEN);
  400.                             userBoard[row + i][col] = cruiserID;
  401.                         }
  402.                     }else{
  403.                         JOptionPane.showMessageDialog(null, "Cant Place there!");
  404.                         CruiserCount--;
  405.                     }
  406.  
  407.                 }else{
  408.                     JOptionPane.showMessageDialog(null, "Cant Place there!");
  409.                     CruiserCount--;
  410.                 }
  411.             }
  412.         }else{
  413.             if(boatID == subID){
  414.                 if(userBoard[row][col] != 0){
  415.                     JOptionPane.showMessageDialog(null, "Cant Place there!");
  416.                     SubmarineCount--;
  417.                 }else{
  418.                     boxes[row][col].setText("Submarine");
  419.                     boxes[row][col].setBackground(Color.RED);
  420.                     userBoard[row][col] = subID;
  421.                 }
  422.             }else if(boatID == carriersubID){
  423.                 boolean valid = true;
  424.                 if(col < 8 && row < 12){
  425.                     for(int i = 0; i < noOFDecks; i++){
  426.                         if(userBoard[row][col + i] !=0){
  427.                             valid = false;
  428.                             break;
  429.                         }
  430.  
  431.                     }
  432.                     if(valid){
  433.                         for(int i = 0; i < noOFDecks; i++){
  434.                             boxes[row][col + i].setText("Carrier");
  435.                             boxes[row][col + i].setBackground(Color.GRAY);
  436.                             userBoard[row][col + i] = carriersubID;
  437.                         }
  438.                     }else{
  439.                         JOptionPane.showMessageDialog(null, "Cant Place there!");
  440.                         CarrierCount--;
  441.                     }
  442.  
  443.                 }else{
  444.                     JOptionPane.showMessageDialog(null, "Cant Place there!");
  445.                     CarrierCount--;
  446.                 }
  447.                
  448.                
  449.                
  450.             }else if(boatID == desID){
  451.                 boolean valid = true;
  452.                 if(col < 11 && row < 12){
  453.                     for(int i = 0; i < noOFDecks; i++){
  454.                         if(userBoard[row][col + i] !=0){
  455.                             valid = false;
  456.                             break;
  457.                         }
  458.  
  459.                     }
  460.                     if(valid){
  461.                         for(int i = 0; i < noOFDecks; i++){
  462.                             boxes[row][col + i].setText("Destroyer");
  463.                             boxes[row][col + i].setBackground(Color.PINK);
  464.                             userBoard[row][col + i] = desID;
  465.                         }
  466.                     }else{
  467.                         JOptionPane.showMessageDialog(null, "Cant Place there!");
  468.                         DestroyerCount--;
  469.                     }
  470.  
  471.                 }else{
  472.                     JOptionPane.showMessageDialog(null, "Cant Place there!");
  473.                     DestroyerCount--;
  474.                 }
  475.             }else if(boatID == cruiserID){
  476.                 boolean valid = true;
  477.                 if(col < 10 && row < 12){
  478.                     for(int i = 0; i < noOFDecks; i++){
  479.                         if(userBoard[row][col + i] !=0){
  480.                             valid = false;
  481.                             break;
  482.                         }
  483.  
  484.                     }
  485.                     if(valid){
  486.                         for(int i = 0; i < noOFDecks; i++){
  487.                             boxes[row][col + i].setText("Cruiser");
  488.                             boxes[row][col + i].setBackground(Color.GREEN);
  489.                             userBoard[row][col + i] = cruiserID;
  490.                         }
  491.                     }else{
  492.                         JOptionPane.showMessageDialog(null, "Cant Place there!");
  493.                         CruiserCount--;
  494.                     }
  495.  
  496.                 }else{
  497.                     JOptionPane.showMessageDialog(null, "Cant Place there!");
  498.                     CruiserCount--;
  499.                 }
  500.             }
  501.         }
  502.     }
  503. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement