05週作業 翻翻樂記憶遊戲

1.程式碼

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張的文字,用來比較用
        double Score_batter = 1, Score = 0, Pair_Num = 0;
        DialogResult bt;
        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張牌
            {
                isRepeat = false;
                do
                {
                    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,離開
                        }
                        else isRepeat = false;  //若無發現重複的牌,設定isRepeat為false
                    }
                } while (isRepeat);
                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的數字字串
            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(500); //delay1秒,讓玩家可以看出牌被蓋住
                    n1.Image = Properties.Resources.black_joker; //把2張牌再度用鬼牌蓋上
                    n2.Image = n1.Image; //第2張牌也是用鬼牌來蓋上
                    n1.Enabled = true; n2.Enabled = true;
                    n1 = null; n2 = null;
                    s1 = null; s2 = null;
                    Score -= 60;
                    Score_lb.Text = Score.ToString();
                    Score_batter = 1;
                }
                else //若牌一樣的話
                {
                    n1 = null; n2 = null;
                    s1 = null; s2 = null;
                    Score += 100 * Score_batter;
                    Score_lb.Text = Score.ToString();
                    Pair_Num++;
                    Score_batter += 0.2;
                }
                if (Pair_Num == 6) bt = MessageBox.Show("還要再玩一次嗎?", "遊戲結束", MessageBoxButtons.OK, MessageBoxIcon.Question);
                if (bt == DialogResult.OK) Game_again();
            }
        }

        private void Game_again() //重新開始副程式
        {
            for (int i = 0; i < 12; i++) Cards[i] = ""; //清楚字元陣列
            prepareCards(); //洗牌
            n1 = null; n2 = null;
            s1 = null; s2 = null;
            Score = 0; Pair_Num = 0;
            Score_lb.Text = Score.ToString(); //將分數歸零
            bt = DialogResult.Yes;
        }

    }
}

2.流程圖

3.程式結果

4.執行過程影片

results matching ""

    No results matching ""