- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用Flutter + Dart用5个骰子制作类似Yahtzee的游戏。我将骰子值保留在List<int>
中。检查房屋是否满员的最佳方法是什么?总和或相关的骰子是多少?
如果我只想确定我是否满屋,this solution会很好。但是我必须在事后计算总和,所以我需要知道我有多少个数字。
使30个if
涵盖每种情况是一种解决方案,但可能不是最佳解决方案。有谁有更好的主意吗?
最佳答案
这是使用List
/ Iterable
方法的简单Dart实现:
bool fullHouse(List<int> dice) {
final counts = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0};
dice.forEach((n) => counts[n]++);
return counts.containsValue(3) && counts.containsValue(2);
}
int diceSum(List<int> dice) => dice.reduce((v, e) => v + e);
2.6
或更高版本,则还可以为此创建一个漂亮的
extension
:
void main() {
print([1, 1, 2, 1, 2].fullHouseScore);
}
extension YahtzeeDice on List<int> {
int get fullHouseScore {
if (isFullHouse) return diceSum;
return 0;
}
bool get isFullHouse {
final counts = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0};
forEach((n) => counts[n]++);
return counts.containsValue(3) && counts.containsValue(2);
}
int get diceSum => reduce((v, e) => v + e);
}
int checkFullHouse(List<int> dice) {
if (fullHouse(dice)) {
final sum = diceSum(dice);
print('Dice are a full house. Sum is $sum.');
return sum;
} else {
print('Dice are not a full house.');
return 0;
}
}
void main() {
const fullHouses = [
[1, 1, 1, 2, 2],
[1, 2, 1, 2, 1],
[2, 1, 2, 1, 1],
[6, 5, 6, 5, 5],
[4, 4, 3, 3, 3],
[3, 5, 3, 5, 3],
],
other = [
[1, 2, 3, 4, 5],
[1, 1, 1, 1, 2],
[5, 5, 5, 5, 5],
[6, 5, 5, 4, 6],
[4, 3, 2, 5, 6],
[2, 4, 6, 3, 2],
];
print('Testing dice that are full houses.');
fullHouses.forEach(checkFullHouse);
print('Testing dice that are not full houses.');
other.forEach(checkFullHouse);
}
关于flutter - 如何检查和计算骰子满屋,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59237151/
我刚刚向自己介绍了 WebGL 和 Threejs,作为开始,我正在尝试创建一个 3D 骰子。我已经到了创建立方体的地步,但我真的不知道如何在立方体的面上放置数字,我能找到的只是如何更改颜色。我已经查
这就是我现在得到的...... var max = 7; var min = 1; $('#dice').click(function() { random = Math.floor(Math.r
我是 Unity 的新手,一直在尝试掷骰子。我遇到了一组教程,它们允许我创建一个 3d 模具(模具使用 Rigidbody 和 Mesh Collider)并编写脚本使其在空格键上滚动,如下所示: 骰
我是 JavaScript 新手,我需要一些想法/帮助来了解如何使我的脚本正常工作。所以我们的想法是,你掷两个骰子,通过按下“掷骰子”按钮然后按下重置按钮来重置整个 HTML 中的所有内容。 所以事情
我正在尝试构建一个掷骰子游戏,如果计算机自动掷出一对骰子,并且如果 cpu 掷出 7 或 11,则用户获胜。然而,如果用户掷出 2、3 或 12,他们将自动失败。如果用户滚动任何其他数字(4、5、6、
背景 如此处所述http://www.ericharshbarger.org/dice/#gofirst_4d12 , “先走”骰子是一组四个骰子,每个都有唯一的编号,因此: 任何两个或更多骰子都不会
我是一名优秀的程序员,十分优秀!