Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Random;
- public class Test2 {
- public static int botMiss = 0;
- public static int playerMiss = 0;
- public static int playerHit = 0;
- public static int botHit = 0;
- // Reads a file called fileName and loads a two-dimensional array called board with the file's contents
- public static char[][] loadFile(char[][] board, String fileName) {
- try {
- Scanner file = new Scanner(new File(fileName));
- while (file.hasNextLine()) {
- String[] setChar = file.nextLine().split(" ");
- for (int row = 0; row < board.length; row++) {
- for (int col = 0; col < 10; col++) {
- board[row][col] = setChar[col].charAt(0);
- }
- }
- }
- }
- catch (FileNotFoundException exception) {
- System.out.println("Error opening file");
- }
- return board;
- }
- public static void display(char[][] board) {
- System.out.println(" 0123456789");
- for (int row = 0; row < 10; row++) {
- System.out.print((char) (row + 65));
- for (int col = 0; col < 10; col++) {
- System.out.print(board[row][col]);
- }
- System.out.println();
- }
- }
- public static void main(String[] args) {
- Scanner keyboard = new Scanner(System.in);
- char[][] pBoard = new char[10][10];
- pBoard = loadFile(pBoard, "PLAYER.txt");
- char[][] bBoard = new char[10][10];
- bBoard = loadFile(bBoard, "CPU.txt");
- String coordinate = "";
- // blank board
- char[][] blBoard = new char[10][10];
- for (int row = 0; row < blBoard.length; row++) {
- for (int col = 0; col < 10; col++) {
- blBoard[row][col] = '*';
- }
- }
- display(pBoard);
- play:
- while(!ifWinner(bBoard) || !ifWinner(pBoard)) {
- System.out.println("Please enter an attack coordiante Captain:");
- coordinate = keyboard.nextLine();
- if (coordinate.length() != 2) {
- continue play;
- } else if (coordinate.length() == 2) {
- char yCoord1 = coordinate.toUpperCase().charAt(0);
- int xCoord = coordinate.charAt(1) - 1;
- int yCoord = ((int) (yCoord1)) - 65;
- if (isValid(bBoard, yCoord, xCoord)) {
- if (bBoard[xCoord][yCoord] != '*') {
- System.out.println("Direct hit, nice shot");
- blBoard[xCoord][yCoord] = 'H';
- playerHit++;
- botMove(pBoard);
- continue;
- } else if (bBoard[xCoord][yCoord] == '*') {
- blBoard[xCoord][yCoord] = 'M';
- System.out.println("You have missed sir!");
- playerMiss++;
- botMove(pBoard);
- continue;
- }
- } else {
- continue;
- }
- }
- }
- }
- public static void botMove(char[][] board) {
- char[][] pBoard = new char[10][10];
- Random bot1 = new Random();
- Random bot2 = new Random();
- int row = bot1.nextInt(9);
- int col = bot2.nextInt(9);
- char rowDisplay = (char) (row + 65);
- if (pBoard[row][col] != '*') {
- if (pBoard[row][col] != 'H' && pBoard[row][col] != 'M') {
- System.out.println("The computer has attacked " + (char) (row + 65)+ col + " and hit!");
- pBoard[row][col] = 'H';
- botHit++;
- } else {
- System.out.println("Invalid move");
- }
- } else if (pBoard[row][col] == '*');
- pBoard[row][col] = 'M';
- System.out.println("The computer has attacked " + (char) (row + 65) + col + " and missed");
- botMiss++;
- }
- public static boolean ifWinner(char[][] board) {
- if (playerHit == 17) {
- System.out.println("The user has won the game!");
- return true;
- } else if (botHit == 17) {
- System.out.println("The bot has won the game!");
- return true;
- } else {
- return false;
- }
- }
- public static boolean isValid(char[][] board, int row, int col) {
- if (row >= 'A' && row <= 'J') {
- if (col >= 0 && col <= 9) {
- return true;
- } else {
- System.out.println("Invalid move!");
- return false;
- }
- } else {
- System.out.println("Invalid move!");
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement