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

📄 main.cpp

📁 五子棋程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include <shellapi.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "Main.h"
#include "SockSet.h"
#include "DebugFm.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TMainForm *MainForm;
const int black=1,white=2,empty=4;
//clr:color<1---black,2---white,4---empty>
//black^11=white,white^11=black
//有效棋盘的x,y范围[4,22]
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent *Owner)
	: TForm(Owner)
{
}
//----------------------------------------------------------------------------
void __fastcall TMainForm::HelpContents(TObject *Sender)
{
	Application->HelpCommand(HELP_CONTENTS, 0);
}
//----------------------------------------------------------------------------
void __fastcall TMainForm::HelpAbout(TObject *Sender)
{
ShellAbout(Handle,"","\n五子棋(V1.2)\n版权所有(C) 1999-2002 NowCan. ",NULL);
}
//----------------------------------------------------------------------------
void __fastcall TMainForm::Exit1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormCreate(TObject *Sender)
{
InitBoard();
randomize();
Level=2;
man=true;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::InitBoard()
{
Image2->Canvas->Pen->Color=clGray;
for(int i=0;i<401;i++)
  {
  Image2->Canvas->MoveTo(i,0);
  Image2->Canvas->LineTo(i,401);
  }
Image2->Canvas->Pen->Color=clBlack;
for(int i=20;i<=380;i+=20)
  {
  Image2->Canvas->MoveTo(i,20);
  Image2->Canvas->LineTo(i,381);
  Image2->Canvas->MoveTo(20,i);
  Image2->Canvas->LineTo(380,i);
  }
for(int i=7;i<=19;i+=6)
  for(int j=7;j<=19;j+=6)
    DrawChess(i,j,3,black);
for(int i=0;i<=26;i++)
  for(int j=0;j<=26;j++)
    ch5[i][j]=empty;
for(int i=0;i<=5;i++)
  StatusLine->Panels->Items[i]->Text="";
Label1->Caption="";
Shape1->Left=Image2->Left+200-4;
Shape1->Top=Image2->Top+200-4;
}
//---------------------------------------------------------
void __fastcall TMainForm::DrawChess(int x,int y,int r,int color)
{
x=(x-3)*20;
y=(y-3)*20;
if(color==black)
{
  Image2->Canvas->Pen->Color=clBlack;
  Image2->Canvas->Brush->Color=clBlack;
}
else
{
  Image2->Canvas->Pen->Color=clWhite;
  Image2->Canvas->Brush->Color=clWhite;
}
Image2->Canvas->Ellipse(x-r,y-r,x+r+1,y+r+1);
}
//---------------------------------------------------------
bool __fastcall TMainForm::Check5(int color)
{
int n;
for(int i=4;i<=22;i++)     //down
  for(int j=4;j<=18;j++)
    if(ch5[i][j]==color)
      {
      n=4;
      while(n&&ch5[i][j+n]==color)n--;
      if(!n)return true;
      }
for(int i=4;i<=18;i++)     //right-down
  for(int j=4;j<=18;j++)
    if(ch5[i][j]==color)
      {
      n=4;
      while(n&&ch5[i+n][j+n]==color)n--;
      if(!n)return true;
      }
for(int i=4;i<=18;i++)     //right
  for(int j=4;j<=22;j++)
    if(ch5[i][j]==color)
      {
      n=4;
      while(n&&ch5[i+n][j]==color)n--;
      if(!n)return true;
      }
for(int i=8;i<=22;i++)     //left-down
  for(int j=4;j<=18;j++)
    if(ch5[i][j]==color)
      {
      n=4;
      while(n&&ch5[i-n][j+n]==color)n--;
      if(!n)return true;
      }
return false;
}
//---------------------------------------------------------

void __fastcall TMainForm::Image2MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
int ix,iy;
ix=(X+10)/20+3;
iy=(Y+10)/20+3;
if(!(Check5(black)|Check5(white))&&man)
{
  if(ch5[ix][iy]==empty)
    {
    DrawChess(ix,iy,7,black);
    ch5[ix][iy]=black;
    Shape1->Left=Image2->Left+(ix-3)*20-4;
    Shape1->Top=Image2->Top+(iy-3)*20-4;
    StatusLine->Panels->Items[5]->Text="";
    Update();
    if(Check5(black))
      Label1->Caption="赢";
    else
      Computer();
    }
  else
    StatusLine->Panels->Items[5]->Text="Can't put chess here!";
  }
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::New1Click(TObject *Sender)
{
InitBoard();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::SaveBoard()
{
for(int i=4;i<=22;i++)
  for(int j=4;j<=22;j++)
    chb[i][j]=ch5[i][j];
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::RestoreBoard()
{
for(int i=4;i<=22;i++)
  for(int j=4;j<=22;j++)
    ch5[i][j]=chb[i][j];
}
//---------------------------------------------------------------------------
bool __fastcall TMainForm::To5(int x,int y,int color)
{
if(ch5[x][y]==empty)
  {
  if(ch5[x-1][y]&ch5[x-2][y]&ch5[x-3][y]&ch5[x-4][y]&color)
    return(true);
  if(ch5[x-1][y-1]&ch5[x-2][y-2]&ch5[x-3][y-3]&ch5[x-4][y-4]&color)
    return(true);
  if(ch5[x-1][y+1]&ch5[x-2][y+2]&ch5[x-3][y+3]&ch5[x-4][y+4]&color)
    return(true);
  if(ch5[x][y-1]&ch5[x][y-2]&ch5[x][y-3]&ch5[x][y-4]&color)
    return(true);
  if(ch5[x][y+1]&ch5[x][y+2]&ch5[x][y+3]&ch5[x][y+4]&color)
    return(true);
  if(ch5[x+1][y]&ch5[x+2][y]&ch5[x+3][y]&ch5[x+4][y]&color)
    return(true);
  if(ch5[x+1][y-1]&ch5[x+2][y-2]&ch5[x+3][y-3]&ch5[x+4][y-4]&color)
    return(true);
  if(ch5[x+1][y+1]&ch5[x+2][y+2]&ch5[x+3][y+3]&ch5[x+4][y+4]&color)
    return(true);

  if(ch5[x-1][y]&ch5[x-2][y]&ch5[x-3][y]&ch5[x+1][y]&color)
    return(true);
  if(ch5[x-1][y-1]&ch5[x-2][y-2]&ch5[x-3][y-3]&ch5[x+1][y+1]&color)
    return(true);
  if(ch5[x-1][y+1]&ch5[x-2][y+2]&ch5[x-3][y+3]&ch5[x+1][y-1]&color)
    return(true);
  if(ch5[x][y-1]&ch5[x][y-2]&ch5[x][y-3]&ch5[x][y+1]&color)
    return(true);
  if(ch5[x][y+1]&ch5[x][y+2]&ch5[x][y+3]&ch5[x][y-1]&color)
    return(true);
  if(ch5[x+1][y]&ch5[x+2][y]&ch5[x+3][y]&ch5[x-1][y]&color)
    return(true);
  if(ch5[x+1][y-1]&ch5[x+2][y-2]&ch5[x+3][y-3]&ch5[x-1][y+1]&color)
    return(true);
  if(ch5[x+1][y+1]&ch5[x+2][y+2]&ch5[x+3][y+3]&ch5[x-1][y-1]&color)
    return(true);

  if(ch5[x-1][y]&ch5[x-2][y]&ch5[x+2][y]&ch5[x+1][y]&color)
    return(true);
  if(ch5[x-1][y-1]&ch5[x-2][y-2]&ch5[x+2][y+2]&ch5[x+1][y+1]&color)
    return(true);
  if(ch5[x-1][y+1]&ch5[x-2][y+2]&ch5[x+2][y-2]&ch5[x+1][y-1]&color)
    return(true);
  if(ch5[x][y-1]&ch5[x][y-2]&ch5[x][y+2]&ch5[x][y+1]&color)
    return(true);
  if(ch5[x][y+1]&ch5[x][y+2]&ch5[x][y-2]&ch5[x][y-1]&color)
    return(true);
  if(ch5[x+1][y]&ch5[x+2][y]&ch5[x-2][y]&ch5[x-1][y]&color)
    return(true);
  if(ch5[x+1][y-1]&ch5[x+2][y-2]&ch5[x-2][y+2]&ch5[x-1][y+1]&color)
    return(true);
  if(ch5[x+1][y+1]&ch5[x+2][y+2]&ch5[x-2][y-2]&ch5[x-1][y-1]&color)
    return(true);

  return(false);
  }
return(false);
}
//---------------------------------------------------------------------------
int __fastcall TMainForm::To4(int x,int y,int color)
{
int ret=0;
if(ch5[x][y]==empty)
  {
  //Ebbbe
  if((ch5[x+4][y]==empty)&&(ch5[x+1][y]&ch5[x+2][y]&ch5[x+3][y]&color))
    ret++;
  if((ch5[x+4][y+4]==empty)&&(ch5[x+1][y+1]&ch5[x+2][y+2]&ch5[x+3][y+3]&color))
    ret++;
  if((ch5[x+4][y-4]==empty)&&(ch5[x+1][y-1]&ch5[x+2][y-2]&ch5[x+3][y-3]&color))
    ret++;
  if((ch5[x][y+4]==empty)&&(ch5[x][y+1]&ch5[x][y+2]&ch5[x][y+3]&color))
    ret++;
  if((ch5[x][y-4]==empty)&&(ch5[x][y-1]&ch5[x][y-2]&ch5[x][y-3]&color))
    ret++;
  if((ch5[x-4][y]==empty)&&(ch5[x-1][y]&ch5[x-2][y]&ch5[x-3][y]&color))
    ret++;
  if((ch5[x-4][y+4]==empty)&&(ch5[x-1][y+1]&ch5[x-2][y+2]&ch5[x-3][y+3]&color))
    ret++;
  if((ch5[x-4][y-4]==empty)&&(ch5[x-1][y-1]&ch5[x-2][y-2]&ch5[x-3][y-3]&color))
    ret++;
  //Ebbeb
  if((ch5[x+3][y]==empty)&&(ch5[x+4][y]&ch5[x+2][y]&ch5[x+1][y]&color))
    ret++;
  if((ch5[x+3][y+3]==empty)&&(ch5[x+4][y+4]&ch5[x+2][y+2]&ch5[x+1][y+1]&color))
    ret++;
  if((ch5[x+3][y-3]==empty)&&(ch5[x+4][y-4]&ch5[x+2][y-2]&ch5[x+1][y-1]&color))
    ret++;
  if((ch5[x][y+3]==empty)&&(ch5[x][y+4]&ch5[x][y+2]&ch5[x][y+1]&color))
    ret++;
  if((ch5[x][y-3]==empty)&&(ch5[x][y-4]&ch5[x][y-2]&ch5[x][y-1]&color))
    ret++;
  if((ch5[x-3][y]==empty)&&(ch5[x-4][y]&ch5[x-2][y]&ch5[x-1][y]&color))
    ret++;
  if((ch5[x-3][y+3]==empty)&&(ch5[x-4][y+4]&ch5[x-2][y+2]&ch5[x-1][y+1]&color))
    ret++;
  if((ch5[x-3][y-3]==empty)&&(ch5[x-4][y-4]&ch5[x-2][y-2]&ch5[x-1][y-1]&color))
    ret++;
  //Ebebb
  if((ch5[x+2][y]==empty)&&(ch5[x+1][y]&ch5[x+3][y]&ch5[x+4][y]&color))
    ret++;
  if((ch5[x+2][y+2]==empty)&&(ch5[x+1][y+1]&ch5[x+3][y+3]&ch5[x+4][y+4]&color))
    ret++;
  if((ch5[x+2][y-2]==empty)&&(ch5[x+1][y-1]&ch5[x+3][y-3]&ch5[x+4][y-4]&color))
    ret++;
  if((ch5[x][y+2]==empty)&&(ch5[x][y+1]&ch5[x][y+3]&ch5[x][y+4]&color))
    ret++;
  if((ch5[x][y-2]==empty)&&(ch5[x][y-1]&ch5[x][y-3]&ch5[x][y-4]&color))
    ret++;
  if((ch5[x-2][y]==empty)&&(ch5[x-1][y]&ch5[x-3][y]&ch5[x-4][y]&color))
    ret++;
  if((ch5[x-2][y+2]==empty)&&(ch5[x-1][y+1]&ch5[x-3][y+3]&ch5[x-4][y+4]&color))
    ret++;
  if((ch5[x-2][y-2]==empty)&&(ch5[x-1][y-1]&ch5[x-3][y-3]&ch5[x-4][y-4]&color))
    ret++;
  //Eebbb
  if((ch5[x+1][y]==empty)&&(ch5[x+2][y]&ch5[x+3][y]&ch5[x+4][y]&color))
    ret++;
  if((ch5[x+1][y+1]==empty)&&(ch5[x+2][y+2]&ch5[x+3][y+3]&ch5[x+4][y+4]&color))
    ret++;
  if((ch5[x+1][y-1]==empty)&&(ch5[x+2][y-2]&ch5[x+3][y-3]&ch5[x+4][y-4]&color))
    ret++;
  if((ch5[x][y+1]==empty)&&(ch5[x][y+2]&ch5[x][y+3]&ch5[x][y+4]&color))
    ret++;
  if((ch5[x][y-1]==empty)&&(ch5[x][y-2]&ch5[x][y-3]&ch5[x][y-4]&color))
    ret++;
  if((ch5[x-1][y]==empty)&&(ch5[x-2][y]&ch5[x-3][y]&ch5[x-4][y]&color))
    ret++;
  if((ch5[x-1][y+1]==empty)&&(ch5[x-2][y+2]&ch5[x-3][y+3]&ch5[x-4][y+4]&color))
    ret++;
  if((ch5[x-1][y-1]==empty)&&(ch5[x-2][y-2]&ch5[x-3][y-3]&ch5[x-4][y-4]&color))
    ret++;
  //bEbbe
  if((ch5[x+3][y]==empty)&&(ch5[x+2][y]&ch5[x+1][y]&ch5[x-1][y]&color))
    ret++;
  if((ch5[x+3][y+3]==empty)&&(ch5[x+2][y+2]&ch5[x+1][y+1]&ch5[x-1][y-1]&color))
    ret++;
  if((ch5[x+3][y-3]==empty)&&(ch5[x+2][y-2]&ch5[x+1][y-1]&ch5[x-1][y+1]&color))
    ret++;
  if((ch5[x][y+3]==empty)&&(ch5[x][y+2]&ch5[x][y+1]&ch5[x][y-1]&color))
    ret++;
  if((ch5[x][y-3]==empty)&&(ch5[x][y-2]&ch5[x][y-1]&ch5[x][y+1]&color))
    ret++;
  if((ch5[x-3][y]==empty)&&(ch5[x-2][y]&ch5[x-1][y]&ch5[x+1][y]&color))
    ret++;
  if((ch5[x-3][y+3]==empty)&&(ch5[x-2][y+2]&ch5[x-1][y+1]&ch5[x+1][y-1]&color))
    ret++;
  if((ch5[x-3][y-3]==empty)&&(ch5[x-2][y-2]&ch5[x-1][y-1]&ch5[x+1][y+1]&color))
    ret++;
  //bEebb
  if((ch5[x+1][y]==empty)&&(ch5[x-1][y]&ch5[x+2][y]&ch5[x+3][y]&color))
    ret++;
  if((ch5[x+1][y+1]==empty)&&(ch5[x-1][y-1]&ch5[x+2][y+2]&ch5[x+3][y+3]&color))
    ret++;
  if((ch5[x+1][y-1]==empty)&&(ch5[x-1][y+1]&ch5[x+2][y-2]&ch5[x+3][y-3]&color))
    ret++;
  if((ch5[x][y+1]==empty)&&(ch5[x][y-1]&ch5[x][y+2]&ch5[x][y+3]&color))
    ret++;
  if((ch5[x][y-1]==empty)&&(ch5[x][y+1]&ch5[x][y-2]&ch5[x][y-3]&color))
    ret++;
  if((ch5[x-1][y]==empty)&&(ch5[x+1][y]&ch5[x-2][y]&ch5[x-3][y]&color))
    ret++;
  if((ch5[x-1][y+1]==empty)&&(ch5[x+1][y-1]&ch5[x-2][y+2]&ch5[x-3][y+3]&color))
    ret++;
  if((ch5[x-1][y-1]==empty)&&(ch5[x+1][y+1]&ch5[x-2][y-2]&ch5[x-3][y-3]&color))
    ret++;
  //bEbeb
  if((ch5[x+2][y]==empty)&&(ch5[x-1][y]&ch5[x+1][y]&ch5[x+3][y]&color))
    ret++;
  if((ch5[x+2][y+2]==empty)&&(ch5[x-1][y-1]&ch5[x+1][y+1]&ch5[x+3][y+3]&color))
    ret++;
  if((ch5[x+2][y-2]==empty)&&(ch5[x-1][y+1]&ch5[x+1][y-1]&ch5[x+3][y-3]&color))
    ret++;
  if((ch5[x][y+2]==empty)&&(ch5[x][y-1]&ch5[x][y+1]&ch5[x][y+3]&color))
    ret++;
  if((ch5[x][y-2]==empty)&&(ch5[x][y+1]&ch5[x][y-1]&ch5[x][y-3]&color))
    ret++;
  if((ch5[x-2][y]==empty)&&(ch5[x+1][y]&ch5[x-1][y]&ch5[x-3][y]&color))
    ret++;
  if((ch5[x-2][y+2]==empty)&&(ch5[x+1][y-1]&ch5[x-1][y+1]&ch5[x-3][y+3]&color))
    ret++;
  if((ch5[x-2][y-2]==empty)&&(ch5[x+1][y+1]&ch5[x-1][y-1]&ch5[x-3][y-3]&color))
    ret++;
  //bbEbe
  if((ch5[x+2][y]==empty)&&(ch5[x-2][y]&ch5[x-1][y]&ch5[x+1][y]&color))
    ret++;
  if((ch5[x+2][y+2]==empty)&&(ch5[x-2][y-2]&ch5[x-1][y-1]&ch5[x+1][y+1]&color))
    ret++;
  if((ch5[x+2][y-2]==empty)&&(ch5[x-2][y+2]&ch5[x-1][y+1]&ch5[x+1][y-1]&color))
    ret++;
  if((ch5[x][y+2]==empty)&&(ch5[x][y-2]&ch5[x][y-1]&ch5[x][y+1]&color))
    ret++;
  if((ch5[x][y-2]==empty)&&(ch5[x][y+2]&ch5[x][y+1]&ch5[x][y-1]&color))
    ret++;
  if((ch5[x-2][y]==empty)&&(ch5[x+2][y]&ch5[x+1][y]&ch5[x-1][y]&color))
    ret++;
  if((ch5[x-2][y+2]==empty)&&(ch5[x+2][y-2]&ch5[x+1][y-1]&ch5[x-1][y+1]&color))
    ret++;
  if((ch5[x-2][y-2]==empty)&&(ch5[x+2][y+2]&ch5[x+1][y+1]&ch5[x-1][y-1]&color))
    ret++;
  //bbEeb
  if((ch5[x+1][y]==empty)&&(ch5[x-2][y]&ch5[x-1][y]&ch5[x+2][y]&color))
    ret++;
  if((ch5[x+1][y+1]==empty)&&(ch5[x-2][y-2]&ch5[x-1][y-1]&ch5[x+2][y+2]&color))
    ret++;
  if((ch5[x+1][y-1]==empty)&&(ch5[x-2][y+2]&ch5[x-1][y+1]&ch5[x+2][y-2]&color))
    ret++;
  if((ch5[x][y+1]==empty)&&(ch5[x][y-2]&ch5[x][y-1]&ch5[x][y+2]&color))
    ret++;
  if((ch5[x][y-1]==empty)&&(ch5[x][y+2]&ch5[x][y+1]&ch5[x][y-2]&color))
    ret++;
  if((ch5[x-1][y]==empty)&&(ch5[x+2][y]&ch5[x+1][y]&ch5[x-2][y]&color))
    ret++;
  if((ch5[x-1][y+1]==empty)&&(ch5[x+2][y-2]&ch5[x+1][y-1]&ch5[x-2][y+2]&color))
    ret++;
  if((ch5[x-1][y-1]==empty)&&(ch5[x+2][y+2]&ch5[x+1][y+1]&ch5[x-2][y-2]&color))
    ret++;
  //bbbEe
  if((ch5[x+1][y]==empty)&&(ch5[x-3][y]&ch5[x-2][y]&ch5[x-1][y]&color))
    ret++;
  if((ch5[x+1][y+1]==empty)&&(ch5[x-3][y-3]&ch5[x-2][y-2]&ch5[x-1][y-1]&color))
    ret++;
  if((ch5[x+1][y-1]==empty)&&(ch5[x-3][y+3]&ch5[x-2][y+2]&ch5[x-1][y+1]&color))
    ret++;
  if((ch5[x][y+1]==empty)&&(ch5[x][y-3]&ch5[x][y-2]&ch5[x][y-1]&color))
    ret++;
  if((ch5[x][y-1]==empty)&&(ch5[x][y+3]&ch5[x][y+2]&ch5[x][y+1]&color))
    ret++;
  if((ch5[x-1][y]==empty)&&(ch5[x+3][y]&ch5[x+2][y]&ch5[x+1][y]&color))
    ret++;
  if((ch5[x-1][y+1]==empty)&&(ch5[x+3][y-3]&ch5[x+2][y-2]&ch5[x+1][y-1]&color))
    ret++;
  if((ch5[x-1][y-1]==empty)&&(ch5[x+3][y+3]&ch5[x+2][y+2]&ch5[x+1][y+1]&color))
    ret++;
  }
return(ret);
}
//---------------------------------------------------------------------------
int __fastcall TMainForm::To3(int x,int y,int color)
{
int ret=0;
if(ch5[x][y]==empty)
  {
  //eEbbe
  if((ch5[x-1][y]==empty)&&(ch5[x+3][y]==empty)&&(ch5[x+1][y]&ch5[x+2][y]&color))
    ret++;

⌨️ 快捷键说明

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