2023年面向对象程序设计实验级.doc
面向对象程序设计实践设计报告姓名:李香智系别:信息工程系班别:09512102学号:设计时间:2023.1.29-2023.1.7日应用程序的名称:参选题目35B猜数字游戏程序。应用程序的主题、设计目的:加强算法的准确性,巩固学过的面向对象设计知识。应用程序的介绍:本程序是以VC+6.0的MFC为编译环境设计的。VC比较适合制作游戏,应为他的运营速度快,不会产生过大的余赘。而VC+6.0的MFC是一个极好的应用程序框架,程序员可以在不做任何操作的情况下获得大量的代码,以及MFC生成的框架、封装的函数。本猜数字游戏程序是由基本的MFC的控件、框架、封装函数等构成,内容将在下面介绍。应用程序的总体设计构造:如下图所示:这幅图为ID为IDD_MY2023_12_28_2_DIALOG的一个资源控件,上面有一幅插图、两个可见文本编辑框、三个按钮以及若干个不可见的只读文本编辑框和静态文本编辑框组成。又上面的按钮标题可以得知其相应的用途。下图为连接编译后的图像:可以看到,不可见的文本编辑框是不显示出来的,而静态的文本编辑框只显示其标题。那两个灰色的按钮是被禁用的,在点击开始游戏之后才会被激活。左边的可写文本编辑框是输入你要猜的数字,在输入4个数字以后按拟定键,电脑就会给你判断出是否有对的的数字,以及位子对的的数字,这些判断的结果会在A或B前显示。A为位置对的数字也对的的数的个数,B为位置不对的而存在几个对的的数的个数。右边的文本编辑框是一个计数器,虽然在事后知道这个并不一定要开线程,但是还是保存,没有用定期器。这个计时器会计算你获胜说用的时间。在这之后就是这个游戏的玩法,这个会在最后面介绍。重要代码为:/ 2023_12_28_2Dlg.cpp : implementation file/#include "stdafx.h"#include "2023_12_28_2.h"#include "2023_12_28_2Dlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutint online=0;int kaiguan=1;int suiji;int A=0;int B=0;CString EditText;CString EditA;CString EditB;CString EditTextAK;int TIME=0;CString CTIME;int w;int a4=0;int b4=0;int Sum=0;CTime time_1,time_2;CTimeSpan time_3;class CAboutDlg : public CDialogpublic:CAboutDlg();/ Dialog Data/AFX_DATA(CAboutDlg)enum IDD = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:/AFX_MSG(CAboutDlg)/AFX_MSGDECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CMy2023_12_28_2Dlg dialogCMy2023_12_28_2Dlg:CMy2023_12_28_2Dlg(CWnd* pParent /*=NULL*/): CDialog(CMy2023_12_28_2Dlg:IDD, pParent)/AFX_DATA_INIT(CMy2023_12_28_2Dlg)/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);void CMy2023_12_28_2Dlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CMy2023_12_28_2Dlg)DDX_Control(pDX, IDC_EDIT1, m_Time);DDX_Control(pDX, IDC_BUTTON_OK, m_Button_OK);DDX_Control(pDX, IDC_BUTTON_NO, m_Button_NO);DDX_Control(pDX, IDC_EDIT_AS, m_AS);DDX_Control(pDX, IDC_EDIT_TURN_9, m_TURN_9);DDX_Control(pDX, IDC_EDIT_TURN_7, m_TURN_7);DDX_Control(pDX, IDC_EDIT_TURN_8, m_TURN_8);DDX_Control(pDX, IDC_EDIT_TURN_6, m_TURN_6);DDX_Control(pDX, IDC_EDIT_TURN_5, m_TURN_5);DDX_Control(pDX, IDC_EDIT_TURN_4, m_TURN_4);DDX_Control(pDX, IDC_EDIT_TURN_3, m_TURN_3);DDX_Control(pDX, IDC_EDIT_TURN_2, m_TURN_2);DDX_Control(pDX, IDC_EDIT_TURN_1, m_TURN_1);DDX_Control(pDX, IDC_EDIT_PUT_9, m_PUT_9);DDX_Control(pDX, IDC_EDIT_PUT_8, m_PUT_8);DDX_Control(pDX, IDC_EDIT_PUT_7, m_PUT_7);DDX_Control(pDX, IDC_EDIT_PUT_6, m_PUT_6);DDX_Control(pDX, IDC_EDIT_PUT_5, m_PUT_5);DDX_Control(pDX, IDC_EDIT_PUT_4, m_PUT_4);DDX_Control(pDX, IDC_EDIT_PUT_3, m_PUT_3);DDX_Control(pDX, IDC_EDIT_PUT_2, m_PUT_2);DDX_Control(pDX, IDC_EDIT_PUT_1, m_PUT_1);DDX_Control(pDX, IDC_EDIT_FT_9, m_FT_9);DDX_Control(pDX, IDC_EDIT_FT_8, m_FT_8);DDX_Control(pDX, IDC_EDIT_FT_7, m_FT_7);DDX_Control(pDX, IDC_EDIT_FT_6, m_FT_6);DDX_Control(pDX, IDC_EDIT_FT_5, m_FT_5);DDX_Control(pDX, IDC_EDIT_FT_4, m_FT_4);DDX_Control(pDX, IDC_EDIT_FT_3, m_FT_3);DDX_Control(pDX, IDC_EDIT_FT_2, m_FT_2);DDX_Control(pDX, IDC_EDIT_FT_1, m_FT_1);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CMy2023_12_28_2Dlg, CDialog)/AFX_MSG_MAP(CMy2023_12_28_2Dlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON_OK, OnButtonOk)ON_COMMAND(IDM_START, OnStart)ON_BN_CLICKED(IDC_BUTTON_NO, OnButtonNo)ON_COMMAND(IDM_PAIHANG, OnPaihang)ON_BN_CLICKED(IDC_BUTTON_START1, OnButtonStart1)ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CMy2023_12_28_2Dlg message handlersBOOL CMy2023_12_28_2Dlg:OnInitDialog()CDialog:OnInitDialog();/ Add "About." menu item to system menu./ IDM_ABOUTBOX must be in the system command range.ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL)CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);LOGFONT logfont; CFont *pfont =m_Time.GetFont(); pfont-> GetLogFont( &logfont ); logfont.lfHeight=logfont.lfHeight * 2; logfont.lfWidth =logfont.lfWidth * 2; CFont font; font.CreateFontIndirect(&logfont); m_Time.SetFont(&font); / Set the icon for this dialog. The framework does this automatically/ when the application's main window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small icon/ TODO: Add extra initialization herereturn TRUE; / return TRUE unless you set the focus to a controlvoid CMy2023_12_28_2Dlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialog:OnSysCommand(nID, lParam);/ If you add a minimize button to your dialog, you will need the code below/ to draw the icon. For MFC applications using the document/view model,/ this is automatically done for you by the framework.void CMy2023_12_28_2Dlg:OnPaint() if (IsIconic()CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;/ Draw the icondc.DrawIcon(x, y, m_hIcon);elseCDialog:OnPaint();/ The system calls this to obtain the cursor to display while the user drags/ the minimized window.HCURSOR CMy2023_12_28_2Dlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;/int Torf()w=atoi(EditText);for(int i=3;i>=0;i-)ai=w%10;w=w/10;for( i=0;i<4;i+)if(ai=bi) A+;else for(int j=0;j<4;j+) if(ai=bj) B+;EditA.Format("%d",A);EditB.Format("%d",B);return 0;void CMy2023_12_28_2Dlg:OnButtonOk() int t;A=0;B=0;if(online=1)UpdateData(TRUE);pThread=AfxBeginThread(ThreadFunc,&m_Time);/ m_Time.SetSel(0,-1);/m_Time.SetWindowText(CTIME);/ TODO: Add your control notification handler code here/if(!EditText|EditText) m_AS.SetSel(0,-1);m_AS.GetWindowText(EditText);suiji=atoi(EditText);if(suiji>9999|suiji<1000) MessageBox("请输入4位有效数字!","提醒",MB_OK);goto loop;Torf();for(t=0;t<3;t+)for(int k=0;k<4;k+)if(t=k) k+;if(at=ak)MessageBox("请输入4个不反复的数字!","提醒",MB_OK); m_AS.ReplaceSel(""); goto updown;switch(kaiguan)case 1: m_TURN_1.SetWindowText(EditA);m_FT_1.SetWindowText(EditB);m_PUT_1.SetWindowText(EditText);m_AS.ReplaceSel("");/ m_PUT_1.SetSel(0,-1);kaiguan+; break;case 2:m_PUT_2.SetWindowText(EditText);m_AS.ReplaceSel("");/ m_PUT_1.SetSel(0,-1);kaiguan+;m_TURN_2.SetWindowText(EditA);m_FT_2.SetWindowText(EditB);break;case 3:m_PUT_3.SetWindowText(EditText);m_AS.ReplaceSel("");/ m_PUT_1.SetSel(0,-1);kaiguan+; m_TURN_3.SetWindowText(EditA);m_FT_3.SetWindowText(EditB);break;case 4:m_AS.ReplaceSel("");/ m_PUT_1.SetSel(0,-1);m_PUT_4.SetWindowText(EditText);kaiguan+; m_TURN_4.SetWindowText(EditA);m_FT_4.SetWindowText(EditB);break;case 5:m_AS.ReplaceSel("");/ m_PUT_1.SetSel(0,-1);m_PUT_5.SetWindowText(EditText);kaiguan+; m_TURN_5.SetWindowText(EditA);m_FT_5.SetWindowText(EditB);break;case 6:m_AS.ReplaceSel("");/ m_PUT_1.SetSel(0,-1);m_PUT_6.SetWindowText(EditText);kaiguan+;m_TURN_6.SetWindowText(EditA);m_FT_6.SetWindowText(EditB);break;case 7:m_AS.ReplaceSel("");/ m_PUT_1.SetSel(0,-1);m_PUT_7.SetWindowText(EditText);kaiguan+; m_TURN_7.SetWindowText(EditA);m_FT_7.SetWindowText(EditB);break;case 8:m_AS.ReplaceSel("");/ m_PUT_1.SetSel(0,-1);m_PUT_8.SetWindowText(EditText);kaiguan+;m_TURN_8.SetWindowText(EditA);m_FT_8.SetWindowText(EditB);break;case 9:m_AS.ReplaceSel("");/ m_PUT_1.SetSel(0,-1);m_PUT_9.SetWindowText(EditText);kaiguan+;m_TURN_9.SetWindowText(EditA);m_FT_9.SetWindowText(EditB);if(A=4)MessageBox("厉害!你赢了","结束",MB_OK);m_Button_OK.EnableWindow(false);m_Button_NO.EnableWindow(false);UpdateData(FALSE);/time_2=CTime:GetCurrentTime();if(kaiguan=10)MessageBox("傻逼!你输了!","结束",MB_OK);m_AS.SetWindowText(EditTextAK);m_Button_OK.EnableWindow(false);m_Button_NO.EnableWindow(false);UpdateData(FALSE);loop:updown: online=2;/if()void CMy2023_12_28_2Dlg:OnStart() m_AS.SetSel(0,-1);m_AS.ReplaceSel("");int panduan=1;int i=0;int save=0;A=4;TIME=0;online=1;CTIME.Format("%d",TIME);m_Time.SetSel(0,-1);m_Time.SetWindowText(CTIME);time_1=CTime:GetCurrentTime();m_Button_OK.EnableWindow(true);m_Button_NO.EnableWindow(true);/suiji=1234;/ TODO: Add your command handler code here/srand(time(NULL);/suiji=rand()%10;srand(time(NULL);for(;)if(i=0)while(1)if(bi=rand()%10) i+; break;switch(i)case 1: bi=rand()%10; if(i>0&&(bi!=bi-1) i+;panduan+; break;case 2: bi=rand()%10; if(bi!=bi-2)&&(bi!=bi-1) i+;panduan+;break;case 3:bi=rand()%10; if(bi!=bi-2)&&(bi!=bi-1)&&(bi!=bi-3) i+;panduan+;break;/if(i>0&&(bi!=bi-1)/if(i>1&&(bi!=bi-2)/if(i>2&&(bi!=bi-3)if(panduan=4)break;save=b0*1000+save;save=b1*100+save;save=b2*10+save;save=b3*1+save;EditTextAK.Format("%d",save);kaiguan=1;/UpdateData(FALSE);/m_PUT_1.SetSel(0,-1);m_PUT_1.ReplaceSel("");m_PUT_2.SetSel(0,-1);m_PUT_2.ReplaceSel("");m_PUT_3.SetSel(0,-1);m_PUT_3.ReplaceSel("");m_PUT_4.SetSel(0,-1);m_PUT_4.ReplaceSel("");m_PUT_5.SetSel(0,-1);m_PUT_5.ReplaceSel("");m_PUT_6.SetSel(0,-1);m_PUT_6.ReplaceSel("");m_PUT_7.SetSel(0,-1);m_PUT_7.ReplaceSel("");m_PUT_8.SetSel(0,-1);m_PUT_8.ReplaceSel("");m_PUT_9.SetSel(0,-1);m_PUT_9.ReplaceSel("");/m_TURN_1.SetSel(0,-1);m_TURN_1.ReplaceSel("");m_FT_1.SetSel(0,-1);m_FT_1.ReplaceSel("");m_TURN_2.SetSel(0,-1);m_TURN_2.ReplaceSel("");m_FT_2.SetSel(0,-1);m_FT_2.ReplaceSel("");m_TURN_3.SetSel(0,-1);m_TURN_3.ReplaceSel("");m_FT_3.SetSel(0,-1);m_FT_3.ReplaceSel("");m_TURN_4.SetSel(0,-1);m_TURN_4.ReplaceSel("");m_FT_4.SetSel(0,-1);m_FT_4.ReplaceSel("");m_TURN_5.SetSel(0,-1);m_TURN_5.ReplaceSel("");m_FT_5.SetSel(0,-1);m_FT_5.ReplaceSel("");m_TURN_6.SetSel(0,-1);m_TURN_6.ReplaceSel("");m_FT_6.SetSel(0,-1);m_FT_6.ReplaceSel("");m_TURN_7.SetSel(0,-1);m_TURN_7.ReplaceSel("");m_FT_7.SetSel(0,-1);m_FT_7.ReplaceSel("");m_TURN_8.SetSel(0,-1);m_TURN_8.ReplaceSel("");m_FT_8.SetSel(0,-1);m_FT_8.ReplaceSel("");m_TURN_9.SetSel(0,-1);m_TURN_9.ReplaceSel("");m_FT_9.SetSel(0,-1);m_FT_9.ReplaceSel("");/Sum=0;void CMy2023_12_28_2Dlg:OnButtonNo() / TODO: Add your control notification handler code herem_AS.SetSel(0,-1);m_AS.ReplaceSel("");void CMy2023_12_28_2Dlg:OnPaihang() / TODO: Add your command handler code herevoid CMy2023_12_28_2Dlg:OnButtonStart1() / TODO: Add your control notification handler code hereOnStart();UINT ThreadFunc(LPVOID lpParam)CEdit *m_Time_1=(CEdit*)lpParam;while(1)CTIME.Format("%d",TIME);TIME+;m_Time_1->SetSel(0,-1);m_Time_1->SetWindowText(CTIME);if(A=4|kaiguan=10)break;Sleep(1000);return 0;void CMy2023_12_28_2Dlg:OnChangeEdit1() / TODO: If this is a RICHEDIT control, the control will not/ send this notification unless you override the CDialog:OnInitDialog()/ function and call CRichEditCtrl().SetEventMask()/ with the ENM_CHANGE flag ORed into the mask./m_Time.SetSel(0,-1);/m_Time.SetWindowText(CTIME);/ TODO: Add your control notification handler code here/ 2023_12_28_2Dlg.h : header file/#if !defined(AFX_2023_12_28_2DLG_H_9EDEB403_89EE_4C29_A1F6_A19EFCEDC811_INCLUDED_)#define AFX_2023_12_28_2DLG_H_9EDEB403_89EE_4C29_A1F6_A19EFCEDC811_INCLUDED_#if _MSC_VER > 1000#pragma once#endif / _MSC_VER > 1000/ CMy2023_12_28_2Dlg dialogclass CMy2023_12_28_2Dlg : public CDialog/ Constructionpublic:CMy2023_12_28_2Dlg(CWnd* pParent = NULL);/ standard constructor/ Dialog Data/AFX_DATA(CMy2023_12_28_2Dlg)enum IDD = IDD_MY2023_12_28_2_DIALOG ;CEditm_Time;CButtonm_Button_OK;CButtonm_Button_NO;CEditm_AS;CEditm_TURN_9;CEditm_TURN_7;CEditm_TURN_8;CEditm_TURN_6;CEditm_TURN_5;CEditm_TURN_4;CEditm_TURN_3;CEditm_TURN_2;CEditm_TURN_1;CEditm_PUT_9;CEditm_PUT_8;CEditm_PUT_7;CEditm_PUT_6;CEditm_PUT_5;CEditm_PUT_4;CEditm_PUT_3;CEditm_PUT_2;CEditm_PUT_1;CEditm_FT_9;CEditm_FT_8;CEdit