针对Android的BattleShips游戏。两通连接组件船碰撞

时间:2015-02-25 15:44:43

标签: java android collision connected-components

我目前的任务是为大学制作战舰游戏。因此,我设法让船只在电网的范围内。我现在唯一的问题是验证与其他船舶相撞的船舶布局。我已经读完了,大概最好的行动方案是两通连接组件算法,但我不完全确定如何实现它。

这是我目前在PlaceShips方法中所拥有的:

public void placeShips(){
    for(int iter = 0; iter < 5; iter++){
        size = shipSizes[iter];
        rotation = randNum.nextInt(2)+1;
        while(!valid){
            rows = randNum.nextInt(10)+1;
            cols = randNum.nextInt(10)+1;
            if(rotation ==1){
                if (cols > mGame.getmColumns()- size){
                    valid = false;
                }
                else{
                    //assumed that the connected component algorithm needs to go here
                    valid = true;
                }
            }
            else if(rotation == 2){
                if (rows > mGame.getmRows() - size){
                    valid = false;
                }
                else{
                    //assumed that the connected component algorithm needs to go here
                    valid = true;
                }
            }
        }
        valid = false;
        //Draw ships
        for(int i = 0; i < size; i++){
            if(rotation == 1){
                gridPos[cols-1][rows-1] = 1;
                cols = cols + 1;
            }
            else if(rotation == 2){
                gridPos[cols-1][rows-1] = 1;
                rows = rows + 1;
            }
        }
    }
}

}

0 个答案:

没有答案