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

📄 新建 文本文档.txt

📁 这个代码花了我不少时间的
💻 TXT
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, ComCtrls,Unit2;

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    N1: TMenuItem;
    Menustart: TMenuItem;
    N3: TMenuItem;
    ListView1: TListView;
    MenuUndo: TMenuItem;
    N5: TMenuItem;
    procedure MenustartClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
      function isWin(WhoPlay:string): boolean;
    procedure MenuUndoClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  // '无子'代表没有棋子,'黑子','白子'
   mChess:array[1..15,1..15] of string;
     //走棋方(黑、白、无)
  mWhoPlay  : string;
    //棋盘背景位图对象
  BitmapBG   : TBitmap;
    //人脸位图对象
  BitmapFace : TBitmap;
  //棋子位图对象
  BitmapChess: TBitmap;
   //开始游戏标志
  mStartFlag:boolean;

implementation

{$R *.dfm}
//isWin是显示谁胜利的程序
function TForm1.isWin(WhoPlay:string): boolean;
var
  i,j:integer;
begin
  for i:=1 to 15 do
    for j:=1 to 15 do
      begin
        //水平方向是否有5个连成5个
        if(i<=11)
        and (mChess[i,j]=WhoPlay)
        and (mChess[i+1,j]=WhoPlay)
        and (mChess[i+2,j]=WhoPlay)
        and (mChess[i+3,j]=WhoPlay)
        and (mChess[i+4,j]=WhoPlay)
        then
        begin
          isWin:=true;
          exit;
        end;

        //竖直方向是否有5个连成5个
        if(j<=11)
        and (mChess[i,j]=WhoPlay)
        and (mChess[i,j+1]=WhoPlay)
        and (mChess[i,j+2]=WhoPlay)
        and (mChess[i,j+3]=WhoPlay)
        and (mChess[i,j+4]=WhoPlay)
        then
        begin
          isWin:=true;
          exit;
        end;

        //主对角线方向是否有5个连成5个
        if(i<=11) and (j<=11)
        and (mChess[i,j]=WhoPlay)
        and (mChess[i+1,j+1]=WhoPlay)
        and (mChess[i+2,j+2]=WhoPlay)
        and (mChess[i+3,j+3]=WhoPlay)
        and (mChess[i+4,j+4]=WhoPlay)
        then
        begin
          isWin:=true;
          exit;
        end;

        //副对角线方向是否有5个连成5个
        if(j<=11)and (i>=5)
        and (mChess[i,j]=WhoPlay)
        and (mChess[i-1,j+1]=WhoPlay)
        and (mChess[i-2,j+2]=WhoPlay)
        and (mChess[i-3,j+3]=WhoPlay)
        and (mChess[i-4,j+4]=WhoPlay)
        then
        begin
          isWin:=true;
          exit;
        end;
      end;
      isWin:=false;
end;

//响应开局菜单
procedure TForm1.MenustartClick(Sender: TObject);
var
  row,column:Integer;
begin
  for row:=1 to 15 do
    for column:=1 to 15 do

      mChess[row,column]:='无子';
  //黑棋先下
  mWhoPlay:='黑';
  mStartFlag:=true;
  //清空列表视图
  ListView1.Items.Clear();
  //强制刷新屏幕
  Invalidate();
end;
//初始化
procedure TForm1.FormCreate(Sender: TObject);
var
row,column:Integer;
begin
  //初始化所有棋子值为'无子'
  for row:=1 to 15  do
    for column:=1 to 15  do
      mChess[row,column]:='无子';

  //初始化走棋方为无
  mWhoPlay:='无';
  //设置游戏开始标志为FALSE
  mStartFlag:=false;
  
  //创建位图背景对象
  BitmapBG:=TBitmap.Create();
  //装载位图文件
  BitmapBG.LoadFromFile('棋盘.bmp');

  //创建棋子背景对象
  BitmapChess:=TBitmap.Create();
  //设置棋子位图的透明属性为True
  BitmapChess.Transparent:=true;
  //设置棋子位图的透明颜色为白色($FFFFFF)
  BitmapChess.TransparentColor:=clWhite;

  //创建人脸位图
  BitmapFace:=TBitmap.Create();
  //设置人脸位图的透明属性为True
  BitmapFace.Transparent:=true;
  //设置人脸位图的透明颜色为白色
  BitmapFace.TransparentColor:=clWhite;

  //设置列表视图背景色
  //ListView1.Color:=$4499DD;

end;
//冲会
procedure TForm1.FormPaint(Sender: TObject);
var
  row,column:Integer;
begin
  //绘制棋盘背景图
  Canvas.Draw(0,0,BitmapBG);


  //逐一绘制棋子
  for row:=1 to 15 do
    for column:=1 to 15 do
      if(mChess[row,column]<>'无子')then
      begin
        //装载相应的棋子位图
        BitmapChess.LoadFromFile(mChess[row,column]+'.bmp');
        //在相应的位置绘制棋子
        Canvas.Draw(column*25-25,row*25-25,BitmapChess);
        //把棋子保存到重绘位图对象
        //BitmapRepaint.Canvas.Draw(column*60-29,row*60-29,BitmapChess);
      end;

  //绘制人脸
  if(mWhoPlay='黑')then
  begin
    //装载对应的棋子位图文件
    BitmapFace.LoadFromFile('黑.bmp');
    //绘制人脸
    Canvas.Draw(580,327,BitmapFace);

  end;
  if(mWhoPlay='白')then
  begin
    //装载对应的棋子位图文件
    BitmapFace.LoadFromFile('白.bmp');
    //绘制人脸
    Canvas.Draw(580,150,BitmapFace);

  end;

end;
 //响应鼠标下压事件
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  //鼠标落点所在的行号和列号
  row,column:integer;
  //鼠标落点距离棋盘交叉点的距离
  Radius:real;
  //允许落子标志
  CanDropFlag:boolean;
  item: TListItem;
  temp:string;
begin
 //如果mStartFlag为TRUE的时候就可以开始
 if(mStartFlag=true)then
  begin
   //如果单击鼠标左键(处理拾子和落子动作)
  if(Button=mbLeft)then
 begin
  //求鼠标下压点的行号和列号
  row:=(y-12) div 34+1;
  if((y-12) mod 34>=19)then
    row:=row+1;
  column:=(x-12) div 34+1;
  if((x-12) mod 34>=19)then
    column:=column+1;
// 如果行号在1-10之间,列号在1-9之间
  if(row<=15)and(row>=1)and(column>=1)and(column<=15)then
  begin
   //求鼠标落点与对应的棋盘交叉点距离
   Radius:=sqrt(sqr((x-12)-(column-1)*34)+sqr((y-12)-(row-1)*34));
   if(Radius<=19)then
          //设CanDropFlag为 false(不允许落子)
        CanDropFlag:=false;
       begin
          //只有当落点无棋子或存在对方棋子时,才能落子
          if (mChess[Row,Column]='无子') then
          begin
            mChess[Row,Column]:=mWhoPlay;
            CanDropFlag:=true;
            //在listview添加一行
        item:=ListView1.Items.Add;
        item.Caption:=mWhoPlay;
        //显示落子位置
        if(row<10)then
          temp:='0'+inttostr(row)+'行'
        else
          temp:=inttostr(row)+'行';
        if(column<10)then
          temp:=temp+'0'+IntToStr(column)+'列'
        else
          temp:=temp+IntToStr(column)+'列';
        item.SubItems.Add(temp);
           end;

              //强制刷新窗口
             Invalidate();
             //如果ISWIN是TRUE的时候,就出来胜利
            if (isWin(mWhoPlay)=true) then
       begin

          if(mWhoPlay='黑')then
            application.MessageBox('恭喜黑方得胜','HOHO~',MB_OK OR MB_ICONINFORMATION)
          else if(mWhoPlay='白')then
            application.MessageBox('恭喜白方得胜','HOHO~',MB_OK OR MB_ICONINFORMATION);
          //当落子成功的时候,改变敌我双方的下棋的顺序
          if(mWhoPlay='黑')then
            mWhoPlay:='白'
          else if (mWhoPlay='白')then
            mWhoPlay:='黑';
          MenustartClick(Menustart);//重新开始游戏
        end;
        if(CanDropFlag=true) then
            begin
            //交换走棋方
              if(mWhoPlay='黑')then
                mWhoPlay:='白'
              else
                mWhoPlay:='黑';
                end;
           end ;

          end;
    end;
   end;
  end;


//响应悔棋按钮
procedure TForm1.MenuUndoClick(Sender: TObject);
var
  count:integer;
  item :TListItem;
  temp : string;
  //走棋方
  WhoPlay:string;
  //落子的行号和列号
  DropRow,DropColumn:integer;
begin
  //获取列表视图控件中的行数
  count:=ListView1.Items.Count;
  //如果存在列表行,则执行悔棋动作
  if(count>0)then
  begin
    //获取列表视图控件中的最后一行
    item:=ListView1.Items.Item[count-1];
    //获取走棋方
    WhoPlay:=item.Caption;
    //获取落子的行号和列号
    temp:=Item.SubItems.Strings[0];
    DropRow:=StrToInt(Copy(temp,0,2));
    DropColumn:=StrToInt(Copy(temp,5,2));
    //执行悔棋动作
    mChess[DropRow,DropColumn]:='无子';
    //删除列表视图控件中的最后一行
    ListView1.Items.Delete(count-1);
    //交换走棋方
    if(mWhoPlay='黑')then
       mWhoPlay:='白'
    else if(mWhoPlay='白')then
       mWhoPlay:='黑';
    //强制刷新窗口
    Invalidate();
  end;
end;



procedure TForm1.N3Click(Sender: TObject);
begin
  Form1.Close();
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Form2.ShowModal();
end;

end.

⌨️ 快捷键说明

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