Games-Cards-1.45 > Games::Cards
Games-Cards-1.45

名前

Games::Cards -- Perl module for writing and playing card games

Games::Cards -- カードゲームを描画し、遊ぶためのPerlモジュール

概要

    use Games::Cards;
    my $Rummy = new Games::Cards::Game;

    # ラミーのための正しいデッキを生成
    my $Deck = new Games::Cards::Deck ($Rummy, "Deck");

    # デッキをシャッフルし、札捨て場を生成
    $Deck->shuffle;
    my $Discard = new Games::Cards::Queue "Discard Pile";

    # カードを配る
    foreach my $i (1 .. 3) {
        my $hand = new Games::Cards::Hand "Player $i" ;
        $Deck->give_cards($hand, 7);
        $hand->sort_by_value;
        push @Hands, $hand;
    }

    # 手札の表示 (例: "Player 1: AS  2C  3C  3H 10D  QS  KH")
    foreach (@Hands) { print ($_->print("short"), "\n") }

    $Hands[1]->give_a_card ($Discard, "8D"); # ダイヤの8を捨てる

解説

This module creates objects and methods to allow easier programming of card games in Perl. It allows you to do things like create decks of cards, have piles of cards, hands, and other sets of cards, turn cards face-up or face-down, and move cards from one set to another. Which is pretty much all you need for most card games.

このモジュールは、Perlでカードゲームのプログラミングを容易にしてくれる オブジェクトとメソッドを生成する。このモジュールを使えば、カード一揃いの生成、 カードのやま・手札・その他のカードの組を持つこと、カードをめくること、 一方から他方の組へとカードを移すことなどが可能になる。これは、大概の カードゲームであなたが必要とすることだ。

Sub-packages include:

サブパッケージには以下のものが含まれる:

Games::Cards::Undo

This package handles undoing and redoing moves (important for solitaire).

このパッケージはやり直し操作と繰り返し操作を行う(ソリティアには重要だ)。

Games::Cards::Tk

This package allows you to write games that use a Tk graphical interface. It's designed so that it will be relatively easy to write a game that uses i<either> a GUI or a simple text interface, depending on the player's circumstances (availability of Tk, suspicious boss, etc.). See Games::Cards::Tk for more details on writing Tk games.

このパッケージを使うとTkインターフェースを利用してゲームを描画することができる。 プレーヤーの環境(Tkが利用できるか、疑い深い上司がいるか、等々)に応じて、 GUIあるいは単純なテキストインターフェースのどちらかを使って比較的簡単に ゲームを描画できるように設計されている。Tkゲームの描画に関する詳細は Games::Cards::Tkを参照して欲しい。

クラスの概観

A GC::Game stores information like what cards are in the starting deck, plus pointers to the various Cards and CardSets.

GC::Game(Games::Cards::Game)は開始時のデッキにどんなカードが含まれているのか といった情報を保持し、様々なCardとCardSetへのポインタを付け加える。

A GC::Card represents one playing card. Every Card must belong to one (and only one) CardSet at every point during the game.

GC::Cardは一枚のプレイ中のカードを表す。全てのCardはゲーム通じて 一組の(そして唯一の)CardSetに属さねばならない。

A GC::CardSet is mostly just a set of GC::Cards. A CardSet has a unique name. Many also have short nicknames, which make it easier to write games that move cards between the sets. (See Klondike or FreeCell, for example.)

GC::CardSetはGC::Cardの一組だ。CardSetは一意の名前を持つ。多くは短い ニックネームも持っている。カードの組をまたいで移動するゲームをつくる のを簡単にしてくれる(例えば、クロンダイクやフリーセルをみよ)。

There are many sorts of CardSet. The basic differentiation is Piles, for which you only access the top or bottom card (or cards) and Hands, where you might access any one of the cards in the Hand. Piles are broken up into Stacks and Queues, and every game starts with a Deck of cards (or more than one).

たくさんの種類のCardSetがある。基本的な違いは、一番上あるいは下からカードを とるための「やま」(Pile)と、任意のカードを一枚引くための「手札」(Hand)だ。 PilesはStacksとQueuesに分かれる。そしてあらゆるゲームは(一枚以上の)カードの 一揃い(Deck)から始まる。

Games::Cards::Game

This class represents a certain game, like War, or Solitaire. This is necessary to store the various rules for a given game, like the ranking of the cards. (Or, for more exotic games, how many cards of what type are in the deck.) Methods:

このクラスは戦争(War)とかソリティア(Solitaire)といった、ある一つのゲームを 表す。これはゲームのための様々なルールを保持するのに必要である(あるいは、 よりエキゾチックなゲームの場合には、そのデックの中にどんな種類のカードが 何枚入っているのかなど)。メソッド:

current_game

Returns the current Game object. In almost every case, you'll only be working with one at a time.

現在のGameオブジェクトを返す。ほとんど全ての場合において、一度に ひとつだけ動作させるだろう。

set_current_game(GAME)

In theory, these subs let you handle multiple Games at once, as long as you set_current_game to the right one. Note that Game->new automatically sets the current Game to be that game, so in 99% of cases, you won't have to call set_current_game.

理論的には、set_current_gameを正しく使う限り、このサブルーチンは一度に 複数のGamesを操作できる。だが注意して欲しいのは、Game->newは自動的に そのゲームを現在のGameにセットするということだ。 だから九分九厘、あなたがset_current_gameを呼ぶことはないだろう。

new(HASHREF)

creates a new game. HASHREF is a reference to a hash containing zero or more of the keys "suits" and "cards_in_suit". "suits" is a list of the suits in a deck, "cards_in_suit" is a reference to a hash whose keys are the names of the cards in one suit and whose values are the values (or ranks) of those cards. If "suits" is not given, the default suits (Clubs, Diamonds, Hearts, Spades) are used. If "cards_in_suit" is not given, the default cards (Ace, 2..10, Jack, Queen, King with values 1..13) are used. For example, war would require "Ace"=>14.

新しいゲームを生成。HASHREFは0個以上のキー"suits"と"cards_in_suit"を含んだ ハッシュへのリファレンスだ。"suits"はデッキに含まれるスート(種類)のリスト。 "cards_in_suit"はスートに属するカードの名前がキーとなるハッシュへの リファレンスで、ハッシュの値はカードの値(ランク)である。 もし"suit"が与えられない場合、デフォルトのスート(Clubs、 Diamonds、 Hearts、Spades)が利用される。もし"cards_in_suit"が与えられない場合は、 デフォルトのカードは(Ace、2..10、Jack、Queen、Kingでその値は1〜13)が 使われる。例えば、戦争というゲームは"Ace"=>14が必要となる。

get_cardset_by_name(NAME)

Returns the CardSet with name NAME

NAMEという名前を持ったCardSetを返す。

get_cardset_by_nickname(NAME)

Returns the CardSet with nickname NAME

NAMEという名前のnicknameを持ったCardSetを返す。

get_card_by_truename(NAME)

Returns the Card with truename NAME

NAMEという真の名前(truename)をもったCardを返す。

Games::Cards::Deck

A deck is a deck of cards. The number of cards and identities of the cards in the deck depend on the particular Game for which the deck is used.

デッキはカードの一揃いのことだ。デッキ中のカードの枚数とカードの特徴は、その デッキが利用されるゲームに依存する。

new (GAME, NAME)

creates an unshuffled deck of cards. For each card in the deck it creates a name, suit, value, and suit value. GAME is the GC::Game this deck belongs to, and it stipulates the number of cards in the deck, etc. NAME is the name to give the deck, e.g. "Deck".

シャッフルされていないデッキを生成。デッキの中のそれぞれのカードのために、 名前、スート、値、そしてスートの値が生成される。GAMEはこのデッキが属する GC::Gameであり、その中に含まれるカードの番号その他を規定する。NAMEは与えられた デッキの名前だ。例えば、"Deck"。

Games::Cards::Queue

A Queue (cf. computer science terminology, or the C++ stdlib) is a first-in first-out pile of cards. Cards are removed from the top of the pile, but new cards are added to the bottom of the pile. This might represent, say, a pile of face-down cards, like the player's hand in War.

