Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Battleship {
- public static void main(String[] args) {
- boolean gameOver = false;
- boolean playAgain = true;
- while (playAgain = true) {
- Scanner myScanner = new Scanner(System.in);
- BattleshipCommands bc = new BattleshipCommands();
- System.out.println("Welcome to battleship");
- bc.ResetAllGrids();
- bc.ShowGrid();
- bc.SetUserGrid();
- bc.SetCpuGrid();
- while (gameOver != true) {
- bc.ShowGrid();
- bc.UserTurn();
- bc.CpuTurn();
- }
- }
- }
- }
- public class BattleshipCommands {
- public String userGrid[][] = new String[10][10];
- public String opponentGrid[][] = new String[10][10];
- public String cpuGrid[][] = new String[10][10];
- public String shipName[] = {"Patrol", "Destroyer", "Submarine", "Battleship", "Carrier"};
- public int shipSize[] = {2, 3, 3, 4, 5};
- public Scanner myScanner = new Scanner(System.in);
- public char horizGrid[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
- public int verGrid[] = {1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10};
- char horizPosition;
- int verPosition;
- int horizArrayPos;
- int verArrayPos;
- public static final char ESC = (char) 27;
- public static final String BLACK = ESC + "[30m",
- GREY = ESC + "[47;1m",
- BLUE = ESC + "[44;1m",
- WHITE = ESC + "[37m",
- RED = ESC + "[31m";
- Random rand = new Random();
- public BattleshipCommands() {
- this.myScanner.useDelimiter("\r|\n");
- }
- public void SetUserGrid() {
- for (int index = 0; index < 5; index++) {
- System.out.println("Please enter location for the front of your "
- + shipName[index] + " which is " + shipSize[index] + " spaces wide.");
- String position = myScanner.next();
- horizPosition = position.charAt(0);
- System.out.println(horizPosition);
- verPosition = Integer.parseInt(new Character(position.charAt(1)).toString())-1;
- System.out.println(verPosition);
- horizArrayPos = 0;
- for (int secondIndex = 0; secondIndex < horizGrid.length; secondIndex++) {
- if (horizGrid[secondIndex] == horizPosition) {
- horizArrayPos = secondIndex;
- break;
- }
- }
- System.out.println(horizArrayPos);
- System.out.println(verPosition);
- boolean correctPlacement = false;
- while (correctPlacement != true) {
- System.out.println("Place ship going up, down, left, or right?");
- String placement = myScanner.next();
- userGrid[horizArrayPos][verPosition] = GREY + " 1 ";
- switch (placement.toLowerCase()) {
- case "up":
- if (horizArrayPos == 0) {
- System.out.println("Incorrect Placement: Cannot place outside grid");
- break;
- } else {
- for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
- userGrid[horizArrayPos + secondIndex][verPosition] = GREY + " " + secondIndex + 1 + " ";
- correctPlacement = true;
- break;
- }
- }
- case "down":
- if (horizArrayPos == 9) {
- System.out.println("Incorrect Placement: Cannot place outside grid");
- break;
- } else {
- for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
- userGrid[horizArrayPos - secondIndex][verPosition] = GREY + " " + secondIndex + 1 + " ";
- correctPlacement = true;
- break;
- }
- }
- case "right":
- if (verPosition == 9) {
- System.out.println("Incorrect Placement: Cannot place outside grid");
- break;
- } else {
- for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
- userGrid[horizArrayPos][verPosition + secondIndex] = GREY + " " + secondIndex + 1 + " ";
- correctPlacement = true;
- break;
- }
- }
- case "left":
- if (verPosition == 0) {
- System.out.println("Incorrect Placement: Cannot place outside grid");
- break;
- } else {
- for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
- userGrid[horizArrayPos][verPosition - secondIndex] = GREY + " " + secondIndex + 1 + " ";
- correctPlacement = true;
- break;
- }
- }
- }
- }
- }
- }
- public void SetCpuGrid() {
- for (int index = 0; index < 5; index++) {
- horizArrayPos = rand.nextInt(9) + 0;
- verArrayPos = rand.nextInt(9) + 0;
- boolean correctPlacement = false;
- while (correctPlacement != true) {
- cpuGrid[horizArrayPos][verArrayPos] = GREY + " 1 ";
- String direction[] = {"up", "down", "right", "left"};
- int position = rand.nextInt(3) + 0;
- switch (direction[position]) {
- case "up":
- if (horizArrayPos == 0) {
- break;
- } else {
- for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
- cpuGrid[horizArrayPos + secondIndex][verArrayPos] = GREY + " " + secondIndex + 1 + " ";
- correctPlacement = true;
- break;
- }
- }
- case "down":
- if (horizArrayPos == 9) {
- break;
- } else {
- for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
- cpuGrid[horizArrayPos - secondIndex][verArrayPos] = GREY + " " + secondIndex + 1 + " ";
- correctPlacement = true;
- break;
- }
- }
- case "right":
- if (verArrayPos == 9) {
- break;
- } else {
- for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
- cpuGrid[horizArrayPos][verArrayPos + secondIndex] = GREY + " " + secondIndex + 1 + " ";
- correctPlacement = true;
- break;
- }
- }
- case "left":
- if (verArrayPos == 0) {
- break;
- } else {
- for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
- cpuGrid[horizArrayPos][verArrayPos - secondIndex] = GREY + " " + secondIndex + 1 + " ";
- correctPlacement = true;
- break;
- }
- }
- }
- }
- }
- }
- public void ShowGrid() {
- System.out.println("_| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |");
- for (int index = 0; index < 9; index++) {
- System.out.println(horizGrid[index] + "|" + cpuGrid[index][0] + "|"
- + cpuGrid[index][1] + "|" + cpuGrid[index][2] + "|" + cpuGrid[index][3]
- + "|" + cpuGrid[index][4] + "|" + cpuGrid[index][5] + "|" + cpuGrid[index][6] + "|"
- + cpuGrid[index][7] + "|" + cpuGrid[index][8] + "|" + cpuGrid[index][9]);
- }
- }
- public void ResetAllGrids() {
- for (int index = 0; index < 10; index++) {
- for (int secondIndex = 0; secondIndex < 10; secondIndex++) {
- userGrid[index][secondIndex] = BLUE + "~~~";
- opponentGrid[index][secondIndex] = BLUE + "~~~";
- }
- }
- }
- public String HitOrMissUser(int verPosition, int horizArrayPos) {
- String output = "hit";
- if (cpuGrid[horizArrayPos][verPosition].equals(" ")) {
- output = "missed";
- }
- return output;
- }
- private String HitOrMissCpu(int verArrayPos, int horizArrayPos) {
- String output = "hit";
- if (userGrid[horizArrayPos][verArrayPos].equals(" ")) {
- output = "missed";
- }
- return output;
- }
- public String UserTurn() {
- System.out.println("Please enter location of your shot ");
- String position = myScanner.next();
- horizPosition = position.charAt(0);
- verPosition = Integer.parseInt(new Character(position.charAt(1)).toString())-1;
- System.out.println(verPosition);
- horizArrayPos = 0;
- for (int secondIndex = 0; secondIndex < horizGrid.length; secondIndex++) {
- if (horizGrid[secondIndex] == horizPosition) {
- horizArrayPos = secondIndex;
- break;
- }
- }
- String result = HitOrMissUser(verPosition, horizArrayPos);
- return result;
- }
- public String CpuTurn() {
- horizArrayPos = rand.nextInt(9) + 0;
- verArrayPos = rand.nextInt(9) + 0;
- String result = HitOrMissCpu(verArrayPos, horizArrayPos);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement