【发布时间】:2016-05-11 10:59:39
【问题描述】:
我正在尝试制作纸牌游戏。玩家 1 和玩家 2 随机获得一张牌,拥有最高牌的人获胜。下面是一组卡片(黑桃)的简单数组。我已将卡片的图像文件保存在名为“images”的文件夹中,如下所示。
我的问题是:如何让这个数组显示牌,并随机分配一张牌给两个玩家,并获得最高牌的胜利?我知道可能需要if/else/else if 声明来确定获胜者,但我该如何将我的牌组分配给两个玩家?会是某种功能吗?下面是我创建的数组:
var cards = []; //variable for cards
cards [0] = 'images/aceofspades.jpg';
cards [1] = 'images/twoofspades.jpg';
cards [2] = 'images/threeofspades.jpg';
cards [3] = 'images/fourofspades.jpg';
cards [4] = 'images/fiveofspades.jpg';
cards [5] = 'images/sixofspades.jpg';
cards [6] = 'images/sevenofspades.jpg';
cards [7] = 'images/eightofspades.jpg';
cards [8] = 'images/nineofspades.jpg';
cards [9] = 'images/tenofspades.jpg';
cards [10] = 'images/jackofspades.jpg';
cards [11] = 'images/queenofspades.jpg';
cards [12] = 'images/kingofspades.jpg';
下面这段代码是我对下一步做什么的想法,但我不确定这是否正确:
function choose (cards);
document.image [cards].src = cardsimage [cards];
【问题讨论】:
-
我建议研究随机数生成。在 JavaScript 中是 Math.random(),但是您需要将其修改为介于 0 和 12 之间。
-
请不要破坏您的问题。
标签: javascript arrays