📄 apple.cpp
字号:
#include<dos.h>
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<bios.h>
#include"bmp16.h"
#include"ourmouse.h"
#include"apple.h"
#define WHITECHESS 1
#define BLACKCHESS 2
#define BACKCOLOR 1
#define KEY_UP 0x0011
#define KEY_DOWN 0x001f
#define KEY_LEFT 0x001e
#define KEY_RIGHT 0x0020
#define KEY_PUT 0x19
#define KEY1_UP 0x0048
#define KEY1_DOWN 0x0050
#define KEY1_LEFT 0x004b
#define KEY1_RIGHT 0x004d
#define KEY1_PUT 0x0015
#define KEY_BACK 0X003b
#define KEY_ESC 0x0001
#define KEY_MvsM 0x003c
#define KEY_MvsC 0x003d
#define KEY_CHANGE 0x003e
#define KEY_LEVEL 0x003f
/*--------------------the functions list-------------------------*/
/*------------list the function according to the position--------*/
/*------------------where the function is located----------------*/
/*--------------------------------------------------------------*/
/*----------functions about the chessrules------------- */
/*--------------from line 114 to line 381---------------*/
/*void newgame(); start a new game*/
/*void initboard(); redraw the board when a game starts*/
/*void manchess(int x,int y); man :how to put a chess*/
/*void newchess(int *mx,int *my,int *mbutt); function to put a chess*/
/*void chessrules(int x,int y); the general rules about the chess*/
/*int scan(int r,int c);
int test(void);*/
/*void maintest(int *psingle); test if there is space to put*/
/*void eatchess(int initrow,int initcolumn);
void realeat(int irow,int icolumn,int crow,int ccolumn,int direct);
void countscores(void);*/
/*void addhead(void); add the head to the backstep chain*/
/*void add(struct backstep *head);
void del(struct backstep *head,int single);
void freememory(struct backstep* head);*/
/*-------------- functions of the graphy--------------- */
/*----------------from line 385 to line 519-----------*/
/*void showversion(void);
void drawboard(void),drawbutton(void); */
/*void drawpreboard(struct backstep *p,int single); draw a new board when backup*/
/*void drawchess(float i,float j,int color);
void drawsign(int oldsignr,int oldsignc,int newsignr,int newsignc);
void showscores(void);
void showresult(int single);*/
/*------ functions about the controlmeans and mainmenu---------*/
/*------------from line 523 to line 627------------------- */
/*void mousecontrol(int *psingle);
void keycontrol(int *psingle,int *psignrow,int *psigncolumn);*/
/*int readkeyboard(void);
/*void mvsmcontrol(void); let man vs man */
/*void mvsccontrol(void); let man vs computer*/
/*void backcontrol(int single);back up a step*/
/*void esccontrol(void); quit the game*/
/*-----------------digui functions-----------------*/
/*-------------------from line 630--------------*/
/*int think(int pcomx,int pcomy,int mind,int *presult);
void computer1(void);
int decidepth();
void virtualeat(int irow,int icolumn,int crow,int ccolumn,int direct);
void virtualeatchess(int initrow,int initcolumn);
int virtualscan(int r,int c);
int virtualscore(int *presult);
int virtualprofit(int *presult);
int sortout(int result[],int counters,int *pfinal);
void virtualrules(int x,int y);
void initvirtualboard(int depth); */
/*void initvirtualboardnum(int depth); memorize the different boards*/
/*void sortpossibility(int arrayx[],int arrayy[],int counters);*/
/*---------------normal computer chess-------------------------*/
/*void computer2();*/
/*-----------------declare the variable-----------------*/
union REGS regs;
int direction[8][2]={{0,1},{0,-1},{1,1},{1,-1},{1,0},{-1,1},{-1,0},{-1,-1}};
int board[10][10];
int preboard[10][10];
int ws=2,bs=2; /* white vs black 's score at first*/
int chesscolor=BLACKCHESS;
int mandone=1;
int apple_flag=1;
int manfirst=0;
int thinkdepth;
int level=1;
struct backstep *head=NULL;
/*----------------------------------------------------*/
void apple()
{
int r,c;
int single=0;
int signrow=5,signcolumn=3;
apple_flag=1;
level=1;
ws=2,bs=2; /* white vs black 's score at first*/
chesscolor=BLACKCHESS;
mandone=1;
apple_flag=1;
manfirst=0;
thinkdepth;
level=1;
head=NULL;
drawback();
showversion();
drawbutton();
ShowBmp16("mainmenu.bmp",430,20);
initmouse();
showmouse();
newgame();
do {
maintest(&single); /*test whether there are freespace to put chess*/
if(bioskey(1)!=0) /*do when the keyboard is pushed*/
keycontrol(&single,&signrow,&signcolumn);
else
mousecontrol(&single);
if(mandone==1&&single==1)
{
if(level==1)
computer2();
else computer1();
}
}while(apple_flag==1);
hidemouse();
}
/*---------------------------------------------*/
/*---------------------------------------------*/
/*---------------------------------------------*/
/*---------- start a new game------------- */
void newgame()
{
int r,c;
for(r=0;r<=9;r++)
for(c=0;c<=9;c++)
board[r][c]=0;
if(manfirst==1)
{ mandone=1;
chesscolor=WHITECHESS; }
else
{ mandone=0; /*decide if the man put chess first*/
chesscolor=BLACKCHESS;}
ws=2,bs=2;
addhead();
initboard();
}
/*----------initialile the board-------------*/
void initboard()
{
hidemouse();
drawboard();
showscores();
delay(200);drawchess(4,4,WHITE);
delay(200);drawchess(4,5,BLACK);
delay(200);drawchess(5,5,WHITE);
delay(200);drawchess(5,4,BLACK);
drawchess(10.3,3.5,BLACK);
showmouse();
}
/*---------- draw a chess(x,y)------------- */
void manchess(int x ,int y)
{
int minrow=40,maxrow=440,mincolumn=40,maxcolumn=440;
int i,j,row,column;
if(x<=minrow||x>=maxrow||y<=mincolumn||y>=maxcolumn)
{}
else
{
for(i=40;i<=440;i+=50)
for(j=40;j<=440;j+=50)
{ if(x>=i&&x<=i+50&&y<=j+50&&y>=j)
{ row=(i+10)/50;
column=(j+10)/50;
break;}
}
chessrules(row,column);
}
}
/*-------------- do as the chessrules------------------- */
void chessrules(int x,int y)
{
if(board[x][y]==0&&scan(x,y)==1)
{
add(head); /*add a dot to the chain before change the board*/
hidemouse();
eatchess(x,y);
mandone=1;
showmouse();
ws=0,bs=0;
countscores();
showscores();
if(chesscolor==WHITECHESS) /*change the turn*/
{chesscolor=BLACKCHESS;
drawchess(10.3,3.5,BLACK);}
else
{chesscolor=WHITECHESS;
drawchess(10.3,3.5,WHITE);}
}
}
/*--------------- scan in the eight direction around the chess------- */
int scan(int r,int c)
{
int direct,same,jump=0;
int x,y;
for(direct=0;direct<=7;direct++)
{
same=0;
x=r,y=c;
while(x>=1&&x<=8&&y>=1&&y<=8)
{
x+=direction[direct][0];
y+=direction[direct][1];
if(board[x][y]==0||board[x][y]==chesscolor&&same==0)
break;
else if(board[x][y]==chesscolor)
{jump=1;
break;}
else same=1;
}
}
if(jump==1) return 1;
else return 0;
}
/*---------------- try if there are anyplace to put chess--------- */
int test()
{
int r,c,result=0;
for(r=1;r<=8;r++)
for(c=1;c<=8;c++)
if(scan(r,c)==1&&board[r][c]==0)
{result=1;
break;}
if(result==1) return 1;
else return 0;
}
/*------------------ use the test to do the something-------------- */
void maintest(int *psingle)
{
static possible=1;
static impossible=0;
possible=test();
if(possible==1)
impossible=0;
else if(impossible==1)
{showresult(*psingle);
possible=1;}
else if(possible==0)
{
impossible++;
setfillstyle(1,BACKCOLOR);
bar(450,160,640,190);
if(chesscolor==WHITECHESS)
{
chesscolor=BLACKCHESS;
drawchess(10.3,3.5,BLACK);
}
else
{
chesscolor=WHITECHESS;
drawchess(10.3,3.5,WHITE);
if(*psingle==1)
mandone=1;
}
}
if(ws+bs==64)
showresult(*psingle);
}
/*------------ eat the different chess around the chess----------- */
void eatchess(int initrow,int initcolumn)
{
int currentrow,currentcolumn;
int direct,same;
for(direct=0;direct<=7;direct++)
{
currentrow=initrow;
currentcolumn=initcolumn;
same=1;
do
{ currentrow+=direction[direct][0];
currentcolumn+=direction[direct][1];
if(board[currentrow][currentcolumn]==0||board[currentrow][currentcolumn]==chesscolor&&same==1)
break;
else if(board[currentrow][currentcolumn]==chesscolor)
{ realeat(initrow,initcolumn,currentrow,currentcolumn,direct);
break;}
else same=0;
}
while(currentrow>=1&¤trow<=8&¤tcolumn>=1&¤tcolumn<=8);
}
}
/*-------------- eat the chess in one direction -----------------*/
void realeat(int irow,int icolumn,int crow,int ccolumn,int direct)
{
int startrow,startcolumn;
startrow=irow;
startcolumn=icolumn;
do
{
if(chesscolor==WHITECHESS)
{
drawchess(startrow,startcolumn,WHITE);
board[startrow][startcolumn]=WHITECHESS;
}
else if(chesscolor==BLACKCHESS)
{
drawchess(startrow,startcolumn,BLACK);
board[startrow][startcolumn]=BLACKCHESS;
}
startrow+=direction[direct][0];
startcolumn+=direction[direct][1];
delay(100);
}
while(startrow!=crow||startcolumn!=ccolumn);
}
/*---------------- count the scores --------------------*/
void countscores()
{
int ro,co;
for(ro=1;ro<=8;ro++)
for(co=1;co<=8;co++)
{
if(board[ro][co]==1)
ws++;
else if(board[ro][co]==2)
bs++;
}
}
/*-----------add the head to the back chain-----------*/
void addhead()
{
if(head!=NULL);
freememory(head);
head=(struct backstep *)malloc(sizeof(struct backstep));
head->next=NULL;
head->preboard[4][4]=board[4][4]=1;
head->preboard[5][5]=board[5][5]=1;
head->preboard[4][5]=board[4][5]=2;
head->preboard[5][4]=board[5][4]=2;
}
/*---------------add a dot in the chain---------------*/
void add(struct backstep *head)
{ struct backstep *p1,*p2;
int i,j;
p1=head;
while(p1->next!=NULL)
p1=p1->next;
p2=(struct backstep*)malloc(sizeof(struct backstep));
p1->next=p2;
p2->next=NULL;
for(i=1;i<=8;i++)
for(j=1;j<=8;j++)
p2->preboard[i][j]=board[i][j];
}
/*---------------delete a dot in the chain------------*/
void del(struct backstep *head,int single)
{
int dots=1;
struct backstep *p1,*p2;
p1=head;
p2=p1;
while(p1->next!=NULL)
{ dots++;
p2=p1;
p1=p1->next;}
if(dots<=64)
drawpreboard(p1,single);
free(p1);
p2->next=NULL;
}
/*-------free the memory done by malloc function-------------*/
void freememory(struct backstep* head)
{
struct backstep *p1,*temp;
p1=head;
while(p1!=NULL)
{
temp=p1;
p1=p1->next;
free(temp);
}
head=NULL;
}
/*------------------------------------------------------------*/
/*------------------------------------------------------------*/
/*------------------------------------------------------------*/
/* show the version */
void showversion()
{
setcolor(BACKCOLOR);
settextstyle(7,0,3);
outtextxy(10,445," F1help_F2two_F3single_F4change_F5level No.2350 ");
}
/*-----draw the back screen-----------*/
void drawback()
{
setfillstyle(1,WHITE);
bar(0,0,320,479);
setfillstyle(1,BLACK );
bar(321,0,639,479);
}
/*--------------- draw an chess board----------- */
void drawboard()
{
int row,column;
setcolor(DARKGRAY);
setfillstyle(1,LIGHTGRAY);
bar(40,40,440,440);
newbutton(34,34,446,446);
setfillstyle(1,DARKGRAY);
bar(35,35,440,40);
bar(35,40,40,440);
bar(440,440,445,35);
bar(35,440,445,445);
setcolor(DARKGRAY);
for(row=40,column=40;row<=440,column<=440;row+=50,column+=50)
{
line(row,40,row,440);
line(40,column,440,column);
}
}
/*--------------- draw the buttons --------------*/
void drawbutton()
{
newbutton(500,250,595,277);
newbutton(500,300,595,327);
newbutton(500,350,595,377);
newbutton(500,400,595,427);
}
/*----------------back control--------------------*/
void drawpreboard(struct backstep *p,int single)
{
int i,j;
for(i=1;i<=8;i++)
for(j=1;j<=8;j++)
{
if(board[i][j]!=p->preboard[i][j])
{
if(p->preboard[i][j]==1)
drawchess(i,j,WHITE);
else if(p->preboard[i][j]==2)
drawchess(i,j,BLACK);
else if(p->preboard[i][j]==0)
drawchess(i,j,7);}
board[i][j]=p->preboard[i][j];
}
ws=0,bs=0;
countscores();
showscores();
if(p!=head&&single==0) /*decide if change who's turn*/
{
if(chesscolor==WHITECHESS)
{chesscolor=BLACKCHESS;
drawchess(10.3,3.5,BLACK);}
else
{chesscolor=WHITECHESS;
drawchess(10.3,3.5,WHITE);}
}
}
/* draw a chess in the proper color */
void drawchess(float i,float j,int color)
{
setcolor(color);
setfillstyle(1,color);
circle(i*50+15,j*50+15,22);
floodfill(i*50+15,j*50+15,color);
}
/*------- draw the sign where the chess should be put--------------*/
void drawsign(int oldsignr,int oldsignc,int newsignr,int newsignc)
{
struct linesettingstype lineinfo;
getlinesettings(&lineinfo);
setcolor(LIGHTGRAY);
rectangle(oldsignr*50-10,oldsignc*50-10,oldsignr*50+40,oldsignc*50+40);
setcolor(6);
rectangle(newsignr*50-10,newsignc*50-10,newsignr*50+40,newsignc*50+40);
setlinestyle(lineinfo.linestyle,lineinfo.upattern,lineinfo.thickness);
setcolor(BLACK);
rectangle(oldsignr*50-10,oldsignc*50-10,oldsignr*50+40,oldsignc*50+40);
}
/*------------- show the Vs score ---------------------*/
void showscores()
{
char whitescore[5],blackscore[5];
setfillstyle(1,BLACK);
bar(560,50,630,200);
setcolor(5);
sprintf(whitescore,"%d",ws);
sprintf(blackscore,"%d",bs);
settextstyle(7,HORIZ_DIR,5);
outtextxy(560,60,blackscore);
outtextxy(560,115,whitescore);
}
/*-------------- show the result when game is over -------------------*/
void showresult(int single)
{
char howmany[5];
setcolor(5);
ShowBmp16("over.bmp",70,50);
sprintf(howmany,"%d",abs(bs-ws));
settextstyle(DEFAULT_FONT,HORIZ_DIR,8);
outtextxy(220,330,howmany);
if(ws<bs)
{
if(single==1) ShowBmp16("ni.bmp",80,150);
else ShowBmp16("hei.bmp",80,150);
}
else if(ws>bs)
{
if(single==1) ShowBmp16("com.bmp",80,150);
else ShowBmp16("bai.bmp",80,150);
}
else ShowBmp16("frd.bmp",80,150);
chesscolor=WHITECHESS;
}
/*-------------------------------------------------*/
/*-------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -