これまではジョーカーは使わず52枚のカードを使ってきましたが、正式なルールではジョーカー2枚を含むトランプ1組・54枚を使用します。それから合成数出しにおける指数表記を可能にします。そのために既存のコードを修正していたら大幅な書き換えになってしまいました。そこでもう一度全体を書き直すことにします。
完成 合成数出しにおける指数表記にも対応 C#で素数大富豪をつくる(11)
Contents
ジョーカーを表示させる
Cardクラスにジョーカーをイメージを取得するためのメソッドを追加します。ジョーカーのBitmapはProperties.Resources.cardimageのXY座標(0, 2400)、幅360、高さ500で取得できるのでGetJokerImageメソッドで取得します。
ジョーカーは1枚出しのときは最強のカード、革命がおきているときも最強のカードです。また複数枚数出すときは0~13のどのカードにも使えます。
またプレイヤーがジョーカーを出すときはそれはどの数の代わりになるのかを示さなければなりません。そのためのコントロールを作ります。
まずジョーカーの表示を可能にするためにSuit列挙体にJokerを追加します。
| 1 2 3 4 5 6 7 8 | public enum Suit {     Spade,     Club,     Diamond,     Heart,     Joker, } | 
Cardクラスにジョーカーのイメージを取得するメソッドを追加し、GetImageメソッドを修正します。それからカードの番号は変更されないのでこれまでNumberプロパティにsetはありませんでしたが、ジョーカーを使うときに0~13までの値を設定できるようにジョーカーの場合だけNumberByJokerプロパティでNumberプロパティを変更できるようにします。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | public class Card {     static Bitmap CardImageAll = Properties.Resources.cardimage;     public Card(Suit suit, int number, int width, int height)     {         Suit = suit;         Number = number;         Size = new Size(width, height);     }     public int Number     {         get;         private set;     }     public int NumberByJoker     {         set         {             if (Suit == Suit.Joker)                 Number = value;         }     }     Bitmap GetImage(Suit suit, int number)     {         if (suit == Suit.Joker)             return GetJokerImage();         int y = 0;         if (suit == Suit.Club)             y = 0;         if (suit == Suit.Diamond)             y = 600;         if (suit == Suit.Heart)             y = 1200;         if (suit == Suit.Spade)             y = 1800;         Bitmap bitmap = new Bitmap(360, 500);         Graphics graphics = Graphics.FromImage(bitmap);         graphics.DrawImage(             CardImageAll,             new Rectangle(0, 0, 360, 500),             new Rectangle(360 * (number - 1) + 20 * (number - 1), y, 360, 500),             GraphicsUnit.Pixel);         graphics.Dispose();         return bitmap;     }     Bitmap GetJokerImage()     {         Bitmap bitmap = new Bitmap(360, 500);         Graphics graphics = Graphics.FromImage(bitmap);         graphics.DrawImage(             CardImageAll,             new Rectangle(0, 0, 360, 500),             new Rectangle(0, 2400, 360, 500),             GraphicsUnit.Pixel);         graphics.Dispose();         return bitmap;     } } | 
JokerControlクラス
プレイヤーがジョーカーを出すときはそれはどの数の代わりになるのかを示さなければなりません。そのためのコントロールを作ります。

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public partial class JokerControl : UserControl {     public JokerControl()     {         InitializeComponent();     }     public event EventHandler ClickRun;     public event EventHandler ClickCancel;     private void ButtonOK_Click(object sender, EventArgs e)     {         ClickRun?.Invoke(this, new EventArgs());     }     private void ButtonCancel_Click(object sender, EventArgs e)     {         ClickCancel?.Invoke(this, new EventArgs());     }     Card _card = null;     public Card Card     {         set { _card = value; }         get { return _card; }     }     public int Number     {         set { numericUpDown1.Value = value; }         get { return (int)numericUpDown1.Value; }     }     public void Reset()     {         _card = null;         Number = 0;     } } | 
