1

次の C# コードがありますが、JavaScript に簡単に変換できますか? ブラウザで 100% 実行するには、これが必要です。

私が理解しているように、列挙型は存在せず、代わりに文字列に置き換えることができます。

要するに、クラス Deck witch にはカードの配列が保持されています。クラス Card には、ランク、シンボルなど、シャッフル機能などのいくつかのプロパティがあります。

ここで私のコードをすべて変換する人はいないと思いますが、同様のことができる方法を教えてください。

public enum Suit
{
    Club,
    Diamond,
    Heart,
    Spade
};

public class Card
    {
    public Suit suit;
    public int value;
    public string text;
    public int HCP;

    public Card()
    {
    }

    public Card(Suit _suit, int _value, int _HCP, string _text)
    {
        suit = _suit;
        value = _value;
        HCP = _HCP;
        text = _text;
    }
};

public class Deck
{
    public Card[] Cards = new Card[52];
    public Deck()
    {
        Cards[0] = new Card(Suit.Spade, 14, 4, "A");
        Cards[1] = new Card(Suit.Spade, 13, 3, "K");
        Cards[2] = new Card(Suit.Spade, 12, 2, "Q");
        Cards[3] = new Card(Suit.Spade, 11, 1, "J");
        Cards[4] = new Card(Suit.Spade, 10, 0, "T");
        Cards[5] = new Card(Suit.Spade, 9, 0, "9");
        Cards[6] = new Card(Suit.Spade, 8, 0, "8");
        Cards[7] = new Card(Suit.Spade, 7, 0, "7");
        Cards[8] = new Card(Suit.Spade, 6, 0, "6");
        Cards[9] = new Card(Suit.Spade, 5, 0, "5");
        Cards[10] = new Card(Suit.Spade, 4, 0, "4");
        Cards[11] = new Card(Suit.Spade, 3, 0, "3");
        Cards[12] = new Card(Suit.Spade, 2, 0, "2");

        Cards[13] = new Card(Suit.Heart, 14, 4, "A");
        Cards[14] = new Card(Suit.Heart, 13, 3, "K");
        Cards[15] = new Card(Suit.Heart, 12, 2, "Q");
        Cards[16] = new Card(Suit.Heart, 11, 1, "J");
        Cards[17] = new Card(Suit.Heart, 10, 0, "T");
        Cards[18] = new Card(Suit.Heart, 9, 0, "9");
        Cards[19] = new Card(Suit.Heart, 8, 0, "8");
        Cards[20] = new Card(Suit.Heart, 7, 0, "7");
        Cards[21] = new Card(Suit.Heart, 6, 0, "6");
        Cards[22] = new Card(Suit.Heart, 5, 0, "5");
        Cards[23] = new Card(Suit.Heart, 4, 0, "4");
        Cards[24] = new Card(Suit.Heart, 3, 0, "3");
        Cards[25] = new Card(Suit.Heart, 2, 0, "2");

        Cards[26] = new Card(Suit.Diamond, 14, 4, "A");
        Cards[27] = new Card(Suit.Diamond, 13, 3, "K");
        Cards[28] = new Card(Suit.Diamond, 12, 2, "Q");
        Cards[29] = new Card(Suit.Diamond, 11, 1, "J");
        Cards[30] = new Card(Suit.Diamond, 10, 0, "T");
        Cards[31] = new Card(Suit.Diamond, 9, 0, "9");
        Cards[32] = new Card(Suit.Diamond, 8, 0, "8");
        Cards[33] = new Card(Suit.Diamond, 7, 0, "7");
        Cards[34] = new Card(Suit.Diamond, 6, 0, "6");
        Cards[35] = new Card(Suit.Diamond, 5, 0, "5");
        Cards[36] = new Card(Suit.Diamond, 4, 0, "4");
        Cards[37] = new Card(Suit.Diamond, 3, 0, "3");
        Cards[38] = new Card(Suit.Diamond, 2, 0, "2");

        Cards[39] = new Card(Suit.Club, 14, 4, "A");
        Cards[40] = new Card(Suit.Club, 13, 3, "K");
        Cards[41] = new Card(Suit.Club, 12, 2, "Q");
        Cards[42] = new Card(Suit.Club, 11, 1, "J");
        Cards[43] = new Card(Suit.Club, 10, 0, "T");
        Cards[44] = new Card(Suit.Club, 9, 0, "9");
        Cards[45] = new Card(Suit.Club, 8, 0, "8");
        Cards[46] = new Card(Suit.Club, 7, 0, "7");
        Cards[47] = new Card(Suit.Club, 6, 0, "6");
        Cards[48] = new Card(Suit.Club, 5, 0, "5");
        Cards[49] = new Card(Suit.Club, 4, 0, "4");
        Cards[50] = new Card(Suit.Club, 3, 0, "3");
        Cards[51] = new Card(Suit.Club, 2, 0, "2");
    }
    public void Shuffle(Random r)
    {
        for (int n = Cards.Length - 1; n > 0; --n)
        {
            int k = r.Next(n + 1);
            Card temp = Cards[n];
            Cards[n] = Cards[k];
            Cards[k] = temp;
        }
    }
};
4

