A Game of Falling Bricks!

77 views
Skip to first unread message

Seabook

unread,
Mar 20, 2012, 7:46:25 PM3/20/12
to happy-pr...@googlegroups.com

This is some algorithm exercises from Amdocs CodeMania.

Wish you guys enjoy it.

A Game of Falling Bricks!

Imagine playing a game where bricks of different shapes are dropped into the box.  The dropped bricks do not undergo any angular displacement (no rotation & no sideways movement), they just fall down in a linear fashion; one after the other. There will be 7 different types of bricks, labeled A through G as indicated below.

...

Please check the attachment for the entire algorithm exercise.

Thanks,
Seabook
CodeMania_200903.docx

Seabook

unread,
Mar 21, 2012, 8:50:29 PM3/21/12
to happy-pr...@googlegroups.com
My humble solution here.
May have lots of potential bugs.

package amdocs;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FallingBricks {
    private int[][] board = new int[8][6];

    public void fall(Brick brick, int pos) {
        int[][] brickPos = brick.getPoint();

        for (int j = board[0].length - 1; j >= 0; j--) {
            int putPosition = 0;
            for (int i = pos; i < pos + brick.getWidth(); i++) {
                if (board[i][j] == 1) {
                    putPosition = j + 1;
                    break;
                }
            }
           
            if (j != 0 && putPosition == 0) {
                continue;
            }

            for (int m = 0; m < brickPos.length; m++) {
                for (int n = 0; n < brickPos[m].length; n++) {
                    int line = putPosition + n;
                    if (brickPos[m][n] == 1) {
                        board[pos + m][line] = brickPos[m][n];
                    }
                }
            }

            clearFullLine();
            break;
        }
    }

    private void clearFullLine() {
        List<Integer> lines = new ArrayList<Integer>();
        for (int j = 0; j < board[0].length; j++) {
            boolean isFull = true;
            for (int i = 0; i < board.length; i++) {
                if (board[i][j] == 0) {
                    isFull = false;
                    break;
                }
            }

            if (isFull) {
                lines.add(j);
            }
        }

        if (lines.size() == 0)
            return;

        Collections.sort(lines);

        for (int i = 0; i < board.length; i++) {
            for (Integer l : lines) {
                board[i][l] = 0;
            }
        }

        for (int m = 0; m < board.length; m++) {
            for (int n = lines.get(0); n < board[m].length; n++) {
                if (n + lines.size() < board[m].length)
                    board[m][n] = board[m][n + lines.size()];
            }
        }
    }

    public void printBoard() {
        for (int j = board[0].length - 1; j >= 0; j--) {
            for (int i = 0; i < board.length; i++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        FallingBricks fb = new FallingBricks();
        fb.testCase4(fb);
        fb.printBoard();
    }
   
    public void testCase1(FallingBricks fb ) {
        fb.fall(Brick.B, 2);
        fb.fall(Brick.A, 0);
        fb.fall(Brick.A, 0);
        fb.fall(Brick.E, 2);
        fb.fall(Brick.A, 5);
        fb.fall(Brick.G, 4);
    }
   
    public void testCase2(FallingBricks fb ) {
        fb.fall(Brick.D, 0);
        fb.fall(Brick.B, 1);
        fb.fall(Brick.D, 2);
    }
   
    public void testCase3(FallingBricks fb ) {
        fb.fall(Brick.F, 0);
        fb.fall(Brick.F, 1);
        fb.fall(Brick.F, 2);
        fb.fall(Brick.F, 3);
        fb.fall(Brick.F, 4);
        fb.fall(Brick.F, 5);
        fb.fall(Brick.F, 6);
        fb.fall(Brick.F, 7);
    }
   
    public void testCase4(FallingBricks fb ) {
        fb.fall(Brick.G, 4);
        fb.fall(Brick.G, 0);
    }
}

enum Brick {
    A(new int[][] { { 1, 1 }, { 1, 1 } }, 2),
    B(new int[][] { { 1, 1 }, { 1, 0 } }, 2),
    C(new int[][] { { 0, 1 }, { 1, 1 } }, 2),
    D(new int[][] { { 1, 1 }, { 0, 1 } }, 2),
    E(new int[][] { { 1, 0 }, { 1, 1 } }, 2),
    F(new int[][] { { 1, 1, 1, 1 }, { 0, 0, 0, 0 },{ 0, 0, 0, 0 }, { 0, 0, 0, 0 } }, 1),
    G(new int[][] { { 1, 0, 0, 0 }, { 1, 0, 0, 0 }, { 1, 0, 0, 0 }, { 1, 0, 0, 0 } }, 4);

    private int[][] point;
    private int width;

    private Brick(int[][] point, int width) {
        this.point = point;
        this.width = width;
    }

    public int getWidth() {
        return width;
    }

    public int[][] getPoint() {
        return point;
    }

}


Thanks,
Seabook

Seabook

unread,
Mar 29, 2012, 9:17:13 PM3/29/12
to happy-pr...@googlegroups.com
Continue Amdocs Code Mania.

Climb That Treacherous Mountain!

A group of travellers reach the foot of a mountain and need to climb it to reach their destination. There is a narrow pathway for climbing the mountain and there is a risk of wild beasts attacking unarmed humans. The pathway is so narrow such that no more than two people can walk safely at any point of time. The group has only one gun to protect themselves and hence, the travellers need to devise some sort of shuttle arrangement through which the gun is returned by somebody after climbing the mountain to enable more travellers to climb the mountain.

CodeMania_201112.doc
Reply all
Reply to author
Forward
0 new messages