文档视图结构

文档/视图体系结构是一个神奇的架构,通过这个架构,我们可以分离应用程序的数据和显示。文档包含了全部的数据,视图从文档获取数据并且显示给用户。针对这个游戏例子,我们得数据是游戏板和有颜色的砖块以及消耗的时间等信息。视图显示游戏板和有颜色的砖块,并且允许用户点击他们。视图同时与用户交互,并且修改文档中的游戏数据,相应地视图被更新以反应数据的变化,如此循环。

文档: 保有数据

终于来到编码时间. 在显示任何数据到屏幕之前,我们要设计好数据的保存方式,所以我们将先从文档部分开始,然后再考虑如何实现将这些数据显示出来。

首先,我们创建一个游戏板类型,让我们叫它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的更多相关文章

  1. git的一些配置

    git使用socks代理加速 原来git可以配置socks代理的,真好,从github上clone了opencv的代码,基本上是满速了. 首先需要配置shadowsocks,然后通过GUI客户端(或命 ...

  2. PHP设计模式(一)简单工厂模式 (Simple Factory For PHP)

    最近天气变化无常,身为程序猿的寡人!~终究难耐天气的挑战,病倒了,果然,程序猿还需多保养自己的身体,有句话这么说:一生只有两件事能报复你:不够努力的辜负和过度消耗身体的后患.话不多说,开始吧. 一.什 ...

  3. Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】

    原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...

  4. WATERHAMMER: A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION

    开启阅读模式 WATERHAMMER A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION Waterhammer is an impact load that is ...

  5. BZOJ 3489: A simple rmq problem

    3489: A simple rmq problem Time Limit: 40 Sec  Memory Limit: 600 MBSubmit: 1594  Solved: 520[Submit] ...

  6. 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 ...

  7. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  8. 设计模式之简单工厂模式Simple Factory(四创建型)

    工厂模式简介. 工厂模式专门负责将大量有共同接口的类实例化 工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类. 工厂模式有三种形态: 1.简单工厂模式Simple Factory ...

  9. HDU 5795 A Simple Nim 打表求SG函数的规律

    A Simple Nim Problem Description   Two players take turns picking candies from n heaps,the player wh ...

随机推荐

  1. 15_AOP入门准备_静态代理模式

    [工程截图] [PersonDao.java] package com.HigginCui.daoProxy; public interface PersonDao { public void sav ...

  2. vim 自動化配置

    Vim是Linux系統上常用的編輯器/Text Editor.不過很多人由於不瞭解如何配置,增加了很多煩惱. 今天介紹一個自動化的配置spf13,直接下載製作好的配置並進行自動設置. 1.官方的安裝步 ...

  3. MongoDB源码分析——mongod程序源码入口分析

    Edit 说明:第一次写笔记,之前都是看别人写的,觉得很简单,开始写了之后才发现真的很难,不知道该怎么分析,这篇文章也参考了很多前辈对MongoDB源码的分析,也有一些自己的理解,后续将会继续分析其他 ...

  4. 处理safari缓存的办法

    window.onpageshow = function(event) {        if (event.persisted) {             alert("From bac ...

  5. Oracle课堂实验一“表的使用”代码。

    --创建本地管理表空间CustomerTBSCREATE TABLESPACE CustomerTBS         DATAFILE 'd:\Oracle11\product\11.2.0\ora ...

  6. GitHub使用教程for Eclipse

    1.下载egit插件http://www.eclipse.org/egit/ http://www.eclipse.org/egit/download/ Installing the Latest R ...

  7. arm-linux-gcc编译器定义寄存器变量

    uboot代码中有这么一句话“#define DECLARE_GLOBAL_DATA_PTR     register volatile gd_t *gd asm ("r8")”, ...

  8. C# return dynamic/anonymous type value as function result

    function: public static dynamic GetAppSecret() { //string[] result = new string[] { "", &q ...

  9. java 容器类大集结

    这个世界是程序员的世界,归根到底是数据的世界,要统治这个世界,首先要学会征服数据. 没有最好的,只有最合适的,如何在不同的环境先选择最优的存储的结构呢?且看下文分解: 以下内容部分来自网络,参考: h ...

  10. 240多个jQuery插件

    概述 jQuery 是继 prototype 之后又一个优秀的 Javascript 框架.其宗旨是—写更少的代码,做更多的事情.它是轻量级的 js 库(压缩后只有21k) ,这是其它的 js 库所不 ...