Advertisement
davidtmi

test3

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