五子棋程序设计报告
班 级:
姓 名:
学 号:
指导老师:
2012/1/07
一、 课程设计题目与要求
题目:网络五子棋游戏。
功能要求:该软件应包括服务器端程序和客户端程序两个部分,要求
实现的功能如下。
客户端功能要求:
(1) 友好的用户登录界面设计;
(2) 登录服务器后,请求用户列表;
(3) 相其他用户发出邀请,做游戏;
(4) 当用户加入或离开时,客户端接受服务器发送的成员列表,更
新用户列表;
(5) 当用户开始游戏或游戏结束时,服务器通知客户端用户更新该
用户状态,客户端接受该消息后,更新该用户状态;
(6) 根据五子棋的游戏规则实现五子棋的游戏功能,包括游戏判断、
画图等。
服务器端功能要求:
(1) 服务器接受用户注册信息,服务器最多容纳64 个成员,成员信
息包括:用户名、IP 地址、端口和状态,当服务器退出时,保
存用户信息;
(2) 当用户加入或退出,服务器向所有用户发送成员列表,通知客
户端更新成员列表;
(3) 当用户开始游戏或者结束游戏时,服务器通知其他用户更新该
用户状态,通知客户端更新成员状态;
(4) 和客户端一起维护游戏的进行,发送游戏信息。
二、 需求分析以及模型框架
1. 五子棋游戏是一历史悠久,对抗性强,技巧性高的棋类游戏。而且我对五子棋有着独特的爱好,希望自己也可以编写出一款可以实现人机对战的五子棋游戏。把自己的思想付给电脑,让自己有些成就感,给自己继续努力的动力。借此次课程设计的机会,把想法变成现实。而且五子棋不仅能增强思维能力,提高智力,而且富含哲理,有助于修身养性。五子棋既有现代休闲的明显特征“短、平、快”,又有古典哲学的高深学问“阴阳易理”;它既有简单易学的特性,为人民群众所喜闻乐见,又有深奥的技巧和高水平的国际性比赛;它的棋文化源渊流长,具有东方的神秘和西方的直观;既有“场”的概念,亦有“点”的连接。它是中西文化的交流点,是古今哲理的结晶。所以此次c++课程设计选择五子棋这个题目。
2. 程序所实现的功能
通过给电脑赋予人工智能,利用电脑运算速度快的特点,结合五子棋的游戏规则,给电脑设定固定的扫描方式,设定权值,使电脑能够选择最合适的落子点,然后调用加载已做好的棋盘棋子图像,最终实现简单的人机对弈。
3. 该游戏为普通玩家使用,只要连成五子即获胜.
三、设计内容
1.根据 所选题目,给出模块图
2.画出主程序及其主要模块的流程图
四、相关截图:
1、开始游戏的时候,会根据提示来选择游模式。
2、如果选的对手是电脑。则会提示是哪个级别的。初级、中级和高级。
3、系统会根据你下棋子来判断来怎么下。
4、在任何一方下到五个子的时候,系统会提示输赢,以及下棋过程中有无悔棋,和总共下了几手。
五、相关代码:
1.Pente.h
#if !defined(AFX_PENTE_H__9A01CC66_D589_11D1_92B0_000021003EA5__INCLUDED_)
#define AFX_PENTE_H__9A01CC66_D589_11D1_92B0_000021003EA5__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h" // main symbols
#define MODE_WITH_COMPUTER 0
#define MODE_2PLAYER 1
#define MODE_2PLAYER_NET 2
#define MODE_2PLAYER_MODEM 3
#define ID_TIMER_NET 100
#define MESSAGE_SIZE 512
/////////////////////////////////////////////////////////////////////////////
// CPenteApp:
// See Pente.cpp for the implementation of this class
//
class CPenteApp : public CWinApp
{
public:
CPenteApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPenteApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
// Implementation
//{{AFX_MSG(CPenteApp)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined
2. Pente.cpp :
#include "stdafx.h"
#include "Pente.h"
#include "PenteDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPenteApp
BEGIN_MESSAGE_MAP(CPenteApp, CWinApp)
//{{AFX_MSG_MAP(CPenteApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPenteApp construction
CPenteApp::CPenteApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CPenteApp object
CPenteApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CPenteApp initialization
BOOL CPenteApp::InitInstance()
{
// stardard initialization begin...
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
SetRegistryKey(_T("MultiMediaGuid"));
CPenteDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
3.AboutDlg.h
#if !defined(AFX_ABOUTDLG_H__701A5C62_DBBE_11D1_92B0_000021003EA5__INCLUDED_)
#define AFX_ABOUTDLG_H__701A5C62_DBBE_11D1_92B0_000021003EA5__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// AboutDlg.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog
class CAboutDlg : public CDialog
{
// Construction
public:
CAboutDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTDLG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CAboutDlg)
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined
4. AboutDlg.cpp :
#include "stdafx.h"
#include <dos.h>
#include <direct.h>
#include <mmsystem.h>
#include "resource.h"
#include "HelperAPI.h"
#include "Globalvar.h"
#include "AboutDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog
CAboutDlg::CAboutDlg(CWnd* pParent /*=NULL*/)
: CDialog(::g_bChinese ? IDD_ABOUTDLG1 : IDD_ABOUTDLG, pParent)
{
//{{AFX_DATA_INIT(CAboutDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg message handlers
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
SetIcon(hIcon, TRUE); // Set big icon
SetIcon(hIcon, FALSE); // Set small icon
CString str;
// fill memory info
MEMORYSTATUS MemStat;
MemStat.dwLength = sizeof(MEMORYSTATUS);
GlobalMemoryStatus(&MemStat);
// Total Physical mem
str.Format("%lu KB", MemStat.dwTotalPhys / 1024L);
SetDlgItemText(IDC_PHYSICAL_MEM, str);
// Free Physical mem
str.Format("%lu KB", MemStat.dwAvailPhys / 1024L);
SetDlgItemText(IDC_AVAIL_MEM, str);
// Free Virtual mem
str.Format("%lu KB", MemStat.dwAvailVirtual / 1024L);
SetDlgItemText(IDC_AVAIL_VIRTUAL_MEM, str);
// fill CPU info
SYSTEM_INFO SysInfo;
GetSystemInfo(&SysInfo);
switch (SysInfo.dwProcessorType)
{
case PROCESSOR_INTEL_486:
str = "Intel 80486";
break;
case PROCESSOR_INTEL_PENTIUM:
str = "Intel Pentium";
break;
default:
str = "";
break;
}
SetDlgItemText(IDC_CPU, str);
// fill OS info
OSVERSIONINFO OsInfo;
OsInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (GetVersionEx(&OsInfo))
{
// platform
switch (OsInfo.dwPlatformId)
{
case VER_PLATFORM_WIN32_WINDOWS:
str = "Windows 95";
break;
case VER_PLATFORM_WIN32_NT:
str = "Windows NT";
break;
default:
str = "";
break;
}
SetDlgItemText(IDC_OS, str);
// version and language
char szVer[512];
GetWindowsVersion((LPSTR)szVer);
str.Format("%ld.%ld, %s", OsInfo.dwMajorVersion,
OsInfo.dwMinorVersion, szVer);
SetDlgItemText(IDC_VERSION, str);
}
// fill disk information
char szWindowsPath[_MAX_PATH];
GetWindowsDirectory(szWindowsPath, _MAX_PATH);
ULARGE_INTEGER TotalNumberOfBytes, TotalNumberOfFreeBytes;
GetDiskFreeSpaceEx((LPSTR)szWindowsPath,
NULL,
&TotalNumberOfBytes,
&TotalNumberOfFreeBytes);
// total space
str.Format("%lu KB", TotalNumberOfBytes.QuadPart / 1024L);
SetDlgItemText(IDC_TOTAL_SPACE, str);
// free space
str.Format("%lu KB", TotalNumberOfFreeBytes.QuadPart / 1024L);
SetDlgItemText(IDC_FREE_SPACE, str);
PlaySound(IDSOUND_WELCOME);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
5. BestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Pente.h"
#include "Globalvar.h"
#include "BestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBestDlg dialog
CBestDlg::CBestDlg(CWnd* pParent /*=NULL*/)
: CDialog(::g_bChinese ? IDD_BEST1 : IDD_BEST)
{
//{{AFX_DATA_INIT(CBestDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CBestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBestDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBestDlg, CDialog)
//{{AFX_MSG_MAP(CBestDlg)
ON_BN_CLICKED(IDC_RESTARTCOUNT, OnRestartcount)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBestDlg message handlers
BOOL CBestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
Name1Edit().SetWindowText(g_strName1);
Name2Edit().SetWindowText(g_strName2);
Name3Edit().SetWindowText(g_strName3);
char str[4];
wsprintf(str,"%d",g_nTime1);
Time1Edit().SetWindowText(str);
wsprintf(str,"%d",g_nTime2);
Time2Edit().SetWindowText(str);
wsprintf(str,"%d",g_nTime3);
Time3Edit().SetWindowText(str);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CBestDlg::OnRestartcount()
{
g_strName1 = "Anonymous";
g_strName2 = "Anonymous";
g_strName3 = "Anonymous";
g_nTime1 = 225;
g_nTime2 = 225;
g_nTime3 = 225;
Name1Edit().SetWindowText(g_strName1);
Name2Edit().SetWindowText(g_strName2);
Name3Edit().SetWindowText(g_strName3);
Time1Edit().SetWindowText("225");
Time2Edit().SetWindowText("225");
Time3Edit().SetWindowText("225");
}
6. #if !defined(AFX_BESTDLG_H__0740A355_D3AB_11D1_A77A_444553540000__INCLUDED_)
#define AFX_BESTDLG_H__0740A355_D3AB_11D1_A77A_444553540000__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// BestDlg.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CBestDlg dialog
class CBestDlg : public CDialog
{
// Construction
public:
CBestDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CBestDlg)
enum { IDD = IDD_BEST };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
HICON m_hIcon;
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CBestDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
CEdit& Name1Edit()
{ return *(CEdit*)GetDlgItem(IDC_EDIT1); }
CEdit& Name2Edit()
{ return *(CEdit*)GetDlgItem(IDC_EDIT2); }
CEdit& Name3Edit()
{ return *(CEdit*)GetDlgItem(IDC_EDIT3); }
CEdit& Time1Edit()
{ return *(CEdit*)GetDlgItem(IDC_EDIT4); }
CEdit& Time2Edit()
{ return *(CEdit*)GetDlgItem(IDC_EDIT5); }
CEdit& Time3Edit()
{ return *(CEdit*)GetDlgItem(IDC_EDIT6); }
// Generated message map functions
//{{AFX_MSG(CBestDlg)
virtual BOOL OnInitDialog();
afx_msg void OnRestartcount();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_BESTDLG_H__0740A355_D3AB_11D1_A77A_444553540000__INCLUDED_)
六、 设计总结
课程设计是检验知识能力的一种途径,可以让我们学到许多书本中学不到的知识。
使我所学知识融会贯通,能熟练掌握c++中一些基本知识点,这是一次很好的实践机会,通过动手操作找出不足,不断提高自己。把我所学到的和我所想到的都运用设计一个自己的作品。通过课程设计培养了我的动手能力以及综合运用所学c++基本理论,基础知识,基本技能,进行程序开发和程序分析,提高在实际开发中解决实际问题的能力,达到了能够利用c++语言进行程序的规划,分析,设计和实施,更能进一步使我对这门语言有更深刻的理解和更好的得到巩固。更能对我所学的知识得到检验。