// Dlg.h
CImage m_card_image[19]; // 0 - λ·λ©΄μΉ΄λ 1 ~ 18 - κ° μΉ΄λ
char m_table[36];
char m_view_flag = 1;
char m_first_pos = -1; // 0 ~ 35
char m_find_count = 0;
CProgressCtrl m_time_progress;
void EndOfGame(const wchar_t * ap_ment);
void StartGame();
BOOL CMFCApplication1Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// μ΄ λν μμμ μμ΄μ½μ μ€μ ν©λλ€. μμ© νλ‘κ·Έλ¨μ μ£Ό μ°½μ΄ λν μμκ° μλ κ²½μ°μλ
// νλ μμν¬κ° μ΄ μμ
μ μλμΌλ‘ μνν©λλ€.
SetIcon(m_hIcon, TRUE); // ν° μμ΄μ½μ μ€μ ν©λλ€.
SetIcon(m_hIcon, FALSE); // μμ μμ΄μ½μ μ€μ ν©λλ€.
// TODO: μ¬κΈ°μ μΆκ° μ΄κΈ°ν μμ
μ μΆκ°ν©λλ€.
CString str;
for (int i = 0; i < 19; i++)
{
str.Format(L".\\card_image\\%03d.bmp", i);
m_card_image[i].Load(str);
}
m_time_progress.SetRange(0, 60);
srand((unsigned int)time(NULL));
SetTimer(1, 3000, NULL);
SetTimer(10, 1000, NULL);
StartGame();
return TRUE; // ν¬μ»€μ€λ₯Ό 컨νΈλ‘€μ μ€μ νμ§ μμΌλ©΄ TRUEλ₯Ό λ°νν©λλ€.
}
void CMFCApplication1Dlg::StartGame()
{
SetDlgItemInt(IDC_HINT_BTN, 3);
for (int i = 0; i < 18; i++) {
m_table[i] = i + 1;
m_table[18 + i] = i + 1;
}
// μ«μλ₯Ό μ’ μμ΄λ³΄μ.
int first, second, temp;
for (int i = 0; i < 100; i++) {
first = rand() % 36;
second = rand() % 36;
if (first != second) {
// m_table[first] <--> m_table[second]
temp = m_table[first];
m_table[first] = m_table[second];
m_table[second] = temp;
}
}
m_time_progress.SetPos(60);
}
void CMFCApplication1Dlg::OnPaint()
{
CPaintDC dc(this); // 그리기λ₯Ό μν λλ°μ΄μ€ 컨ν
μ€νΈμ
λλ€.
if (IsIconic())
{
// ...
}
else
{
CString str;
int card_index;
for (int i = 0; i < 36; i++)
{
if (m_table[i] == 0) continue;; // μ κ±°λ μΉ΄λ
if (m_view_flag) card_index = m_table[i];
else card_index = 0;
m_card_image[card_index].Draw(dc, (i%6) * 36, i/6*56);
str.Format(L"%d", m_table[i]);
dc.TextOutW(5+ (i % 6) * 36,5+ i / 6 * 56, str);
}
// CDialogEx::OnPaint();
}
}
void CMFCApplication1Dlg::OnLButtonDown(UINT nFlags, CPoint point)
{
if (m_view_flag != 0) return; // μλ©΄μ 보μ¬μ£Όλ μνμμλ 리ν΄
int x = point.x / 36;
int y = point.y / 56;
if (x < 6 && y < 6) {
int select_pos = y*6+x; // 0 ~ 35
if (m_table[select_pos] == 0) return; // μ¬λΌμ§ μΉ΄λλ₯Ό μ ν
if (m_first_pos == -1) {
// 첫 λ²μ§Έ μΉ΄λ μ ν
m_first_pos = select_pos;
}
else {
// λ λ²μ§Έ μΉ΄λ μ ν
if (m_first_pos == select_pos) return; // κ°μ μμΉμ μΉ΄λ μ ν
if (m_table[m_first_pos] == m_table[select_pos]) {
m_table[m_first_pos] = m_table[select_pos] = 0;
Invalidate();
m_find_count++;
if (m_find_count == 18) {
// κ²μ μ’
λ£(μΉλ¦¬)
EndOfGame(L"λΉμ μ κ²μμμ μΉλ¦¬νμ΅λλ€.");
}
}
else {
m_view_flag = 2;
SetTimer(2, 1000, NULL);
}
m_first_pos = -1;
}
CClientDC dc(this);
int card_index = m_table[select_pos];
m_card_image[card_index].Draw(dc, x*36, y*56);
}
CDialogEx::OnLButtonDown(nFlags, point);
}
void CMFCApplication1Dlg::EndOfGame(const wchar_t * ap_ment)
{
KillTimer(10);
if (IDOK == MessageBox(L"λ€μ κ²μμ νμκ² μ΅λκΉ?", ap_ment, MB_OKCANCEL | MB_ICONQUESTION))
{
StartGame();
Invalidate();
}
else {
// νλ‘κ·Έλ¨ μ’
λ£
EndDialog(IDOK);
}
}
void CMFCApplication1Dlg::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 1) {
KillTimer(1);
m_view_flag = 0;
Invalidate();
}
else if (nIDEvent == 2) {
KillTimer(2);
Invalidate();
}
else if (nIDEvent == 10) {
int num = m_time_progress.GetPos() - 1;
if (num > 0) {
m_time_progress.SetPos(num);
}
else {
// κ²μ μ’
λ£(ν¨λ°°)
EndOfGame(L"λΉμ μ κ²μμμ μ‘μ΅λλ€.");
}
}
else {
CDialogEx::OnTimer(nIDEvent);
}
}
void CMFCApplication1Dlg::OnBnClickedHintBtn()
{
int num = GetDlgItemInt(IDC_HINT_BTN);
if (num > 0) {
SetDlgItemInt(IDC_HINT_BTN, num - 1);
m_view_flag = 1;
Invalidate();
SetTimer(1, 3000, NULL);
}
}