扫雷-添加输出边框效果(2013-08-24)

// Miner_Console.cpp : 定义控制台应用程序的入口点。   
//   
  
#include "stdafx.h"   
#include<stdio.h>         
#include<stdlib.h>         
#include<time.h>         
  
#define nMine   10          // Number of mines       
#define nRow    10       
#define nCol    8       
  
class Miner  
{  
public:  
    Miner(int,int,int);  
    Miner();  
    int *m_MineLoc();  
    int *m_PlateSet(int *Loc1D);  
    void TextOutFull(int *PlateStateP);  
    void TextOut(int *PlateStateP);  
  
private:  
    int m_Row;  
    int m_Col;  
    int m_Area;           
    int m_MineNum;  
    int *m_LocArray;  
    int *m_PlateStateP;  
    int m_IndexRow,m_IndexCol;  
};  
  
//default parameters   
Miner::Miner()  
{  
    m_Row=10;  
    m_Col=10;  
    m_MineNum=10;  
    m_Area=m_Row*m_Col;   
  
}  
  
// parameters set by user.   
Miner::Miner(int a,int b,int c)  
{  
    // The length of each "square" should be added with 2, ...   
    // ... e.g. , if we want a 3-rowed matrix, the "m_Row" should be defined "5". ...   
    // ... for in the following "generating" part ignores the data on the 1st and last rows/columns, ...   
    // ... and the "print" part will hide those.   
    m_Row=a;  
    m_Col=b;  
    m_MineNum=c;  
    m_Area=m_Row*m_Col;  
}  
  
//Miner::m_FiledGen()   
  
int *Miner::m_MineLoc()  
{  
  
    int *(m_LocArray)=new int [m_MineNum*sizeof(int)];  
  
    srand(time(NULL));  
    for(int q=0;q<m_MineNum;q++)      
    {     
        //      1D location of the mines    ->   
        *(m_LocArray+q)=(rand()%(m_Row*m_Col));  
  
        // Avoid overlap     
        {  
            for(int t=0;t<q;t++)    
            {    
                if(*(m_LocArray+q)==*(m_LocArray+t))    
                {    
                    q--;    
                }    
            }  
        }         
  
        // <-    1D location of the mines    
    }  
    return m_LocArray;  
}  
  
int *Miner::m_PlateSet(int *m_LocArray)  
{  
    int *m_PlateStateP=new int [(m_Row+2)*(m_Col+2)*sizeof(int)];  
    for (int OriPSi=0;OriPSi<(m_Row+2)*(m_Col+2);OriPSi++)       // 初始化 is necessary,   
    {                                               // for room 分配ed by 动态 will holds value !=0.   
        *(m_PlateStateP+OriPSi)=0;  
    }  
    //坐标转化,加一圈边   
    for(int q=0;q<m_MineNum;q++)   
    {  
  
        m_IndexRow=(*(m_LocArray+q))/m_Col;      
        m_IndexCol=(*(m_LocArray+q))%m_Col;  
        (*(m_LocArray+q))=(m_IndexRow+1)*(m_Col+2)+(m_IndexCol+1);  
  
  
  
        // Set "Mine Point" .   
        *(m_PlateStateP+(*(m_LocArray+q)))=9;  
  
  
        //  2D location of the mines        ->   
        //  m_IndexRow=(*(m_LocArray+q))/m_Col;       
        //  m_IndexCol=(*(m_LocArray+q))%m_Col;   
        // <-    2D location of the mines           
  
  
  
        // Set neighbours of 'Mine's.                   ->   
        // 2 lines added to both Row & Col,...   
        // ...to avoid an "edge" mine.       
  
  
  
  
        // "Squares" of this vector abandon.       
        // To be modificated here.       
  
        if(*(m_PlateStateP+(*(m_LocArray+q))-1)<9)  // left   
        {++(*(m_PlateStateP+(*(m_LocArray+q))-1));}  
  
        if(*(m_PlateStateP+(*(m_LocArray+q))+1)<9)  // right   
        {++(*(m_PlateStateP+(*(m_LocArray+q))+1));}  
  
        if(*(m_PlateStateP+(*(m_LocArray+q))-m_Col-2)<9)  // up   
        {++(*(m_PlateStateP+(*(m_LocArray+q))-m_Col-2));}  
  
        if(*(m_PlateStateP+(*(m_LocArray+q))+m_Col+2)<9)  // down   
        {++(*(m_PlateStateP+(*(m_LocArray+q))+m_Col+2));}  
  
        if(*(m_PlateStateP+(*(m_LocArray+q))-m_Col-3)<9)  // leftup   
        {++(*(m_PlateStateP+(*(m_LocArray+q))-m_Col-3));}  
  
        if(*(m_PlateStateP+(*(m_LocArray+q))-m_Col-1)<9)  // rightup   
        {++(*(m_PlateStateP+(*(m_LocArray+q))-m_Col-1));}  
  
        if(*(m_PlateStateP+(*(m_LocArray+q))+m_Col+1)<9)  // leftdown   
        {++(*(m_PlateStateP+(*(m_LocArray+q))+m_Col+1));}  
  
        if(*(m_PlateStateP+(*(m_LocArray+q))+m_Col+3)<9)  // rightdown   
        {++(*(m_PlateStateP+(*(m_LocArray+q))+m_Col+3));}  
  
  
  
    }   
  
  
  
    return m_PlateStateP;  
  
}  
  
  
void Miner::TextOutFull(int *m_PlateStateP)
{



	//2013-08-23 添加边框效果
	printf("   ");											//上边框.两个空格是为了好看,要跟下面左边框一致。
	for(int zi=m_Col+2;zi>0;zi--) printf("----- ");			//上边框
	printf("\n");

	for(int z=0;z<(m_Row+2)*(m_Col+2);z++)
	{	
		


		printf("  |  %d",*(m_PlateStateP+z));					//左边框。两个空格是为了好看,要跟上面上边框一致。
		if(0==(z+1)%(m_Col+2))
		{
			printf("  |");										//右边框
			printf("\n");

			printf("   ");										//下边框.
			for(int zi=m_Col+2;zi>0;zi--) printf("----- ");	
			
			printf("\n");										//下边框
		}

	}


}
  
void Miner::TextOut(int *m_PlateStateP)  
{  
    for(int z=m_Col+2;z<(m_Row+2)*(m_Col+2)-m_Col-2;z++)  
    {  
  
        if(0==z%(m_Col+2)) {z++;}  
        if(0==(z+1)%(m_Col+2))      {z++;printf("\n\n\n");}  
        else {printf("%d   ",*(m_PlateStateP+z));}  
    }  
}  
  
void main()  
{  
    Miner m1=Miner(4,3,4);  
    int *Loc=m1.m_MineLoc();  
    int *PlateSet=m1.m_PlateSet(Loc);  
    m1.TextOutFull(PlateSet);  
    m1.TextOut(PlateSet);  
    system("pause");  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值