⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dicedlg.cpp

📁 用vc++开发的一个经典程序:Dice。自己开发的
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// diceDlg.cpp : implementation file
//

#include "stdafx.h"
#include "dice.h"
#include "diceDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

//##ModelId=4507556B016E
class CAboutDlg : public CDialog
{
public:
	//##ModelId=4507556B018B
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	//##ModelId=4507556B01A9
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

//##ModelId=4507556B018B
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

//##ModelId=4507556B01A9
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDiceDlg dialog

//##ModelId=4507556A034E
CDiceDlg::CDiceDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDiceDlg::IDD, pParent)
	, m_Display1(_T("")),
	m_Display2 (_T("")),
	m_Display3 (_T("")),
	m_Display4 (_T("")),
	m_Display5 (_T("")),
	m_Display6 (_T("")),
	m_Display7 (_T("")),
	m_Display8 (_T("")),
	m_Display9 (_T("")),
	m_Display10 (_T("")),
	m_Display11 (_T("")),
	m_Display12 (_T("")),
	m_Display13 (_T(""))
	, m_Display14(_T(""))
	, m_Display15(_T(""))
	, m_Display16(_T(""))
	, m_Display17(_T(""))
{
	//{{AFX_DATA_INIT(CDiceDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

//##ModelId=4507556A03BC
void CDiceDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1,m_Display1);
	DDX_Text(pDX, IDC_EDIT2,m_Display2);
	DDX_Text(pDX, IDC_EDIT3,m_Display3);
	DDX_Text(pDX, IDC_EDIT4,m_Display4);
	DDX_Text(pDX, IDC_EDIT5,m_Display5);
	DDX_Text(pDX, IDC_EDIT6,m_Display6);
	DDX_Text(pDX, IDC_EDIT7,m_Display7);
	DDX_Text(pDX, IDC_EDIT8,m_Display8);
	DDX_Text(pDX, IDC_EDIT9,m_Display9);
	DDX_Text(pDX, IDC_EDIT10,m_Display10);
	DDX_Text(pDX, IDC_EDIT11,m_Display11);
	DDX_Text(pDX, IDC_EDIT12,m_Display12);
	DDX_Text(pDX, IDC_EDIT13,m_Display13);
	DDX_Text(pDX, IDC_EDIT14,m_Display14);
	DDX_Text(pDX, IDC_EDIT15,m_Display15);
	DDX_Text(pDX, IDC_EDIT16,m_Display16);
	DDX_Text(pDX, IDC_EDIT17,m_Display17);
	//{{AFX_DATA_MAP(CDiceDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDiceDlg, CDialog)
	//{{AFX_MSG_MAP(CDiceDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnBnClickedButton5)
	ON_EN_CHANGE(IDC_EDIT1, OnEnChangeEdit1)
	ON_EN_CHANGE(IDC_EDIT2, OnEnChangeEdit2)
	ON_EN_CHANGE(IDC_EDIT3, OnEnChangeEdit3)
	ON_EN_CHANGE(IDC_EDIT4, OnEnChangeEdit4)
	ON_EN_CHANGE(IDC_EDIT5, OnEnChangeEdit5)
	ON_EN_CHANGE(IDC_EDIT6, OnEnChangeEdit6)
	ON_EN_CHANGE(IDC_EDIT7, OnEnChangeEdit7)
	ON_EN_CHANGE(IDC_EDIT8, OnEnChangeEdit8)
	ON_EN_CHANGE(IDC_EDIT9, OnEnChangeEdit9)
	ON_EN_CHANGE(IDC_EDIT10, OnEnChangeEdit10)
	ON_EN_CHANGE(IDC_EDIT11, OnEnChangeEdit11)
	ON_EN_CHANGE(IDC_EDIT12, OnEnChangeEdit12)
	ON_EN_CHANGE(IDC_EDIT14, OnEnChangeEdit14)
	ON_EN_CHANGE(IDC_EDIT15, OnEnChangeEdit15)
	ON_EN_CHANGE(IDC_EDIT13, OnEnChangeEdit13)
	ON_EN_CHANGE(IDC_EDIT16, OnEnChangeEdit16)
	ON_EN_CHANGE(IDC_EDIT17, OnEnChangeEdit17)
	//}}AFX_MSG_MAP
	//ON_BN_CLICKED(IDOK, OnBnClickedOk)
	//ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON5, OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON4, OnBnClickedButton4)
	ON_EN_CHANGE(IDC_EDIT1, OnEnChangeEdit1)
	ON_EN_CHANGE(IDC_EDIT2, OnEnChangeEdit2)
	ON_EN_CHANGE(IDC_EDIT3, OnEnChangeEdit3)
	ON_EN_CHANGE(IDC_EDIT4, OnEnChangeEdit4)
	ON_EN_CHANGE(IDC_EDIT5, OnEnChangeEdit5)
	ON_EN_CHANGE(IDC_EDIT6, OnEnChangeEdit6)
	ON_EN_CHANGE(IDC_EDIT7, OnEnChangeEdit7)
	ON_EN_CHANGE(IDC_EDIT8, OnEnChangeEdit8)
	ON_EN_CHANGE(IDC_EDIT9, OnEnChangeEdit9)
	ON_EN_CHANGE(IDC_EDIT10, OnEnChangeEdit10)
	ON_EN_CHANGE(IDC_EDIT11, OnEnChangeEdit11)
	ON_EN_CHANGE(IDC_EDIT12, OnEnChangeEdit12)
	ON_EN_CHANGE(IDC_EDIT13, OnEnChangeEdit13)
	ON_EN_CHANGE(IDC_EDIT14, OnEnChangeEdit14)
	ON_EN_CHANGE(IDC_EDIT15, OnEnChangeEdit15)
	ON_EN_CHANGE(IDC_EDIT16, OnEnChangeEdit16)
	ON_EN_CHANGE(IDC_EDIT17, OnEnChangeEdit17)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDiceDlg message handlers

//##ModelId=4507556A03D0
BOOL CDiceDlg::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);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	//初始化用户
	player1.setIngame(true);
	DisplayDice();
	//player1.setRound(1);
	//UpdateData(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

//##ModelId=4507556A03E3
void CDiceDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::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.

//##ModelId=4507556B000F
void CDiceDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int 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 icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
//##ModelId=4507556B0019
HCURSOR CDiceDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


//##ModelId=4507556B002F
void CDiceDlg::OnBnClickedButton1()   // roll 6 Dices
{
	// TODO: Add your control notification handler code here
	dicebox.roll();
	int score = dicebox.getScore();
	int scoreIncrement=0;
	DisplayDice();
	CString info="";

/////////////////////    Player1's Turn    /////////////////////

	if((player1.getIngame()==true)&&(player2.getIngame()==false))
	{	scoreIncrement=score-player1.getScore();
		player1.setScore(score);
		player1.addRound();
		DisplayTXT();
		if(score>=3000)//分数在3000以上,赢
		{
			info="Player1 wins. Game over.";
			DisplayInfo(info);
			player1.setIngame(false);
			player2.setIngame(false);
			//player1.addRound();
			player1.setRank(1);
			player2.setRank(2);
			DisplayTXT();
				//reset
			dicebox.score=score=0;
			dicebox.reset();

		}				
		else if((player1.getRound()==1)&&(score>0)&&(score<300))//第一局,300分以下,出局
		{
			info="Score<300. Player1 isn't IN. Player2's turn.";
			DisplayInfo(info);
			player1.setIngame(false);
			player2.setIngame(true);
			player1.setScore(0);
			//player1.addRound();
			DisplayTXT();
				//reset
			dicebox.score=score=0;
			dicebox.reset();
		}
		else if((player1.getRound()==1)&&(score>=300))//第一局,300分以上,入局
		{
			info="Score>300. You IN! Press roll to continue?";
			DisplayInfo(info);
		}
		else if(scoreIncrement==0)//分数增量是0,输,得分清零,换人,重设分数、色子
		{
			info="Score=0. Player1 OUT. Player2's turn.";
			DisplayInfo(info);
			player1.setIngame(false);
			player2.setIngame(true);
			player1.setScore(0);
			//player1.addRound();
			DisplayTXT();
				//reset
			dicebox.score=score=0;
			dicebox.reset();
		}				
		else if(scoreIncrement>0)
		{
			if((dicebox.avail_1==false)&&(dicebox.avail_2==false)&&(dicebox.avail_3==false)&&
				(dicebox.avail_4==false)&&(dicebox.avail_5==false)&&(dicebox.avail_6==false))
			{
				info="You have no dice. Player2's turn.";
				DisplayInfo(info);
				player1.setIngame(false);
				player2.setIngame(true);
				//player1.addRound();
				DisplayTXT();
					//reset
				dicebox.score=score=0;
				dicebox.reset();
			}
			else 
			{
				info="You get it! Press roll to continue?";
				DisplayInfo(info);
			}
			
		}
	}

////////////////////       Player2's Turn       ////////////////////
	else if((player1.getIngame()==false)&&(player2.getIngame()==true))
	{
		scoreIncrement=score-player2.getScore();
		player2.setScore(score);
		player2.addRound();
		DisplayTXT();
		if(score>=3000)//分数在3000以上,赢
		{
			info="Player2 wins. Game over.";
			DisplayInfo(info);
			player1.setIngame(false);
			player2.setIngame(false);
			//player1.addRound();
			player1.setRank(2);
			player2.setRank(1);
			DisplayTXT();
				//reset
			dicebox.score=score=0;
			//dicebox.reset();

		}				
		else if((player2.getRound()==1)&&(score>0)&&(score<300))//第一局,300分以下,出局
		{
			info="Score<300. Player2 isn't IN. Game over.";
			DisplayInfo(info);
			player1.setIngame(false);
			player2.setIngame(false);
			player2.setScore(0);
			//player1.addRound();
			DisplayTXT();
				//reset
			dicebox.score=score=0;
			//dicebox.reset();
		}
		else if((player2.getRound()==1)&&(score>=300))//第一局,300分以上,入局
		{
			info="Score>300. You IN! Press roll to continue?";
			DisplayInfo(info);
		}
		else if(scoreIncrement==0)//分数增量是0,输,得分清零,重设分数
		{
			info="Score=0. Player2 OUT. Game over.";
			DisplayInfo(info);
			player1.setIngame(false);
			player2.setIngame(false);
			player2.setScore(0);
			//player1.addRound();
			DisplayTXT();
				//reset
			dicebox.score=score=0;
			//dicebox.reset();
		}				
		else if(scoreIncrement>0)
		{
			if((dicebox.avail_1==false)&&(dicebox.avail_2==false)&&(dicebox.avail_3==false)&&
				(dicebox.avail_4==false)&&(dicebox.avail_5==false)&&(dicebox.avail_6==false))
			{
				info="You have no dice. Game over.";
				DisplayInfo(info);
				player1.setIngame(false);
				player2.setIngame(false);
				//player1.addRound();
				DisplayTXT();
					//reset
				dicebox.score=score=0;
				//dicebox.reset();
			}
			else 
			{
				info="You get it! Press roll to continue?";
				DisplayInfo(info);
			}
			
		}
	}

/////////////////    Game Over    //////////////////

	else if((player1.getIngame()==false)&&(player2.getIngame()==false))
	{
		//游戏结束,设置rank,显示info,
		if(player1.getScore()>player2.getScore())
		{
			player1.setRank(1);
			player2.setRank(2);
			DisplayTXT();
			info="Game over. Player1 wins.";
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -