黑白棋对战(电脑下不过我)



#include<iostream>
#include<windows.h>
using namespace std;

int comcolur=1,colur;//comcolur=1电脑走
//八个方向的偏移量。
int dr[8]={0, 1, 1, 1, 0, -1, -1, -1};
int dc[8]={-1, -1, 0, 1, 1, 1, 0, -1};
int map[8][8]={0};
int tmp;

void showmap();
void playgame();
int value(int row,int col,int k);
int rvalue(int row,int col,int k);
int canplay();
int isright(int x,int y);

int main()
{
 map[3][3]=1;
 map[3][4]=2;
 map[4][3]=2;
 map[4][4]=1;
 
 showmap();
 cout<<"请选择你的颜色 1表示黑色 2表示白色"<<endl;
 cin>>tmp;
 
 comcolur=tmp%2+1;
 
 playgame();
 
 return 0;
}


void showmap()
{
 int i,j;
 for(i=0; i<8; i++)
 {
  for(j=0; j<8; j++)
  {
   cout<<map[i][j];
   cout<<" ";
  }
  cout<<endl;
 }
}


void playgame()
{
 int i,j,k;
 int px,py;
 int tmp3,tmp1,tmp2;
 int sum[8][8]={0};
 int ntmp1,ntmp2,flag=1;
 int flag1=0;
 int px1,py1;
 int count1,count2;
 
 
 
 while(canplay()==1||flag1==1)//如果还可以继续玩
 {
  if(comcolur==1)
  {//电脑搜索每一个位置看能不能走
   flag1=0;
   for(i=0; i<8; i++)
   {
    for(j=0; j<8; j++)
    {
     sum[i][j]=0;
    }
   }
   for(i=0; i<8; i++)
   {
    for(j=0; j<8; j++)
    {
     for(k=0; k<8; k++)
     {
      sum[i][j]=sum[i][j]+value(i,j,k);
     }
    }
   }
   tmp3= sum[0][0];
   tmp1=0;
   tmp2=0;
   for(i=0; i<8; i++)
   {
    for(j=0; j<8; j++)
    {
     if(tmp3<sum[i][j])
     {
      tmp3=sum[i][j];
      tmp1=i;
      tmp2=j;
     }
    }
   }
   if(tmp3>0)
   {
    map[tmp1][tmp2]=tmp%2+1;
    
    for(i=0; i<8; i++)
    {
     ntmp1=tmp1+dr[i];
     ntmp2=tmp2+dc[i];
     flag=0;
     while(ntmp1>=0&&ntmp1<8&&ntmp2>=0&&ntmp2<8&&map[ntmp1][ntmp2]==tmp)
     {
      flag++;
      ntmp1=ntmp1+dr[i];
      ntmp2=ntmp2+dc[i];
     }
     if(flag!=0&&ntmp1>=0&&ntmp1<8&&ntmp2>=0&&ntmp2<8&&map[ntmp1][ntmp2]==tmp%2+1)
     {
      ntmp1=0;
      ntmp2=0;
      flag1=1;
      for(j=0; j<flag; j++)
      {
       ntmp1=tmp1+dr[i]*(j+1);
       ntmp2=tmp2+dc[i]*(j+1);
       map[ntmp1][ntmp2]=tmp%2+1;
      }
     }
    }
   }
   
  }
  
  for(i=0; i<8; i++)
  {
   for(j=0; j<8; j++)
   {
    cout<<map[i][j]<<" ";
   }
   cout<<endl;
  }
  cout<<endl;
  
  
  
  if(canplay())
  {
   flag1=1;
   comcolur=1;
   cin>>px>>py;
   while(isright(px,py)==0)
   {
    cout<<"输入不合法,请重新输入"<<endl;
    cin>>px>>py;
   }
   
   map[px][py]=tmp;
   for(i=0; i<8; i++)
   {
    px1=px+dr[i];
    py1=py+dc[i];
    flag=0;
    while(px1>=0&&px1<8&&py1>=0&&py1<8&&map[px1][py1]==tmp%2+1)
    {
     flag++;
     px1=px1+dr[i];
     py1=py1+dc[i];
    }
    if(flag!=0&&px1>=0&&px1<8&&py1>=0&&py1<8&&map[px1][py1]==tmp)
    {
     px1=0;
     py1=0;
     for(j=0; j<flag; j++)
     {
      px1=px+dr[i]*(j+1);
      py1=py+dc[i]*(j+1);
      map[px1][py1]=tmp;
     }
    }
   }
  }
  
  
  for(i=0; i<8; i++)
  {
   for(j=0; j<8; j++)
   {
    if(map[i][j+1]==-1&&j+1<8)
    {
     cout<<map[i][j];
    }
    else
    {
     cout<<map[i][j]<<" ";
    }
   }
   cout<<endl;
  }
  cout<<endl;
 }

 for(i=0; i<8; i++)
 {
  for(j=0; j<8; j++)
  {
   if(map[i][j]==tmp)
   {
    count1++;
   }
   else
   {
    count2++;
   }
  }
 }
 if(count1>count2)
 {
  cout<<"You Win"<<endl;
 }
 else if(count1==count2)
 {
  cout<<"平局"<<endl;
 }
 else
 {
  cout<<"You Lose"<<endl;
 }
}


int value(int row,int col,int k)
{
 int sum;
 int nrow,ncol;
 nrow=row+dr[k];
 ncol=col+dc[k];
 if(map[row][col]==0)
 {
  map[row][col]=tmp%2+1;
 }
 else
 {
  return 0;
 }
 sum=0;
 if(nrow<0||nrow>=8||ncol>=8||ncol<0||map[nrow][ncol]*map[row][col]!=2)
 {
  map[row][col]=0;
  return 0;
 }
 while(nrow>=0&&nrow<8&&ncol<8&&ncol>=0&&map[nrow][ncol]*map[row][col]==2)
 {
  sum++;
  nrow=nrow+dr[k];
  ncol=ncol+dc[k];
 }
 if(nrow>=0&&nrow<8&&ncol<8&&ncol>=0&&map[nrow][ncol]==map[row][col])
 {
  map[row][col]=0;
  return sum;
 }
 else
 {
  map[row][col]=0;
  return 0;
 }
}


int canplay()
{
 int i,j,k;
 int bug[8][8];
 
 for(i=0; i<8; i++)
 {
  for(j=0; j<8; j++)
  {
   bug[i][j]=0;
  }
 }
 
 for(i=0; i<8; i++)
 {
  for(j=0; j<8; j++)
  {
   for(k=0; k<8; k++)
   {
    bug[i][j]=bug[i][j]+rvalue(i,j,k);
   }
  }
 }
 for(i=0; i<8; i++)
 {
  for(j=0; j<8; j++)
  {
   if(bug[i][j]!=0)
   {
    return 1;
   }
  }
 }
 return 0;
}


int rvalue(int row,int col,int k)
{
 int nrow,ncol;
 int sum=0;
 
 if(map[row][col]==0)
 {
  sum=0;
  nrow=row+dr[k];
  ncol=col+dc[k];
  while(nrow>=0&&nrow<8&&ncol>=0&&ncol<8&&map[nrow][ncol]==tmp%2+1)
  {
   sum++;
   nrow=nrow+dr[k];
   ncol=ncol+dc[k];
  }
  if(sum>0&&nrow>=0&&nrow<8&&ncol<8&&ncol>=0&&map[nrow][ncol]==tmp)
  {
   return sum;
  }
  else
  {
   return 0;
  }
 }
 return 0;
}

int isright(int x,int y)
{
 int right=0;
 int i;
 
 for(i=0; i<8; i++)
 {
  right=right+rvalue(x,y,i);
 }
 if(right==0)
 {
  return 0;
 }
 else
 {
  return 1;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include "stdafx.h" #include "Draw.h" #include #include #include "DrawDoc.h" #include "DrawView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif //zhan// //////////////////////////////////////////////////////////////////////////// // CDrawView IMPLEMENT_DYNCREATE(CDrawView, CView) BEGIN_MESSAGE_MAP(CDrawView, CView) //{{AFX_MSG_MAP(CDrawView) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDrawView construction/destruction CDrawView::CDrawView() { // TODO: add construction code here } CDrawView::~CDrawView() { } BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CDrawView drawing int board[8][8]; CBrush white(RGB(255,255,255)); CBrush black(RGB(0,0,0)); int player = 1; struct node{ int data[8][8]; struct node*next; }; struct node *head; void CDrawView::OnDraw(CDC* pDC) { int i,j; CDrawDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here pDC->Rectangle(CRect(490,0,550,20)); pDC->DrawText("悔棋",CRect(500,5,600,20),0); if(player==1) { pDC->DrawText("白棋",CRect(0,0,100,20),0); } else { pDC->DrawText("黑棋",CRect(0,0,100,20),0); } for(i=0;iMoveTo(50,50+50*i);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值