4 に答える 4

3

列挙型はハッシュに変換できます:

var Suit = {
  Club: 0,
  Diamond: 1,
  Heart: 2,
  Spade: 4
};

Card クラスは非常に単純です。

function Card(suit, value, hcp, text) {
  this.suit = suit;
  this.value = value;
  this.hcp = hcp;
  this.text = text;
}

そしてDeckあまりにも:

function Deck() {
  this.cards = [
    new Card(Suit.Heart, 14, 4 "A"),
    new Card(Suit.Heart, 13, 3 "K"),
    // ...
  ];
}

次に追加shuffleDeckます:

Deck.prototype.shuffle = function() {
  this.cards.sort(function() { return 0.5 - Math.random() });
};
于 2012-07-12T09:14:38.040 に答える
2

JavaScript には、クラスの概念はありません。オブジェクトを作成し、プロパティとメソッドをオブジェクトに直接追加します。

こちらをご覧ください。

于 2012-07-12T09:11:05.603 に答える
1
var Suit = {
    Club: 1,
    Diamond: 2,
    Heart: 3,
    Spade: 4
};
var Card = function (_suit, _value, _HCP, _text) {
    this.suit = _suit;
    this.value = _value;
    this.HCP = _HCP;
    this.text = _text;
};

var myCard = new Card(Suit.Club, 1, 0, "kkkj");

開始するにはhttp://javascript.crockford.com/private.htmlを読むことをお勧めします

于 2012-07-12T09:11:38.017 に答える
0

純粋な JS を使用したもう少し一般的なマッピングを提供しようとします。(JSで伝統的なクラスを真剣にシミュレートしたい場合は、ライブラリを使用する必要があります)

1. Enum
C++:                JS:
enum E{ A,B };      var E = { A:0, B:1 };

2. Class
C++:                JS:
class C{            function C(a,b){
  int a,b;             this.a = a;// When a function is called with new,
  C(int a,int b){      this.b = b;// it will be considered a constructor
    this->a = a;    }             // and the this in it will be a new object.
    this->b = b;                  // Objects are open: any object.name = something
  }                               // is valid and can be read afterwards
  int sum(){        C.prototype.sum = function(){      
    return a + b;     return this.a + this.b; // There are no real classes
  }                 }                         // in JS, only objects
                                              // that were constructed 
}                                             // the same way.
                                              // That is why things shared
                                              // by all instances are put in a
                                              // special place; the prototype
                                              // (methods are shared by all instances
                                              // if you think about it; it is always
                                              // the same function, just different
                                              // implicit this parameter)
3. Use
C++:                               JS:
unique_ptr<C*> c = new C(1,2);     var c = new C(1,2);
c->sum();                          c.sum();

4. Array
C++                   JS:
char arr[32];         var arr = []; // Create empty array
arr[0]=2;             arr[3]  = 4;  // You can safely assign anywhere, the array will
                                    // grow and be filled with undefined if necessary
于 2012-07-12T09:36:24.503 に答える