cs61b 24sp project0 2048

24sp的skeleton 和之前的不一样,搜不到(可能是我不会搜),磕磕绊绊写出来了,记录一下,方便和我一样的初学者参考;

更新:原来去github搜一下就行了= =

Task 1

public boolean emptySpaceExists() {
        // TODO: Task 2. Fill in this function.
        int size = board.size();
        for(int i = 0; i < size; i++){
            for(int j = 0; j < size; j++){
                if(board.tile(i,j) == null) return true;
            }
        }

        return false;
    }

Task 2

public boolean maxTileExists() {
        // TODO: Task 3. Fill in this function.
        int size = board.size();
        for(int i = 0; i < size; i++){
            for(int j = 0; j < size; j++){
                if(board.tile(i,j) == null) continue;
                else {
                    Tile t = tile(i,j);
                    if(t.value() == MAX_PIECE) return true;
                }
            }
        }
        return false;
    }

Task 3

public boolean atLeastOneMoveExists() {
        // TODO: Fill in this function.
        if(emptySpaceExists()) return true;

        int size = board.size();
        for(int i = 0; i<size-1; i++){
            for(int j = 0; j<size-1; j++){
                if(tile(i,j).value() == tile(i+1,j).value() || tile(i,j).value() == tile(i,j+1).value())
                    return true;
            }
        }

        if(size > 1){
            if(tile(size-1,size-1).value() == tile(size-2, size-1).value() || tile(size-1,size-1).value() == tile(size-1, size-2).value() )
                return true;
        }
        
        return false;
    }

Task 5, 6, and 10

public void moveTileUpAsFarAsPossible(int x, int y) {
        Tile currTile = board.tile(x, y);
        int myValue = currTile.value();
        int targetY = y;

        // TODO: Tasks 5, 6, and 10. Fill in this function.
        int r = y;
        while(r + 1 < size()){
            if(tile(x, r+1) == null) r += 1;
            else {
                if(tile(x, r+1).value() == currTile.value() && !tile(x, r+1).wasMerged()) {
                    r += 1;
                    score = score + 2*currTile.value();
                }
                break;
            }
        }
        if(r != y)  board.move(x, r, currTile);
    }

Task 7

public void tiltColumn(int x) {
        // TODO: Task 7. Fill in this function.
        for (int r = size() -1 ; r >= 0; r--){
            if(tile(x,r) != null){
                moveTileUpAsFarAsPossible(x, r);
            }
        }
    }

Task 8 and 9

public void tilt(Side side) {
        // TODO: Tasks 8 and 9. Fill in this function.
        board.setViewingPerspective(side);
        for (int x=0; x<size(); x++){
            tiltColumn(x);
        }

        board.setViewingPerspective(Side.NORTH);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值