Advertisement
davidtmi

test3

Feb 4th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.Random;
  5.  
  6. public class Test2 {
  7. public static int botMiss = 0;
  8. public static int playerMiss = 0;
  9. public static int playerHit = 0;
  10. public static int botHit = 0;
  11.  
  12. // Reads a file called fileName and loads a two-dimensional array called board with the file's contents
  13. public static char[][] loadFile(char[][] board, String fileName) {
  14. try {
  15. Scanner file = new Scanner(new File(fileName));
  16. while (file.hasNextLine()) {
  17. String[] setChar = file.nextLine().split(" ");
  18. for (int row = 0; row < board.length; row++) {
  19. for (int col = 0; col < 10; col++) {
  20. board[row][col] = setChar[col].charAt(0);
  21. }
  22. }
  23. }
  24. }
  25. catch (FileNotFoundException exception) {
  26. System.out.println("Error opening file");
  27. }
  28. return board;
  29. }
  30. public static void display(char[][] board) {
  31. System.out.println(" 0123456789");
  32. for (int row = 0; row < 10; row++) {
  33. System.out.print((char) (row + 65));
  34. for (int col = 0; col < 10; col++) {
  35. System.out.print(board[row][col]);
  36. }
  37. System.out.println();
  38. }
  39. }
  40. public static void main(String[] args) {
  41. Scanner keyboard = new Scanner(System.in);
  42. char[][] pBoard = new char[10][10];
  43. pBoard = loadFile(pBoard, "PLAYER.txt");
  44. char[][] bBoard = new char[10][10];
  45. bBoard = loadFile(bBoard, "CPU.txt");
  46. String coordinate = "";
  47.  
  48. // blank board
  49. char[][] blBoard = new char[10][10];
  50. for (int row = 0; row < blBoard.length; row++) {
  51. for (int col = 0; col < 10; col++) {
  52. blBoard[row][col] = '*';
  53. }
  54. }
  55. display(pBoard);
  56.  
  57. play:
  58. while(!ifWinner(bBoard) || !ifWinner(pBoard)) {
  59. System.out.println("Please enter an attack coordiante Captain:");
  60. coordinate = keyboard.nextLine();
  61.  
  62. if (coordinate.length() != 2) {
  63. continue play;
  64. } else if (coordinate.length() == 2) {
  65. char yCoord1 = coordinate.toUpperCase().charAt(0);
  66. int xCoord = coordinate.charAt(1) - 1;
  67. int yCoord = ((int) (yCoord1)) - 65;
  68. if (isValid(bBoard, yCoord, xCoord)) {
  69. if (bBoard[xCoord][yCoord] != '*') {
  70. System.out.println("Direct hit, nice shot");
  71. blBoard[xCoord][yCoord] = 'H';
  72. playerHit++;
  73. botMove(pBoard);
  74. continue;
  75. } else if (bBoard[xCoord][yCoord] == '*') {
  76. blBoard[xCoord][yCoord] = 'M';
  77. System.out.println("You have missed sir!");
  78. playerMiss++;
  79. botMove(pBoard);
  80. continue;
  81. }
  82. } else {
  83. continue;
  84. }
  85. }
  86. }
  87. }
  88. public static void botMove(char[][] board) {
  89. char[][] pBoard = new char[10][10];
  90. Random bot1 = new Random();
  91. Random bot2 = new Random();
  92.  
  93. int row = bot1.nextInt(9);
  94. int col = bot2.nextInt(9);
  95. char rowDisplay = (char) (row + 65);
  96.  
  97. if (pBoard[row][col] != '*') {
  98. if (pBoard[row][col] != 'H' && pBoard[row][col] != 'M') {
  99. System.out.println("The computer has attacked " + (char) (row + 65)+ col + " and hit!");
  100. pBoard[row][col] = 'H';
  101. botHit++;
  102. } else {
  103. System.out.println("Invalid move");
  104. }
  105. } else if (pBoard[row][col] == '*');
  106. pBoard[row][col] = 'M';
  107. System.out.println("The computer has attacked " + (char) (row + 65) + col + " and missed");
  108. botMiss++;
  109. }
  110. public static boolean ifWinner(char[][] board) {
  111. if (playerHit == 17) {
  112. System.out.println("The user has won the game!");
  113. return true;
  114. } else if (botHit == 17) {
  115. System.out.println("The bot has won the game!");
  116. return true;
  117. } else {
  118. return false;
  119. }
  120. }
  121. public static boolean isValid(char[][] board, int row, int col) {
  122. if (row >= 'A' && row <= 'J') {
  123. if (col >= 0 && col <= 9) {
  124. return true;
  125. } else {
  126. System.out.println("Invalid move!");
  127. return false;
  128. }
  129. } else {
  130. System.out.println("Invalid move!");
  131. return false;
  132. }
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement