技术交流,DH讲解.

今天来实现一个简单的五子棋,直接用GDI来画的一个游戏.
首先我们来想下怎么存放数据,哪些地方是白棋,哪些地方是黑棋,哪些地方没有下棋?
对,我们用一个二维数组,如果数组中某一个位置的值为0代表没有下棋,为1代表是白棋,为2代表是黑棋.
好就这么说定了,为了使用方便,我们打算做成一个控件,因为要画界面所以我们从TGraphicControl继承下来.
看一下类定义的代码:

TypeTLastPlayer= (LpP1, LpP2);TGameEvent= Procedure(S: TObject; P: TLastPlayer) Of Object;TFiveGame= Class(TGraphicControl)Private// 如果是空,就是0,白棋就是1,黑棋就是2FData: Array Of Array Of Byte;// 棋盘的长和宽FRows, FColumns: Integer;// 格子的宽度/FCellWidth: Integer;// 是否自动大小FAutoSize: Boolean;// 棋盘线条的颜色.FLineColor: TColor;// 2个选手的颜色FPlayerOneColor: TColor;FPlayerTwoColor: TColor;// 边距FHDistance, FVDistance: Integer;// 上次操作的选手FLastPlayer: TLastPlayer;// 三个事件FWinEvent: TGameEvent;FErrorEvent: TGameEvent;FSuccessEvent: TGameEvent;Procedure SetRows(Const Value: Integer);Procedure SetColumns(Const Value: Integer);Procedure SetCellWidth(Const Value: Integer);Procedure SetAutoSize(Const Value: Boolean);Procedure SetLineColor(Const Value: TColor);Procedure SetPlayerOneColor(Const Value: TColor);Procedure SetPlayerTwoColor(Const Value: TColor);Procedure SetHDistance(Const Value: Integer);Procedure SetVDistance(Const Value: Integer);Procedure Paint; Override;Function AddChessMan(X, Y, V: Integer): Boolean;Procedure MyButtonDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);Function Judge(X, Y, V: Integer): Boolean;ProtectedPublicConstructor Create(AOwner: TComponent); Override;Destructor Destroy; Override;//Procedure ReStart;Function AddP1Chessman(X, Y: Integer): Boolean;Function AddP2Chessman(X, Y: Integer): Boolean;PublishedProperty Rows: Integer Read FRows Write SetRows;Property Columns: Integer Read FColumns Write SetColumns;Property CellWidth: Integer Read FCellWidth Write SetCellWidth;Property AutoSize: Boolean Read FAutoSize Write SetAutoSize;Property LineColor: TColor Read FLineColor Write SetLineColor;Property PlayerOneColor: TColor Read FPlayerOneColor Write SetPlayerOneColor;Property PlayerTwoColor: TColor Read FPlayerTwoColor Write SetPlayerTwoColor;Property HDistance: Integer Read FHDistance Write SetHDistance;Property VDistance: Integer Read FVDistance Write SetVDistance;Property WinEvent: TGameEvent Read FWinEvent Write FWinEvent;Property LastPlayer: TLastPlayer Read FLastPlayer Write FLastPlayer;Property ErrorEvent: TGameEvent Read FErrorEvent Write FErrorEvent;Property SuccessEvent: TGameEvent Read FSuccessEvent Write FSuccessEvent;End;

我们这样定义了一个类,接下来看看怎么实现它的:

{ TFiveGame }
// 增加一颗棋子
Function TFiveGame.AddChessMan(X, Y, V: Integer): Boolean;
BeginResult:= False;If (X< 0)Or (Y< 0)Or (X> Columns- 1)Or (Y> Rows- 1) ThenExit;If FData[X, Y]<> 0 ThenExit;FData[X, Y]:= Byte(V);Result:= True;
End;// 一号选手增加棋子
Function TFiveGame.AddP1Chessman(X, Y: Integer): Boolean;
BeginResult:= AddChessMan(X, Y, 1);
End;// 二号选手增加棋子
Function TFiveGame.AddP2Chessman(X, Y: Integer): Boolean;
BeginResult:= AddChessMan(X, Y, 2)
End;Constructor TFiveGame.Create(AOwner: TComponent);
BeginInherited;// 初始化FAutoSize:= False;FRows:= 5;FColumns:= 5;FCellWidth:= 30;FLineColor:= ClRed;FPlayerOneColor:= ClBlack;FPlayerTwoColor:= ClYellow;FHDistance:= 20;FVDistance:= 20;FLastPlayer:= LpP2;SetLength(FData, FRows, FColumns);// 要响应鼠标,必须加上这个ControlStyle:= ControlStyle+ [CsReplicatable, CsCaptureMouse, CsClickEvents];Self.OnMouseDown:= MyButtonDown;
End;Destructor TFiveGame.Destroy;
BeginInherited;
End;// 判断是否胜利
Function TFiveGame.Judge(X, Y, V: Integer): Boolean;
VarC1, C2: Integer;X1, Y1: Integer;Procedure Reset;BeginC1:= 0;C2:= 0;X1:= X;Y1:= Y;End;BeginResult:= True;// 这个编译指令是代码折叠,但是D7以及之前版本没有这个编译指令.不要惊恐// 横向{$IFDEF D7UP}{$REGION '横向'}{$ENDIF}BeginReset;// 向左Dec(X1);While X1>= 0 DoBeginIf FData[X1, Y]<> V ThenBreak;Inc(C1);If C1= 4 ThenExit;Dec(X1);End;// 向右X1:= X+ 1;While X1< Columns DoBeginIf FData[X1, Y]<> V ThenBreak;Inc(C2);If C1+ C2= 4 ThenExit;Inc(X1);End;End;{$IFDEF D7UP}{$ENDREGION}// 竖向{$REGION '竖向'}{$ENDIF}BeginReset;// 向上Dec(Y1);While Y1>= 0 DoBeginIf FData[X, Y1]<> V ThenBreak;Inc(C1);If C1= 4 ThenExit;Dec(Y1);End;// 向下Y1:= Y+ 1;While Y1< Rows DoBeginIf FData[X, Y1]<> V ThenBreak;Inc(C2);If C1+ C2= 4 ThenExit;Inc(Y1);End;End;{$IFDEF D7UP}{$ENDREGION}// 斜向1{$REGION '斜向1'}{$ENDIF}BeginReset;Dec(X1);Dec(Y1);While (X1>= 0)And (Y1>= 0) DoBeginIf FData[X1, Y1]<> V ThenBreak;Inc(C1);If C1= 4 ThenExit;Dec(X1);Dec(Y1);End;X1:= X+ 1;Y1:= Y+ 1;While (X1< Columns)And (Y1< Rows) DoBeginIf FData[X1, Y1]<> V ThenBreak;Inc(C2);If C1+ C2= 4 ThenExit;Inc(X1);Inc(Y1)End;End;{$IFDEF D7UP}{$ENDREGION}// 斜向2{$REGION '斜向2'}{$ENDIF}BeginReset;Dec(X1);Inc(Y1);While (X1>= 0)And (Y1< Rows) DoBeginIf FData[X1, Y1]<> V ThenBreak;Inc(C1);If C1= 4 ThenExit;Dec(X1);Inc(Y1);End;X1:= X+ 1;Y1:= Y- 1;While (X1< Columns)And (Y1>= 0) DoBeginIf FData[X1, Y1]<> V ThenBreak;Inc(C2);If C1+ C2= 4 ThenExit;Inc(X1);Dec(Y1);End;End;{$IFDEF D7UP}{$ENDREGION}{$ENDIF}Result:= False;
End;Procedure TFiveGame.MyButtonDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
VarI, J, K: Integer;B: Boolean;
Begin// 不是左键If Button<> MbLeft ThenExit;// 计算x,y的位置If (X< HDistance)Or (X> HDistance+ Columns* CellWidth) ThenExit;If (Y< VDistance)Or (Y> VDistance+ Rows* CellWidth) ThenExit;I:= (X- HDistance)Div CellWidth;J:= (Y- VDistance)Div CellWidth;// 加棋子Case FLastPlayer OfLpP1:BeginB:= AddP2Chessman(I, J);If B ThenFLastPlayer:= LpP2End;LpP2:BeginB:= AddP1Chessman(I, J);If B ThenFLastPlayer:= LpP1End;End;// 结果处理If B ThenBeginInvalidate;If Assigned(FSuccessEvent) ThenFSuccessEvent(Self, FLastPlayer);If Judge(I, J, Ord(FLastPlayer)+ 1) ThenIf Assigned(FWinEvent) ThenFWinEvent(Self, FLastPlayer);EndElseIf Assigned(FErrorEvent) ThenFErrorEvent(Self, FLastPlayer);
End;Procedure TFiveGame.Paint;Procedure DrawBkg;VarI: Integer;OldColor: TColor;BeginWith Canvas DoBeginOldColor:= Pen.Color;Pen.Color:= LineColor;Rectangle(HDistance, VDistance, HDistance+ Columns* CellWidth,VDistance+ Rows* CellWidth);For I:= 1 To Columns- 1 DoBeginMoveTo(HDistance+ I* CellWidth, VDistance);LineTo(HDistance+ I* CellWidth, VDistance+ Rows* CellWidth);End;For I:= 1 To Rows- 1 DoBeginMoveTo(HDistance, VDistance+ I* CellWidth);LineTo(HDistance+ Columns* CellWidth, VDistance+ I* CellWidth);End;Pen.Color:= OldColor;End;End;Procedure DrawChessMan(X, Y, V: Integer);VarC1, C2: TColor;BeginWith Canvas DoBeginC1:= Brush.Color;C2:= Pen.Color;Case V Of1:Brush.Color:= PlayerOneColor;2:Brush.Color:= PlayerTwoColor;End;Pen.Color:= Brush.Color;Ellipse(HDistance+ X* CellWidth+ 1, VDistance+ Y* CellWidth+ 1,HDistance+ (X+ 1)* CellWidth- 1, VDistance+ (Y+ 1)* CellWidth- 1);Pen.Color:= C2;Brush.Color:= C1;End;End;VarJ: Integer;K: Integer;
Begin//如果自动大小 就调整大小If AutoSize ThenBeginWidth:= HDistance* 2+ Columns* CellWidth;Height:= VDistance* 2+ Rows* CellWidth;End;// 画棋盘DrawBkg;// 画棋子For J:= 0 To Rows- 1 DoFor K:= 0 To Columns- 1 DoIf FData[J, K]<> 0 ThenDrawChessMan(J, K, FData[J, K]);End;Procedure TFiveGame.ReStart;
VarI: Integer;J: Integer;
BeginFor I:= 0 To Rows- 1 DoFor J:= 0 To Columns- 1 DoFData[I, J]:= 0;Invalidate;
End;Procedure TFiveGame.SetAutoSize(Const Value: Boolean);
BeginIf FAutoSize= Value ThenExit;FAutoSize:= Value;Invalidate;
End;Procedure TFiveGame.SetCellWidth(Const Value: Integer);
BeginIf FCellWidth= Value ThenExit;FCellWidth:= Value;Invalidate;
End;Procedure TFiveGame.SetColumns(Const Value: Integer);
BeginIf FColumns= Value ThenExit;FColumns:= Value;SetLength(FData, FRows, FColumns);Invalidate;
End;Procedure TFiveGame.SetHDistance(Const Value: Integer);
BeginIf FHDistance= Value ThenExit;FHDistance:= Value;Invalidate;
End;Procedure TFiveGame.SetPlayerOneColor(Const Value: TColor);
BeginIf FPlayerOneColor= Value ThenExit;FPlayerOneColor:= Value;Invalidate;
End;Procedure TFiveGame.SetLineColor(Const Value: TColor);
BeginIf FLineColor= Value ThenExit;FLineColor:= Value;Invalidate;
End;Procedure TFiveGame.SetPlayerTwoColor(Const Value: TColor);
BeginIf FPlayerTwoColor= Value ThenExit;FPlayerTwoColor:= Value;Invalidate;
End;Procedure TFiveGame.SetRows(Const Value: Integer);
BeginIf FRows= Value ThenExit;FRows:= Value;SetLength(FData, FRows, FColumns);Invalidate;
End;Procedure TFiveGame.SetVDistance(Const Value: Integer);
BeginIf FVDistance= Value ThenExit;FVDistance:= Value;Invalidate;
End;

要想把这个类加到控件面板上面去就需要再添加一个Register全局函数,我这里没有添加,所以也不演示那句话了.

类给出来了,我们看看怎么使用这个类.
定义一个该类的对象:

FiveGame:TFiveGame;

窗体创建的时候的时候,实例化这个对象:

procedure TForm1.FormCreate(Sender: TObject);
beginFiveGame:=TFiveGame.Create(Self);FiveGame.Parent:=Self;FiveGame.Align:=alClient;FiveGame.WinEvent:=OnWin;FiveGame.ErrorEvent:=Onerror;FiveGame.SuccessEvent:=OnSuccess;
end;

窗体结束的时候不要忘记释放这个对象哟.代码我就不贴了,给出一个完整代码下载的链接吧.

下载地址:点这里

代码很简单,也很不完善,打算有空加入人机对战和联网对战,然后界面再改漂亮一些吧,比如用贴图或者用DX来弄.
这里只是一个抛砖引玉吧.

我是DH,今天到这里.

转载于:https://www.cnblogs.com/huangjacky/archive/2009/12/22/1629768.html

Delphi – 我的代码之简单五子棋相关推荐

  1. 编写五子棋的完整python代码_python制作简单五子棋游戏

    本文实例为大家分享了python五子棋游戏的具体代码,供大家参考,具体内容如下 #五子棋 '" 矩阵做棋盘 16*16 "+" 打印棋盘 for for 游戏是否结束 开 ...

  2. C# winform 简单五子棋 200行代码实现双人对战

    1.需求 基于C# winform用200行代码实现简易五子棋双人对战,支持悔棋,需要的知识有C# winform界面,C#,以及几张素材图片. 2.界面 界面设计如图1所示,背影图是用Graphic ...

  3. 简单五子棋问题,java实现

    **题目要求:**实现一个控制台下五子棋的程序.用一个二维数组模拟一个15*15路的五子棋棋盘,把每个元素赋值位"┼"可以画出棋盘, "○"代表该交叉点下了一颗 ...

  4. python写游戏棋牌游戏_使用python实现简单五子棋游戏

    使用python实现简单五子棋游戏 发布时间:2020-08-29 06:12:30 来源:脚本之家 阅读:73 作者:weixin_42874933 用python实现五子棋简单人机模式的练习过程, ...

  5. canvas简单五子棋

    canvas简单五子棋 效果 思路 代码 效果 思路 canvans 绘制棋盘,绘制时候边缘预留棋子位置 监听点击事件绘制落子并记录到字典中 获胜判定,在四个方向上检测是否有足够数量的连贯棋子 代码 ...

  6. python简单网格五子棋_python实现简单五子棋游戏

    本文实例为大家分享了python实现简单五子棋游戏的具体代码,供大家参考,具体内容如下 from graphics import * from math import * import numpy a ...

  7. 用c语言做一个五子棋程序,C语言制作简单五子棋游戏

    原标题:C语言制作简单五子棋游戏 C语言制作简单的五子棋游戏 学习C语言的人很多,但是用C语言很少,而用来为自己所用,来做游戏的人就更少了,很多人都是跟着学校学习,学校讲到哪就坐到哪,但是以后却还是不 ...

  8. 第18篇 Qt实现简单五子棋游戏(二)算法说明

    第18篇 Qt实现简单五子棋游戏(二)算法说明 5.算法说明 5.1.画棋盘: void drawChessboard(); 5.2.画棋子:void drawChess(); 5.3.鼠标点击响应: ...

  9. 简单五子棋算法——初级篇

    简单五子棋算法--初级篇 前言 设计思路 算法实现 后言 进阶设计 前言 五子是中国古老的棋类之一,是老少咸宜的娱乐项目.也是人机博弈中最简单的一类,相较于围棋.象棋变化更少,算法实现起来就相对比较简 ...

最新文章

  1. 手写一个简单的HashMap,搞定挑剔面试官
  2. 这是一篇“团队”博客
  3. C++中数字和字符串类型的转换
  4. 取得成本中心组、成本要素组层级的几个BAPI
  5. Hadoop vs Spark
  6. 操作系统 哈工大 李治军
  7. golang实现聊天室(四)
  8. 2-5:套接字(Socket)编程之从内核角度深入理解套接字
  9. 转 mysql处理高并发,防止库存超卖
  10. 计算机操作系统在线阅读,计算机操作系统3-1.ppt
  11. href 和 src 区别
  12. Core Animation基础 1
  13. php mysql登陆页面完整代码_PHP实现用户登录的案例代码
  14. python pyhook_pyhook的简单使用
  15. python数据分析师面试题选
  16. 快速排序与冒泡排序的效率对比
  17. Linux错误:warning: here-document at line 5 delimited by end-of-file (wanted `EOF`
  18. 阿里云服务器共享型、计算型和通用型有什么区别?
  19. 创建基于vue的H5
  20. Centos7中Docker安装Redis

热门文章

  1. php7扩展开发教程,Laravel 7 扩展开发教程
  2. Maven:IDEA 使用maven 下载源码包
  3. spark学习-58-Spark的EventLoggingListener
  4. 95-138-010-源码-Function-KeyedProcessFunction
  5. 为什么redis可以做分布式锁
  6. php 内置mail 包,PHP使用pear自带的mail类库发邮件的方法
  7. 记一次项目代码重构:使用Spring容器干掉条件判断
  8. SQLServer之深度分析Select
  9. java 中的锁 aqs_Java并发编程系列-(4) 显式锁与AQS
  10. DataGrip使用入门