프로젝트에 좀 써먹어볼라는데 징징,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace gogothink
{
public enum EStatus { CLOSE, OPEN, MATCH }
public partial class CardGameE : Form
{
// 이미지데이터의 값을 저장
int[] arrData = new int[16];
// 이미지데이터의 상태를 저장
EStatus[] eStatus = new EStatus[16];
// 이미지 컨트롤 배열
PictureBox[] picView = new PictureBox[16];
// 열어본 횟수
int nOpenCnt = 0;
// 1/100초 카운터
int nTimer = 0;
// 스레드에 이용할 변수
int nClose1, nClose2;
bool nClickLock = false;
public CardGameE()
{
InitializeComponent();
// 이미지컨트롤 위치를 고정시킨다.
for (int i = 0; i < arrData.Length; i++)
{
picView[i] = new PictureBox();
picView[i].Width = 60;
picView[i].Height = 80;
picView[i].Top = ((i / 4) * 5 /* 공백 */ + (i / 4) * 80) /* 이미지높이 */ + 10;
picView[i].Left = ((i % 4) * 5 /* 공백 */ + (i % 4) * 60) /* 이미지넓이 */ + 10;
picView[i].Click += picView_Click; // 이벤트 처리 (델리게이트 참조)
picView[i].Name = i.ToString(); // 이름을 이용해서 값을 저장한다. 이벤트시에 넘겨받을 값 필요.
panFloor.Controls.Add(picView[i]);
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void FormImgMatch_Load(object sender, EventArgs e)
{
InitGame();
}
private void btnReset_Click(object sender, EventArgs e)
{
InitGame();
}
// 게임 초기화
private void InitGame()
{
Random r = new Random();
bool bCheck = false;
nOpenCnt = 0;
nTimer = 0;
if(timerCnt.Enabled == true) timerCnt.Stop();
// 중복없는 값을 만든다.
for (int i = 0; i < arrData.Length; i++, bCheck = false)
{
int tempI = r.Next(0, arrData.Length);
for (int uI = 0; uI < i; uI++)
{
if (arrData[uI] == tempI)
{
i--;
bCheck = true;
break;
}
}
if (bCheck) continue;
arrData[i] = tempI;
eStatus[i] = EStatus.CLOSE;
picView[i].Image = global::gogothink.Properties.Resources.nophoto_60x80;
Cnt.Text = "횟수 : ";
lblTime.Text = "시간 : ";
}
}
// 컨트롤 클릭시 처리.
private void picView_Click(object sender, EventArgs e) {
if (nClickLock) return; // 틀린 그림을 찾았을 때 Sleep 타임동안 클릭방지.
if (timerCnt.Enabled == false) timerCnt.Start(); // 타이머가 꺼져있다면 실행.
PictureBox picTemp = (PictureBox)sender;
int nNumber = int.Parse(picTemp.Name);
int nMatch = -1;
// 닫혀있는 컨트롤 처리
if (eStatus[nNumber] == EStatus.CLOSE)
{
int nValue = arrData[nNumber];
int nRealV = nValue / 2; // 나머지는 버리는 int 의 속성을 이용. 값을 2로 나누면 2와 3은 같게된다. 이것으로 같은 그림을 판단한다.
ImageChange(picTemp, nRealV); // 숨겨진 이미지 오픈.
// OPEN 되어있는 데이터 확인
for(int i = 0; i < eStatus.Length; i ++)
{
if (eStatus[i] == EStatus.OPEN)
{
nMatch = i;
break;
}
}
// 열려있는 데이터가 같다면 MATCH 로 변경 후 열어두고 다르다면 스레드를 이용 1초를 딜레이 시킨 후 다시 닫는다.
if (nMatch != -1)
{
if (arrData[nMatch] / 2 == nRealV)
{
eStatus[nMatch] = EStatus.MATCH;
eStatus[nNumber] = EStatus.MATCH;
}
else
{
nClickLock = true;
nClose1 = nMatch;
nClose2 = nNumber;
Thread th = new Thread(new ThreadStart(CloseThreading));
th.Start();
}
nOpenCnt++;
Cnt.Text = "횟수 : " + nOpenCnt + "회";
}
else
{
eStatus[nNumber] = EStatus.OPEN;
}
}
SucCheck();
}
// 성공 여부 확인. 모든 상태가 MATCH 면 성공.
private void SucCheck()
{
foreach (EStatus status in eStatus)
{
if (status != EStatus.MATCH)
{
return;
}
}
timerCnt.Stop();
MessageBox.Show(string.Format("{0}초, {1}회 만에 성공하였습니다.", (float)nTimer / 100, nOpenCnt));
}
// 1초의 딜레이 후 열려있는 MATCH 되지 않은 이미지를 닫는다.
private void CloseThreading()
{
Thread.Sleep(1000);
ImageChange(picView[nClose1], -1);
ImageChange(picView[nClose2], -1);
eStatus[nClose1] = EStatus.CLOSE;
eStatus[nClose2] = EStatus.CLOSE;
nClickLock = false;
}
// 이미지 변경 처리
private void ImageChange(PictureBox picTemp, int nValue)
{
switch ((nValue))
{
case 0:
picTemp.Image = global::gogothink.Properties.Resources._1;
break;
case 1:
picTemp.Image = global::gogothink.Properties.Resources._2;
break;
case 2:
picTemp.Image = global::gogothink.Properties.Resources._3;
break;
case 3:
picTemp.Image = global::gogothink.Properties.Resources._4;
break;
case 4:
picTemp.Image = global::gogothink.Properties.Resources._5;
break;
case 5:
picTemp.Image = global::gogothink.Properties.Resources._6;
break;
case 6:
picTemp.Image = global::gogothink.Properties.Resources._7;
break;
case 7:
picTemp.Image = global::gogothink.Properties.Resources._8;
break;
default:
picTemp.Image = global::gogothink.Properties.Resources.nophoto_60x80;
break;
}
}
private void timerCnt_Tick(object sender, EventArgs e)
{
nTimer += 1;
int millTime = nTimer % 100;
int secTime = (nTimer % (60 * 100)) / 100;
int minTime = (nTimer % (60 * 60 * 100)) / (60 * 100);
int hourTime = nTimer / (60 * 60 * 100);
lblTime.Text = string.Format("시간 : {0}:{1}:{2}.{3}", hourTime, minTime,secTime, millTime);
}
private void panFloor_Paint(object sender, PaintEventArgs e)
{
}
private void CardGameE_Load(object sender, EventArgs e)
{
}
}
}
댓글 영역
획득법
① NFT 발행
작성한 게시물을 NFT로 발행하면 일주일 동안 사용할 수 있습니다. (최초 1회)
② NFT 구매
다른 이용자의 NFT를 구매하면 한 달 동안 사용할 수 있습니다. (구매 시마다 갱신)
사용법
디시콘에서지갑연결시 바로 사용 가능합니다.