论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

孔明棋,你玩过吗

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:46:44

//在tc3++下运行

//游戏名孔明棋

#define Up  0x4800
#define Down  0x5000
#define P    0x1970
#define R    0x1372
#define Enter  0x1c0d
#define Esc   0x11b
#define Right  0x4d00
#define Left  0x4b00

#include<bios.h>
#include<stdio.h>
#include<dos.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>

//定义核心数据
int LargeStartx,LargeStarty;  //大方格图形左上角坐标
int SmallStartx,SmallStarty;
int RedNum;  //红格子的个数。假如红格子的个数为1,则游戏过关
char State;  //按键的状态M(move)与S(select),M表示移动红格子。S表示选择要移动的红格子
int Attr[7][7];  //描述7*7矩阵格子的属性N(none),W(white),R(red).N表示没有格子,W表示有白色格子,R示 

                 //有红色格子
int row,tier;    //7*7矩阵的行(row)与列(tier)
int Mission;     //表示第几关
int MissionNum;  //总的关数

typedef struct rect1  //描述大方格的属性
{
int startx;
int starty;
int nowx;
int nowy;
int agox;
int agoy;
char color;
int size;
}Large;
Large LargeRect;

typedef struct rect2  //描述小方格的属性
{
int nowx;
int nowy;
char color;
int size;
}Small;
Small SmallRect;

typedef struct add
{
int x;
int y;
}Add;

//declare functions
void InitData1();
void InitData();
void InitMission(int );
void InitPic();
void Help();
void DrawSmallRect(int ,int ,int ,char );
void DrawLargeRect(int ,int ,int ,char );
void ChangeState();
void MoveLargeRect();
void MoveSmallRect(int );

void main()
{
int key;
int gdriver=DETECT, gmode;
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver, &gmode, "");
setbkcolor(BLUE);
cleardevice();
InitData1();  
InitData();    //随着关数的变化需要恢复的数据
Mission=1;
InitMission(Mission);  //初始化关数.主要是改变一些方格的属性
InitPic();
do
  {
   key=bioskey(0);
   LargeRect.agox=LargeRect.nowx;
   LargeRect.agoy=LargeRect.nowy;
   SmallRect.nowx=LargeRect.nowx+10;
   SmallRect.nowy=LargeRect.nowy+10;
   switch(key)
 {
 case Up:
    {
    if(State=='S')
         {
         if( (row-1)>=0&&Attr[row-1][tier]!='N' )
      {
      row--;
      MoveLargeRect();
      break;
      }
         }
    if(State=='M')
         {
         if(Attr[row-1][tier]=='R'&&Attr[row-2][tier]=='W')
      {
      MoveSmallRect(key);
      Attr[row][tier]='W';Attr[row-1][tier]='W';Attr[row-2][tier]='R';
      row-=2;
      MoveLargeRect();
      RedNum--;
      ChangeState();
      }
         }
     break;
    }
 case Down:
    {
    if(State=='S')
         {
         if( (row+1)<7&&Attr[row+1][tier]!='N' )
      {
      row++;
      MoveLargeRect();
      }
         }
    if(State=='M')
         {
         if(Attr[row+1][tier]=='R'&&Attr[row+2][tier]=='W')
      {
      MoveSmallRect(key);
      Attr[row][tier]='W';Attr[row+1][tier]='W';Attr[row+2][tier]='R';
      row+=2;
      MoveLargeRect();
      RedNum--;
      ChangeState();
      }
         }
    break;
    }
 case Left:
    {
    if(State=='S')
         {
         if( (tier-1)>=0&&Attr[row][tier-1]!='N' )
      {
      tier--;
      MoveLargeRect();
      }
         }
    if(State=='M')
         {
         if(Attr[row][tier-1]=='R'&&Attr[row][tier-2]=='W')
      {
      MoveSmallRect(key);
      Attr[row][tier]='W';Attr[row][tier-1]='W';Attr[row][tier-2]='R';
      tier-=2;
      MoveLargeRect();
      RedNum--;
      ChangeState();
      }
         }
    break;
    }
 case Right:
    {
    if(State=='S')
         {
         if( (tier+1)>=0&&Attr[row][tier+1]!='N' )
      {
      tier++;
      MoveLargeRect();
      }
         }
    if(State=='M')
         {
         if(Attr[row][tier+1]=='R'&&Attr[row][tier+2]=='W')
      {
      MoveSmallRect(key);
      Attr[row][tier]='W';Attr[row][tier+1]='W';Attr[row][tier+2]='R';
      tier+=2;
      MoveLargeRect();
      RedNum--;
      ChangeState();
      }
         }
    break;
    }
 case R:
    {
    InitData();
    InitMission(Mission);
    InitPic();
    break;
    }
 case P:
    {
    InitData();
    if(Mission<MissionNum)  Mission++;
    else Mission=1;
    InitMission(Mission);
    InitPic();
    break;
    }
 case Enter:
    {
    if(State=='S'&&Attr[row][tier]=='R')
        ChangeState();
    else if(State=='M'&&Attr[row][tier]=='R')
        ChangeState();
    else break;
    break;
    }
 case Esc:exit(1);
 }
if(RedNum==1)  //过关
      {
      outtextxy(100,350,"CONGRATULATIONS!");
      outtextxy(45,380,"Wait for next mission...");
      sleep(3);
      InitData();
      if(Mission<MissionNum)  Mission++;
      else Mission=1;
      InitMission(Mission);
      InitPic();
      }
  }while(1);
}

