⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 apple.cpp

📁 一个在tc3.0下面写的棋类游戏 很经典 值得好好研究
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*-------------------------------------------------*/
/*-------------control by the mouse-------------- */
void mousecontrol(int *psingle)
{
  int x,y;
  int buttons;
  readmouse(&buttons,&x,&y);
  if(buttons)
     manchess(x,y);
  if(x>=500&&y>=250&&x<=595&&y<=275&&buttons)
     { mvsmcontrol();
       *psingle=0;}
   else if(x>=500&&y>=300&&x<=595&&y<=327&&buttons)
     { mvsccontrol();
	*psingle=1;}
   else if(x>=500&&y>=350&&x<=595&&y<=377&&buttons)
      backcontrol(*psingle);
   else if(x>=500&&y>=400&&x<=595&&y<=427&&buttons)
      esccontrol();
   }
/*------------draw a sign where you may put a chess------------------*/
void keycontrol(int *psingle,int *psignrow,int *psigncolumn)
{  int key;
   int oldsr=*psignrow;
   int oldsc=*psigncolumn;
   key=readkeyboard();
   switch(key)
     {
      case KEY_CHANGE:
		       if(*psingle==1)
		       {
			newgame();
			setfillstyle(1,WHITE);
			bar(170,0,320,30);
			settextstyle(7,0,3);
			if(manfirst==0){ manfirst=1;
			     outtextxy(170,0,"Man first!");}
			else {manfirst=0;
			     outtextxy(170,0,"Comp first!");}
		       }
			break;
      case KEY_LEVEL:
		       if(*psingle==1)
		       {
			setfillstyle(1,WHITE);
			bar(170,0,320,30);
			settextstyle(7,0,3);
			if(level==1){level=2;
			     outtextxy(170,0,"LEVEL 2!");}
			else {level=1;
			     outtextxy(170,0,"LEVEL 1!");}
		       }
			break;
      case KEY1_UP:
      case KEY_UP:  {if(*psigncolumn>1)(*psigncolumn)--;
		     else *psigncolumn=8;
		     break;}
      case KEY1_DOWN:
      case KEY_DOWN: {if(*psigncolumn<8)
			(*psigncolumn)++;
		      else *psigncolumn=1;
		      break;}
      case KEY1_LEFT:
      case KEY_LEFT: {if(*psignrow>1) (*psignrow)--;
		      else *psignrow=8;
		      break;}
      case KEY1_RIGHT:
      case KEY_RIGHT: {if(*psignrow<8) (*psignrow)++;
		       else *psignrow=1;
		       break;}
      case KEY_MvsC:mvsccontrol(); *psingle=1;break;
      case KEY_MvsM:mvsmcontrol(); *psingle=0;break;
      case KEY_BACK:backcontrol(*psingle);break;
      case KEY_ESC:esccontrol();break;
      default: break;
     }
   drawsign(oldsr,oldsc,*psignrow,*psigncolumn);
   if((key==KEY_PUT||key==KEY1_PUT)&&scan(*psignrow,*psigncolumn)==1&&board[*psignrow][*psigncolumn]==0)
      chessrules(*psignrow,*psigncolumn);
}
/*--------------- get the key you push------------ */
int readkeyboard()
{
  union REGS rg;
  rg.h.ah=0;
  int86(0x16,&rg,&rg);
  return rg.h.ah;
}
/*-------------------- let man versus man----------------- */
void mvsmcontrol()
{
   hidemouse();
   pushbutton(500,250,595,277);
   delay(200);
   newbutton(500,250,595,277);
   showmouse();
   newgame();
}
/*--------------------let man versus computer ---------------*/
 void mvsccontrol()
{
  hidemouse();
  pushbutton(500,300,595,327);
  delay(200);
  newbutton(500,300,595,327);
  showmouse();
  newgame();
}
/*------------------- let the man back a step-------------------- */
void backcontrol(int single)
{
  hidemouse();
  pushbutton(500,350,595,377);
  delay(200);
  newbutton(500,350,595,377);
  if(single==0)
   del(head,single);   /*if man vs man ,getback one step*/
  else
    {del(head,single);
     del(head,single);}/*if man vs computer,getback two steps*/
  showmouse();
}
/*---------------- let the man quit-------------------------*/
void esccontrol()
{
  hidemouse();
  pushbutton(500,400,595,427);
  showmouse();
  freememory(head);
  apple_flag=0;
  cleardevice();
}
/*---------This part is a simple edition in the program
      It is not very good now and going to be improved
      in the future.Thank you very much---------------*/
