📄 mine.cpp
字号:
case 5:chess_fore[i][j]=NUM5;break;
case 6:chess_fore[i][j]=NUM6;break;
case 7:chess_fore[i][j]=NUM7;break;
case 8:chess_fore[i][j]=NUM8;break;
case BOMB:chess_fore[i][j]=BOMBED;break;
}
else
{
if(chess_back[i][j]!=BOMB)chess_fore[i][j]=CHAHAO;
}
return OK;
}
int BOARD::ReflectAllChess()
{
int i,j;
for(i=0;i<height;i++)
for(j=0;j<width;j++)
ReflectChess(i,j);
return OK;
}
int BOARD::Edge(int x,int y)
{
if(x<0||x>height||y<0||y>width||chess_fore[x][y]!=NORMAL)return OK;
if(chess_back[x][y]>=1&&chess_back[x][y]<=8){TurnBack(x,y);return OK;}
if(chess_back[x][y]!=0)return OK;
TurnBack(x,y);
if(chess_back[x][y]==0)
{
Edge(x-1,y);
Edge(x+1,y);
Edge(x,y-1);
Edge(x,y+1);
}
return OK;
}
int BOARD::TurnBack(int x,int y)
{
if(chess_fore[x][y]==NORMAL)
{
ReflectChess(x,y);
DrawChess(x,y);
}
return OK;
}
/*-------------------------------------------------------------------------*/
FACE::FACE()
{
offx=0;
offy=0;
width=22;
height=22;
status=0;
}
int FACE::SetCoordinate()
{
offx=board.offset_x+board.width*board.interval/2-10;
offy=board.offset_y-43;
return OK;
}
int FACE::DrawFace()
{
if(status==0)
{
frame3d(offx,offy,offx+width,offy+height,2,MAINCOLOR,1);
drawface(offx+(width-16)/2,offy+(height-16)/2,0);
}
if(status==1)
{
frame3d(offx,offy,offx+width,offy+height,2,MAINCOLOR,1);
drawface(offx+(width-16)/2,offy+(height-16)/2,1);
}
if(status==2)
{
frame3d(offx,offy,offx+width,offy+height,2,MAINCOLOR,1);
drawface(offx+(width-16)/2,offy+(height-16)/2,2);
}
if(status==3)
{
frame3d(offx,offy,offx+width,offy+height,2,MAINCOLOR,0);
drawface(offx+(width-16)/2+1,offy+(height-16)/2+1,0);
}
if(status==4)
{
frame3d(offx,offy,offx+width,offy+height,2,MAINCOLOR,1);
drawface(offx+(width-16)/2+1,offy+(height-16)/2+1,3);
}
return 0;
}
int FACE::FaceEvent()
{
if(nowx>=offx&&nowx<=offx+width&&nowy>=offy&&nowy<=offy+height)
{
if(mouse_state==1&&old_state==0)
{hide();face.status=3;DrawFace();show();}
if(mouse_state==0&&old_state==1)
{hide();face.status=0;DrawFace();GameInit();show();}
}
else
{
if(mouse_state==1&&old_state==0&&!died)
{hide();status=1;DrawFace();show();}
if(mouse_state==0&&old_state==1&&!died)
{hide();status=0;DrawFace();show();}
if(mouse_state==0&&old_state==1&&died)
{hide();status=2;DrawFace();show();}
if(status!=4&&win)
{hide();status=4;DrawFace();show();}
}
return OK;
}
/*-------------------------------------------------------------------------*/
int COUNTER::SetCoordinate()
{
count=0;
offx=board.offset_x+board.width*board.interval-60;
offy=board.offset_y-40;
width=50;
height=20;
now_time=0;
now_time=0;
return OK;
}
int COUNTER::CounterEvent()
{
int flag=0;
if(died)return OK;
old_time=now_time;
now_time=time(NULL);
if(now_time-old_time>=1)
{
if(nowx>=offx-20&&nowx<=offx+width+20&&nowy>=offy-20&&nowy<=offy+height+20){hide();flag=1;}
PutNum(offx,offy+2,count,4);
if(flag==1)show();
if(count<999)count++;
}
return OK;
}
int COUNTER::DrawCounter()
{
frame3d(offx,offy,offx+width,offy+height,2,4,0);
return OK;
}
int COUNTER::ClearCounter()
{
count=0;
frame3d(offx,offy,offx+width,offy+height,2,4,0);
return OK;
}
/*-------------------------------------------------------------------------*/
int BOMB_COUNTER::SetCoordinate()
{
count=0;
offx=board.offset_x+10;
offy=board.offset_y-40;
width=50;
height=20;
now_num=0;
now_num=0;
return OK;
}
int BOMB_COUNTER::CounterEvent()
{
int flag=0;
if(died)return OK;
old_num=now_num;
now_num=board.leftover;
if(now_num<-99)return ERROR;
if(now_num!=old_num)
{
if(nowx>=offx-20&&nowx<=offx+width+20&&nowy>=offy-20&&nowy<=offy+height+20){hide();flag=1;}
PutNum(offx,offy+2,now_num,4);
if(flag==1)show();
}
return OK;
}
int BOMB_COUNTER::DrawCounter()
{
frame3d(offx,offy,offx+width,offy+height,2,4,0);
return OK;
}
int BOMB_COUNTER::ClearCounter()
{
frame3d(offx,offy,offx+width,offy+height,2,4,0);
return OK;
}
/*-------------------------------------------------------------------------*/
int** Alloc2dArray(int hang,int lie)
{
int i;
int **a;
a=new int* [hang];
for(i=0;i<hang;i++)
a[i]=new int [lie];
return a;
}
int Delete2dArray(int **a,int hang,int lie)
{
int i;
for(i=0;i<hang;i++)
delete [] a[i];
delete [] a;
i=lie; //nousing
return OK;
}
int Fill(int left,int top,int right,int bottom,int color)
{
int i;
setcolor(color);
for(i=top;i<+bottom;i++)
line(left,i,right,i);
return OK;
}
void box3d(int left,int top,int right,int bottom,int main_color,int state)
{
Fill(left,top,right,bottom,main_color);
if(state==0)
{setcolor(8);
line(left,top,left,bottom);
line(left,top,right,top);
setcolor(15);
line(right,bottom,right,top);
line(right,bottom,left,bottom);
}
if(state==1)
{setcolor(15);
line(left,top,left,bottom);
line(left,top,right,top);
setcolor(8);
line(right,bottom,right,top);
line(right,bottom,left,bottom);
}
}
void frame3d(int left,int top,int right,int bottom,int thick,int color,int status)
{
int i;
for(i=0;i<thick;i++)
box3d(left+i,top+i,right-i,bottom-i,color,status);
}
/*-------------------------------------------------------------------------*/
int MouseEvent()
{
static x,y,old_x=0,old_y=0;
old_x=x;old_y=y;
y=board.Get_X(nowx);x=board.Get_Y(nowy);
if(y==-1||x==-1)return ERROR;
if(died||win)return ERROR;
if(mouse_state==1&&old_state==0)
{
hide();
if(board.chess_fore[x][y]==NORMAL)
{board.chess_fore[x][y]=PRESSED;board.DrawChess(x,y);}
show();
}
if(mouse_state==0&&old_state==1)
{
hide();
if(board.chess_fore[x][y]==PRESSED)
{
board.ReflectChess(x,y);board.DrawChess(x,y);
if(board.chess_back[x][y]==BOMB)
{board.ReflectAllChess();board.chess_fore[x][y]=CLICKBOMB;board.DrawAllChess();died=TRUE;}
if(board.chess_back[x][y]==0){board.chess_fore[x][y]=NORMAL;board.Edge(x,y);}
}
show();
}
if(mouse_state==1&&old_state==1)
if(x!=old_x||y!=old_y)
if(board.chess_fore[old_x][old_y]==PRESSED)
{hide();board.chess_fore[old_x][old_y]=NORMAL;board.DrawChess(old_x,old_y);show();}
if(mouse_state==2&&old_state==0&&!died)
{
hide();
switch(board.chess_fore[x][y])
{
case NORMAL:
board.leftover--;board.chess_fore[x][y]=MARKED;board.DrawChess(x,y);
if(board.chess_back[x][y]==BOMB)board.correct++;
break;
case MARKED:
board.leftover++;board.chess_fore[x][y]=UNDECIDED;board.DrawChess(x,y);
if(board.chess_back[x][y]==BOMB)board.correct--;
break;
case UNDECIDED:board.chess_fore[x][y]=NORMAL;board.DrawChess(x,y);break;
}
if(board.correct==board.bomb_num)win=TRUE;
show();
}
return OK;
}
/*-------------------------------------------------------------------------*/
void Background()
{
frame3d(board.offset_x-12,board.offset_y-55,board.offset_x+board.width*board.interval+12,board.offset_y+board.height*board.interval+12,3,MAINCOLOR,1);
frame3d(board.offset_x-5,board.offset_y-5,board.offset_x+board.width*board.interval+5,board.offset_y+board.height*board.interval+5,3,MAINCOLOR,0);
frame3d(board.offset_x-5,board.offset_y-50,board.offset_x+board.width*board.interval+5,board.offset_y-15,3,MAINCOLOR,0);
counter.DrawCounter();
b_counter.DrawCounter();
}
void GameInit()
{
board.GenerateMine();
board.DrawAllChess();
died=FALSE;win=FALSE;
counter.ClearCounter();
b_counter.ClearCounter();
}
/*-------------------------------------------------------------------------*/
main()
{
int driver=VGA,mode=VGAHI;
int i;
//////////////////////////
initgraph(&driver,&mode,"");
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
randomize();
SetIniFileName("mine.ini");
level=ReadINI_int("LEVEL");
MAINCOLOR=ReadINI_int("MAINCOLOR");
board.SetCoordinate();
face.SetCoordinate();
counter.SetCoordinate();
b_counter.SetCoordinate();
Background();
signature(240,10,0);
face.DrawFace();
board.DrawBoard();
board.GenerateMine();
board.DrawAllChess();
/*=================================*/
Mouse_Reset();
Set_Range(0,0,639,479);
nowx=320;nowy=240;
Set_XY(nowx,nowy);
show();
while(!kbhit())
{oldx=nowx;oldy=nowy;old_state=mouse_state;
Get_XY(&nowx,&nowy);
mouse_state=MouseState();
if(nowx!=oldx||nowy!=oldy)
{putpic(oldx,oldy,*buf);show();}
MouseEvent();
face.FaceEvent();
counter.CounterEvent();
b_counter.CounterEvent();
delay(10);
}
/*=================================*/
closegraph();
return OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -