package ru.agolovin;

import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author agolovin (agolovin@list.ru)
 * @version $Id$
 * @since 0.1
 */

public class Bomberman {

    /**
     * Concurrent game board.
     */
    private final ReentrantLock[][] board;
    /**
     * Size game board.
     */
    private final int size;

    /**
     * Number of players in the game.
     */
    private final int bots;

    /**
     * Players array.
     */
    private final Player[] players;

    /**
     * Cell status.
     */
    private final Boolean[][] cellStatus;

    /**
     * Constructor.
     *
     * @param size int
     * @param bots int
     */
    private Bomberman(int size, int bots) {
        this.size = size;
        this.board = new ReentrantLock[size][size];
        this.bots = bots;
        this.players = new Player[bots];
        this.cellStatus = new Boolean[size][size];
    }

    /**
     * Main method.
     *
     * @param args String[]
     */
    public static void main(String[] args) {
        Bomberman bomb = new Bomberman(7, 2);
        bomb.init();
        try {
            Thread.sleep(10_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bomb.stop();
        }

    }

    /**
     * Prepare game board.
     */
    private void prepareBoard() {
        for (int i = 0; i < this.size; i++) {
            for (int j = 0; j < this.size; j++) {
                this.board[i][j] = new ReentrantLock();
                this.cellStatus[i][j] = false;
            }
        }
    }

    /**
     * Initialization.
     */
    private void init() {
        Random random = new Random();
        prepareBoard();
        createPlayersOnBoard();

        for (int i = 0; i < this.bots;) {

            int xCell = random.nextInt(this.size);
            int yCell = random.nextInt(this.size);

            if (!this.cellStatus[xCell][yCell]) {
                this.players[i]
                        .setPlayersPosition(this.board[xCell][yCell],
                                xCell, yCell);
                this.cellStatus[xCell][yCell] = true;
                System.out.println(
                        String.format(
                                "Players %s start in %d, %d",
                                this.players[i].getName(), xCell, yCell));
                i++;
            }
        }

        createThreads();
    }

    /**
     * Create players on board.
     */
    private void createPlayersOnBoard() {
        for (int i = 0; i < this.players.length; i++) {
            this.players[i] = new Player(this.board, Integer.toString(i));
        }
    }

    /**
     * Start players running.
     */
    private void createThreads() {
        for (Player player : this.players) {
            new Thread(player).start();
        }
    }

    /**
     * Stop game.
     */
    private void stop() {
        for (Player player : this.players) {
            player.interrupt();
        }

    }
}

@@ -586,24 +586,55 @@
 ellStatus;%0A%0A
+    private final int blocks;%0A%0A
     /**%0A    
@@ -750,20 +750,33 @@
 int bots
+, int blocks
 ) %7B%0A
+%0A
         
@@ -959,24 +959,54 @@
 ize%5D%5Bsize%5D;%0A
+        this.blocks = blocks;%0A
     %7D%0A%0A    /
@@ -1157,16 +1157,19 @@
 erman(7,
+ 2,
  2);%0A   
@@ -1878,16 +1878,17 @@
 is.bots;
+ 
 ) %7B%0A%0A   
@@ -2501,31 +2501,509 @@
   %7D%0A
-%0A        createThreads(
+        setUpBlockCell();%0A        createThreads();%0A    %7D%0A%0A    private void setUpBlockCell() %7B%0A        int xPos, yPos;%0A        for (int i = 0; i %3C this.blocks; i++) %7B%0A            xPos = getRandomCell(cellStatus);%0A            yPos = getRandomCell(cellStatus);%0A            if (cellStatus%5BxPos%5D%5ByPos%5D.equals(false)) %7B%0A                cellStatus%5BxPos%5D%5ByPos%5D = true;%0A            %7D%0A        %7D%0A    %7D%0A%0A    private int getRandomCell(Boolean%5B%5D%5B%5D cellStatus) %7B%0A        return new Random().nextInt(cellStatus.length
 );%0A 

add test task in part6 chapter7 package2