值保持返回0并且不知道原因

时间:2013-10-09 18:38:15

标签: javascript

我正在制作一个包含HTML,CSS和JavaScript的二十一点游戏,这是我到目前为止所做的,它应该做的是让玩家和计算机初始化并将其打印到屏幕上。但是手的价值总是返回0,我不知道为什么......任何帮助都会受到赞赏,这里有一个小提琴:http://jsfiddle.net/KR9jJ/

//加载窗口时,将画布垂直和水平放置在屏幕中心

window.onload = window.onresize = function() {
    var canvas = document.getElementById('canvas');
    var viewportWidth = window.innerWidth;
    var viewportHeight = window.innerHeight;
    var canvasWidth = viewportWidth * 0.8;
    var canvasHeight = canvasWidth / 2;
    canvas.style.position = "absolute";
    canvas.setAttribute("width", canvasWidth);
    canvas.setAttribute("height", canvasHeight);
    canvas.style.top = (viewportHeight - canvasHeight) / 2 + "px";
    canvas.style.left = (viewportWidth - canvasWidth) / 2 + "px";
};

//随机数生成器1-13的函数

function random() {
    var number = (Math.random()*13)+1;
    return number;
}

function rank() {
    var randomCard = random(); 
    var output = "randomCard";
    switch (randomCard) {
        case 1:
            output = "Ace";
            break;
        case 2:
            output = "Two";
            break;
        case 3:
            output = "Three";
            break;
        case 4:
            output = "Four";
            break;
        case 5:
            output = "Five";
            break;
        case 6:
            output = "Six";
            break;
        case 7:
            output = "Seven";
            break;
        case 8:
            output = "Eight";
            break;
        case 9:
            output = "Nine";
            break;
        case 10:
            output = "Ten";
            break;
        case 11:
            output = "Jack";
            break;
        case 12:
            output = "Queen";
            break;
        case 13:
            output = "King";
        break;
    }
return output;
 }

//函数根据随机结果

返回卡片的值
function value(randomCard) {
var output = 0;
    if (randomCard === ("Ace")) {
        output = 11;
    }

    if (randomCard === ("Two")) {
        output = 2;
    }

    if (randomCard === ("Three")) {
        output = 3;
    }

    if (randomCard === ("Four")) {
        output = 4;
    }

    if (randomCard === ("Five")) {
        output = 5;
    }

    if (randomCard === ("Six")) {
        output = 6;
    }

    if (randomCard === ("Seven")) {
        output = 7;
    }

    if (randomCard === ("Eight")) {
        output = 8;
    }

    if (randomCard === ("Nine")) {
        output = 9;
    }

    if (randomCard === ("Ten")) {
        output = 10;
    }

    if (randomCard === ("Jack")) {
        output = 10;
    }

    if (randomCard === ("Queen")) {
        output = 10;
    }

    if (randomCard === ("King")) {
        output = 10;
    }
return output;
}

//指定播放器和计算机默认手

var playerHand = 0;
var compHand = 0;

//处理计算机和播放器的初始牌

var playerFirstCard = rank();
var playerCardOne = value(playerFirstCard);

var compFirstCard = rank();
var compCardOne = value(compFirstCard);

var playerSecondCard = rank();
var playerCardTwo = value(playerSecondCard);
playerHand = playerHand + (playerCardOne+playerCardTwo);

var compSecondCard = rank();
var compCardTwo = value(compSecondCard);
compHand = compHand + (compCardOne+compCardTwo);

//显示球员和比赛手

document.getElementById('p').innerHTML = playerHand;
document.getElementById('c').innerHTML = compHand;

请注意:请不要提出jQuery建议。

2 个答案:

答案 0 :(得分:2)

您的rank()始终返回“randomCard”,因为(Math.random()*13)+1会出现类似9.1245986的内容,而不是整数。

答案 1 :(得分:2)

在等级函数。 从Math实验室添加地板功能。当你检查你的号码是否自然时,你的随机数是十进制的。

var randomCard = Math.floor(random());