Advertisement
bbbbas

BandM

May 12th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. package bam;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. /**
  7. *
  8. * @author bas
  9. */
  10.  
  11. public class BAM {
  12.  
  13. private static int[] shot = new int[2];
  14. private static int[][] board = new int[3][7];
  15. private static int[][] animals = new int[3][7];
  16. private static int counterBugs = 0;
  17. private static int counterMonkeys = 0;
  18. private static int numAnimals = 0;
  19. private static int playerTurn = 2;
  20.  
  21. public static void main(String[] args) {
  22.  
  23. BAM.play();
  24. }
  25. // set value -1 in all blocks of the board
  26.  
  27. public static void initBoard() {
  28. for (int row = 0; row < board.length; row++) {
  29. for (int column = 0; column < board[row].length; column++) {
  30. if (column < board[row].length / 2) {
  31. board[row][column] = -1;
  32. } else {
  33. board[row][column] = -2;
  34. }
  35. }
  36. }
  37. for (int dash = 0; dash < board.length; dash++) {
  38. board[dash][board[0].length / 2] = '|';
  39. }
  40.  
  41. }
  42.  
  43. public static void showBoard() {
  44.  
  45. for (int coord = 1; coord < board[0].length; coord++) {
  46. if (coord == board[0].length / 2 + 1) {
  47. System.out.print("\t ");
  48. }
  49. System.out.print("\t" + coord);
  50. }
  51. System.out.println();
  52. for (int i = 0; i< board[0].length*9; i++){
  53. System.out.print("_");
  54. }
  55.  
  56. System.out.println();
  57. System.out.println();
  58.  
  59. for (int row = 0; row < board.length; row++) {
  60. System.out.print((row + 1) + " |");
  61. for (int column = 0; column < board[row].length; column++) {
  62. if (board[row][column] == -1) {
  63. System.out.print("\t" + '>');
  64. } else if (board[row][column] == -2) {
  65. System.out.print("\t" + '<');
  66. } else if (board[row][column] == '|') {
  67. System.out.print("\t" + '|');
  68. } else if (board[row][column] == 0) {
  69. System.out.print("\t" + 'x');
  70. } else if (board[row][column] == 1) {
  71.  
  72. System.out.print("\t" + (char) 164);
  73.  
  74. } else if (board[row][column] == 2) {
  75.  
  76. System.out.print("\t" + (char) 182);
  77.  
  78. }
  79.  
  80. }
  81. System.out.print(" |");
  82. System.out.println();
  83. }
  84.  
  85. for (int i = 0; i< board[0].length*9; i++){
  86. System.out.print("_");
  87. }
  88. System.out.println();
  89. }
  90.  
  91. // randomly choose the locations of a number of monkeys/bugs
  92. // for bugs - field = 1, for monkeys - field = 2
  93. private static void initAnimals(int field) {
  94.  
  95. Random random = new Random();
  96.  
  97. int row;
  98. if (field == 1) {
  99. row = 0;
  100. } else {
  101. row = board[0].length / 2 + 1;
  102. }
  103.  
  104. for (int i = row; i < row + board.length / 2; i++) {
  105. int randomColumn = random.nextInt(board.length / 2);
  106. int randomRow = random.nextInt(board.length);
  107. if (row != 0) {
  108. randomColumn += row;
  109. animals[randomRow][randomColumn] = 2;
  110. numAnimals++;
  111. } else if (row == 0) {
  112.  
  113. animals[randomRow][randomColumn] = 1;
  114. numAnimals++;
  115. }
  116. }
  117.  
  118. }
  119.  
  120. public static void shootRegulated() {
  121. if (playerTurn % 2 == 0) {
  122. System.out.printf("Player 1: ([1,%d], [1, %d])\n", board.length, board[0].length/2);
  123. shoot();
  124. playerTurn++;
  125. }
  126. else {
  127. System.out.printf("Player 2: ([1, %d], [%d,%d])\n", board.length, board[0].length/2 + 1, board[0].length - 1);
  128. shoot();
  129. playerTurn++;
  130. }
  131. }
  132. public static void shoot() {
  133.  
  134. @SuppressWarnings("resource")
  135. Scanner input = new Scanner(System.in);
  136.  
  137. System.out.print("Row: ");
  138. shot[0] = input.nextInt();
  139. shot[0]--;
  140.  
  141.  
  142. System.out.print("Column: ");
  143. shot[1] = input.nextInt();
  144. if (shot[1] <= board[0].length/2) {
  145. shot[1]--;
  146. }
  147. if (shot[0] >= board.length || shot[0] < 0 || shot[1] >= board[0].length || shot[1] < 0) {
  148. shoot();
  149. }
  150.  
  151. }
  152.  
  153. public static boolean hit() {
  154. boolean hit = false;
  155.  
  156. if (animals[shot[0]][shot[1]] == 1) {
  157. board[shot[0]][shot[1]] = 1;
  158. counterBugs++;
  159. hit = true;
  160. } else if (animals[shot[0]][shot[1]] == 2) {
  161. board[shot[0]][shot[1]] = 2;
  162. counterMonkeys++;
  163. hit = true;
  164. } else {
  165. board[shot[0]][shot[1]] = 0;
  166. }
  167.  
  168. return hit;
  169. }
  170.  
  171. public static void play() {
  172.  
  173. initBoard();
  174. initAnimals(1);
  175. initAnimals(2);
  176. String winners = "";
  177.  
  178. System.out.println();
  179. do {
  180. showBoard();
  181. shootRegulated();
  182.  
  183. if (hit()) {
  184. System.out.printf("Shot!");
  185.  
  186. }
  187. } while (counterBugs < numAnimals / 2
  188. && counterMonkeys < numAnimals / 2);
  189.  
  190. if (counterBugs == numAnimals / 2) {
  191. winners = "monkeys";
  192. } else {
  193. winners = "bugs";
  194. }
  195. System.out.printf("\n\n\nBugs against Monkeys finished! The %s win.", winners);
  196.  
  197. System.out.println();
  198. showBoard();
  199.  
  200. }
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement