黑白棋改进

  • Post author:
  • Post category:其他


#include<iostream>

#include<windows.h>

#include<stdlib.h>

#include<time.h>

using namespace std;//黑白棋,茂。

int comcolor;//电脑颜色

int color; //用户颜色

int row = 0, col = 0;

int cb[8][8] = {0};

int dr[8] = {0,1,1,1,0,-1,-1,-1};

int dc[8] = {-1,-1,0,1,1,1,0,-1};

int flag = 0;

int can = 0;

int s[8][8]={0};

void playgame();

void comgo();

int isend();

void show();

void mango();

int canmove(int nowrow,int nowcol,int dire,int tmpcolor);

void clean(int nowrow,int nowcol,int tmpcolor);

int clean2(int nowrow,int nowcol,int tmpcolor);

void end();


int main()

{


cb[3][3]=1;

cb[4][4]=1;

cb[3][4]=-1;

cb[4][3]=-1;

show();

cout <<“选择你的棋子颜色,1表示黑棋,-1表示白棋”<<endl;

cin >> color;

comcolor = color *(-1);

if(comcolor == 1)

{


comgo();

}

playgame();

end();

return 0;

}

void playgame()//游戏

{


system(“cls”);

show();

while(1)

{


flag = 0;

can = 0;

mango();

comgo();

system(“cls”);

//清屏

show();  //显示新地图

if(can == 0)

{


break;

}

}

}

void comgo() //电脑走

{


int i,j,k;

int tmp1=-1,tmp2=-1,tmp3=0;

for(i=0;i<8;i++)

{


for(j=0;j<8;j++)

{


for(k=0;k<8;k++)

{


if(cb[i][j] == 0 && canmove(i,j,k,comcolor)!=0)

{


s[i][j] = clean2(i,j,comcolor);

}

}

}

}

for(i=0; i < 8;i ++)

{


for(j=0;j < 8;j++ )

{


if(s[i][j] > 0)

{


if(s[i][j] > tmp3)

{


tmp1 = i;

tmp2 = j;

tmp3 = s[i][j];

}

}

}

}

if(tmp1 >= 0 && tmp2 >= 0)

{


cb[tmp1][tmp2] = comcolor;

can++;

clean(tmp1,tmp2,comcolor);

}

for(i=0;i<8;i++)

{


for(j=0;j<8;j++)

{


s[i][j]=0;

}

}

}

void end() //统计黑白棋数目,判断谁赢。

{


int i,j;

int com=0,r=0;

for(i=0;i<8;i++)

{


for(j=0;j<8;j++)

{


if(cb[i][j] == comcolor)

{


com ++;

}

if(cb[i][j] == color)

{


r++;

}

}

}

if(com > r)

{


cout<<“你输啦!”<<endl;

}

else

{


cout<<“你赢了!”<<endl;

}

}

int isend() //判断是否还能走

{


int i,j,k;

for(i=0;i<8;i++)

{


for(j=0;j<8;j++)

{


for(k=0;k<8;k++)

{


if(canmove(i,j,k,color))

{


return 1;

}

}

}

}

return 0;

}

void show() //显示地图

{


int i,j;

for(i=0;i<8;i++)

{


for(j=0;j<8;j++)

{


if(cb[i][j] != -1)

{


cout<< cb[i][j]<<” “;

}

else

{


cout<<cb[i][j]+3<<” “;

}

}

cout<< endl;

}

}

void mango() //用户走

{


int i,j,k;

int x,y;

int q=0;

if(isend() == 1)

{


while(1)

{


cin>>x >> y;

q=0;

for(i=0;i<8;i++)

{


if(cb[x-1][y-1] != 0 )

{


break;

}

if(canmove(x-1,y-1,i,color))

{

cb[x-1][y-1] = color;

clean(x-1,y-1,color);

q++;

can++;

break;

}

}

if(q != 0)

{


break;

}

}

}

}

int canmove(int nowrow,int nowcol,int dire,int tmpcolor)

//判断是否能走

{


int i,j;

int row1 = 0,col1 = 0;

int num=0;

int tmp=0;

flag = 0;

if(cb[nowrow][nowcol] != 0)

{


return 0;

}

nowrow = nowrow + dr[dire];

nowcol = nowcol + dc[dire];

while(cb[nowrow][nowcol] == tmpcolor*(-1) && nowrow >= 0 && nowrow < 8 && nowcol >= 0 && nowcol <8)

{


nowrow = nowrow + dr[dire];

nowcol = nowcol + dc[dire];

num++;

tmp++;

}

if(cb[nowrow][nowcol] == tmpcolor && num > 0)

{


flag = 1;

}

if(flag == 1 && nowrow >= 0 && nowrow < 8 && nowcol>=0 && nowcol<8)

{


return (1);

}

return 0;

}


void clean(int nowrow,int nowcol,int tmpcolor)  //清理被吃掉的棋子

{


int i;

int t1,t2;

int p=0;

t1=nowrow;

t2=nowcol;

for(i=0;i<8;i++)

{


nowrow=t1;

nowcol=t2;

nowrow = nowrow + dr[i];

nowcol = nowcol + dc[i];

p=0;

while(cb[nowrow][nowcol] == tmpcolor*(-1) && nowrow >= 0 && nowrow < 8 && nowcol >= 0 && nowcol <8)

{

nowrow = nowrow + dr[i];

nowcol = nowcol + dc[i];

p++;

}

if(cb[nowrow][nowcol] == tmpcolor && p != 0)

{

nowrow = nowrow – dr[i];

nowcol = nowcol – dc[i];

while(cb[nowrow][nowcol] == tmpcolor*(-1) && nowrow >= 0 && nowrow < 8 && nowcol >= 0 && nowcol <8)

{

cb[nowrow][nowcol] = tmpcolor;

nowrow = nowrow – dr[i];

nowcol = nowcol – dc[i];

}

}

}

}


int clean2(int nowrow,int nowcol,int tmpcolor)  //清理被吃掉的棋子

{


int i;

int t1,t2;

int p=0;

int q;

t1 = nowrow;

t2 = nowcol;

q = 0;

for(i=0;i<8;i++)

{


nowrow=t1;

nowcol=t2;

nowrow = nowrow + dr[i];

nowcol = nowcol + dc[i];

p=0;

while(cb[nowrow][nowcol] == tmpcolor*(-1) && nowrow >= 0 && nowrow < 8 && nowcol >= 0 && nowcol <8)

{

nowrow = nowrow + dr[i];

nowcol = nowcol + dc[i];

p++;

}

if(cb[nowrow][nowcol] == tmpcolor && p != 0)

{

nowrow = nowrow – dr[i];

nowcol = nowcol – dc[i];

while(cb[nowrow][nowcol] == tmpcolor*(-1) && nowrow >= 0 && nowrow < 8 && nowcol >= 0 && nowcol <8)

{

q++;

nowrow = nowrow – dr[i];

nowcol = nowcol – dc[i];

}

}

}

return q;

}





版权声明:本文为zym339原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。