Queue(cf. コンピュータサイエンス、C++ stdlib)は先入先出型のカードの山だ。 カードは山の上から取り除かれ、新しいカードは山の下に付け加えられる。これは つまり、裏向きのカードの山で、Warでのプレーヤーの手札のようなものだ。

Class Games::Cards::Stack

A stack (cf. computer science terminology, or the C++ stdlib) is a last-in first-out pile of cards. Cards are removed from the top of the pile, and new cards are also added to the top of the pile. This would usually represent a pile of cards with its top card (and perhaps all cards) face up.

Stack(cf. コンピュータサイエンス、C++ stdlib)は後入先出型のカードの山だ。 カードは山の上から取り除かれ、新しいカードも山の上に付け加えられる。これは通常、 一番上のカードが(そしてたぶん全部のカードが)表向きになっている山を表すだろう。

Games::Cards::Pile

A Pile is a pile of cards. That is, it is a CardSet where we will only access the beginning or end of the set. (This may include the first N cards in the set, but we will never reference the 17'th card.) This is a super class of Queue and Stack, and those classes should be used instead, so that we know whether the cards in the pile are FIFO or LIFO. Methods:

Pileはカードの山だ。それはつまり、カードの組の最初と最後にだけアクセスするで あろうCardSetということだ(組の最初のN枚のカードも含むかもしれない。だが17番目 のカード、というような参照はしない)。これはQueueとStackのスーパークラスであり、 むしろそちらのクラスを使うべきである。だから、山になっているカードが FIFO(先入先出)なのかLIFO(後入先出)なのかを知るべきだ。

give_cards(RECEIVER, NUMBER)

Transfers NUMBER cards from the donor (the object on which this method was called) to the CardSet RECEIVER. This method can used for dealing cards from a deck, giving cards to another player (Go Fish), putting cards on the table (War), or transferring a card or cards between piles in solitaire.

NUMBER枚のカードを、渡し手(このメソッドを呼び出すオブジェクト)からCardSet オブジェクトRECEIVERへ移動する。このメソッドはデッキからカードを配ったり、 別のプレーヤーにカードを渡したり(魚釣り)、カードをテーブルに置いたり(戦争)、 あるいはソリティア系で札置き場から札置き場へとカードを移動させたりするのに 利用できる。

If NUMBER is "all", then the donor gives all of its cards.

もしNUMBERが"all"だったら、渡し手は持っている全てのカードを渡す。

Returns 1 usually. If the donor has too few cards, it returns 0 and does not transfer any cards.

通常は1を返す。もし渡し手が十分なカードを持っていない場合は、0を返し、 カードの移動は行われない。

top_card

Returns the top Card in the CardSet (or 0 if CardSet is empty)

CardSetの一番上のCardを返す(CardSetが空のときは0)。

Games::Cards::Hand

A Hand represents a player's hand. Most significantly, it's a CardSet which is different from a Pile because the Cards in it are unordered. We may reference any part of the CardSet, not just the top or bottom. Methods:

Handはプレーヤーの手を表す。最も重要なのはPileとは違うCardSetであるという ことだ。なぜなら、この中のCardは順番づけられていないからだ。一番上や一番下だけ でなく、CardSetのどの部分でも参照することができる。メソッド:

give_a_card(RECEIVER, DESCRIPTION)

Transfers Card described by DESCRIPTION from the donor (the Hand on which this method was called) to the CardSet RECEIVER. This method can used for discarding a card from a hand, e.g.

DESCRIPTIONで説明されるCardを、渡し手(このメソッドを呼び出すHand)から RECEIVERという名のCardSetへ移動する。

If DESCRIPTION matches /^-?\d+$/, then it is the index in the cards array of the Card to give. Otherwise, DESCRIPTION is passed to Hand::index.

DESCRIPTIONが正規表現 /^-?\d+$/ にマッチした時は、与えられたCardの配列の インデックスになる。さもなければ、DESCRIPTIONはHand::indexに渡される。

Returns 1 usually. If the donor does not have the card, it returns 0 and does not transfer anything.

通常は1を返す。もし渡し手がカードを持っていない場合は0を返し、移動は行われない。

move_card(DESCRIPTION, INDEX)

Rearrange a Hand by putting Card described by DESCRIPTION at index INDEX.

DESCRIPTIONで説明されたカードをINDEXという名のインデックスに置いてHandを 再配置する。

If DESCRIPTION matches /^-?\d+$/, then it is the index in the cards array of the Card to give. Otherwise, DESCRIPTION is passed to Hand::index.

DESCRIPTIONが正規表現 /^-?\d+$/ にマッチした時は、与えられたCardの配列の インデックスになる。さもなければ、DESCRIPTIONはHand::indexに渡される。

Returns 1 usually. If the donor does not have the card, it returns 0 and does not transfer anything.

通常は1を返す。もし渡し手がカードを持っていない場合は0を返し、移動は行われない。

index(DESCRIPTION)

Given a card description DESCRIPTION return the index of that Card in the Hand, or undef if it was not found. DESCRIPTION may be a Card or a string (like "8H" or "KC").

カードの説明DESCRIPTIONを与えるとHandにあるCardのインデックスを返す。カードが 見つからないときはundefを返す。DESCRIPTIONはCardか、文字("8H"とか"KC")。

Games::Cards::CardSet

A CardSet is just an array of cards (stored in the "cards" field). It could be a player's hand, a deck, or a discard pile, for instance. This is a super class of a number of other classes, and those subclasses should be used instead.

CardSetは単なるカードの配列だ("cards"フィールドに収められている)。CardSetは 例えば、プレーヤーの手札、デッキ、札捨て場などにすることができる。これは、 他の多くのクラスのスーパークラスであるので、代わりにそれらサブクラスを 利用するべきだ。

new(GAME, NAME, NICKNAME)

create a new (empty) CardSet. GAME is the Game object that this set belongs to. NAME is a unique string that e.g. can be output when you print the CardSet. Optionally, NICKNAME is a (unique!) short name that will be used to reference the set.

新規の(空)CardSetを生成。GAMEはこのセットが属すGameオブジェクト。NAMEは一意な 文字列であり、例えばCardSetをプリントする際のアウトプットになりうる。オプション として、NICKNAMEはセットを参照するために用いられる(一意な!)短い名前である。

shuffle

shuffles the cards in the CardSet. Shuffling is not undoable.

CardSetのカードを切る。シャッフルはやり直し(undo)できない。

sort_by_value

Sorts the Set by value. This and other sort routines will probably be used mostly on Hands, which are "ordered sets", but you might want to reorder a deck or something. Sorting is not undoable.

CardSetを値でソートする。おそらく、これや他のソートルーチンを順序付け られているCardSetであるHandに対して用いることがあるだろう。しかしあなたは デッキや何かを再度順序付けたいかもしれない。ソートはやり直しできない。

sort_by_suit

Sorts the Set by suit, but not by value within the suit.

CardSetをスートでソートする。スート内の値でではない。

sort_by_suit_and_value

Sorts the Set by suit, then by value within the suit.

CardSetをまずスートで、その後にスート内の値でソートする。

clone(GAME, NAME, NICKNAME)

Create a copy of this CardSet. That is, create an object with the same class as arg0. Then make a copy of each Card in the CardSet (true copy, not a reference). arg1 is the Game that the set belongs to. arg2 is the name to give the new CardSet. arg3 (optional) is the nickname.

CardSetのコピーを生成する。つまり、arg0[訳補足:暗黙で渡される引数]と同じ クラスのオブジェクトを生成する。それからCardSetに含まれる全てのCardのコピーを つくる(本当のコピーであって、リファレンスではない)。arg1はセットが属する Game。arg2は新しいCardSetに与える名前。arg3(省略可)はニックネーム。

face_down

Makes a whole CardSet face down

CardSet全体を裏にする。

face_up

Makes a whole CardSet face up

CardSet全体を表にする。

print(LENGTH)

Returns a string containing a printout of the Cards in the CardSet. Prints a long printout if LENGTH is "long", short if "short" (or nothing). The CardSet is printed out in reverse order, so that the top card of the set is printed first.

CardSetに含まれるCardの出力情報を含む文字列を返す。もしLENGTHが"long"なら長い 出力情報で、"short"(あるいは指定無し)なら短い。CardSetは逆順でプリントアウト される。つまりCardSetの一番上のカードからプリントされる。

name

Returns the name of the Set

CardSetの名前を返す。

nickname

Returns the nickname of the Set (or undef if there is none)

CardSetのニックネームを返す(名前がなければundefを返す)。

cards

Returns a reference to the array of Cards in the set

CardSet内にあるCardの配列リファレンスを返す。

size

Tells how many cards are in the set

CardSetの中に何枚カードがあるかを教える。

Games::Cards::Card

A Card is a playing card. Methods:

Cardはゲーム中に使われるカードだ。メソッド:

new(GAME, HASHREF)

creates a new card. GAME is the Game this card is being created in. HASHREF references a hash with keys "suit" and "name".

新しいカードを生成。GAMEはこのカードがつくられるGame。HASHREFは"suit"と"name" というキーを持ったハッシュを参照する。

clone(GAME)

makes a copy of the Card (not just a reference to it).

Cardのコピーを作る(Cardへのリファレンスだけではない)。

print(LENGTH)

returns a string with the whole card name ("King of Hearts") if LENGTH is "long", otherwise a short version ("KH").

LENGTHが"long"ならカード全体の名前(例えば"King of Hearts")を持つ文字列を 返す。それ以外なら短い名前("KH")を返す。

truename

Gives a unique description of this card, i.e., you're guaranteed that no other card in the Game will have the same description.

このカードの一意な説明を与える。つまり、Game内のどのカードも同じ説明を持た ないことが保証される。

name(LENGTH)

prints the name of the card. The full name ("King") if LENGTH is "long"; otherwise a short version ("K");

そのカードの名前を出力する。LENGTHが"long"ならフルネーム("King")が、 そうでなければ短縮形("K")が出力される。

suit(LENGTH)

Returns the suit of the card. Returns the long version ("Diamonds") if LENGTH is "long", else a short version ("D").

カードのスートを返す。LENGTHが"long"なら長い形("Diamonds")で、それ以外は 短縮形("D")が返される。

color

Is the card "red" or "black"? Returns the color or undef for unknown color.

カードは赤か黒か?その色を返す。不明な色はundefが返る。

value

Calculates the value of a card

カードの値を計算。

suit_value

Returns the suit_value of a card (1..number of suits)

カードのsuit_value(スートの1..number)を返す。

is_face_up

Returns true if a card is face up

カードが表向きなら真を返す。

is_face_down

Returns true if a card is face down

カードが裏向きなら真を返す。

face_up

Makes a card face up

カードを表向きにする。

face_down

Makes a card face down

カードを裏向きにする。

owning_cardset

Returns the CardSet which this Card is a part of

このCardが含まれるCardSetを返す。

set_owning_cardset(SET_OR_NAME)

Makes the Card a part of a CardSet. Arg0 is either an actual CardSet ref, or the name of a CardSet.

そのCardをCardSetの一部に含める。arg0は実際のCardSetのリファレンスか、 CardSetの名前。

An implementation of Klondike (aka standard solitaire) demonstrates how to use this module in a simple game. Other card game examples are included as well. The Games::Cards README should list them all.

クロンダイク(標準的なソリティアとも呼ばれる)の実装を使って、 簡単なゲームでどのようにこのモジュールを使うかのデモンストレーションを行う。 他のカードゲームの例も含まれている。Games::CardsモジュールのREADMEにそれらの 一覧がある。

[訳補足:CPANモジュールやPPMでインストールした場合、 Tkを利用する例は含まれていないようです。tarballには含まれています。 http://search.cpan.org/src/AKARGER/Games-Cards-1.45/script/freecell.tk http://search.cpan.org/src/AKARGER/Games-Cards-1.45/script/klondike.tk ]

注意

いかにしてゲームを描画するか

So you want to write a card game using Games::Cards (or even Games::Cards::Tk)? Great! That's what the module is for. Here are some tips about how to write a game.

さて、Cames::Cards(あるいはGames::Cards::Tk)を使ってカードゲームを 描写したいって?素晴らしい!これはそのためのモジュールだ。 どうやってゲームを描画するかについていくつかの小技を示そう。

コードを盗む

Laziness applies in Games::Cards just like in the rest of Perl. It will be much easier if you start with an existing game.

Perlがそうであるように、[訳補足:三大美徳の一つ]怠惰をGames::Cardsにも 適用しよう。既存のゲームを持っているなら簡単だ。

Stack 対 Queue

Think carefully about whether the Piles in your game are Stacks (LIFO) or Queues (FIFO). As a general rule, piles of cards that are usually face down will be Stacks; face up will be Queues. CardSets where you want to access specific cards (i.e., not just the first or last) will be Hands.

あなたのゲームに登場するカードの山(Pile)がスタック(LIFO)なのか キュー(FIFO)なのか、慎重に考えてみよう。一般的なルールとして、いつも裏向きに なっているカードの山はスタックであるだろう。表向きならキューだ。特定のカード (つまり一番最初か最後だけじゃなく)にアクセスするようなCardSetならHandだ。

GUIゲーム

I've tried to design Games::Cards::Tk and the games that use it so that the Tk game is very similar to the text game. This allows the most code reuse. GUI games may involve clicking, dragging, and a different way to display the game; but the underlying game is still the same. Also note that serious timewasters may prefer to use the keyboard to play GUI games. See Games::Cards::Tk for more details.

私はGames::Cards::Tkの設計を試みてきた。そしてこれを使ったTkゲームは テキスト版のゲームと非常によく似ている。だからほとんどのコードは再利用が できる。GUIゲームはクリックやドラッグ、そしてゲームの表示の違いといったことを 伴う。だが基礎にあるゲームはまだ同じだ。また、キーボードを使って遊ぶGUIゲーム の方が極度のゲーム好きには好まれることにも注意して欲しい。詳細は Games::Cards::Tkを参照のこと。

PublicとPrivate

This module contains a bunch of methods. The public methods are documented here. That means any method not documented here is probably private, which means you shouldn't call it directly.

このモジュールには一群のメソッドが含まれている。パブリックメソッドについては この文書で取り上げた。ということは、ここであげられていないメソッドは、 プライベートメソッドであるかもしれないということだ。これはあなたが 直接呼び出すべきものではない。

There are also a bunch of classes. Most private classes are not documented here. A couple private classes are mentioned, since they have methods which public classes inherit. In that case, the privateness is mentioned.

クラスも多くある。ほとんどのプライベートクラスはドキュメントに含めていない。 二つのプライベートクラスには言及している。なぜならそれらのメソッドは パブリッククラスから継承しているからだ。そういう場合はプライベートであることに 言及してある。

TODO

See the TODO file in the distribution

配布物のTODOファイルを見よ。

Not TODO

Computer AI and GUI display were left as exercises for the reader. Then Michael Houghton wrote a Tk card game, so I guess the readers are doing their exercises.

コンピューターAIとGUIディスプレイは読者への課題として残した。Michael Houghtonが Tkカードゲームを書いたので、私は読者が課題に取り組むのではないかと思っている。

バグ

You betcha. It's still alpha.

そりゃもちろん。これはまだアルファ版だし。

test.pl doesn't work with MacPerl, because it uses backticks. However, (as far as I know) the games released with Games::Cards do work.

test.plはMacPerlでは動かない。これはバックスラッシュを使っているためだ。 だが(私の知る限り)Games::Cardsを使ったゲームは動作している。

作者

Amir Karger

Andy Bach wrote a Free Cell port, and gets points for the first submitted game, plus some neat ideas like CardSet::clone.

Andy Bachはフリーセルの報告を書き、最初に実行されたゲームの得点を得た。 それからCardSet::cloneのような巧いアイディアを付け加えてくれた。

Michael Houghton herveus@Radix.Net wrote the initial Tk Free Cell (two days after Andy submitted his Free Cell!) I changed almost all of the code eventually, to fit in with Games::Cards::Tk, but he gave me the initial push (and code to steal).

Michael Houghton herveus@Radix.Net は最初のTk版フリーセルを書いた(二日後に Andyが彼のフリーセルを実行した!)。結局、Games::Cards::Tkに合うように私は コードのほとんど全てを変更した。だが彼は私に最初の励まし (と盗むためのコード)を与えてくれた。

コピーライト

Copyright (c) 1999-2000 Amir Karger

ライセンス

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

perl(1), Tk(1)