mfc_same_1 - 8BitsCoding/RobotMentor GitHub Wiki


// 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);
	}
}