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 Test3 {
- 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));
- int row = 0;
- while (file.hasNextLine()) {
- String[] setChar = file.nextLine().split(" ");
- for (int col = 0; col < 10; col++) {
- board[row][col] = setChar[col].charAt(0);
- }
- row++;
- }
- }
- catch (FileNotFoundException exception) {
- System.out.println("Error opening file");
- }
- return board;
- }
- public static void display(char[][] player, char[][] bot) {
- System.out.println(" 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9");
- for (int row = 0; row < 10; row++) {
- System.out.print((char) (row + 65));
- for (int col = 0; col < 10; col++) {
- System.out.print(player[row][col] + " ");
- }
- System.out.print("\t");
- for (int col = 0; col < 10; col++) {
- System.out.print(bot[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 = "";
- 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, blBoard);
- 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 = Character.getNumericValue(coordinate.charAt(1));
- int yCoord = ((int) (yCoord1)) - 65;
- if (isValid(bBoard, xCoord, yCoord)) {
- if (bBoard[xCoord][yCoord] != '*') {
- System.out.println("Direct hit, nice shot");
- blBoard[yCoord][xCoord] = 'H';
- playerHit++;
- pBoard = botMove(pBoard);
- display(pBoard, blBoard);
- continue;
- } else if (bBoard[yCoord][xCoord] == '*') {
- blBoard[yCoord][xCoord] = 'M';
- System.out.println("You have missed sir!");
- playerMiss++;
- display(pBoard, blBoard);
- continue;
- }
- } else {
- continue;
- }
- }
- }
- }
- public static char[][] 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' && pBoard[row][col] != '*') {
- System.out.println("The computer has attacked " + (char) (row + 65)+ col + " and hit!");
- pBoard[row][col] = 'H';
- botHit++;
- return pBoard;
- } 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++;
- }
- return pBoard;
- }
- 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 >= 0 && row <= 9) {
- if (col >= 0 && col <= 9) {
- return true;
- } else {
- System.out.println("dd move!");
- return false;
- }
- } else if (board[row][col] == 'M' || board[row][col] == 'H') {
- 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