code.club
標題:
memory game/ card matching code
[打印本頁]
作者:
csharp
時間:
2021-4-4 15:59
標題:
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[2];
PictureBox[] OriginalPic = new PictureBox[cardNumber / 2];
PictureBox[] P = new PictureBox[cardNumber / 2];
PictureBox[] Q = new PictureBox[cardNumber / 2];
PictureBox[] G = new PictureBox[cardNumber];
int[] isWaiting = new int[cardNumber];
int[] randomNow = new int[cardNumber / 2];
static int cardNumber = 12;
public Form1()
{
InitializeComponent();
OriginalPic[0] = original0;
OriginalPic[1] = original1;
OriginalPic[2] = original2;
OriginalPic[3] = original3;
OriginalPic[4] = original4;
OriginalPic[5] = original5;
P[0] = p0;
P[1] = p1;
P[2] = p2;
P[3] = p3;
P[4] = p4;
P[5] = p5;
Q[0] = q0;
Q[1] = q1;
Q[2] = q2;
Q[3] = q3;
Q[4] = q4;
Q[5] = q5;
G[0] = gameCard0;
G[1] = gameCard1;
G[2] = gameCard2;
G[3] = gameCard3;
G[4] = gameCard4;
G[5] = gameCard5;
G[6] = gameCard6;
G[7] = gameCard7;
G[8] = gameCard8;
G[9] = gameCard9;
G[10] = gameCard10;
G[11] = gameCard11;
temp[0] = temp0;
temp[1] = temp1;
for (int i = 0; i < G.Length / 2; i++)
{
randomNow[i] = -1;
}
for (int i = 0; i < G.Length; i++)
isWaiting[i] = -1;
}
bool isNumberNotExisted(int r, int[] a)
{
for (int i = 0; i < a.Length; i++)
if (r == a[i])
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[i] = x;
i++;
}
}
}
void cardManage(PictureBox[] X, PictureBox[] Y, int[] Z)
{
numberRandomPicks(randomNow, cardNumber / 2);
for (int i = 0; i < cardNumber / 2; i++)
X[i].Image = Y[Z[i]].Image;
}
private void btnStart_Click(object sender, EventArgs e)
{
for (int i = 0; i < G.Length; i++)
{
isWaiting[i] = 0;
G[i].Image = Properties.Resources.bgcard;
temp0.Image = null;
temp1.Image = null;
}
for (int i = 0; i < cardNumber / 2; i++) randomNow[i] = -1;
cardManage(P, OriginalPic, randomNow);
for (int i = 0; i < cardNumber / 2; i++) randomNow[i] = -1;
cardManage(Q, OriginalPic, randomNow);
}
void flipCard(object sender)
{
if (flippedTimes == 2)
{
temp[0].Image = null;
temp[1].Image = null;
timer1.Start();
flippedTimes = 0;
}
}
private void gameCard0_Click(object sender, EventArgs e)
{
if (isWaiting[0] == 0)
{
isWaiting[0] = 1;
G[0].Image = p0.Image;
flippedTimes++;
if (temp[0].Image != null) temp[1].Image = G[0].Image;
else temp[0].Image = G[0].Image;
if (flippedTimes == 2)
timer1.Start();
}
}
private void gameCard1_Click(object sender, EventArgs e)
{
if (isWaiting[1] == 0)
{
isWaiting[1] = 1; flippedTimes++;
gameCard1.Image = p1.Image;
if (temp[0].Image != null) temp[1].Image = G[1].Image;
else temp[0].Image = G[1].Image;
if (flippedTimes == 2)
timer1.Start();
}
}
private void gameCard2_Click(object sender, EventArgs e)
{
if (isWaiting[2] == 0)
{
isWaiting[2] = 1; flippedTimes++;
gameCard2.Image = p2.Image;
if (temp[0].Image != null) temp[1].Image = G[2].Image;
else temp[0].Image = G[2].Image;
if (flippedTimes == 2)
timer1.Start();
}
}
private void gameCard3_Click(object sender, EventArgs e)
{
if (isWaiting[3] == 0)
{
isWaiting[3] = 1; flippedTimes++;
gameCard3.Image = p3.Image;
if (temp[0].Image != null) temp[1].Image = G[3].Image;
else temp[0].Image = G[3].Image;
if (flippedTimes == 2)
timer1.Start();
}
}
private void gameCard4_Click(object sender, EventArgs e)
{
if (isWaiting[4] == 0)
{
isWaiting[4] = 1; flippedTimes++;
gameCard4.Image = p4.Image;
if (temp[0].Image != null) temp[1].Image = G[4].Image;
else temp[0].Image = G[4].Image;
if (flippedTimes == 2)
timer1.Start();
}
}
private void gameCard5_Click(object sender, EventArgs e)
{
if (isWaiting[5] == 0)
{
isWaiting[5] = 1; flippedTimes++;
gameCard5.Image = q0.Image;
if (temp[0].Image != null) temp[1].Image = G[5].Image;
else temp[0].Image = G[5].Image;
if (flippedTimes == 2)
timer1.Start();
}
}
private void gameCard6_Click(object sender, EventArgs e)
{
if (isWaiting[6] == 0)
{
isWaiting[6] = 1; flippedTimes++;
gameCard6.Image = q1.Image;
if (temp[0].Image != null) temp[1].Image = G[6].Image;
else temp[0].Image = G[6].Image;
if (flippedTimes == 2)
timer1.Start();
}
}
private void gameCard7_Click(object sender, EventArgs e)
{
if (isWaiting[7] == 0)
{
isWaiting[7] = 1; flippedTimes++;
gameCard7.Image = q2.Image;
if (temp[0].Image != null) temp[1].Image = G[7].Image;
else temp[0].Image = G[7].Image;
if (flippedTimes == 2)
timer1.Start();
}
}
private void gameCard8_Click(object sender, EventArgs e)
{
if (isWaiting[8] == 0)
{
isWaiting[8] = 1; flippedTimes++;
gameCard8.Image = q3.Image;
if (temp[0].Image != null) temp[1].Image = G[8].Image;
else temp[0].Image = G[8].Image;
if (flippedTimes == 2)
timer1.Start();
}
}
private void gameCard9_Click(object sender, EventArgs e)
{
if (isWaiting[9] == 0)
{
isWaiting[9] = 1; flippedTimes++;
gameCard9.Image = q4.Image;
if (temp[0].Image != null) temp[1].Image = G[9].Image;
else temp[0].Image = G[9].Image;
if (flippedTimes == 2)
timer1.Start();
}
}
private bool isOver()
{
for (int i = 0; i < G.Length; i++)
if (isWaiting[i] != 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[i] == 1) isWaiting[i] = 2;
}
else if (temp0.Image != temp1.Image)
for (int j = 0; j < G.Length; j++)
{
if (isWaiting[j] == 1)
{
G[j].Image = Properties.Resources.bgcard;
isWaiting[j] = 0;
}
}
temp[0].Image = null;
temp[1].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[10] == 0)
{
isWaiting[10] = 1; flippedTimes++;
gameCard10.Image = p5.Image;
if (temp[0].Image != null) temp[1].Image = G[10].Image;
else temp[0].Image = G[10].Image;
if (flippedTimes == 2)
timer1.Start();
}
}
private void gameCard11_Click(object sender, EventArgs e)
{
if (isWaiting[11] == 0)
{
isWaiting[11] = 1; flippedTimes++;
gameCard11.Image = q5.Image;
if (temp[0].Image != null) temp[1].Image = G[11].Image;
else temp[0].Image = G[11].Image;
if (flippedTimes == 2)
timer1.Start();
}
}
}
}
複製代碼
作者:
csharp
時間:
2021-4-4 16:00
http://www.youtube.com/watch?v=qX7qUJPWgVQ
歡迎光臨 code.club (https://code.club/)
Powered by Discuz! X3.2