void InitData1()  //const变量
{
MissionNum=12;
LargeStartx=50;
LargeStarty=50;
SmallStartx=LargeStartx+10;
SmallStarty=LargeStarty+10;
LargeRect.size=40;
LargeRect.color=RED;
SmallRect.size=20;
for(int j=0;j<2;j++)     //定义7*7矩阵的四个角的16个方格的属性为'N'
   for(int i=0;i<2;i++)
      Attr[i][j]=Attr[i+5][j]=Attr[i][j+5]=Attr[i+5][j+5]='N';
}

void InitData()   //随着关数的变化需要恢复的数据
{
State='S';
row=3;tier=3;
LargeRect.nowx=LargeRect.startx=LargeStartx+tier*40;
LargeRect.nowy=LargeRect.starty=LargeStarty+row*40;
for(int j=2;j<5;j++)    //定义7*7矩阵的其它方格的属性为'W'
   for(int i=0;i<7;i++)
      Attr[i][j]='W';
for(j=0;j<7;j++)
   for(i=2;i<5;i++)
      Attr[i][j]='W';
}


void InitMission(int m)  //改变一些方格的属性与RedNum的值
{
int i,j;
switch(m)
     {
     case 1:         //第一关红色格子初始化
     {
     for(i=1;i<=4;i++)
        Attr[i][3]='R';
     for(j=2;j<=4;j++)
        Attr[2][j]='R';
     RedNum=6;
     break;
     }
     case 2: 
     {
     for(i=1;i<=5;i++)
        Attr[i][3]='R';
     for(j=1;j<=5;j++)
        Attr[3][j]='R';
     RedNum=9;
     break;
     }
     case 3:
     {
     for(i=0;i<=3;i++)
        Attr[i][2]=Attr[i][4]='R';
     for(i=0;i<=2;i++)
        Attr[i][3]='R';
     RedNum=11;
     break;
     }
     case 4:
     {
     for(i=2;i<=3;i++)
         Attr[i][0]=Attr[i][6]='R';
     for(j=1;j<=5;j+=2)
         Attr[4][j]='R';
     for(j=2;j<=4;j++)
         Attr[3][j]=Attr[5][j]='R';
     for(i=1;i<=4;i++)
         Attr[i][3]='R';
     RedNum=15;
     break;
     }
     case 5:
     {
     for(j=1;j<=2;j++)
        Attr[2][j]=Attr[4][j]=Attr[2][j+3]=Attr[4][j+3]='R';
     for(i=0;i<=1;i++)
        Attr[i][3]=Attr[i+5][3]='R';
     for(j=2;j<=4;j++)
        Attr[3][j]='R';
     RedNum=15;
     break;
     }
     case 6:
     {
     for(j=1;j<=5;j++)
         Attr[3][ j]='R';
     for(i=5;i<=6;i++)
         Attr[i][2]=Attr[i][4]='R';
     for(i=2;i<=5;i++)
          Attr[i][3]='R';
     Attr[2][1]=Attr[2][5]=Attr[2][6]='R';
     RedNum=15;
     break;
     }
     case 7:
     {
     for(j=2;j<=4;j++)
         Attr[1][j]=Attr[5][j]=Attr[6][j]='R';
     for(i=0;i<=4;i++)
         Attr[i][3]='R';
     for(j=1;j<=5;j++)
         Attr[2][j]='R';
     RedNum=17;
     break;
     }
     case 8:
     {
     for(j=0;j<=6;j++)
         Attr[3][j]=Attr[4][j]='R';
     for(j=2;j<=4;j++)
         Attr[2][j]='R';
     Attr[1][3]='R';
     RedNum=18;
     break;
     }
     case 9:
     {
     for(j=2;j<=4;j++)
         Attr[1][j]=Attr[5][j]='R';
     for(j=1;j<=5;j++)
         Attr[4][j]='R';
     for(j=0;j<=6;j++)
         Attr[2][j]=Attr[3][j]='R';
     Attr[6][3]='R';
     RedNum=26;
     break;
     }
     case 10:
     {
     for(i=0;i<=6;i++)
        Attr[i][3]='R';
     for(i=2;i<=4;i++)
        for(j=0;j<=1;j++)
            Attr[i][j]=Attr[i][j+5]='R';
     for(i=1;i<=5;i+=2)
          Attr[i][2]=Attr[i][4]='R';
     RedNum=25;
     break;
     }
     case 11:
     {
     for(i=1;i<=6;i++)
         for(j=2;j<=4;j++)
            Attr[i][j]='R';
     for(i=2;i<=4;i++)
     for(j=0;j<=1;j++)
         Attr[i][j]=Attr[i][j+5]='R';
     Attr[0][3]='R';
     RedNum=31;
     break;
     }
     case 12:
     {
     for(i=0;i<=6;i++)
        for(j=2;j<=4;j++)
           Attr[i][j]='R';
     for(i=2;i<=4;i++)
     for(j=0;j<=1;j++)
        Attr[i][j]=Attr[i][j+5]='R';
     Attr[3][3]='W';
     RedNum=32;
     break;
     }
     }
}

void InitPic()
{
setbkcolor(BLUE);
cleardevice();
Help();
settextstyle(0,0,2);
for(int j=0;j<7;j++)
   for(int i=0;i<7;i++)
      switch(Attr[i][j])
   {
    case 'W': DrawSmallRect(SmallStartx+j*40,SmallStarty+i*40,SmallRect.size,WHITE);break;
    case 'R': DrawSmallRect(SmallStartx+j*40,SmallStarty+i*40,SmallRect.size,RED);break;
    case 'N': break;
   }
DrawLargeRect(LargeRect.startx,LargeRect.starty,LargeRect.size,LargeRect.color);
outtextxy(400,100,"Select");
setfillstyle(0,BLUE);
bar(120,30,170,50);
switch(Mission)
       {
       case 1:outtextxy(120,30,"Mission 1");break;
       case 2:outtextxy(120,30,"Mission 2");break;
       case 3:outtextxy(120,30,"Mission 3");break;
       case 4:outtextxy(120,30,"Mission 4");break;
       case 5:outtextxy(120,30,"Mission 5");break;
       case 6:outtextxy(120,30,"Mission 6");break;
       case 7:outtextxy(120,30,"Mission 7");break;
       case 8:outtextxy(120,30,"Mission 8");break;
       case 9:outtextxy(120,30,"Mission 9");break;
       case 10:outtextxy(120,30,"Mission 10");break;
       case 11:outtextxy(120,30,"Mission 11");break;
       case 12:outtextxy(120,30,"Mission 12");break;
       }
}

void Help()
{
setcolor(GREEN);
settextstyle(0,0,1);
outtextxy(350,140,"'enter': select the red pane");
outtextxy(350,150,"'r':     restart the mission");
outtextxy(350,160,"'p':     pass the current mission");
outtextxy(350,170,"         for the next mission");
outtextxy(350,180,"'esc':   exit the game");
outtextxy(350,200,"author:  litigo");
outtextxy(350,210,"time:    10/22/2003");
outtextxy(350,220,"e-mail:  litigo@sohu.com");
outtextxy(350,230,"oicq:    25317747");
}

void DrawSmallRect(int x,int y,int size,char color)
{
setfillstyle(1,color);
bar(x,y,x+size,y+size);
}

void DrawLargeRect(int x,int y,int size,char color)
{
setcolor(color);
rectangle(x,y,x+size,y+size);
}


void ChangeState()
{
if(State=='S')
    {
    setfillstyle(0,BLUE);
    bar(400,100,500,130);
    State='M';
    outtextxy(400,100,"Move");
    }
else
    {
    setfillstyle(0,BLUE);
    bar(400,100,500,130);
    State='S';
    outtextxy(400,100,"Select");
    }
}

void MoveLargeRect()
{
LargeRect.nowx=LargeStartx+tier*40;
LargeRect.nowy=LargeStarty+row*40;
setwritemode(1);
DrawLargeRect(LargeRect.agox,LargeRect.agoy,LargeRect.size,LargeRect.color);
DrawLargeRect(LargeRect.nowx,LargeRect.nowy,LargeRect.size,LargeRect.color);
setwritemode(0);
}

void MoveSmallRect(int k)
{
Add Add;
switch(k)
       {
       case Up:
                 {
                 Add.x=0;Add.y=-40;break;
                 }
       case Down:
                 {
                 Add.x=0;Add.y=40;break;
                 }
       case Left:
                 {
                 Add.x=-40;Add.y=0;break;
                 }
       case Right:
                 {
                 Add.x=40;Add.y=0;break;
                 }
       }
DrawSmallRect(SmallRect.nowx,SmallRect.nowy,SmallRect.size,WHITE);
DrawSmallRect(SmallRect.nowx+Add.x,SmallRect.nowy+Add.y,SmallRect.size,WHITE);
DrawSmallRect(SmallRect.nowx+Add.x*2,SmallRect.nowy+Add.y*2,SmallRect.size,RED);
}

点这里下载
上一篇:{应用}边锋密码保护计算器 人气:6217
下一篇:{应用}手机上的记忆力游戏 人气:6027
视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058