Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.29 KB | None | 0 0
  1. /**
  2.  * @author Pallavi Jasti
  3.  * @author Denyse Luzon
  4.  * @author Joanna Amezcua
  5.  * @author Kayla Tapiador
  6.  * CIS 36B
  7.  */
  8.  
  9. import java.util.InputMismatchException;
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12. import java.io.*;
  13.  
  14. public class Game {
  15.  
  16.     public static Ship ship1 = new Ship1();
  17.     public static Ship ship2 = new Ship1();
  18.     public static Ship ship3 = new Ship1();
  19.     private static int toBeFound = 3;
  20.     private static int numPoints = 100; //deplete every time answered wrong
  21.  
  22.  
  23.     /**
  24.      * Initializes the board by assigning all array elements the value of '-'
  25.      *
  26.      * @param board2 the array representing the Connect4 board
  27.      */
  28.     public static void initializeGame(char[][] board) {
  29.         for (int i = 0; i < board.length; i++) {
  30.             for (int j = 0; j < board[i].length; j++) {
  31.                 board[i][j] = '~';
  32.             }
  33.         }
  34.         ship1.ShipPlacement();
  35.         ship2.ShipPlacement();
  36.         ship3.ShipPlacement();
  37.     }
  38.  
  39.     /**
  40.      * Prints the board to the console in the form of a grid, including column and
  41.      * row numbers. Also prints Connect4 Board above the board.
  42.      *
  43.      * @param board the array representing the Connect4 board
  44.      */
  45.     public static void printBoard(char board[][]) {
  46.         System.out.println("Points: " + numPoints);
  47.         System.out.println("\n  1 2 3 4 5 6");
  48.         System.out.println("1 " + board[0][0] + " " + board[0][1] + " " + board[0][2] + " " + board[0][3] + " " + board[0][4] + " " + board[0][5]);
  49.         System.out.println("2 " + board[1][0] + " " + board[1][1] + " " + board[1][2] + " " + board[1][3] + " " + board[1][4] + " " + board[1][5]);
  50.         System.out.println("3 " + board[2][0] + " " + board[2][1] + " " + board[2][2] + " " + board[2][3] + " " + board[2][4] + " " + board[2][5]);
  51.         System.out.println("4 " + board[3][0] + " " + board[3][1] + " " + board[3][2] + " " + board[3][3] + " " + board[3][4] + " " + board[3][5]);
  52.         System.out.println("5 " + board[4][0] + " " + board[4][1] + " " + board[4][2] + " " + board[4][3] + " " + board[4][4] + " " + board[4][5]);
  53.         System.out.println("6 " + board[5][0] + " " + board[5][1] + " " + board[5][2] + " " + board[5][3] + " " + board[5][4] + " " + board[5][5]);
  54.     }
  55.  
  56.     /**
  57.      * Converts the player choice of row and column in the form of RC into the
  58.      * correct index of the board array. Hint: Use integer division by 10 to extract
  59.      * the row Hint: Use modulus by 10 to extract the column
  60.      *
  61.      * @param rowCol the row and column, e.g. 12 or 33
  62.      * @return the correct index of the array that corresponds to the row and column
  63.      */
  64.     public static void checkIfHit (char board[][], int rowCol) {
  65.         if (rowCol == 11) {
  66.             checkValidity(board, 0, 0);
  67.         } else if (rowCol == 12) {
  68.             checkValidity(board, 0, 1);
  69.         } else if (rowCol == 13) {
  70.             checkValidity(board, 0, 2);
  71.         } else if (rowCol == 14) {
  72.             checkValidity(board, 0, 3);
  73.         } else if (rowCol == 15) {
  74.             checkValidity(board, 0, 4);
  75.         } else if (rowCol == 16) {
  76.             checkValidity(board, 0, 5);
  77.         } else if (rowCol == 21) {
  78.             checkValidity(board, 1, 0);
  79.         } else if (rowCol == 22) {
  80.             checkValidity(board, 1, 1);
  81.         } else if (rowCol == 23) {
  82.             checkValidity(board, 1, 2);
  83.         } else if (rowCol == 24) {
  84.             checkValidity(board, 1, 3);
  85.         } else if (rowCol == 25) {
  86.             checkValidity(board, 1, 4);
  87.         } else if (rowCol == 26) {
  88.             checkValidity(board, 1, 5);
  89.         } else if (rowCol == 31) {
  90.             checkValidity(board, 2, 0);
  91.         } else if (rowCol == 32) {
  92.             checkValidity(board, 2, 1);
  93.         } else if (rowCol == 33) {
  94.             checkValidity(board, 2, 2);
  95.         } else if (rowCol == 34) {
  96.             checkValidity(board, 2, 3);
  97.         } else if (rowCol == 35) {
  98.             checkValidity(board, 2, 4);
  99.         } else if (rowCol == 36) {
  100.             checkValidity(board, 2, 5);
  101.         } else if (rowCol == 41) {
  102.             checkValidity(board, 3, 0);
  103.         } else if (rowCol == 42) {
  104.             checkValidity(board, 3, 1);
  105.         } else if (rowCol == 43) {
  106.             checkValidity(board, 3, 2);
  107.         } else if (rowCol == 44) { 
  108.             checkValidity(board, 3, 3);
  109.         } else if (rowCol == 45) {
  110.             checkValidity(board, 3, 4);
  111.         } else if (rowCol == 46) {
  112.             checkValidity(board, 3, 5);
  113.         } else if (rowCol == 51) {     
  114.             checkValidity(board, 4, 0);
  115.         } else if (rowCol == 52) {
  116.             checkValidity(board, 4, 1);
  117.         } else if (rowCol == 53) {
  118.             checkValidity(board, 4, 2);
  119.         } else if (rowCol == 54) {
  120.             checkValidity(board, 4, 3);
  121.         } else if (rowCol == 55) {
  122.             checkValidity(board, 4, 4);
  123.         } else if (rowCol == 56) {
  124.             checkValidity(board, 4, 5);
  125.         } else if (rowCol == 61) {
  126.             checkValidity(board, 5, 0);
  127.         } else if (rowCol == 62) {
  128.             checkValidity(board, 5, 1);
  129.         } else if (rowCol == 63) {
  130.             checkValidity(board, 5, 2);
  131.         } else if (rowCol == 64) {
  132.             checkValidity(board, 5, 3);
  133.         } else if (rowCol == 65) {
  134.             checkValidity(board, 5, 4);
  135.         } else if (rowCol == 66) {
  136.             checkValidity(board, 5, 5);
  137.         } else {
  138.             System.out.println("Invalid Location!!!\n");
  139.         }
  140.  
  141.     }
  142.  
  143.     public static void checkValidity(char board[][], int x, int y) {
  144.         if (board[x][y] == '-') {
  145.             System.out.println("You already tried that!\n");
  146.         } else if ((ship1.getShipX() == x && ship1.getShipY() == y) || (ship2.getShipX() == x && ship2.getShipY() == y) || (ship3.getShipX() == x && ship3.getShipY() == y)) {
  147.             toBeFound--;
  148.             System.out.println("You found a ship! " + toBeFound + " left to go!\n");
  149.             board[x][y] = 'X';
  150.         } else {
  151.             System.out.println("There's nothing there. Try Again!\n");
  152.             board[x][y] = '-';
  153.             numPoints--;
  154.         }
  155.     }
  156.  
  157.     /**
  158.      * Determines whether the game is over due to one player winning, using a series
  159.      * of if statements. Calls the fourInRow method for each possible row on the
  160.      * board.
  161.      *
  162.      */
  163.     public static boolean gameOverWinner() {
  164.         if (toBeFound == 0) {
  165.             return true;
  166.         }
  167.         return false;
  168.     }
  169.  
  170.     /*public int binarySearch(StringBuilder name, ArrayList<String> scores) {
  171.  
  172.         int low = 0;
  173.         int high = scores.size()-1;
  174.                                                                                                                                                                                
  175.         while(low<=high) {
  176.             int mid = (low+high)/2;
  177.             if( scores.get(mid).equals(name) ) {
  178.                 return mid;
  179.                 //System.out.print("hello");
  180.             }
  181.             else if(scores.get(mid).compareTo(name)> 0 ) {
  182.                 high = mid -1;
  183.  
  184.             }
  185.             else {
  186.                 low = mid + 1;
  187.  
  188.             }
  189.         }
  190.  
  191.         return -1;
  192.     }*/
  193.     public void bubbleSort(ArrayList<String> scores){
  194.         for(int i = 0; i<=scores.size()-2;i++) {
  195.             for(int j = 0; j<=scores.size()-2;j++) {
  196.                 if(scores.get(j).compareTo(scores.get(j+1)) > 0) {
  197.                     String temp = scores.get(j);
  198.                     scores.set(j, scores.get(j+1));
  199.                     scores.set(j+1, temp);
  200.                 }
  201.             }
  202.         }
  203.     }
  204.     public static int LinearSearch(ArrayList<StringBuilder> ary, StringBuilder value) {
  205.         for(int i = 0; i<ary.size();i++) {
  206.             if(ary.get(i).equals(value)) {
  207.                 return i;
  208.             }  
  209.         }
  210.         return -1;
  211.    }
  212.  
  213.  
  214.     @SuppressWarnings("unchecked")
  215.     public static void main(String[] args) throws IOException {
  216.  
  217.         Game g = new Game();
  218.         char board[][] = new char[6][6];
  219.         int coordinates = -1;
  220.         String choice = "";
  221.         String continueGame = "y";
  222.         StringBuilder name = new StringBuilder();
  223.        
  224.        
  225.         while (continueGame.equalsIgnoreCase("y")) {
  226.            
  227.             System.out.println("Welcome to Battleship!\n\nTry to sink all three ships,\nbut don't take up too much time!\n");
  228.  
  229.             ArrayList<String> scoreList = new ArrayList<>();
  230.             File scores = new File("scores.txt");
  231.             Scanner infile = new Scanner(scores);
  232.             if(scoreList.size()==0 && name.length()==0) {
  233.                 System.out.println("High Scores: ");
  234.                 System.out.println("No stored scores--yet");
  235.             }else {
  236.             System.out.println("High Scores: ");
  237.             g.bubbleSort(scoreList);
  238.             System.out.print(scoreList.get(0));
  239.             }
  240.  
  241.             while (infile.hasNextLine()) {
  242.                 scoreList.add(infile.nextLine());
  243.             }
  244.             for (int i = 0; i < scoreList.size(); i++) {
  245.                 System.out.println(scoreList.get(i));
  246.             }
  247.             System.out.println();
  248.             infile.close();
  249.  
  250.             initializeGame(board);
  251.             Scanner input = new Scanner(System.in);
  252.            
  253.  
  254.             do {
  255.                 printBoard(board);
  256.  
  257.                 boolean more = true;
  258.                 while (more) {
  259.                     try {
  260.                         System.out.print("\nEnter the coordinates in the form: row, then column without a space (eg: 11 or 45): ");
  261.                         coordinates = input.nextInt();
  262.                         more = false;
  263.                     } catch (InputMismatchException e) {
  264.                         System.out.println("Error -- Digits only!");
  265.                         input.nextLine();
  266.                     }
  267.                 }
  268.  
  269.                 checkIfHit(board, coordinates);
  270.  
  271.             } while (!gameOverWinner());
  272.             PrintWriter out = new PrintWriter("scores.txt");
  273.             Scanner in = new Scanner(System.in);
  274.  
  275.             printBoard(board);
  276.  
  277.             if (numPoints == 0) {
  278.                 System.out.print("\nGame Over");
  279.             } else {
  280.                 System.out.print("\nCongrats! You win!");
  281.                 System.out.print("\nScore: " + (numPoints));
  282.                 System.out.print("\n\nSave score (enter y or n): ");
  283.                 choice = input.next();
  284.                 if (choice.equalsIgnoreCase("Y")) {
  285.  
  286.                     System.out.print("\nEnter your full name: ");
  287.  
  288.                     name.append(in.nextLine());
  289.                     out.print(name + " " + numPoints);
  290.                     System.out.println("Thank you!");
  291.                     System.out.print("\nContinue to Play?");
  292.                     continueGame =input.next();
  293.  
  294.                 } else {
  295.                     System.out.print("\nThank you for playing! May the odds be forever in your favor... ");
  296.                     System.out.print("\nContinue to Play?");
  297.            
  298.                     continueGame =input.next();
  299.                    
  300.                 }
  301.             }
  302.            
  303.         out.close();
  304.             in.close();
  305.             input.close();
  306.             }
  307.  
  308.     }
  309.  
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement