Advertisement
vanbestglitch

Battleship (Using Java)

Jan 26th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.40 KB | None | 0 0
  1. import java.util.Scanner;
  2. /*
  3. To play compile this in a java compiler (preferably eclipse) and run. You can play with an "AI" or another player. Use the console to play.
  4.  
  5. Sorry for any spaghetti code this was made in my sophomore year of high school, so feel free to modify it. Also feel free to view any other projects I made.
  6. */
  7.  
  8. public class MainGame {
  9.     private int wincount = 0;
  10.     private boolean win = false;
  11.    
  12.     private String[][] playerboard = new String[][] {
  13.         {"_","_","_","_","_","_","_","_",},
  14.         {"_","_","_","_","_","_","_","_",},
  15.         {"_","_","_","_","_","_","_","_",},
  16.         {"_","_","_","_","_","_","_","_",},
  17.         {"_","_","_","_","_","_","_","_",},
  18.         {"_","_","_","_","_","_","_","_",},
  19.         {"_","_","_","_","_","_","_","_",},
  20.         {"_","_","_","_","_","_","_","_",},
  21.     };
  22.    
  23.    
  24.     private String[][] playerboard2 = new String[][] {
  25.         {"_","_","_","_","_","_","_","_",},
  26.         {"_","_","_","_","_","_","_","_",},
  27.         {"_","_","_","_","_","_","_","_",},
  28.         {"_","_","_","_","_","_","_","_",},
  29.         {"_","_","_","_","_","_","_","_",},
  30.         {"_","_","_","_","_","_","_","_",},
  31.         {"_","_","_","_","_","_","_","_",},
  32.         {"_","_","_","_","_","_","_","_",},
  33.     };
  34.    
  35.    
  36.     String[][] enemyboard = new String[][] {
  37.         {"🚢","🚢","🚢","🚢","_","_","_","_",},
  38.         {"_","_","_","_","_","_","🚢","_",},
  39.         {"_","_","_","_","_","_","🚢","_",},
  40.         {"_","_","_","_","_","_","_","_",},
  41.         {"_","_","_","_","🚢","🚢","🚢","_",},
  42.         {"_","_","🚢","🚢","🚢","🚢","🚢","_",},
  43.         {"_","_","_","_","_","_","_","_",},
  44.         {"_","_","_","_","_","_","_","_",},
  45.     };
  46.    
  47.     private int turn = 1;
  48.     private int enemyships;
  49.     private int playerships;
  50.  
  51.     public String showPlayerBoard(boolean canShow) {
  52.     playerships = 0;
  53.     String s = "";
  54.     for(int i = 0; i < 8; i++) {   
  55.         for(int j =0; j < 8; j++) {
  56.         if((playerboard[i][j] == "🚢" && canShow == false)) {
  57.             s += "_ ";
  58.             playerships++; 
  59.         }else {
  60.             s += playerboard[i][j] + " ";
  61.             if(playerboard[i][j] == "🚢") {
  62.             playerships++; 
  63.             }
  64.             }
  65.         }
  66.         s += "\n";
  67.     }
  68.     turn++;
  69.     return s;  
  70.     }
  71.    
  72.     public String showPlayerBoard2(boolean canShow) {
  73.         enemyships = 0;
  74.         String s = "";
  75.         for(int i = 0; i < 8; i++) {   
  76.             for(int j =0; j < 8; j++) {
  77.             if((playerboard2[i][j] == "🚢" && canShow == false)) {
  78.                 s += "_" + " ";
  79.                 enemyships++;
  80.             }else {
  81.                 s += playerboard2[i][j] + " ";
  82.                 if(playerboard2[i][j] == "🚢") {
  83.                 enemyships++;  
  84.                 }
  85.                 }
  86.             }
  87.             s += "\n";
  88.         }
  89.         turn++;
  90.         return s;  
  91.         }
  92.    
  93.    
  94.     public String showEnemyBoard(boolean canShow) {
  95.     String s = "";
  96.     enemyships = 0;
  97.    
  98.     for(int i = 0; i < 8; i++) {   
  99.         for(int j =0; j < 8; j++) {
  100.    
  101.         if(enemyboard[i][j] == "🚢" && canShow == false) {
  102.             s += "_ ";
  103.             enemyships++;
  104.         }else {
  105.             s += enemyboard[i][j]+ " ";
  106.             if(enemyboard[i][j] == "🚢") {
  107.                 enemyships++;  
  108.                 }
  109.         }
  110.         }
  111.         s += "\n";
  112.     }
  113.     return s;  
  114.     }
  115.    
  116.     public String[][] setupPlayerBoard(int X1, int Y1, int X2, int Y2) {
  117.                
  118.        
  119.             for(int a = 0; a < X2 - X1; a++) {
  120.                 for(int l = 0; l < Y2 - Y1; l++) {
  121.                     if(X1 + a < 9 || Y1 + a < 9) { 
  122.                         playerboard[X1 + a][Y1 + l] = "🚢";
  123.                        
  124.                        
  125.                     }
  126.                    
  127.                 }
  128.             }
  129.            
  130.             for(int a = 0; a < Y2 - Y1; a++) {
  131.                 for(int l = 0; X2 - X1 < 1; l++) {
  132.                     if(X1 + a < 9 || Y1 + a < 9) { 
  133.                         playerboard[X1 + a][Y1 + l] = "🚢";
  134.                        
  135.                        
  136.                     }
  137.                 }
  138.                
  139.         }
  140.        
  141.        
  142.         return playerboard;
  143.     }
  144.    
  145.     public String[][] setupPlayer2Board(int X1, int Y1, int X2, int Y2) {
  146.        
  147.             for(int a = 0; a < X2 - X1; a++) {
  148.                 for(int l = 0; l < Y2 - Y1; l++) {
  149.                     if(X1 + a < 9 || Y1 + a < 9) { 
  150.                         playerboard2[X1 + a][Y1 + l] = "🚢";
  151.                        
  152.                        
  153.                     }
  154.                    
  155.                 }
  156.             }
  157.            
  158.             for(int a = 0; a < Y2 - Y1; a++) {
  159.                 for(int l = 0; X2 - X1 < 1; l++) {
  160.                     if(X1 + a < 9 || Y1 + a < 9) { 
  161.                         playerboard2[X1 + a][Y1 + l] = "🚢";
  162.                        
  163.                        
  164.                     }
  165.                 }
  166.             }  
  167.        
  168.        
  169.        
  170.         return playerboard2;
  171.     }
  172.    
  173.    
  174.     public String[][] fireatEnemyBoard(int X, int Y){
  175.         String s[][] = enemyboard;
  176.        
  177.         if(enemyboard[X][Y] == "🚢") {
  178.         s[X][Y] = "💥";
  179.         enemyships--;
  180.         }
  181.         else{
  182.         s[X][Y] = "💦";  
  183.         }
  184.        
  185.         if(enemyships <= 0) {
  186.             System.out.println("You Win!");
  187.             win = true;
  188.             }
  189.         return s;
  190.     }
  191.    
  192.     public String[][] fireatPlayer1Board(int X, int Y){
  193.         String s[][] = playerboard;
  194.        
  195.         if(playerboard[X][Y] == "🚢") {
  196.         s[X][Y] = "💥";
  197.         playerships--;
  198.         }
  199.         else{
  200.         s[X][Y] = "💦";  
  201.         }
  202.        
  203.         if(playerships == 0 && wincount == 0) {
  204.             System.out.println("You Win!");
  205.             win = true;
  206.             wincount++;
  207.             }
  208.         return s;
  209.     }
  210.    
  211.     public String[][] fireatPlayer2Board(int X, int Y){
  212.         String s[][] = playerboard2;
  213.        
  214.         if(playerboard2[X][Y] == "🚢") {
  215.         s[X][Y] = "💥";
  216.         enemyships--;
  217.         }
  218.         else{
  219.         s[X][Y] = "💦";  
  220.         }
  221.        
  222.         if(enemyships == 0 && wincount == 0) {
  223.             System.out.println("You Win!");
  224.             wincount++;
  225.             win = true;
  226.             }
  227.         return s;
  228.     }
  229.    
  230.     public String[][] fireatPlayerBoard(){
  231.         String s[][] = playerboard;
  232.         int X = (int)(Math.random()*8);
  233.         int Y = (int)(Math.random()*8);
  234.        
  235.         if(playerboard[X][Y] == "🚢") {
  236.         s[X][Y] = "💥";
  237.         playerships--;
  238.         }
  239.         else{
  240.         s[X][Y] = "💦";  
  241.         }
  242.        
  243.         if(playerships == 0) {
  244.         System.out.println("You Lose!");   
  245.         win = true;
  246.         }
  247.         return s;
  248.    
  249.     }
  250.    
  251.     public boolean detectWin() {
  252.     boolean b = false;
  253.     if(win == true || playerships <= 0 || enemyships <=0) {
  254.     b = true;  
  255.     }
  256.     return b;
  257.     }
  258.  
  259.     public static void main(String[] args) {
  260.     int X1;
  261.     int X2;
  262.     int Y1;
  263.     int Y2;
  264.     Scanner scan = new Scanner(System.in);
  265.     MainGame gameboard = new MainGame();
  266.     boolean b = true;
  267.     boolean multiplayer = false;
  268.    
  269.     System.out.println("Do You Want To Play Single Player or Multiplayer (multiplayer for multi player, single player for singleplayer)");
  270.     if(scan.next().contains("m")) {
  271.     multiplayer = true;
  272.     }else{
  273.     multiplayer = false;       
  274.     }
  275.    
  276.    
  277.     //Set Up Boards
  278.    
  279.     if(multiplayer == true) {
  280.        
  281.         System.out.println("Time To Set Up the Board (one of second coordinates must be one more than original coordinate)");
  282.    
  283.     for(int i = 2; i < 6; i++) {
  284.     System.out.println("Type First X Coordinate");
  285.     X1 = scan.nextInt();
  286.     System.out.println("Type Second X Coordinate");
  287.     X2 = scan.nextInt();
  288.     System.out.println("Type First Y Coordinate");
  289.     Y1 = scan.nextInt();
  290.     System.out.println("Type Second Y Coordinate");
  291.     Y2 = scan.nextInt();
  292.     //                  x1  y1 x2 y2   
  293.    
  294.     if(!(X2 - X2 == 1) & (Y2 - Y1 == i) || X2 - X1 == 1 & Y2 - Y1 == i) {
  295.     while((X2 - X2 != 1) & (Y2 - Y1 != i) || (X2 - X1 != 1) & (Y2 - Y1 != i)) {
  296.         System.out.println("Invalid Input");
  297.         System.out.println("Type First X Coordinate");
  298.         X1 = scan.nextInt();
  299.         System.out.println("Type Second X Coordinate");
  300.         X2 = scan.nextInt();
  301.         System.out.println("Type First Y Coordinate");
  302.         Y1 = scan.nextInt();
  303.         System.out.println("Type Second Y Coordinate");
  304.         Y2 = scan.nextInt();   
  305.     }
  306.     }
  307.     gameboard.setupPlayerBoard(X1, Y1, X2, Y2);
  308.     System.out.println(gameboard.showPlayerBoard(true));
  309.     }
  310.    
  311.     for(int j = 0; j < 50; j++) {
  312.     System.out.println(" ");   
  313.     }
  314.    
  315.    
  316.     System.out.println("Time To Set Up the Board (it's in order of smallest to largest and one of second coordinates must be one more than original coordinate)");
  317.    
  318.     for(int i = 2; i < 6; i++) {
  319.     System.out.println("Type First X Coordinate");
  320.     X1 = scan.nextInt();
  321.     System.out.println("Type Second X Coordinate");
  322.     X2 = scan.nextInt();
  323.     System.out.println("Type First Y Coordinate");
  324.     Y1 = scan.nextInt();
  325.     System.out.println("Type Second Y Coordinate");
  326.     Y2 = scan.nextInt();
  327.     //                        x1  y1 x2 y2 
  328.     if(!(X2 - X2 == 1) & (Y2 - Y1 == i) || X2 - X1 == 1 & Y2 - Y1 == i) {
  329.         while((X2 - X2 != 1) & (Y2 - Y1 != i) || (X2 - X1 != 1) & (Y2 - Y1 != i)) {
  330.             System.out.println("Invalid Input");
  331.             System.out.println("Type First X Coordinate");
  332.             X1 = scan.nextInt();
  333.             System.out.println("Type Second X Coordinate");
  334.             X2 = scan.nextInt();
  335.             System.out.println("Type First Y Coordinate");
  336.             Y1 = scan.nextInt();
  337.             System.out.println("Type Second Y Coordinate");
  338.             Y2 = scan.nextInt();   
  339.         }
  340.         }
  341.     gameboard.setupPlayer2Board(X1, Y1, X2, Y2);
  342.     System.out.println(gameboard.showPlayerBoard2(true));
  343.     }
  344.    
  345.     for(int j = 0; j < 50; j++) {
  346.         System.out.println(" ");   
  347.         }
  348.    
  349.     while(gameboard.detectWin() != true) {
  350.         while(gameboard.detectWin() != true) {
  351.             if(b == true) {
  352.                 System.out.println("Your Board");
  353.                 System.out.println(gameboard.showPlayerBoard(true));
  354.                 System.out.println("Fire At Other Player!");
  355.                 System.out.println("Type X Coordinate");
  356.                 X1 = scan.nextInt();
  357.                 System.out.println("Type Y Coordinate");
  358.                 Y1 = scan.nextInt();   
  359.                 gameboard.fireatPlayer2Board(X1, Y1);
  360.                 System.out.println("Enemy Board");
  361.                 System.out.println(gameboard.showPlayerBoard2(false));
  362.                
  363.                 System.out.println("Press X to Continue");
  364.                 if(scan.next().contains("X") || scan.next().contains("x")) {
  365.                 for(int j = 0; j < 50; j++) {
  366.                     System.out.println(" ");   
  367.                     }
  368.                 b = false;
  369.                 }
  370.             }else {
  371.                 System.out.println("Your Board");
  372.                 System.out.println(gameboard.showPlayerBoard2(true));
  373.                 System.out.println("Fire At Other Player!");
  374.                 System.out.println("Type X Coordinate");
  375.                 X1 = scan.nextInt();
  376.                 System.out.println("Type Y Coordinate");
  377.                 Y1 = scan.nextInt();   
  378.                 gameboard.fireatPlayer1Board(X1, Y1);
  379.                 System.out.println("Enemy Board");
  380.                 System.out.println(gameboard.showPlayerBoard(false));
  381.                
  382.                 System.out.println("Press X to Continue");
  383.                 if(scan.next().contains("X") || scan.next().contains("x")) {
  384.                 for(int j = 0; j < 50; j++) {
  385.                     System.out.println(" ");   
  386.                     }
  387.                 b = true;
  388.                 }
  389.             }
  390.         }
  391.     }
  392.    
  393.    
  394.     }
  395.     else {
  396.         System.out.println("Time To Set Up the Board (one of second coordinates must be one more than original coordinate)");
  397.        
  398.         for(int i = 2; i < 6; i++)  {
  399.         System.out.println("Type First X Coordinate");
  400.         X1 = scan.nextInt();
  401.         System.out.println("Type Second X Coordinate");
  402.         X2 = scan.nextInt();
  403.         System.out.println("Type First Y Coordinate");
  404.         Y1 = scan.nextInt();
  405.         System.out.println("Type Second Y Coordinate");
  406.         Y2 = scan.nextInt();
  407.         if(!(X2 - X2 == 1) & (Y2 - Y1 == i) || X2 - X1 == 1 & Y2 - Y1 == i) {
  408.             while((X2 - X2 != 1) & (Y2 - Y1 != i) || (X2 - X1 != 1) & (Y2 - Y1 != i)) {
  409.                 System.out.println("Invalid Input");
  410.                 System.out.println("Type First X Coordinate");
  411.                 X1 = scan.nextInt();
  412.                 System.out.println("Type Second X Coordinate");
  413.                 X2 = scan.nextInt();
  414.                 System.out.println("Type First Y Coordinate");
  415.                 Y1 = scan.nextInt();
  416.                 System.out.println("Type Second Y Coordinate");
  417.                 Y2 = scan.nextInt();   
  418.             }
  419.             }
  420.         //                        x1  y1 x2 y2 
  421.         gameboard.setupPlayerBoard(X1, Y1, X2, Y2);
  422.        
  423.         System.out.println(gameboard.showPlayerBoard(true));
  424.         }
  425.         gameboard.showEnemyBoard(false);
  426.        
  427.         while(gameboard.detectWin() != true) {
  428.         if(b == true) {
  429.             System.out.println("Fire At Enemy!");
  430.             System.out.println("Type X Coordinate");
  431.             X1 = scan.nextInt();
  432.             System.out.println("Type Y Coordinate");
  433.             Y1 = scan.nextInt();   
  434.             gameboard.fireatEnemyBoard(X1, Y1);
  435.             System.out.println("Enemy Board");
  436.             System.out.println(gameboard.showEnemyBoard(false));
  437.             System.out.println("Press X to Continue");
  438.             if(scan.next().contains("X")|| scan.next().contains("x")) {
  439.             b = false;
  440.             }
  441.         }else {
  442.             gameboard.fireatPlayerBoard();
  443.             System.out.println("Your Board");
  444.             System.out.println(gameboard.showPlayerBoard(true));
  445.             b = true;
  446.         }
  447.         }
  448.        
  449.     }
  450.     scan.close();
  451.     }
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement