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
    {
        
        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; 
        String s1 = null, s2 = null; 
        double Score_batter = 1, Score = 0, Pair_Num = 0;
        DialogResult bt;
        public Form1()
        {
            InitializeComponent();
            randomNums = new Random(); 
            prepareCards(); 
        }
        private void prepareCards() 
        {
            int i;
            int row, col; 
            bool isRepeat;
            for (i = 0; i <= 5; i++)
            {
                isRepeat = false;
                do
                {
                    row = randomNums.Next(0, 4); 
                    col = randomNums.Next(0, 13); 
                    for (int j = i - 1; j >= 0; j--)
                    {
                        if (Cards[j].Equals(PicNames[row, col]))
                        {
                            isRepeat = true; break; 
                        }
                        else isRepeat = false;  
                    }
                } while (isRepeat);
                Cards[i] = PicNames[row, col]; Cards[i + 6] = PicNames[row, col]; 
            }
            for (i = 0; i <= 11; i++) 
            {
                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", "");
            
            int index = Int32.Parse(s);
            pb.Image = (Image)Properties.Resources.ResourceManager.GetObject(Cards[index - 1]);
            if (n1 == null) 
            {
                n1 = pb; pb.Enabled = false; 
                s1 = Cards[index - 1]; 
            }
            else 
            {
                n2 = pb; n2.Enabled = false; 
                s2 = Cards[index - 1];
                if (s1 != s2) 
                {
                    System.Threading.Thread.Sleep(500); 
                    n1.Image = Properties.Resources.black_joker; 
                    n2.Image = n1.Image; 
                    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.執行過程影片