"The same game": a simple game from start to finish

Source: Internet
Author: User
Document view structure

Document/view architecture is a magical architecture through which data and display of applications can be separated. The document contains all the data. The view obtains data from the document and displays it to the user. For this game example, we have to use game boards, color bricks, time consumed, and other information. The view displays game boards and colored bricks and allows users to click on them. The view interacts with the user at the same time, and the game data in the document is modified. The view is updated accordingly to reflect data changes.

Document: retain data

The encoding time has finally arrived. before displaying any data to the screen, we need to design a data storage method, so we will start from the document section and then consider how to display the data.

First, we create a game board type. Let's call it csamegameboard. Right-click Solution Explorer and select "Add-> class..." or "add class ...".

In the pop-up generic C ++ Class Wizard interface, enter the class name csamegameboard.

The following is the header file of the csamegameboard class:

#pragma onceclass 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;};

This class is very simple. It contains a pointer called m_arrboard, which is a two-dimensional array, the value of each element in the array is 0 or one of the three colors (1-3). This variable represents all the color bricks we see. We also added variables for tracking the number of bricks (m_nrows) and number of columns (m_ncolumns), the width of bricks (m_nheight) and the height of bricks (m_nheight), and some functions for managing the game board.

The create function needs to allocate space to the two-dimensional array m_arrboard to store game board data, and all the bricks for initialization are empty. The setup function will reset the game board and select a random color for each brick on the game board. Finally, the delete function re-allocates the game board memory and releases the previous memory to prevent memory leakage.

In the game board class, there is an array of the colorref type, and colorref stores a 32-bit color value. This array contains four colors: 0 represents the background color, and 1-3 represents three possible colors (red, yellow, and blue) of bricks ). Each element of the Two-dimensional array m_arrboard stores one of the four color indexes.

The following is the implementation of the csamegameboard class in the samegameboard. cpp file.

#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;  }}

Now we have encapsulated the game board into a type. Next, we can create an instance of this type in the document class. It should be noted that the document class has all the data of our application, which is isolated from the View class of the displayed data. The following is the header file samegamedoc. H of the document class:

#pragma once#include "SameGameBoard.h"class CSameGameDoc : public CDocument{protected: // create from serialization only  CSameGameDoc();  virtual ~CSameGameDoc();  DECLARE_DYNCREATE(CSameGameDoc)// Attributespublic:  // Operationspublic:  /*  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(); }  // Overridespublic:  virtual BOOL OnNewDocument();protected:  /*  Instance of the game board */  CSameGameBoard m_board;  // Generated message map functionsprotected:  DECLARE_MESSAGE_MAP()};

The document class is actually a simple encapsulation of the game board class. More functions will be added here in the following articles. However, it is very simple at present. We have added an instance of the game board class and Seven functions, which will allow the View class to access the information of our game board class through the document class. The implementation of all functions of the document class is very simple. They all indirectly call the game board class.

#include "stdafx.h"#include "SameGame.h"#include "SameGameDoc.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// CSameGameDocIMPLEMENT_DYNCREATE(CSameGameDoc, CDocument)BEGIN_MESSAGE_MAP(CSameGameDoc, CDocument)END_MESSAGE_MAP()// CSameGameDoc construction/destructionCSameGameDoc::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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.