【程式設計-C#】記憶遊戲

【設計基礎一:排列圖片、發牌、洗牌】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace plateDealing
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rnd = new Random();
            int[] plateHoles = new int[16];
            for (int i = 0; i <= 7; i++) //將2副牌1~8編號的牌放在0~15編號的位置上
            {
                plateHoles[i] = i + 1;
                plateHoles[i + 8] = i + 1;
            }
            for (int k = 1; k <= 10; k++) //10副牌
            {
                Console.Write("第{0:D2}副牌:", k);
                for (int i = 0; i <= 15; i++) //將每1個位置的牌随機的和任一位置的牌進行交換
                {
                    int rndHole = rnd.Next(0, 15); //任選一個位置,進行牌的交換
                    int temp = plateHoles[i];
                    plateHoles[i] = plateHoles[rndHole];
                    plateHoles[rndHole] = temp;
                }

                for (int i = 0; i <= 15; i++) //印出牌
                {
                    Console.Write(plateHoles[i] + "   ");
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

上面的程式實現以下的想法:

想像你有2副牌,各8張,編號1~8,一開始先把這2副牌順序地放在16個格子(陣列),然後再洗牌 (位置交換的意思)。

參考作法
1.先將2副牌各1~8編號的牌放在16個位置上
2.從第1張牌開始,與任選一個位置的牌交換 (打亂牌,就像洗牌的意思)

排出來的牌,就是每一張圖的位置。

【程式碼】FlipMatchGame

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        //PicNames這個二維陣列用來建立所有卡片在資源裏的名稱,我用二維陣列,列是"花色",行是數字和j~k
        String[,] PicNames = { 
                             { "ace_of_spades", "_2_of_spades", "_3_of_spades", "_4_of_spades", "_5_of_spades", "_6_of_spades", "_7_of_spades", "_8_of_spades", "_9_of_spades", "_10_of_spades", "jack_of_spades", "queen_of_spades", "king_of_spades"}, 
                             { "ace_of_hearts", "_2_of_hearts", "_3_of_hearts", "_4_of_hearts", "_5_of_hearts", "_6_of_hearts", "_7_of_hearts", "_8_of_hearts", "_9_of_hearts", "_10_of_hearts", "jack_of_hearts", "queen_of_hearts", "king_of_hearts"}, 
                             { "ace_of_diamonds", "_2_of_diamonds", "_3_of_diamonds", "_4_of_diamonds", "_5_of_diamonds", "_6_of_diamonds", "_7_of_diamonds", "_8_of_diamonds", "_9_of_diamonds", "_10_of_diamonds", "jack_of_diamonds", "queen_of_diamonds", "king_of_diamonds"}, 
                             { "ace_of_clubs", "_2_of_clubs", "_3_of_clubs", "_4_of_clubs", "_5_of_clubs", "_6_of_clubs", "_7_of_clubs", "_8_of_clubs", "_9_of_clubs", "_10_of_clubs", "jack_of_clubs", "queen_of_clubs", "king_of_clubs"}
                             };
        String[] Cards = {"","","","","","","","","","","",""};
        Random randomNums;

        PictureBox n1 = null, n2 = null; //分別指向第1張與第2張翻牌
        String s1 = null, s2 = null; //記錄翻牌第1張與第2張的文字,用來比較用
        
        public Form1()
        {
            InitializeComponent();
            randomNums = new Random(); //建立亂數物件
            prepareCards(); //洗牌      
        }

        private void prepareCards() //產生12張卡,其中後6張是跟前面6張一樣,再將其重排
        {
            int i;
            int row, col; //撲克牌組,為2維陣列,用這2個變數來存取此牌組陣列的牌
            bool isRepeat;
            for (i = 0; i <= 5; i++)//只抓前6張牌
            {                
                do
                {
                    isRepeat = false; //此變數用來判讀是否抽出的牌有重覆,預設false…,找到重複的牌之後設為true(繼續抽牌…)
                    //下面二行實現隨機抽一張牌的概念
                    row = randomNums.Next(0, 4); //產生0~3的隨機整數
                    col = randomNums.Next(0, 13); //產生0~12的隨機整數
                    //抽牌後,必須檢查此牌是否跟之前抽出的牌有重覆
                    for (int j = i - 1; j >= 0; j--)//看新抓的牌是否和前面的牌有重複
                    {
                        if (Cards[j].Equals(PicNames[row, col]))
                        {
                            isRepeat = true; break; //若發現有重複的牌,設定isRepeat為true,離開for,讓外層的do-while重複,再隨機抽一張牌…
                        }
                    }
                } while (isRepeat);

                //抽好牌之後,把牌放入Cards陣列中
                Cards[i] = PicNames[row, col]; Cards[i+6] = PicNames[row, col]; //一次二張牌
            }

            for (i = 0; i <= 11; i++) //打亂/重排12張牌
            {
                string temp = Cards[i];
                int a = randomNums.Next(0, 12); //任找一位置來進行牌的交換
                Cards[i] = Cards[a];
                Cards[a] = temp;
            }
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;

            String s = pb.Name.Replace("pictureBox", ""); 
            //把元件的名字pictureBox1中的pictureBox去掉,賸下數字的部份,取得1~12的數字字串
            //上面這行的目的是想要知道是那一個PictureBox被點擊了1~12個
            int index = Int32.Parse(s);            
            pb.Image = (Image)Properties.Resources.ResourceManager.GetObject(Cards[index-1]);            

            if (n1 == null) //翻第1張牌的時候
            {
                n1 = pb; pb.Enabled = false; //用n1指向翻出來的牌的pictureBox, 並將該pictureBox設為不作用,以防止再按該pictureBox
                s1 = Cards[index - 1]; //s1字串用來記錄第1張牌的文字
            }
            else //翻第2張牌
            {
                n2 = pb; n2.Enabled = false; //同第1張牌一樣,此部份係處理第2張牌
                s2 = Cards[index - 1];

                if (s1 != s2) //比較翻出來的2張牌,若不一樣,則蓋牌,重新啟動二張卡的作用…
                {
                    System.Threading.Thread.Sleep(1000); //delay1秒,讓玩家可以看出牌被蓋住
                    n1.Image = Properties.Resources.black_joker; //把2張牌再度用鬼牌蓋上
                    n2.Image = n1.Image; //第2張牌也是用鬼牌來蓋上
                    n1.Enabled = true; n2.Enabled = true;//若翻出來的2張牌不一樣,該2張牌蓋上後再設enabled為true
                }
                //n1~n2、s1~s2再設為null,重新進行翻牌判斷
                n1 = null; n2 = null;
                s1 = null; s2 = null;
            }
        }
    }
}