code.club

 找回密碼
 立即註冊
搜索
查看: 4452|回復: 1

memory game/ card matching code

[複製鏈接]
發表於 2021-4-4 15:59:09 | 顯示全部樓層 |閱讀模式
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;

  10. namespace WindowsFormsApp12
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         Random rnd = new Random();
  15.         int flippedTimes = 0;

  16.         PictureBox[] temp = new PictureBox[2];
  17.         PictureBox[] OriginalPic = new PictureBox[cardNumber / 2];
  18.         PictureBox[] P = new PictureBox[cardNumber / 2];
  19.         PictureBox[] Q = new PictureBox[cardNumber / 2];
  20.         PictureBox[] G = new PictureBox[cardNumber];

  21.         int[] isWaiting = new int[cardNumber];
  22.         int[] randomNow = new int[cardNumber / 2];
  23.         static int cardNumber = 12;

  24.         public Form1()
  25.         {
  26.             InitializeComponent();
  27.             OriginalPic[0] = original0;
  28.             OriginalPic[1] = original1;
  29.             OriginalPic[2] = original2;
  30.             OriginalPic[3] = original3;
  31.             OriginalPic[4] = original4;
  32.             OriginalPic[5] = original5;
  33.             P[0] = p0;
  34.             P[1] = p1;
  35.             P[2] = p2;
  36.             P[3] = p3;
  37.             P[4] = p4;
  38.             P[5] = p5;
  39.             Q[0] = q0;
  40.             Q[1] = q1;
  41.             Q[2] = q2;
  42.             Q[3] = q3;
  43.             Q[4] = q4;
  44.             Q[5] = q5;
  45.             G[0] = gameCard0;
  46.             G[1] = gameCard1;
  47.             G[2] = gameCard2;
  48.             G[3] = gameCard3;
  49.             G[4] = gameCard4;
  50.             G[5] = gameCard5;
  51.             G[6] = gameCard6;
  52.             G[7] = gameCard7;
  53.             G[8] = gameCard8;
  54.             G[9] = gameCard9;
  55.             G[10] = gameCard10;
  56.             G[11] = gameCard11;
  57.             temp[0] = temp0;
  58.             temp[1] = temp1;
  59.             for (int i = 0; i < G.Length / 2; i++)
  60.             {

  61.                 randomNow[i] = -1;
  62.             }

  63.             for (int i = 0; i < G.Length; i++)
  64.                 isWaiting[i] = -1;
  65.         }

  66.         bool isNumberNotExisted(int r, int[] a)
  67.         {
  68.             for (int i = 0; i < a.Length; i++)
  69.                 if (r == a[i])
  70.                     return false;
  71.             return true;
  72.         }

  73.         void numberRandomPicks(int[] b, int n)
  74.         {
  75.             int i = 0;
  76.             while (i < n)
  77.             {
  78.                 int x = rnd.Next(0, n);
  79.                 if (isNumberNotExisted(x, b))
  80.                 {
  81.                     b[i] = x;
  82.                     i++;
  83.                 }
  84.             }
  85.         }


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

  92.         private void btnStart_Click(object sender, EventArgs e)
  93.         {

  94.             for (int i = 0; i < G.Length; i++)
  95.             {
  96.                 isWaiting[i] = 0;
  97.                 G[i].Image = Properties.Resources.bgcard;
  98.                 temp0.Image = null;
  99.                 temp1.Image = null;
  100.             }
  101.             for (int i = 0; i < cardNumber / 2; i++) randomNow[i] = -1;
  102.             cardManage(P, OriginalPic, randomNow);
  103.             for (int i = 0; i < cardNumber / 2; i++) randomNow[i] = -1;
  104.             cardManage(Q, OriginalPic, randomNow);
  105.         }

  106.         void flipCard(object sender)
  107.         {

  108.             if (flippedTimes == 2)

  109.             {
  110.                 temp[0].Image = null;
  111.                 temp[1].Image = null;
  112.                 timer1.Start();
  113.                 flippedTimes = 0;
  114.             }

  115.         }

  116.         private void gameCard0_Click(object sender, EventArgs e)
  117.         {
  118.             if (isWaiting[0] == 0)
  119.             {
  120.                 isWaiting[0] = 1;
  121.                 G[0].Image = p0.Image;
  122.                 flippedTimes++;

  123.                 if (temp[0].Image != null) temp[1].Image = G[0].Image;
  124.                 else temp[0].Image = G[0].Image;

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

  127.             }
  128.         }

  129.         private void gameCard1_Click(object sender, EventArgs e)
  130.         {
  131.             if (isWaiting[1] == 0)
  132.             {
  133.                 isWaiting[1] = 1; flippedTimes++;
  134.                 gameCard1.Image = p1.Image;
  135.                 if (temp[0].Image != null) temp[1].Image = G[1].Image;
  136.                 else temp[0].Image = G[1].Image;
  137.                 if (flippedTimes == 2)
  138.                     timer1.Start();
  139.             }
  140.         }


  141.         private void gameCard2_Click(object sender, EventArgs e)
  142.         {
  143.             if (isWaiting[2] == 0)
  144.             {
  145.                 isWaiting[2] = 1; flippedTimes++;
  146.                 gameCard2.Image = p2.Image;
  147.                 if (temp[0].Image != null) temp[1].Image = G[2].Image;
  148.                 else temp[0].Image = G[2].Image;
  149.                 if (flippedTimes == 2)
  150.                     timer1.Start();
  151.             }
  152.         }

  153.         private void gameCard3_Click(object sender, EventArgs e)
  154.         {
  155.             if (isWaiting[3] == 0)
  156.             {
  157.                 isWaiting[3] = 1; flippedTimes++;
  158.                 gameCard3.Image = p3.Image;
  159.                 if (temp[0].Image != null) temp[1].Image = G[3].Image;
  160.                 else temp[0].Image = G[3].Image;
  161.                 if (flippedTimes == 2)
  162.                     timer1.Start();
  163.             }
  164.         }

  165.         private void gameCard4_Click(object sender, EventArgs e)
  166.         {
  167.             if (isWaiting[4] == 0)
  168.             {
  169.                 isWaiting[4] = 1; flippedTimes++;
  170.                 gameCard4.Image = p4.Image;
  171.                 if (temp[0].Image != null) temp[1].Image = G[4].Image;
  172.                 else temp[0].Image = G[4].Image;
  173.                 if (flippedTimes == 2)
  174.                     timer1.Start();
  175.             }
  176.         }
  177.         private void gameCard5_Click(object sender, EventArgs e)
  178.         {
  179.             if (isWaiting[5] == 0)
  180.             {
  181.                 isWaiting[5] = 1; flippedTimes++;
  182.                 gameCard5.Image = q0.Image;
  183.                 if (temp[0].Image != null) temp[1].Image = G[5].Image;
  184.                 else temp[0].Image = G[5].Image;
  185.                 if (flippedTimes == 2)
  186.                     timer1.Start();
  187.             }
  188.         }

  189.         private void gameCard6_Click(object sender, EventArgs e)
  190.         {
  191.             if (isWaiting[6] == 0)
  192.             {
  193.                 isWaiting[6] = 1; flippedTimes++;
  194.                 gameCard6.Image = q1.Image;
  195.                 if (temp[0].Image != null) temp[1].Image = G[6].Image;
  196.                 else temp[0].Image = G[6].Image;
  197.                 if (flippedTimes == 2)
  198.                     timer1.Start();
  199.             }
  200.         }

  201.         private void gameCard7_Click(object sender, EventArgs e)
  202.         {
  203.             if (isWaiting[7] == 0)
  204.             {
  205.                 isWaiting[7] = 1; flippedTimes++;
  206.                 gameCard7.Image = q2.Image;
  207.                 if (temp[0].Image != null) temp[1].Image = G[7].Image;
  208.                 else temp[0].Image = G[7].Image;
  209.                 if (flippedTimes == 2)
  210.                     timer1.Start();
  211.             }
  212.         }

  213.         private void gameCard8_Click(object sender, EventArgs e)
  214.         {
  215.             if (isWaiting[8] == 0)
  216.             {
  217.                 isWaiting[8] = 1; flippedTimes++;
  218.                 gameCard8.Image = q3.Image;
  219.                 if (temp[0].Image != null) temp[1].Image = G[8].Image;
  220.                 else temp[0].Image = G[8].Image;
  221.                 if (flippedTimes == 2)
  222.                     timer1.Start();
  223.             }
  224.         }


  225.         private void gameCard9_Click(object sender, EventArgs e)
  226.         {
  227.             if (isWaiting[9] == 0)
  228.             {
  229.                 isWaiting[9] = 1; flippedTimes++;
  230.                 gameCard9.Image = q4.Image;
  231.                 if (temp[0].Image != null) temp[1].Image = G[9].Image;
  232.                 else temp[0].Image = G[9].Image;
  233.                 if (flippedTimes == 2)
  234.                     timer1.Start();
  235.             }
  236.         }

  237.         private bool isOver()
  238.         {
  239.             for (int i = 0; i < G.Length; i++)
  240.                 if (isWaiting[i] != 2)
  241.                 {
  242.                     return false;

  243.                 }
  244.             return true;
  245.         }
  246.         private void timer1_Tick(object sender, EventArgs e)
  247.         {
  248.             if (flippedTimes == 2)
  249.             {
  250.                 if (temp0.Image == temp1.Image)
  251.                 {
  252.                     for (int i = 0; i < G.Length; i++)
  253.                         if (isWaiting[i] == 1) isWaiting[i] = 2;
  254.                 }
  255.                 else if (temp0.Image != temp1.Image)
  256.                     for (int j = 0; j < G.Length; j++)
  257.                     {
  258.                         if (isWaiting[j] == 1)
  259.                         {
  260.                             G[j].Image = Properties.Resources.bgcard;
  261.                             isWaiting[j] = 0;
  262.                         }
  263.                     }
  264.                 temp[0].Image = null;
  265.                 temp[1].Image = null;
  266.                 if (isOver()) btnStart.Text = "PLAY AGAIN?";
  267.                 else btnStart.Text = "RESET";
  268.                 flippedTimes = 0;
  269.             }


  270.             timer1.Stop();
  271.         }

  272.         private void gameCard10_Click(object sender, EventArgs e)
  273.         {
  274.             if (isWaiting[10] == 0)
  275.             {
  276.                 isWaiting[10] = 1; flippedTimes++;
  277.                 gameCard10.Image = p5.Image;
  278.                 if (temp[0].Image != null) temp[1].Image = G[10].Image;
  279.                 else temp[0].Image = G[10].Image;
  280.                 if (flippedTimes == 2)
  281.                     timer1.Start();
  282.             }
  283.         }

  284.         private void gameCard11_Click(object sender, EventArgs e)
  285.         {
  286.             if (isWaiting[11] == 0)
  287.             {
  288.                 isWaiting[11] = 1; flippedTimes++;
  289.                 gameCard11.Image = q5.Image;
  290.                 if (temp[0].Image != null) temp[1].Image = G[11].Image;
  291.                 else temp[0].Image = G[11].Image;
  292.                 if (flippedTimes == 2)
  293.                     timer1.Start();
  294.             }
  295.         }
  296.     }
  297. }
複製代碼
回復

使用道具 舉報

 樓主| 發表於 2021-4-4 16:00:12 | 顯示全部樓層
回復 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|手機版|Archiver|code.club  

GMT+8, 2024-3-29 13:12 , Processed in 0.090205 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表