Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bam;
- import java.util.Random;
- import java.util.Scanner;
- /**
- *
- * @author bas
- */
- public class BAM {
- private static int[] shot = new int[2];
- private static int[][] board = new int[3][7];
- private static int[][] animals = new int[3][7];
- private static int counterBugs = 0;
- private static int counterMonkeys = 0;
- private static int numAnimals = 0;
- private static int playerTurn = 2;
- public static void main(String[] args) {
- BAM.play();
- }
- // set value -1 in all blocks of the board
- public static void initBoard() {
- for (int row = 0; row < board.length; row++) {
- for (int column = 0; column < board[row].length; column++) {
- if (column < board[row].length / 2) {
- board[row][column] = -1;
- } else {
- board[row][column] = -2;
- }
- }
- }
- for (int dash = 0; dash < board.length; dash++) {
- board[dash][board[0].length / 2] = '|';
- }
- }
- public static void showBoard() {
- for (int coord = 1; coord < board[0].length; coord++) {
- if (coord == board[0].length / 2 + 1) {
- System.out.print("\t ");
- }
- System.out.print("\t" + coord);
- }
- System.out.println();
- for (int i = 0; i< board[0].length*9; i++){
- System.out.print("_");
- }
- System.out.println();
- System.out.println();
- for (int row = 0; row < board.length; row++) {
- System.out.print((row + 1) + " |");
- for (int column = 0; column < board[row].length; column++) {
- if (board[row][column] == -1) {
- System.out.print("\t" + '>');
- } else if (board[row][column] == -2) {
- System.out.print("\t" + '<');
- } else if (board[row][column] == '|') {
- System.out.print("\t" + '|');
- } else if (board[row][column] == 0) {
- System.out.print("\t" + 'x');
- } else if (board[row][column] == 1) {
- System.out.print("\t" + (char) 164);
- } else if (board[row][column] == 2) {
- System.out.print("\t" + (char) 182);
- }
- }
- System.out.print(" |");
- System.out.println();
- }
- for (int i = 0; i< board[0].length*9; i++){
- System.out.print("_");
- }
- System.out.println();
- }
- // randomly choose the locations of a number of monkeys/bugs
- // for bugs - field = 1, for monkeys - field = 2
- private static void initAnimals(int field) {
- Random random = new Random();
- int row;
- if (field == 1) {
- row = 0;
- } else {
- row = board[0].length / 2 + 1;
- }
- for (int i = row; i < row + board.length / 2; i++) {
- int randomColumn = random.nextInt(board.length / 2);
- int randomRow = random.nextInt(board.length);
- if (row != 0) {
- randomColumn += row;
- animals[randomRow][randomColumn] = 2;
- numAnimals++;
- } else if (row == 0) {
- animals[randomRow][randomColumn] = 1;
- numAnimals++;
- }
- }
- }
- public static void shootRegulated() {
- if (playerTurn % 2 == 0) {
- System.out.printf("Player 1: ([1,%d], [1, %d])\n", board.length, board[0].length/2);
- shoot();
- playerTurn++;
- }
- else {
- System.out.printf("Player 2: ([1, %d], [%d,%d])\n", board.length, board[0].length/2 + 1, board[0].length - 1);
- shoot();
- playerTurn++;
- }
- }
- public static void shoot() {
- @SuppressWarnings("resource")
- Scanner input = new Scanner(System.in);
- System.out.print("Row: ");
- shot[0] = input.nextInt();
- shot[0]--;
- System.out.print("Column: ");
- shot[1] = input.nextInt();
- if (shot[1] <= board[0].length/2) {
- shot[1]--;
- }
- if (shot[0] >= board.length || shot[0] < 0 || shot[1] >= board[0].length || shot[1] < 0) {
- shoot();
- }
- }
- public static boolean hit() {
- boolean hit = false;
- if (animals[shot[0]][shot[1]] == 1) {
- board[shot[0]][shot[1]] = 1;
- counterBugs++;
- hit = true;
- } else if (animals[shot[0]][shot[1]] == 2) {
- board[shot[0]][shot[1]] = 2;
- counterMonkeys++;
- hit = true;
- } else {
- board[shot[0]][shot[1]] = 0;
- }
- return hit;
- }
- public static void play() {
- initBoard();
- initAnimals(1);
- initAnimals(2);
- String winners = "";
- System.out.println();
- do {
- showBoard();
- shootRegulated();
- if (hit()) {
- System.out.printf("Shot!");
- }
- } while (counterBugs < numAnimals / 2
- && counterMonkeys < numAnimals / 2);
- if (counterBugs == numAnimals / 2) {
- winners = "monkeys";
- } else {
- winners = "bugs";
- }
- System.out.printf("\n\n\nBugs against Monkeys finished! The %s win.", winners);
- System.out.println();
- showBoard();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement