"The Same Game": A Simple Game from Start to Finish
文档视图结构
文档/视图体系结构是一个神奇的架构,通过这个架构,我们可以分离应用程序的数据和显示。文档包含了全部的数据,视图从文档获取数据并且显示给用户。针对这个游戏例子,我们得数据是游戏板和有颜色的砖块以及消耗的时间等信息。视图显示游戏板和有颜色的砖块,并且允许用户点击他们。视图同时与用户交互,并且修改文档中的游戏数据,相应地视图被更新以反应数据的变化,如此循环。
文档: 保有数据
终于来到编码时间. 在显示任何数据到屏幕之前,我们要设计好数据的保存方式,所以我们将先从文档部分开始,然后再考虑如何实现将这些数据显示出来。
首先,我们创建一个游戏板类型,让我们叫它CSameGameBoard。通过在解决方案浏览界面(Solution Explorer)单击鼠标右键,然后选择"Add -> Class..." or "Add Class..." 。
在弹出的Generic C++ Class Wizard界面中,我们填上类名CSameGameBoard。
下面是CSameGameBoard类的头文件:
#pragma once class CSameGameBoard
{
public:
/* Default Constructor */
CSameGameBoard(void);
/* Destructor */
~CSameGameBoard(void);
/* Function to randomly setup the board */
void SetupBoard(void);
/* Get the color at a particular location */
COLORREF GetBoardSpace(int row, int col);
/* Accessor functions to get board size information */
int GetWidth(void) const { return m_nWidth; }
int GetHeight(void) const { return m_nHeight; }
int GetColumns(void) const { return m_nColumns; }
int GetRows(void) const { return m_nRows; }
/* Function to delete the board and free memory */
void DeleteBoard(void);
private:
/* Function to create the board and allocate memory */
void CreateBoard(void);
/* 2D array pointer */
int** m_arrBoard;
/* List of colors, 0 is background and 1-3 are piece colors */
COLORREF m_arrColors[4];
/* Board size information */
int m_nColumns;
int m_nRows;
int m_nHeight;
int m_nWidth;
};
这个类是十分简单的,它包含了一个指针,叫做m_arrBoard,它是一个二维数组,数组的每一个元素的取值为0或三个颜色之一(1-3),这个变量代表了我们看到的整个彩色砖块的全部。我们也添加了跟踪砖块行数 (m_nRows)和列数(m_nColumns)的变量,砖块宽度 (m_nHeight)和砖块高度 (m_nHeight)的变量,以及管理游戏板的一些函数。
create函数需要给二维数组m_arrBoard分配空间来存储游戏板数据,并且初始化所有的砖块为空。setup函数将reset游戏板,并游戏板上的每一个砖块随机地选择一种颜色。最后delete函数重新分配游戏板内存并释放掉之前的内存以防止内存泄漏。
在游戏板类中,有一个COLORREF类型的数组,COLORREF 存储了一个32位的颜色值。这个数组包含了四种颜色:0代表背景色,以及1-3,代表砖块可能的三种颜色(红黄蓝)。二维数组m_arrBoard的每一个元素中存储着这里的四种颜色索引之一。
以下是CSameGameBoard类的实现,在SameGameBoard.cpp文件中。
#include "StdAfx.h"
#include "SameGameBoard.h" CSameGameBoard::CSameGameBoard(void)
: m_arrBoard(NULL),
m_nColumns(15), m_nRows(15),
m_nHeight(35), m_nWidth(35)
{
m_arrColors[0] = RGB( 0, 0, 0);
m_arrColors[1] = RGB(255, 0, 0);
m_arrColors[2] = RGB(255,255, 64);
m_arrColors[3] = RGB( 0, 0,255);
} CSameGameBoard::~CSameGameBoard(void)
{
// Simply delete the board
DeleteBoard();
} void CSameGameBoard::SetupBoard(void)
{
// Create the board if needed
if(m_arrBoard == NULL)
CreateBoard();
// Randomly set each square to a color
for(int row = 0; row < m_nRows; row++)
for(int col = 0; col < m_nColumns; col++)
m_arrBoard[row][col] = (rand() % 3) + 1;
} COLORREF CSameGameBoard::GetBoardSpace(int row, int col)
{
// Check the bounds of the array
if(row < 0 || row >= m_nRows || col < 0 || col >= m_nColumns)
return m_arrColors[0];
return m_arrColors[m_arrBoard[row][col]];
} void CSameGameBoard::DeleteBoard(void)
{
// Don't delete a NULL board
if(m_arrBoard != NULL)
{
for(int row = 0; row < m_nRows; row++)
{
if(m_arrBoard[row] != NULL)
{
// Delete each row first
delete [] m_arrBoard[row];
m_arrBoard[row] = NULL;
}
}
// Finally delete the array of rows
delete [] m_arrBoard;
m_arrBoard = NULL;
}
} void CSameGameBoard::CreateBoard(void)
{
// If there is already a board, delete it
if(m_arrBoard != NULL)
DeleteBoard();
// Create the array of rows
m_arrBoard = new int*[m_nRows];
// Create each row
for(int row = 0; row < m_nRows; row++)
{
m_arrBoard[row] = new int[m_nColumns];
// Set each square to be empty
for(int col = 0; col < m_nColumns; col++)
m_arrBoard[row][col] = 0;
}
}
现在我们已经将游戏板封装成了一个类型,接下来,我们可以在文档类中,创建一个这个类型的实例。需要牢记的是,文档类拥有我们应用的全部数据,它和显示数据的视图类保持分离。下面是文档类的头文件 SameGameDoc.h:
#pragma once #include "SameGameBoard.h" class CSameGameDoc : public CDocument
{
protected: // create from serialization only
CSameGameDoc();
virtual ~CSameGameDoc();
DECLARE_DYNCREATE(CSameGameDoc) // Attributes
public: // Operations
public:
/* Functions for accessing the game board */
COLORREF GetBoardSpace(int row, int col)
{ return m_board.GetBoardSpace(row, col); }
void SetupBoard(void) { m_board.SetupBoard(); }
int GetWidth(void) { return m_board.GetWidth(); }
int GetHeight(void) { return m_board.GetHeight(); }
int GetColumns(void) { return m_board.GetColumns(); }
int GetRows(void) { return m_board.GetRows(); }
void DeleteBoard(void) { m_board.DeleteBoard(); }
// Overrides
public:
virtual BOOL OnNewDocument(); protected:
/* Instance of the game board */
CSameGameBoard m_board;
// Generated message map functions
protected:
DECLARE_MESSAGE_MAP()
};
文档类实际上是一个游戏板类的简单的封装,在后面的文章中我们将增加更多的功能到这里。但是目前它是保持非常简单的,我们增加了一个游戏板类的实例和7个函数,这将允许视图类接入我们的游戏板类的信息经由文档类。文档类所有功能的实现是非常简单的,它们都是间接的调用了游戏板类。
#include "stdafx.h"
#include "SameGame.h" #include "SameGameDoc.h" #ifdef _DEBUG
#define new DEBUG_NEW
#endif // CSameGameDoc
IMPLEMENT_DYNCREATE(CSameGameDoc, CDocument)
BEGIN_MESSAGE_MAP(CSameGameDoc, CDocument)
END_MESSAGE_MAP() // CSameGameDoc construction/destruction
CSameGameDoc::CSameGameDoc()
{
} CSameGameDoc::~CSameGameDoc()
{
} BOOL CSameGameDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// Set (or reset) the game board
m_board.SetupBoard();
return TRUE;
}
Really all we added was the call to the SetupBoard function in the OnNewDocument handler in the document. All this does is allows the user to start a new game with the built-in accelerator Ctrl+N or from the menu File->New.
As we continue through the series of articles we'll be adding new functions to both the game board and the document to implement different features for the game but for now we are done with the document and are ready to display this information in the view.
"The Same Game": A Simple Game from Start to Finish的更多相关文章
- git的一些配置
git使用socks代理加速 原来git可以配置socks代理的,真好,从github上clone了opencv的代码,基本上是满速了. 首先需要配置shadowsocks,然后通过GUI客户端(或命 ...
- PHP设计模式(一)简单工厂模式 (Simple Factory For PHP)
最近天气变化无常,身为程序猿的寡人!~终究难耐天气的挑战,病倒了,果然,程序猿还需多保养自己的身体,有句话这么说:一生只有两件事能报复你:不够努力的辜负和过度消耗身体的后患.话不多说,开始吧. 一.什 ...
- Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】
原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...
- WATERHAMMER: A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION
开启阅读模式 WATERHAMMER A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION Waterhammer is an impact load that is ...
- BZOJ 3489: A simple rmq problem
3489: A simple rmq problem Time Limit: 40 Sec Memory Limit: 600 MBSubmit: 1594 Solved: 520[Submit] ...
- Le lié à la légèreté semblait être et donc plus simple
Il est toutefois vraiment à partir www.runmasterfr.com/free-40-flyknit-2015-hommes-c-1_58_59.html de ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- 设计模式之简单工厂模式Simple Factory(四创建型)
工厂模式简介. 工厂模式专门负责将大量有共同接口的类实例化 工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类. 工厂模式有三种形态: 1.简单工厂模式Simple Factory ...
- HDU 5795 A Simple Nim 打表求SG函数的规律
A Simple Nim Problem Description Two players take turns picking candies from n heaps,the player wh ...
随机推荐
- bzoj2732: [HNOI2012]射箭 半平面交
这题乍一看与半平面交并没有什么卵联系,然而每个靶子都可以转化为两个半平面. scanf("%lf%lf%lf",&x,&ymin,&ymax); 于是乎就有 ...
- STL库之单链表:forward_list
class template forward_list <forward_list> template < class T, class Alloc = allocator<T ...
- Linux运维工程师面试
一.Linux操作系统知识 1.常见的Linux发行版本都有什么?你最擅长哪一个?它的官网网站是什么?说明你擅长哪一块? 2.Linux开机启动流程详细步骤是什么?系统安装完,忘记密码如何破解? ...
- 系统监控的一些工具w , vmstat
w 命令:--w 查看的是系统整体上的负载 # w 15:23:46 up 3:34, 2 users, load average: 0.03, 0.05, 0.00 USER TTY FROM LO ...
- php 常用五种模式
/* 设计模式之单例模式 $_instance 必须声明为静态的私有变量 构造函数必须声明为私有,防止外部程序 new 类从而失去单例模式的意义 getInstance() 方法必须设置为公有的,必须 ...
- 如何用angularjs制作一个完整的表格之三__在ng-repeat中使用ng-model
在ng-repeat中使用ng-model时会有许多问,有的人碰到无法获取绑定的数据内容,有的人遇到改动绑定的数据内容时所有循环生成的内容一起改变.上面的问题我在开发时也遇到过,但是解决后我却怎么也还 ...
- 在Django中使用Mako模板
用了一个月后,终于忍受不了Django的模板了.主要原因是模板内不能运行原生的python语句,所以用起来总感觉被人绑住手脚,遍历个字典都要搞半天. 所以决定用第三方的模板.查了一下,django用的 ...
- Swift与Objective-C的兼容“黑魔法”:@objc和Dynamic
Cocoa框架早已烙上了不可磨灭的OC印记,而无数的第三方库都是用OC写成的,这些积累无论是谁都不能小觑.苹果采取了允许开发者在同一个项目中同时使用Swift和OC进行开发的做法,但要想实现互通,又需 ...
- 自定义modal一个控制器的效果, presentViewController
presentViewController 一.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UI ...
- C++默认参数值函数
1.默认参数值的函数 C++语言允许在定义函数时给其中或某些形式参数(形参)指定默认值,方法就是在相应的形参后面写上“=默认值”,如果省略了对应位置上的实参的值,则在执行被调函数时以该形参的默认值进行 ...