Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Pallavi Jasti
- * @author Denyse Luzon
- * @author Joanna Amezcua
- * @author Kayla Tapiador
- * CIS 36B
- */
- import java.util.InputMismatchException;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.io.*;
- public class Game {
- public static Ship ship1 = new Ship1();
- public static Ship ship2 = new Ship1();
- public static Ship ship3 = new Ship1();
- private static int toBeFound = 3;
- private static int numPoints = 100; //deplete every time answered wrong
- /**
- * Initializes the board by assigning all array elements the value of '-'
- *
- * @param board2 the array representing the Connect4 board
- */
- public static void initializeGame(char[][] board) {
- for (int i = 0; i < board.length; i++) {
- for (int j = 0; j < board[i].length; j++) {
- board[i][j] = '~';
- }
- }
- ship1.ShipPlacement();
- ship2.ShipPlacement();
- ship3.ShipPlacement();
- }
- /**
- * Prints the board to the console in the form of a grid, including column and
- * row numbers. Also prints Connect4 Board above the board.
- *
- * @param board the array representing the Connect4 board
- */
- public static void printBoard(char board[][]) {
- System.out.println("Points: " + numPoints);
- System.out.println("\n 1 2 3 4 5 6");
- System.out.println("1 " + board[0][0] + " " + board[0][1] + " " + board[0][2] + " " + board[0][3] + " " + board[0][4] + " " + board[0][5]);
- System.out.println("2 " + board[1][0] + " " + board[1][1] + " " + board[1][2] + " " + board[1][3] + " " + board[1][4] + " " + board[1][5]);
- System.out.println("3 " + board[2][0] + " " + board[2][1] + " " + board[2][2] + " " + board[2][3] + " " + board[2][4] + " " + board[2][5]);
- System.out.println("4 " + board[3][0] + " " + board[3][1] + " " + board[3][2] + " " + board[3][3] + " " + board[3][4] + " " + board[3][5]);
- System.out.println("5 " + board[4][0] + " " + board[4][1] + " " + board[4][2] + " " + board[4][3] + " " + board[4][4] + " " + board[4][5]);
- System.out.println("6 " + board[5][0] + " " + board[5][1] + " " + board[5][2] + " " + board[5][3] + " " + board[5][4] + " " + board[5][5]);
- }
- /**
- * Converts the player choice of row and column in the form of RC into the
- * correct index of the board array. Hint: Use integer division by 10 to extract
- * the row Hint: Use modulus by 10 to extract the column
- *
- * @param rowCol the row and column, e.g. 12 or 33
- * @return the correct index of the array that corresponds to the row and column
- */
- public static void checkIfHit (char board[][], int rowCol) {
- if (rowCol == 11) {
- checkValidity(board, 0, 0);
- } else if (rowCol == 12) {
- checkValidity(board, 0, 1);
- } else if (rowCol == 13) {
- checkValidity(board, 0, 2);
- } else if (rowCol == 14) {
- checkValidity(board, 0, 3);
- } else if (rowCol == 15) {
- checkValidity(board, 0, 4);
- } else if (rowCol == 16) {
- checkValidity(board, 0, 5);
- } else if (rowCol == 21) {
- checkValidity(board, 1, 0);
- } else if (rowCol == 22) {
- checkValidity(board, 1, 1);
- } else if (rowCol == 23) {
- checkValidity(board, 1, 2);
- } else if (rowCol == 24) {
- checkValidity(board, 1, 3);
- } else if (rowCol == 25) {
- checkValidity(board, 1, 4);
- } else if (rowCol == 26) {
- checkValidity(board, 1, 5);
- } else if (rowCol == 31) {
- checkValidity(board, 2, 0);
- } else if (rowCol == 32) {
- checkValidity(board, 2, 1);
- } else if (rowCol == 33) {
- checkValidity(board, 2, 2);
- } else if (rowCol == 34) {
- checkValidity(board, 2, 3);
- } else if (rowCol == 35) {
- checkValidity(board, 2, 4);
- } else if (rowCol == 36) {
- checkValidity(board, 2, 5);
- } else if (rowCol == 41) {
- checkValidity(board, 3, 0);
- } else if (rowCol == 42) {
- checkValidity(board, 3, 1);
- } else if (rowCol == 43) {
- checkValidity(board, 3, 2);
- } else if (rowCol == 44) {
- checkValidity(board, 3, 3);
- } else if (rowCol == 45) {
- checkValidity(board, 3, 4);
- } else if (rowCol == 46) {
- checkValidity(board, 3, 5);
- } else if (rowCol == 51) {
- checkValidity(board, 4, 0);
- } else if (rowCol == 52) {
- checkValidity(board, 4, 1);
- } else if (rowCol == 53) {
- checkValidity(board, 4, 2);
- } else if (rowCol == 54) {
- checkValidity(board, 4, 3);
- } else if (rowCol == 55) {
- checkValidity(board, 4, 4);
- } else if (rowCol == 56) {
- checkValidity(board, 4, 5);
- } else if (rowCol == 61) {
- checkValidity(board, 5, 0);
- } else if (rowCol == 62) {
- checkValidity(board, 5, 1);
- } else if (rowCol == 63) {
- checkValidity(board, 5, 2);
- } else if (rowCol == 64) {
- checkValidity(board, 5, 3);
- } else if (rowCol == 65) {
- checkValidity(board, 5, 4);
- } else if (rowCol == 66) {
- checkValidity(board, 5, 5);
- } else {
- System.out.println("Invalid Location!!!\n");
- }
- }
- public static void checkValidity(char board[][], int x, int y) {
- if (board[x][y] == '-') {
- System.out.println("You already tried that!\n");
- } else if ((ship1.getShipX() == x && ship1.getShipY() == y) || (ship2.getShipX() == x && ship2.getShipY() == y) || (ship3.getShipX() == x && ship3.getShipY() == y)) {
- toBeFound--;
- System.out.println("You found a ship! " + toBeFound + " left to go!\n");
- board[x][y] = 'X';
- } else {
- System.out.println("There's nothing there. Try Again!\n");
- board[x][y] = '-';
- numPoints--;
- }
- }
- /**
- * Determines whether the game is over due to one player winning, using a series
- * of if statements. Calls the fourInRow method for each possible row on the
- * board.
- *
- */
- public static boolean gameOverWinner() {
- if (toBeFound == 0) {
- return true;
- }
- return false;
- }
- /*public int binarySearch(StringBuilder name, ArrayList<String> scores) {
- int low = 0;
- int high = scores.size()-1;
- while(low<=high) {
- int mid = (low+high)/2;
- if( scores.get(mid).equals(name) ) {
- return mid;
- //System.out.print("hello");
- }
- else if(scores.get(mid).compareTo(name)> 0 ) {
- high = mid -1;
- }
- else {
- low = mid + 1;
- }
- }
- return -1;
- }*/
- public void bubbleSort(ArrayList<String> scores){
- for(int i = 0; i<=scores.size()-2;i++) {
- for(int j = 0; j<=scores.size()-2;j++) {
- if(scores.get(j).compareTo(scores.get(j+1)) > 0) {
- String temp = scores.get(j);
- scores.set(j, scores.get(j+1));
- scores.set(j+1, temp);
- }
- }
- }
- }
- public static int LinearSearch(ArrayList<StringBuilder> ary, StringBuilder value) {
- for(int i = 0; i<ary.size();i++) {
- if(ary.get(i).equals(value)) {
- return i;
- }
- }
- return -1;
- }
- @SuppressWarnings("unchecked")
- public static void main(String[] args) throws IOException {
- Game g = new Game();
- char board[][] = new char[6][6];
- int coordinates = -1;
- String choice = "";
- String continueGame = "y";
- StringBuilder name = new StringBuilder();
- while (continueGame.equalsIgnoreCase("y")) {
- System.out.println("Welcome to Battleship!\n\nTry to sink all three ships,\nbut don't take up too much time!\n");
- ArrayList<String> scoreList = new ArrayList<>();
- File scores = new File("scores.txt");
- Scanner infile = new Scanner(scores);
- if(scoreList.size()==0 && name.length()==0) {
- System.out.println("High Scores: ");
- System.out.println("No stored scores--yet");
- }else {
- System.out.println("High Scores: ");
- g.bubbleSort(scoreList);
- System.out.print(scoreList.get(0));
- }
- while (infile.hasNextLine()) {
- scoreList.add(infile.nextLine());
- }
- for (int i = 0; i < scoreList.size(); i++) {
- System.out.println(scoreList.get(i));
- }
- System.out.println();
- infile.close();
- initializeGame(board);
- Scanner input = new Scanner(System.in);
- do {
- printBoard(board);
- boolean more = true;
- while (more) {
- try {
- System.out.print("\nEnter the coordinates in the form: row, then column without a space (eg: 11 or 45): ");
- coordinates = input.nextInt();
- more = false;
- } catch (InputMismatchException e) {
- System.out.println("Error -- Digits only!");
- input.nextLine();
- }
- }
- checkIfHit(board, coordinates);
- } while (!gameOverWinner());
- PrintWriter out = new PrintWriter("scores.txt");
- Scanner in = new Scanner(System.in);
- printBoard(board);
- if (numPoints == 0) {
- System.out.print("\nGame Over");
- } else {
- System.out.print("\nCongrats! You win!");
- System.out.print("\nScore: " + (numPoints));
- System.out.print("\n\nSave score (enter y or n): ");
- choice = input.next();
- if (choice.equalsIgnoreCase("Y")) {
- System.out.print("\nEnter your full name: ");
- name.append(in.nextLine());
- out.print(name + " " + numPoints);
- System.out.println("Thank you!");
- System.out.print("\nContinue to Play?");
- continueGame =input.next();
- } else {
- System.out.print("\nThank you for playing! May the odds be forever in your favor... ");
- System.out.print("\nContinue to Play?");
- continueGame =input.next();
- }
- }
- out.close();
- in.close();
- input.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement