示例#1
0
文件: board.py 项目: dkua/Archive
    def random_board(self, dest, sub, batt, carr):
        """Randomly places a given fleet of ships."""

        if dest == 0 and sub == 0 and batt == 0 and carr == 0:
            return
        coord = battleship.rand_coord(self.size)
        direct = battleship.rand_dir()
        if dest > 0:
            length = 2
            name = self.fleet.name_ship(length)
            if self.check(coord[0], coord[1], length, direct) is True:
                self.fleet.add_ship(name, coord[0], coord[1], length, direct)
                return self.random_board(dest - 1, sub, batt, carr)
        elif sub > 0:
            length = 3
            name = self.fleet.name_ship(length)
            if self.check(coord[0], coord[1], length, direct) is True:
                self.fleet.add_ship(name, coord[0], coord[1], length, direct)
                return self.random_board(dest, sub - 1, batt, carr)
        elif batt > 0:
            length = 4
            name = self.fleet.name_ship(length)
            if self.check(coord[0], coord[1], length, direct) is True:
                self.fleet.add_ship(name, coord[0], coord[1], length, direct)
                return self.random_board(dest, sub, batt - 1, carr)
        elif carr > 0:
            length = 5
            name = self.fleet.name_ship(length)
            if self.check(coord[0], coord[1], length, direct) is True:
                self.fleet.add_ship(name, coord[0], coord[1], length, direct)
                return self.random_board(dest, sub, batt, carr - 1)
        return self.random_board(dest, sub, batt, carr)