必须有四种设计模式,需要有概率公⽰说明(屏幕输出查看),抽卡物品可以是⼈物或装备。要求⾄少有三个不 同等级的物品(⽐如3星,4星,5星) ⽰例:抽卡有单抽和10连,⼀般为3星,4星概率8%,5星概率1%,10连必出4星,抽80 次没出5星必出5星。
时间: 2023-09-02 20:14:45 浏览: 42
好的,我可以给你一个简单的设计方案,它包括了四种设计模式:
1. 工厂模式:用于创建抽卡物品对象,根据不同等级的物品返回对应的对象。
2. 策略模式:用于定义不同等级物品的抽取概率,可以根据不同的需求选择不同的抽取概率策略。
3. 单例模式:用于保证在整个系统中只有一个抽卡池对象,避免多个对象同时修改抽卡池状态。
4. 观察者模式:用于在抽卡时触发事件,比如抽到5星物品时触发一个事件通知用户。
以下是一个伪代码示例:
```python
# 工厂模式
class ItemFactory:
def create_item(self, level):
if level == 3:
return Item3Star()
elif level == 4:
return Item4Star()
elif level == 5:
return Item5Star()
# 策略模式
class ProbabilityStrategy:
def get_probability(self, level):
pass
class NormalProbability(ProbabilityStrategy):
def get_probability(self, level):
if level == 3:
return 0.8
elif level == 4:
return 0.08
elif level == 5:
return 0.01
class TenConsecutiveProbability(ProbabilityStrategy):
def get_probability(self, level):
if level == 3:
return 0.6
elif level == 4:
return 0.3
elif level == 5:
return 0.1
# 单例模式
class GachaPool:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance.items = []
cls._instance.probability_strategy = NormalProbability()
return cls._instance
def set_probability_strategy(self, strategy):
self.probability_strategy = strategy
def draw(self):
level = self._draw_level()
item = ItemFactory().create_item(level)
self._add_item(item)
return item
def _draw_level(self):
levels = [3, 4, 5]
probabilities = [self.probability_strategy.get_probability(l) for l in levels]
level = np.random.choice(levels, p=probabilities)
return level
def _add_item(self, item):
self.items.append(item)
# 观察者模式
class GachaObserver:
def on_item_drawn(self, item):
pass
class AlertObserver(GachaObserver):
def __init__(self, level):
self.level = level
def on_item_drawn(self, item):
if item.level == self.level:
print(f"You got a {item.name}!")
# 使用示例
pool = GachaPool()
pool.set_probability_strategy(NormalProbability())
observer = AlertObserver(5)
pool.attach_observer(observer)
for i in range(10):
item = pool.draw()
pool.notify_observers(item)
print(f"You drew {len(pool.items)} items.")
```
在上面的代码中,我们使用工厂模式创建不同等级的物品对象,使用策略模式定义了不同等级物品的抽取概率,使用单例模式保证整个系统中只有一个抽卡池对象,使用观察者模式在抽卡时触发事件通知用户。
具体来说,我们使用 `ItemFactory` 类来创建不同等级的物品对象,使用 `ProbabilityStrategy` 类定义了不同等级物品的抽取概率,在 `GachaPool` 类中使用策略模式获取不同等级物品的抽取概率。在 `GachaPool` 类中,我们使用单例模式保证整个系统中只有一个抽卡池对象,使用 `_draw_level` 方法来抽取一个等级,使用 `_add_item` 方法将抽到的物品加入到抽卡池中。在 `GachaPool` 类中,我们还使用了观察者模式,在 `attach_observer` 方法中将观察者对象注册到抽卡池中,在 `notify_observers` 方法中触发事件通知所有观察者对象。在上面的代码中,我们定义了一个 `AlertObserver` 类来观察抽卡结果,如果抽到了指定等级的物品,就输出提示信息。
最后,我们使用 `GachaPool` 类来进行抽卡,使用 `attach_observer` 方法来注册观察者对象,使用 `notify_observers` 方法在抽到指定物品时触发事件通知观察者对象。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)