我希望用JavaScript制作简单的二十一点游戏。我开始打印{ rank: '7', suit: '♥' }
(以JSON格式),但我希望它将7♥
打印成字符串。
任何想法如何让它工作?我也希望让游戏工作,这样我就可以积极地玩它并且它会响应。谁能给我一些关于如何做到这一点的建议呢?
function Card (rank, suit) {
this.rank = rank;
this.suit = suit;
}
function Deck () {
this.deck = new Array();
this.makeDeck = makeDeck;
this.shuffle = shuffle;
this.deal = deal;
}
function makeDeck() {
var ranks = new Array("A","2", "3", "4", "5", "6", "7", "8", "9", "10"),
suits = new Array("♥", "♦", "♣", "♠"),
deck = new Array(52);
var i, j;
for (i = 0; i < 4; i += 1) {
for (j = 0; j < 13; k += 1) {
this.deck[i * ranks.length + j] = new Card(ranks[j], suits[i]);
}
}
}
function shuffle() {
var i, j, temp, n = 10;
for (i = 0; i < n; i += 1) {
for (j = 0; j < this.deck.length; j += 1) {
k = Math.floor(Math.random() * this.deck.length);
temp = this.deck[j];
this.deck[j] = this.deck[k];
this.deck[k] = temp;
}
}
}
function deal() {
if (this.deck.length > 0) {
return this.deck.shift();
} else {
return null;
}
var deck = new Deck();
deck.makeDeck();
deck.shuffle();
var i, j;
for (i = 0; i < 2; i += 1) {
for (j = 0; j < 4; j += 1) {
var Card = deck.deal();
}
}
}
答案 0 :(得分:0)
你需要解析JSON {rank:&#39; 7&#39;,suit:&#39;♥&#39;获得所需的输出7♥
就这样写
var jsonOutput = { rank: '7', suit: '♥' }
var obj = JSON.parse(jsonOutput)
现在打印obj.rank+ obj.suit
,答案是7♥。