int virtualboard[10][10];
int virtualboard1[10][10];
int virtualboard2[10][10];
int virtualboard3[10][10];
int virtualboard4[10][10];
int virtualboard5[10][10];
int virtualboard6[10][10];
int virtualcolor;
/*----------- let computer put a chess------------- */
void computer1()
{
  int comx[20],comy[20],nc,nr;
  int counters=0,possibility,final;
  int result[20];
  setcolor(0);
  settextstyle(7,0,2);
  outtextxy(30,0,"Thinking ");
  virtualcolor=chesscolor;
  for(nr=1;nr<=8;nr++)
    for(nc=1;nc<=8;nc++)
    if(scan(nr,nc)==1&&board[nr][nc]==0)
     { comx[counters]=nr;
       comy[counters]=nc;
       counters++;}
/*-------decide the next step by digui function------------*/
  decidedepth();
 for(possibility=0;possibility<=counters;possibility++)
  { initvirtualboard(0);
    virtualcolor=chesscolor;
    think(comx[possibility],comy[possibility],1,result+possibility);}
   sortout(result,counters,&final);
   chessrules(comx[final],comy[final]);
   mandone=0;
  setfillstyle(1,WHITE);
  bar(0,0,170,30);
}
/*-----------------the digui part--------------*/
void think(int pcomx,int pcomy,int mind,int *presult)
{
  int comx[20],comy[20],nc,nr;
  int counters=0,possibility;
  int result[20],final;
  int oldcolor;
  virtualrules(pcomx,pcomy);
  oldcolor=virtualcolor;
  initvirtualboard(mind); /*record the old things*/
  if(mind==thinkdepth)
    virtualscore(presult);
  else
   { for(nr=1;nr<=8;nr++)
      for(nc=1;nc<=8;nc++)
      if(virtualscan(nr,nc)==1&&virtualboard[nr][nc]==0)
       {
	comx[counters]=nr;
	comy[counters]=nc;
	counters++;
       }
    for(possibility=0;possibility<=counters;possibility++)
     {
     virtualcolor=oldcolor;
     initvirtualboardnum(mind);
     think(comx[possibility],comy[possibility],mind+1,result+possibility);
     }
    sortout(result,counters,&final);
    *presult=result[final];
  }
}
/*-------all the functions listed below are only ---------*/
/* ------used when the computer is thinking and-----------*/
/*-------done on the virtual board------------------------*/
/*--------------------------------------------------------*/
/*--------------decide the digui depth--------------------*/
void decidedepth()
{
  int chess;
  chess=(ws+bs);
 if(manfirst==0)
 {
 if(chess>=4&&chess<=32)
   thinkdepth=3;
 else if(chess>32&&chess<=48)
   thinkdepth=5;
 else
   thinkdepth=(60-(bs+ws))/2;
 }
 else
 {
 if(chess>=4&&chess<=32)
   thinkdepth=4;
 else if(chess>32&&chess<=48)
   thinkdepth=6;
 else
   thinkdepth=(60-(bs+ws))/2;
  }
}
/*-------------value the profit on the virtualboard by the finalscore------*/
void virtualscore(int *presult)
{
 int a,b;
 int wscore=0,bscore=0;
  for(a=1;a<=8;a++)
   for(b=1;b<=8;b++)
   {
    if(virtualboard[a][b]==WHITECHESS)
      wscore++;
    else if(virtualboard[a][b]==BLACKCHESS)
      bscore++;
    }
  gotoxy(1,1);
  printf("%d",wscore-bscore);
  *presult=(wscore-bscore);
}
/*------------choose the highest score-----------------*/
/*
void minsortout(int result[],int counters,int *pfinal)
{
  int i;
  *pfinal=0;
  for(i=1;i<=counters;i++)
  {
   if(result[i]<result[i-1])
       *pfinal=i;
   else
    result[i]=result[i-1];
  }
} */
/*------------choose the highest score-----------------*/
void sortout(int result[],int counters,int *pfinal)
{
  int i;
  *pfinal=0;
  for(i=1;i<=counters;i++)
  {
   if(result[i]>result[i-1])
       *pfinal=i;
   else
    result[i]=result[i-1];
  }
}
/* do as the virtualchessrules */
void virtualrules(int x,int y)
 {
  if(virtualboard[x][y]==0&&virtualscan(x,y)==1)
   {
      virtualeatchess(x,y);
    if(virtualcolor==WHITECHESS)    /*change the turn*/
      virtualcolor=BLACKCHESS;
    else
      virtualcolor=WHITECHESS;
   }
}
/* ------scan the virtualboard in the eight direction around the chess---- */
int virtualscan(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(virtualboard[x][y]==0||virtualboard[x][y]==virtualcolor&&same==0)
	break;
    else if(virtualboard[x][y]==virtualcolor)
       {jump=1;
	break;}
    else  same=1;
    }
}
 if(jump==1) return 1;
 else return 0;
}
/*----------------virtualeat the different chess around the chess---------- */
void virtualeatchess(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(virtualboard[currentrow][currentcolumn]==0||virtualboard[currentrow][currentcolumn]==chesscolor&&same==1)
	 break;
      else if(virtualboard[currentrow][currentcolumn]==chesscolor)
       { virtualeat(initrow,initcolumn,currentrow,currentcolumn,direct);
	 break;}
      else same=0;
      }
   while(currentrow>=1&&currentrow<=8&&currentcolumn>=1&&currentcolumn<=8);
 }
}
/*-------------virtualeat the chess in one direction----------- */
void virtualeat(int irow,int icolumn,int crow,int ccolumn,int direct)
{
   int startrow,startcolumn;
    startrow=irow;
    startcolumn=icolumn;
    do
      {
	if(virtualcolor==WHITECHESS)
	  virtualboard[startrow][startcolumn]=WHITECHESS;
	else if(virtualcolor==BLACKCHESS)
	  virtualboard[startrow][startcolumn]=BLACKCHESS;
	startrow+=direction[direct][0];
	startcolumn+=direction[direct][1];
      }
    while(startrow!=crow||startcolumn!=ccolumn);
}
/*--------initialize the virtual on the different depth-----------*/
void initvirtualboard(int depth)
{
  int nr,nc;
  switch(depth){
  case(0):
  for(nr=0;nr<=10;nr++)
      for(nc=0;nc<=10;nc++)
      virtualboard[nr][nc]=board[nr][nc];
      break;
  case(1):
   for(nr=0;nr<=10;nr++)
      for(nc=0;nc<=10;nc++)
      virtualboard1[nr][nc]=virtualboard[nr][nc];
      break;
  case(2):
  for(nr=0;nr<=10;nr++)
      for(nc=0;nc<=10;nc++)
      virtualboard2[nr][nc]=virtualboard[nr][nc];
      break;
  case(3):
  for(nr=0;nr<=10;nr++)
      for(nc=0;nc<=10;nc++)
      virtualboard3[nr][nc]=virtualboard[nr][nc];
      break;
  case(4):
  for(nr=0;nr<=10;nr++)
      for(nc=0;nc<=10;nc++)
      virtualboard4[nr][nc]=virtualboard[nr][nc];
      break;
  case(5):
  for(nr=0;nr<=10;nr++)
      for(nc=0;nc<=10;nc++)
      virtualboard5[nr][nc]=virtualboard[nr][nc];
      break;
  case(6):
  for(nr=0;nr<=10;nr++)
      for(nc=0;nc<=10;nc++)
      virtualboard6[nr][nc]=virtualboard[nr][nc];
      break;
   default:break;
 }
}
/*--------initialize the virtual on the different depth-----------*/
void initvirtualboardnum(int depth)
{
  int nr,nc;
  switch(depth){
  case(1):
   for(nr=0;nr<=10;nr++)
      for(nc=0;nc<=10;nc++)
      virtualboard[nr][nc]=virtualboard1[nr][nc];
      break;
  case(2):
  for(nr=0;nr<=10;nr++)
      for(nc=0;nc<=10;nc++)
      virtualboard[nr][nc]=virtualboard2[nr][nc];
      break;
  case(3):
  for(nr=0;nr<=10;nr++)
      for(nc=0;nc<=10;nc++)
      virtualboard[nr][nc]=virtualboard3[nr][nc];
      break;
  case(4):
  for(nr=0;nr<=10;nr++)
      for(nc=0;nc<=10;nc++)
      virtualboard[nr][nc]=virtualboard4[nr][nc];
      break;
  case(5):
  for(nr=0;nr<=10;nr++)
      for(nc=0;nc<=10;nc++)
      virtualboard[nr][nc]=virtualboard5[nr][nc];
      break;
  case(6):
  for(nr=0;nr<=10;nr++)
      for(nc=0;nc<=10;nc++)
      virtualboard[nr][nc]=virtualboard6[nr][nc];
      break;
  default:break;
 }
}
void computer2()
{
 int boardscore[10][10]={{0,0,0,0,0,0,0,0,0,0},
			 {0,8,0,5,4,4,5,0,8,0},
			 {0,0,0,1,1,1,1,0,0,0},
			 {0,5,1,3,2,2,3,1,5,0},
			 {0,4,1,2,2,2,2,1,4,0},
			 {0,4,1,2,2,2,2,1,4,0},
			 {0,5,1,3,2,2,3,1,5,0},
			 {0,0,0,1,1,1,1,0,0,0},
			 {0,8,0,5,4,4,5,0,8,0},
			 {0,0,0,0,0,0,0,0,0,0}};
  int comx,comy;
  int nc,nr;
  int counters=0;
/*-----------the first kind of special things-------------*/
  if(board[1][1]==1)
    boardscore[2][2]=boardscore[1][2]=boardscore[2][1]=2;
  if(board[8][8]==1)
    boardscore[7][7]=boardscore[7][8]=boardscore[8][7]=2;
  if(board[1][8]==1)
    boardscore[2][7]=boardscore[1][7]=boardscore[2][8]=2;
  if(board[8][1]==1)
    boardscore[7][2]=boardscore[8][2]=boardscore[7][1]=2;
 /*-----------the second kind of special things-------------*/
  if(board[2][2]==1)
    boardscore[1][3]=boardscore[3][1]=0;
  else
    boardscore[1][3]=boardscore[3][1]=5;
  if(board[7][7]==2)
    boardscore[8][6]=boardscore[6][8]=0;
  else
    boardscore[8][6]=boardscore[6][8]=5;
  if(board[7][2]==2)
    boardscore[6][1]=boardscore[8][3]=0;
  else
    boardscore[6][1]=boardscore[8][3]=5;
  if(board[2][7]==2)
    boardscore[1][6]=boardscore[3][8]=0;
  else
    boardscore[1][6]=boardscore[3][8]=5;

/*-----------the third kind of special things-------------*/
  if(board[8][3]==1&&board[8][4]==1&&board[8][7]==1&&board[8][3]==1)
    boardscore[8][2]=boardscore[8][6]=7;
  if(board[1][3]==1&&board[1][4]==1&&board[1][5]==1&&board[1][6]==1)
    boardscore[1][2]=boardscore[1][6]=7;
  if(board[3][1]==1&&board[4][1]==1&&board[5][1]==1&&board[6][1]==1)
    boardscore[2][1]=boardscore[6][1]=7;
  if(board[3][8]==1&&board[4][8]==1&&board[4][8]==1&&board[6][8]==1)
    boardscore[2][8]=boardscore[6][8]=7;
/*-----------the forth kind of special things-------------*/
  if(board[1][5]!=1&&board[1][7]!=1&&board[1][6]==2&&board[1][3]!=2)
    boardscore[1][3]=boardscore[1][7]=3;
  if(board[5][1]!=1&&board[4][1]!=2&&board[6][1]==2&&board[3][8]!=2)
    boardscore[3][1]=boardscore[7][1]=3;
  if(board[5][8]!=1&&board[4][8]!=2&&board[6][8]==2&&board[3][8]!=2)
    boardscore[3][8]=boardscore[7][8]=3;
  if(board[8][5]==1&&board[8][4]!=2&&board[8][6]==2&&board[8][3]!=2)
    boardscore[8][3]=boardscore[8][7]=3;
/*-----------the fifth kind of special things-------------*/
  if(board[1][3]==1&&board[1][4]!=1&&board[1][5]==2)
    boardscore[1][2]=boardscore[1][4]=0;
  if(board[8][3]==1&&board[8][4]!=1&&board[8][5]==2)
    boardscore[8][2]=boardscore[8][4]=0;
  if(board[1][6]==1&&board[1][5]!=1&&board[1][4]==2)
    boardscore[1][7]=boardscore[1][5]=0;
  if(board[8][6]==1&&board[8][5]!=1&&board[8][4]==2)
    boardscore[8][7]=boardscore[8][5]=0;
  if(board[3][1]==1&&board[4][1]!=1&&board[5][1]==2)
    boardscore[2][1]=boardscore[4][1]=0;
  if(board[3][8]==1&&board[4][8]!=1&&board[5][8]==2)
    boardscore[2][8]=boardscore[4][8]=0;
  if(board[6][1]==1&&board[5][1]!=1&&board[4][1]==2)
    boardscore[7][1]=boardscore[5][1]=0;
  if(board[6][8]==1&&board[5][8]!=1&&board[4][8]==2)
    boardscore[7][8]=boardscore[5][8]=0;
  randomize();
  for(nr=1;nr<=8;nr++)
    for(nc=1;nc<=8;nc++)
     if(scan(nr,nc)==1&&board[nr][nc]==0)
     {

      if(counters==0||boardscore[nr][nc]>boardscore[comx][comy])
	{comx=nr;
	 comy=nc;
	 counters++;}
       else if(boardscore[nr][nc]==boardscore[comx][comy]&&random(2)==1)
	{comx=nr;
	 comy=nc;
	 counters++;}
   }
   chessrules(comx,comy);
   mandone=0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -