csharp 發表於 2021-4-4 15:59:09

memory game/ card matching code

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

namespace WindowsFormsApp12
{
    public partial class Form1 : Form
    {
      Random rnd = new Random();
      int flippedTimes = 0;

      PictureBox[] temp = new PictureBox;
      PictureBox[] OriginalPic = new PictureBox;
      PictureBox[] P = new PictureBox;
      PictureBox[] Q = new PictureBox;
      PictureBox[] G = new PictureBox;

      int[] isWaiting = new int;
      int[] randomNow = new int;
      static int cardNumber = 12;

      public Form1()
      {
            InitializeComponent();
            OriginalPic = original0;
            OriginalPic = original1;
            OriginalPic = original2;
            OriginalPic = original3;
            OriginalPic = original4;
            OriginalPic = original5;
            P = p0;
            P = p1;
            P = p2;
            P = p3;
            P = p4;
            P = p5;
            Q = q0;
            Q = q1;
            Q = q2;
            Q = q3;
            Q = q4;
            Q = q5;
            G = gameCard0;
            G = gameCard1;
            G = gameCard2;
            G = gameCard3;
            G = gameCard4;
            G = gameCard5;
            G = gameCard6;
            G = gameCard7;
            G = gameCard8;
            G = gameCard9;
            G = gameCard10;
            G = gameCard11;
            temp = temp0;
            temp = temp1;
            for (int i = 0; i < G.Length / 2; i++)
            {

                randomNow = -1;
            }

            for (int i = 0; i < G.Length; i++)
                isWaiting = -1;
      }

      bool isNumberNotExisted(int r, int[] a)
      {
            for (int i = 0; i < a.Length; i++)
                if (r == a)
                  return false;
            return true;
      }

      void numberRandomPicks(int[] b, int n)
      {
            int i = 0;
            while (i < n)
            {
                int x = rnd.Next(0, n);
                if (isNumberNotExisted(x, b))
                {
                  b = x;
                  i++;
                }
            }
      }


      void cardManage(PictureBox[] X, PictureBox[] Y, int[] Z)
      {
            numberRandomPicks(randomNow, cardNumber / 2);
            for (int i = 0; i < cardNumber / 2; i++)
                X.Image = Y].Image;
      }

      private void btnStart_Click(object sender, EventArgs e)
      {

            for (int i = 0; i < G.Length; i++)
            {
                isWaiting = 0;
                G.Image = Properties.Resources.bgcard;
                temp0.Image = null;
                temp1.Image = null;
            }
            for (int i = 0; i < cardNumber / 2; i++) randomNow = -1;
            cardManage(P, OriginalPic, randomNow);
            for (int i = 0; i < cardNumber / 2; i++) randomNow = -1;
            cardManage(Q, OriginalPic, randomNow);
      }

      void flipCard(object sender)
      {

            if (flippedTimes == 2)

            {
                temp.Image = null;
                temp.Image = null;
                timer1.Start();
                flippedTimes = 0;
            }

      }

      private void gameCard0_Click(object sender, EventArgs e)
      {
            if (isWaiting == 0)
            {
                isWaiting = 1;
                G.Image = p0.Image;
                flippedTimes++;

                if (temp.Image != null) temp.Image = G.Image;
                else temp.Image = G.Image;

                if (flippedTimes == 2)
                  timer1.Start();

            }
      }

      private void gameCard1_Click(object sender, EventArgs e)
      {
            if (isWaiting == 0)
            {
                isWaiting = 1; flippedTimes++;
                gameCard1.Image = p1.Image;
                if (temp.Image != null) temp.Image = G.Image;
                else temp.Image = G.Image;
                if (flippedTimes == 2)
                  timer1.Start();
            }
      }


      private void gameCard2_Click(object sender, EventArgs e)
      {
            if (isWaiting == 0)
            {
                isWaiting = 1; flippedTimes++;
                gameCard2.Image = p2.Image;
                if (temp.Image != null) temp.Image = G.Image;
                else temp.Image = G.Image;
                if (flippedTimes == 2)
                  timer1.Start();
            }
      }

      private void gameCard3_Click(object sender, EventArgs e)
      {
            if (isWaiting == 0)
            {
                isWaiting = 1; flippedTimes++;
                gameCard3.Image = p3.Image;
                if (temp.Image != null) temp.Image = G.Image;
                else temp.Image = G.Image;
                if (flippedTimes == 2)
                  timer1.Start();
            }
      }

      private void gameCard4_Click(object sender, EventArgs e)
      {
            if (isWaiting == 0)
            {
                isWaiting = 1; flippedTimes++;
                gameCard4.Image = p4.Image;
                if (temp.Image != null) temp.Image = G.Image;
                else temp.Image = G.Image;
                if (flippedTimes == 2)
                  timer1.Start();
            }
      }
      private void gameCard5_Click(object sender, EventArgs e)
      {
            if (isWaiting == 0)
            {
                isWaiting = 1; flippedTimes++;
                gameCard5.Image = q0.Image;
                if (temp.Image != null) temp.Image = G.Image;
                else temp.Image = G.Image;
                if (flippedTimes == 2)
                  timer1.Start();
            }
      }

      private void gameCard6_Click(object sender, EventArgs e)
      {
            if (isWaiting == 0)
            {
                isWaiting = 1; flippedTimes++;
                gameCard6.Image = q1.Image;
                if (temp.Image != null) temp.Image = G.Image;
                else temp.Image = G.Image;
                if (flippedTimes == 2)
                  timer1.Start();
            }
      }

      private void gameCard7_Click(object sender, EventArgs e)
      {
            if (isWaiting == 0)
            {
                isWaiting = 1; flippedTimes++;
                gameCard7.Image = q2.Image;
                if (temp.Image != null) temp.Image = G.Image;
                else temp.Image = G.Image;
                if (flippedTimes == 2)
                  timer1.Start();
            }
      }

      private void gameCard8_Click(object sender, EventArgs e)
      {
            if (isWaiting == 0)
            {
                isWaiting = 1; flippedTimes++;
                gameCard8.Image = q3.Image;
                if (temp.Image != null) temp.Image = G.Image;
                else temp.Image = G.Image;
                if (flippedTimes == 2)
                  timer1.Start();
            }
      }


      private void gameCard9_Click(object sender, EventArgs e)
      {
            if (isWaiting == 0)
            {
                isWaiting = 1; flippedTimes++;
                gameCard9.Image = q4.Image;
                if (temp.Image != null) temp.Image = G.Image;
                else temp.Image = G.Image;
                if (flippedTimes == 2)
                  timer1.Start();
            }
      }

      private bool isOver()
      {
            for (int i = 0; i < G.Length; i++)
                if (isWaiting != 2)
                {
                  return false;

                }
            return true;
      }
      private void timer1_Tick(object sender, EventArgs e)
      {
            if (flippedTimes == 2)
            {
                if (temp0.Image == temp1.Image)
                {
                  for (int i = 0; i < G.Length; i++)
                        if (isWaiting == 1) isWaiting = 2;
                }
                else if (temp0.Image != temp1.Image)
                  for (int j = 0; j < G.Length; j++)
                  {
                        if (isWaiting == 1)
                        {
                            G.Image = Properties.Resources.bgcard;
                            isWaiting = 0;
                        }
                  }
                temp.Image = null;
                temp.Image = null;
                if (isOver()) btnStart.Text = "PLAY AGAIN?";
                else btnStart.Text = "RESET";
                flippedTimes = 0;
            }


            timer1.Stop();
      }

      private void gameCard10_Click(object sender, EventArgs e)
      {
            if (isWaiting == 0)
            {
                isWaiting = 1; flippedTimes++;
                gameCard10.Image = p5.Image;
                if (temp.Image != null) temp.Image = G.Image;
                else temp.Image = G.Image;
                if (flippedTimes == 2)
                  timer1.Start();
            }
      }

      private void gameCard11_Click(object sender, EventArgs e)
      {
            if (isWaiting == 0)
            {
                isWaiting = 1; flippedTimes++;
                gameCard11.Image = q5.Image;
                if (temp.Image != null) temp.Image = G.Image;
                else temp.Image = G.Image;
                if (flippedTimes == 2)
                  timer1.Start();
            }
      }
    }
}

csharp 發表於 2021-4-4 16:00:12

http://www.youtube.com/watch?v=qX7qUJPWgVQ
頁: [1]
查看完整版本: memory game/ card matching code