CFGReader

Console display board

    A. Second Edition

    These classes are all base classes for console displays and on this edition I finished a board class.

    B.The problem

    How to display in console without using iostream class?

C.The idea of program
¡¡

    These classes are for our comp354 project which is going to make a little game like cross word.

    D.The major functions

    E.Further improvement

    F.File listing

    1. tile.h

    2. tile.cpp

    3. tiles.h

    4. tiles.cpp

    5. token.h

    6. token.cpp

    7. tokens.h

    8. tokens.cpp

    9. RoverType.h

    10. board.h

    11. board.cpp

    12. cell.h

    13. cell.cpp

    14. block.h

    15. block.cpp

    10. main.cpp (main)

¡¡

file name: tile.h

    #ifndef TILE_H
    #define TILE_H

    #include "RoverType.h"

    class CTile
    {
    protected:
    	char ch;
    	WORD ftColor;
    	WORD bkColor;
    public:
    	void display(int x, int y);
    	void setFontColor(WORD theColor){ftColor=theColor;}
    	void setBKGroundColor(WORD theColor){ bkColor=theColor;}
    	void setValue(char myCh) { ch=myCh;}
    	char getValue(){ return ch;}
    	CTile(char myCh=DefaultCharacter);
    };

    #endif

    ¡¡

file name: tile.cpp

    #include "Tile.h"

    HANDLE hOutPut=0;

    CTile::CTile(char myCh)
    {
    	ftColor=DefaultFontColor;
    	bkColor=DefaultBKGroundColor;
    	setValue(myCh);
    	if (hOutPut==0)
    	{
    		hOutPut=GetStdHandle(STD_OUTPUT_HANDLE);
    	}
    }

    void CTile::display(int x, int y)
    {
    	COORD coord;
    	coord.X=x;
    	coord.Y=y;
    	FillConsoleOutputAttribute(hOutPut, ftColor|bkColor, 1, coord, NULL);
    	FillConsoleOutputCharacter(hOutPut, ch, 1, coord, NULL);
    }

    file name: tiles.h

    #ifndef TILES_H
    #define TILES_H

    #include "tile.h"

    class CTiles
    {
    private:
    	CTile* tiles;
    	int len;
    	WORD ftColor;
    	WORD bkColor;
    	bool isVertical;
    	void initialize();
    public:
    	CTiles(const char* str);
    	CTiles();
    	~CTiles();
    	void setDirection(bool beVertical){ isVertical=beVertical;}
    	CTile& operator[](int index){return tiles[index];}
    	void setFontColor(WORD theColor){ftColor=theColor;}
    	void setBKGroundColor(WORD theColor){bkColor=theColor;}
    	int getLength(){return len;}
    	void display(int x, int y);
    	void setValue(const char* str);
    	void setLength(int size);
    };

    #endif

    ¡¡

    file name: tiles.cpp

    #include "tiles.h"

    CTiles::CTiles()
    {
    	initialize();
    }

    void CTiles::initialize()
    {
    	len=0;
    	tiles=NULL;
    	isVertical=false;
    	ftColor=DefaultFontColor;
    	bkColor=DefaultBKGroundColor;
    }

    CTiles::~CTiles()
    {
    	delete[]tiles;
    }

    void CTiles::setValue(const char* str)
    {
    	//len=;
    	setLength(strlen(str));
    	for (int i=0; i<len; i++)
    	{
    		tiles[i].setValue(str[i]);
    	}
    }

    CTiles::CTiles(const char* str)
    {
    	initialize();
    	setValue(str);
    }

    void CTiles::display(int x, int y)
    {
    	for (int i=0; i<len; i++)
    	{
    		if (!isVertical)
    		{
    			tiles[i].display(x+i, y);
    		}
    		else
    		{
    			tiles[i].display(x, y+i);
    		}
    	}
    }

    void CTiles::setLength(int size)
    {
    	if (len!=size)
    	{
    		len=size;
    		delete[]tiles;
    		tiles=new CTile[len] ;
    	}
    }

    file name: token.h

    #ifndef TOKEN_H
    #define TOKEN_H

    #include "block.h"

    class CToken : public CBlock
    {
    protected:

    public:
    	CToken(int num);
    	CToken(char ch=DefaultCharacter);
    	const CToken& operator=(const CToken& theToken);
    	int getNumber();
    	void setNumber(int num);
    	void setSuit(int theSuit);
    };

    #endif

    ¡¡

    file name: token.cpp

    #include "token.h"
    #include "tile.h"

    const CToken& CToken::operator =(const CToken& theToken)
    {
    	for (int i=0; i<3; i++)
    	{
    		for (int j=0; j<3; j++)
    		{
    			block[i][j].setValue(theToken.block[i][j].getValue());
    		}
    	}
    	return *this;
    }

    void CToken::setSuit(int theSuit)
    {
    	block[0][0].setValue(theSuit);
    	block[2][2].setValue(theSuit);
    }

    CToken::CToken(char ch):CBlock(3)
    {
    	block[1][1].setValue(ch);
    }

    int CToken::getNumber()
    {
    	return (block[1][0].getValue()-'0')*10+block[1][1].getValue()-'0';
    }

    void CToken::setNumber(int num)
    {
    	//in no condition, this would happen
    	if (num>99)
    	{
    		return ;
    	}
    	if (num>9)
    	{
    		block[1][0].setValue(num/10+'0');
    		block[1][1].setValue(num%10+'0');
    	}
    	else
    	{
    		block[1][1].setValue(num+'0');
    	}
    }

    CToken::CToken(int num)
    {
    	initialize();
    	setNumber(num);
    }

    ¡¡

    file name: tokens.h

    #ifndef TOKENS_H
    #define TOKENS_H

    #include "token.h"

    class CTokens
    {
    protected:
    	CToken* tokens;
    	int len;
    	WORD ftColor;
    	WORD bkColor;
    	WORD frColor;
    	bool isVertical;//default is false
    	void initialize();
    public:
    	CTokens(int theLen=DefaultTokenLength);
    	CTokens(const char* str);
    	CToken& operator[](unsigned short index){ return tokens[index];}
    	void setDirection(bool beVertical){isVertical=beVertical;}
    	void setLength(int size);
    	void setValue(const char* str);
    	void setFontColor(WORD theColor);
    	void setBKGroundColor(WORD theColor);
    	void setFrameColor(WORD theColor);
    	~CTokens();
    	void display(int x, int y);
    };

    #endif

    file name: tokens.cpp

    #include "tokens.h"

    CTokens::CTokens(int theLen)
    {
    	initialize();
    	setLength(theLen);
    }

    CTokens::CTokens(const char* str)
    {
    	initialize();
    	setValue(str);
    }

    void CTokens::initialize()
    {
    	len=0;
    	tokens=NULL;
    	isVertical=false;
    	ftColor=DefaultFontColor;
    	bkColor=DefaultBKGroundColor;
    	frColor=DefaultFrameColor;
    }

    void CTokens::setBKGroundColor(WORD theColor)
    {
    	for (int i=0; i<len; i++)
    	{
    		tokens[i].setBKGroundColor(theColor);
    	}
    }

    void CTokens::setFontColor(WORD theColor)
    {
    	for (int i=0; i<len; i++)
    	{
    		tokens[i].setFontColor(theColor);
    	}
    }

    void CTokens::setFrameColor(WORD theColor)
    {
    	for (int i=0; i<len; i++)
    	{
    		tokens[i].setFrameColor(theColor);
    	}
    }

    void CTokens::setLength(int size)
    {
    	if (len!=size)
    	{
    		delete []tokens;
    		len=size;
    		tokens=new CToken[len];
    	}
    }

    void CTokens::setValue(const char* str)
    {
    	len=strlen(str);
    	//tokens=new CToken[len];
    	setLength(len);
    	for (int i=0; i<len; i++)
    	{
    		tokens[i].setValue(str[i]);
    	}
    }

    void CTokens::display(int x, int y)
    {
    	for (int i=0; i<len; i++)
    	{
    		if (!isVertical)
    		{
    			tokens[i].display(x+i*4, y);
    		}
    		else
    		{
    			tokens[i].display(x, y+i*4);
    		}
    	}
    }

    CTokens::~CTokens()
    {
    	delete [] tokens;
    }

    ¡¡

    file name: RoverType.h

    #include <windows.h>

    //the font color
    #define  FTBLUE        FOREGROUND_BLUE
    #define	 FTGREEN       FOREGROUND_GREEN
    #define	 FTRED         FOREGROUND_RED
    #define	 FTPURPLE      FOREGROUND_BLUE|FOREGROUND_RED
    #define	 FTGREY        FOREGROUND_BLUE|FOREGROUND_GREEN
    #define	 FTBROWN	   FOREGROUND_RED|FOREGROUND_GREEN,
    #define	 FTWHITE       FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED
    #define	 FTINTENSITY   FOREGROUND_INTENSITY

    //the background color
    #define	 BKBLUE        BACKGROUND_BLUE
    #define  BKGREEN       BACKGROUND_GREEN
    #define	 BKRED         BACKGROUND_RED
    #define	 BKPURPLE      BACKGROUND_BLUE|BACKGROUND_RED
    #define	 BKGREY        BACKGROUND_BLUE|BACKGROUND_GREEN
    #define	 BKBROWN	   BACKGROUND_RED|BACKGROUND_GREEN
    #define	 BKWHITE       BACKGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_RED
    #define	 BKINTENSITY   BACKGROUND_INTENSITY

    #define DefaultFontColor           FTRED
    #define DefaultBKGroundColor       BKWHITE
    #define DefaultFrameColor          BKGREY
    #define DefaultIndexFontColor      FTWHITE
    #define DefaultIndexBKGroundColor  BKBLUE
    #define DefaultIndexFrameColor     BKBLUE
    #define DefaultBoardSize		   15

    #define DefaultCharacter           0
    #define DefaultTokenLength         7
    /*
    enum Suit
    {Club=5, Diamond=4, Heart=3, Spade=6};
    */
    #define Club           5
    #define Diamond        4
    #define Heart          3
    #define Spade          6

    file name: board.h

    #ifndef BOARD_H
    #define BOARD_H

    #include "cell.h"

    class CBoard
    {
    private:
    	CCell** board;
    	int boardWidth;
    	void initialize(int size);
    public:
    	CBoard(int size=DefaultBoardSize);
    	void setSize(int size);
    	void display(int x, int y);
    	char getValue(int r, int c);
    	void setValue(int r, int c, char ch);
    	void setValue(char** matrix);
    };

    #endif

    ¡¡

    file name: board.cpp

    #include "board.h"

    void CBoard::initialize(int size)
    {
    	setSize(size);
    	for (int i=0; i<=size; i++)
    	{
    		for (int j=0; j<=size; j++)
    		{
    			if (i==0||j==0)
    			{
    				board[i][j].setBKGAll(DefaultIndexFontColor, DefaultIndexBKGroundColor);

    				if (i!=0)
    				{
    					board[i][j].setNumber(i-1, false);
    				}
    				if (j!=0)
    				{
    					board[i][j].setNumber(j-1, true);
    				}
    			}
    		}
    	}
    }

    void CBoard::setValue(char** matrix)
    {
    	for (int i=1; i<=boardWidth; i++)
    	{
    		for (int j=1; j<=boardWidth; j++)
    		{
    			board[i][j].setValue(matrix[i-1][j-1]);
    		}
    	}
    }

    void CBoard::setSize(int size)
    {
    	if (boardWidth!=size)
    	{
    		if (board!=NULL)
    		{
    			for (int i=0; i<=boardWidth; i++)
    			{
    				delete []board[i];
    			}
    		}
    		delete[] board;
    		boardWidth=size;
    		board=new CCell*[size+1];
    		for (int i=0; i<=size; i++)
    		{
    			board[i]=new CCell[size+1];
    		}
    	}
    }

    /*
    	column.setBKGroundColor(DefaultIndexBKGroundColor);
    	column.setFontColor(DefaultIndexFontColor);
    	row.setBKGroundColor(DefaultIndexBKGroundColor);
    	row.setFontColor(DefaultIndexFontColor);
    	*/

    CBoard::CBoard(int size)
    {
    	boardWidth=0;
    	board=NULL;
    	initialize(size);
    }

    void CBoard::setValue(int r, int c, char ch)
    {
    	board[r+1][c+1].setValue(ch);
    }

    char CBoard::getValue(int r, int c)
    {
    	return board[r+1][c+1].getValue();
    }

    void CBoard::display(int x, int y)
    {
    	for (int i=0; i<=boardWidth; i++)
    	{
    		for (int j=0; j<=boardWidth; j++)
    		{
    			board[i][j].display(x+2*j, y+i*2);
    		}

    	}
    }

    ¡¡

    ¡¡

    file name: cell.h

    #ifndef CELL_H
    #define CELL_H

    #include "block.h"

    class CCell: public CBlock
    {
    public:
    	CCell();
    	void setNumber(int num, bool atBottom);//special for index bar
    	void setBKGAll(WORD theFont, WORD theBKG);
    };

    #endif

    ¡¡

    ¡¡

    file name: cell.cpp

    #include "cell.h"

    CCell::CCell():CBlock(1)
    {
    }

    void CCell::setNumber(int num, bool atBottom)
    {
    	int y=0;
    	if (atBottom)
    	{
    		y=1;
    	}
    	if (num>9)
    	{
    		block[0][y].setValue(num/10+'0');
    		block[1][y].setValue(num%10+'0');
    	}
    	else
    	{
    		block[0][y].setValue(num+'0');
    	}
    }

    void CCell::setBKGAll(WORD theFont, WORD theBKG)
    {
    	for (int i=0; i<2; i++)
    	{
    		for (int j=0; j<2; j++)
    		{
    			block[i][j].setBKGroundColor(theBKG);
    			block[i][j].setFontColor(theFont);
    		}
    	}
    }

    ¡¡

    file name: block.h

    #ifndef BLOCK_H
    #define BLOCK_H

    #include "tile.h"

    class CBlock
    {
    protected:
    	CTile** block;
    	int width;
    	void initialize();
    public:
    	CBlock(int size=1);
    	void setLength(int size);
    	void setValue(char myCh);
    	char getValue();
    	void setFrameColor(WORD theColor);
    	void setFontColor(WORD theColor);
    	void setBKGroundColor(WORD theColor);
    	void display(int x, int y);
    };

    #endif

    ¡¡

    ¡¡

    file name: block.cpp

    #include "block.h"

    CBlock::CBlock(int size)
    {
    	initialize();
    	setLength(size);
    }

    void CBlock::initialize()
    {
    	block=NULL;
    	width=0;
    }

    void CBlock::setLength(int size)
    {
    	width=size;
    	block=new CTile*[size+1];
    	for (int i=0; i<=size; i++)
    	{
    		block[i]=new CTile[size+1];
    		for (int j=0; j<=size; j++)
    		{
    			if (i==size||j==size)
    			{
    				block[i][j].setBKGroundColor(DefaultFrameColor);
    			}
    		}
    	}
    }

    void CBlock::setValue(char myCh)
    {
    	block[(width-1)/2][(width-1)/2].setValue(myCh);
    }

    void CBlock::setBKGroundColor(WORD theColor)
    {
    	for (int i=0; i<width; i++)
    	{
    		for (int j=0; j<width; j++)
    		{
    			block[i][j].setBKGroundColor(theColor);
    		}
    	}
    }

    void CBlock::setFontColor(WORD theColor)
    {
    	for (int i=0; i<width; i++)
    	{
    		for (int j=0; j<width; j++)
    		{
    			block[i][j].setFontColor(theColor);
    		}
    	}
    }

    void CBlock::setFrameColor(WORD theColor)
    {
    	for (int i=0; i<=width; i++)
    	{
    		for (int j=0; j<=width; j++)
    		{
    			if (i==width||j==width)
    			{
    				block[i][j].setBKGroundColor(theColor);
    			}
    		}
    	}
    }

    char CBlock::getValue()
    {
    	return block[(width-1)/2][(width-1)/2].getValue();
    }

    void CBlock::display(int x, int y)
    {
    	for (int i=0; i<=width; i++)
    	{
    		for (int j=0; j<=width; j++)
    		{
    			block[i][j].display(x+i, y+j);
    		}
    	}
    }

    ¡¡

    ¡¡

    file name: main.cpp(main)

    #include "tokens.h"
    #include "tiles.h"
    #include "board.h"
    #include "block.h"

    int  main()
    {
    	const int Size=14;
    	char** matrix;

    	matrix=new char*[Size];
    	for (int i=0; i<Size; i++)
    	{
    		matrix[i]=new char[Size];
    		for (int j=0; j<Size; j++)
    		{
    			matrix[i][j]='A'+i+j;
    		}
    	}
    	CBoard C(Size);
    	C.setValue(matrix);
    	C.display(1,1);
    	return 0;

    }

    The input is something like following:

    Here is the result:

¡¡