技术交流,DH讲解. 今天来实现一个简单的五子棋,直接用GDI来画的一个游戏.
首先我们来想下怎么存放数据,哪些地方是白棋,哪些地方是黑棋,哪些地方没有下棋?
对,我们用一个二维数组,如果数组中某一个位置的值为0代表没有下棋,为1代表是白棋,为2代表是黑棋.
好就这么说定了,为了使用方便,我们打算做成一个控件,因为要画界面所以我们从TGraphicControl继承下来.
看一下类定义的代码: Type
TLastPlayer= (LpP1, LpP2);
TGameEvent= Procedure(S: TObject; P: TLastPlayer) Of Object;
TFiveGame= Class(TGraphicControl)
Private
// 如果是空,就是0,白棋就是1,黑棋就是2
FData: 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;
Protected
Public
Constructor Create(AOwner: TComponent); Override;
Destructor Destroy; Override;
//
Procedure ReStart;
Function AddP1Chessman(X, Y: Integer): Boolean;
Functi