纸牌游戏,未定义错误

Card Game, Undefined Error

本文关键字:错误 未定义 纸牌游戏      更新时间:2023-09-26

试图通过调用我的纸牌游戏开始,但几乎在第一行出现错误,调用是

var i,j, myDeck = new CardGame.Deck(game),hands = [];

在chrome中调试时获得Type Error: undefined is not a function.。我很确定我已经正确地完成了其余的代码,因为我正在使用我编写的另一个程序的部分,在这里我只是将代码作为一个模块来完成,并试图将其链接到其他JS文件。

这是包含CardGame.Deck 的代码

    "use strict";

var CardGame = function() {

this.Deck = function(o) {
    var length = o;
    var ranks = new Array("A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                "J", "Q", "K");
    var suits = new Array("C", "D", "H", "S");
    this.deck = new Array(length*52);
    var i, j, k;
    var index = 0;
    for (k = 0; k < length; k++) {
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 13; j++) {
                this.deck[i*ranks.length + j] = new Card(ranks[j], suits[i], index);
                index++;
            }
        }
    }
};
var Card = function(suits, ranks, id) {
    var strRank, strSuit, theRank, theSuit;
    strRank = String(ranks);
    strSuit = String(suits);
    var theRank = function getRank() {
       switch (strRank) {
        case "A" :
          theRank = "Ace";
          break;
        case "2" :
          theRank = "Two";
          break;
        case "3" :
          theRank = "Three";
          break;
        case "4" :
          theRank = "Four";
          break;
        case "5" :
          theRank = "Five";
          break;
        case "6" :
          theRank = "Six";
          break;
        case "7" :
          theRank = "Seven";
          break;
        case "8" :
          theRank = "Eight";
          break;
        case "9" :
          theRank = "Nine";
          break;
        case "10" :
          theRank = "Ten";
          break;
        case "J" :
          theRank = "Jack";
          break;
        case "Q" :
          theRank = "Queen";
          break;
        case "K" :
          theRank = "King";
          break;
        default :
          theRank = null;
          break;
        } 
    };
    var theSuit = function getSuit() {
       switch (strSuit) {
        case "C" :
          theSuit = "Clubs";
          break;
        case "D" :
          theSuit = "Diamonds";
          break;
        case "H" :
          theSuit = "Hearts";
          break;
        case "S" :
          theSuit = "Spades";
          break;
        default :
          theSuit = null;
          break;
        }
    };
    return theRank + " of " + theSuit;
};
var Pile = function() {
    function Pile(p) {
        if (p.length < 0) {
            var pile = new Array(); 
        } else {
            var pile = new Array(p);
        }
    }
    function draw(n) {
        return this.deck.slice(0, n);  
    }
    function drawCard() {
        return this.deck.shift();
    }
    function add(o) {
        this.deck.unshift(o);
    }
    function size() {
        return this.deck.length;
    }
    function get(i) {
        return this.deck.splice(i, 1);
    }
    function peek(i) {
        var Card;
        Card = this.deck(i);
        return Card;
    }
    function shuffle() {
        var i, j, temp;
        var n = 10;
        for (i = 0; i < n; i++) {
            for (j = 0; j < this.deck.length; j++) {
                k = Math.floor(Math.random() * this.deck.length);
                temp = this.deck[j];
                this.deck[j] = this.deck[k];
                this.deck[k] = temp;
            }
        }
    }
};
};

目前,deck函数是CardGame函数的局部变量,因此外部代码无法访问它。尝试像这样使用this关键字。

var CardGame = function() {

this.Deck = function(o){
var length = o;
var ranks = new Array("A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "J", "Q", "K");
var suits = new Array("C", "D", "H", "S");
this.deck = new Array(length*52);
var i, j, k;
var index = 0;
for (k = 0; k < length; k++) {
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 13; j++) {
            this.deck[i*ranks.length + j] = new Card(ranks[j], suits[i], index);
            index++;
        }
    }
}
};