Accueil > > > Othello/Othello Test/Unit1.pas
JEUX D' OTHELLO
Othello/Othello Test/Unit1.pas
Informations sur ce code source
Voici mon premier code source !
C' est un jeux d' othello (reversi) écrit en Delphi.
Ce n' est pas un composant graphique mais un composant qui joue via des événements Perso facilement configurable depuis l' IDE
Les événements sont déclenchés pour
Fichier : Othello/Othello Test/Unit1.pas
Nombre de lignes : 280 lignes
Afficher ce fichier en plein écran
- unit Unit1;
-
- //////////////////////////////////////////////////////////////
- /// ///
- /// Content : Example Programm for the TOthello Unit ///
- /// Date : 25/09/2012 ///
- /// Author : Klieber Frederic ///
- /// See corresponding Unit for the Help of the setup !! ///
- /// ///
- //////////////////////////////////////////////////////////////
-
- interface
-
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, StrUtils,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Othello_Unit, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.ImgList;
-
- type
- ImgArr = array[0..63] of TImage ;
-
- TForm1 = class(TForm)
- NewGame: TButton;
- Labelnewgame: TLabel;
- GroupBox1: TGroupBox;
- bl: TLabel;
- GroupBox2: TGroupBox;
- wl: TLabel;
- LabelPlayer: TLabel;
- Othello: TOthello;
- GroupBox4: TGroupBox;
- CHeckHint: TCheckBox;
- CheckPlayable: TCheckBox;
-
- procedure ImageSquareClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure NewGameClick(Sender: TObject);
- procedure OthelloFlip(pt: TPoint; color: TDiscColor);
- procedure ImageSquareMouseLeave(Sender: TObject);
- procedure ImageSquareMouseEnter(Sender: TObject);
- procedure CHeckHintClick(Sender: TObject);
- procedure CheckPlayableClick(Sender: TObject);
- procedure OthelloGameNew(Sender: TObject);
- procedure OthelloGameEnd(Sender: TObject);
- procedure OthelloPlaceBegin(pt: TPoint; color: TDiscColor);
- procedure OthelloPlaceError(pt: TPoint; color: TDiscColor);
- procedure OthelloShowPlayableBegin(pt: TPoint; color: TDiscColor);
- procedure OthelloShowPlayableEnd(pt: TPoint; color: TDiscColor);
- procedure OthelloShowHintBegin(pt: TPoint; color: TDiscColor);
- procedure OthelloShowHintEnd(pt: TPoint; color: TDiscColor);
- procedure OthelloPlaceEnd(pt: TPoint; color: TDiscColor);
-
- private
- Bitmap0,Bitmap1,Bitmap2,Bitmap3 : TBitmap;
- FImgArr :ImgArr ;
- public
- { Déclarations publiques }
- end;
- var
- Form1: TForm1;
-
- implementation
- {$R *.dfm}
-
- // Draw the Board and setup the events handler too th Square
- procedure TForm1.FormCreate(Sender: TObject);
- var
- i : integer ;
- img1 :TImage ;
- begin
- Bitmap0 := TBitmap.Create;
- Bitmap1 := TBitmap.Create;
- Bitmap2 := TBitmap.Create;
- Bitmap3 := TBitmap.Create;
- Bitmap0.Handle := LoadBitmap(hInstance, 'Bitmap_0');
- Bitmap1.Handle := LoadBitmap(hInstance, 'Bitmap_1');
- Bitmap2.Handle := LoadBitmap(hInstance, 'Bitmap_2');
- Bitmap3.Handle := LoadBitmap(hInstance, 'Bitmap_3');
-
- for i := 0 to 63 do
- begin
- img1 := Timage.Create(Form1);
- img1.Width := 45 ;
- img1.Height := 45 ;
- img1.Top := 15 + self.Othello.SqareNumberToPoint(i).X * 45 ;
- img1.Left := 15 + self.Othello.SqareNumberToPoint(i).Y * 45 ;
- img1.Stretch := true;
- img1.Parent := Form1;
- img1.Name := 'Img_'+IntToStr(i);
- Img1.Canvas.Draw(0,0,Bitmap0);
- FImgArr[i]:= img1;
- // Will launch the Play of a disc
- img1.OnClick := ImageSquareClick ;
- // Will Launch the ShowPlayable proc. and the events
- img1.OnMouseEnter := ImageSquareMouseEnter;
- // Will launch the Realease proc.and the Events
- img1.OnMouseLeave := ImageSquareMouseLeave;
- end;
- end;
-
- // Make a New Game
- procedure TForm1.NewGameClick(Sender: TObject);
- var
- i : integer ;
- begin
- // Setting the Image array images to the empty square picture
- for i := 0 to 63 do
- begin
- FImgArr[i].Canvas.Draw(0,0,Bitmap0);
- end;
- // MUST BE CALLED BEFORE EVERYTHING ELSE
- self.Othello.NewGame;
- self.LabelPlayer.Caption := 'Black to play' ;
- self.wl.Caption := '2';
- self.bl.Caption := '2';
- // Show Hints creates Messages OnShowHint as many as required
- self.Othello.Show_Hint := self.CheckHint.Checked ;
- // Show Playable creates Messages OnShowHint as many as required
- self.Othello.Show_Playable := self.CheckPlayable.Checked ;
- end;
-
- // Set the ShowHint property
- procedure TForm1.CHeckHintClick(Sender: TObject);
- begin
- self.Othello.Show_Hint := self.CheckHint.Checked ;
- end;
-
- // Set the ShowPlayable property
- procedure TForm1.CheckPlayableClick(Sender: TObject);
- begin
- self.Othello.Show_Playable := self.CheckPlayable.Checked ;
- end;
-
- // Click to play !!
- procedure TForm1.ImageSquareClick(Sender: TObject);
- var
- name : string ;
- number: integer ;
- begin
- //Get the Number of the Square
- name := (Sender as Timage).Name ;
- number:= StrToInt(StrUtils.SplitString(name,'_')[1]) ;
- self.Othello.PlaceDiscsProc(number,self.Othello.TurnToPlay);
- end;
-
- // Process ShowHint if the corresponding property is TRUE , message will be sent to ShowHintBegin
- procedure TForm1.ImageSquareMouseEnter(Sender: TObject);
- var
- name : string ;
- number: integer ;
- begin
- name := (Sender as Timage).Name ;
- number:= StrToInt(StrUtils.SplitString(name,'_')[1]) ;
-
- // This Must be called, will be executed ONLY if
- // self.Othello.ShowHint is TRUE
- // will send some events for all the squares
- //who *could* be turned
-
- self.Othello.ShowHintProc(number,self.Othello.TurnToPlay);
- end;
-
- // End Process ShowHint if the corresponding property is TRUE , message will be sent to ShowHintEnd
- procedure TForm1.ImageSquareMouseLeave(Sender: TObject);
- var
- name : string ;
- number: integer ;
- begin
- // Will send messages for all the squares who could be turned
- // Back
- self.Othello.ReleaseHintProc(self.Othello.TurnToPlay);
- end;
-
- // Occurs for every Disc who will be turned while playing, message will be sent to Flip
- procedure TForm1.OthelloFlip(pt: TPoint; color: TDiscColor);
- var
- i : integer;
- begin
- // Change the imafe of the flipped Square
- i := self.Othello.PointToSqareNumber(pt) ;
- if color = White then
- FImgArr[i].Canvas.Draw(0,0,Bitmap2);
- if color = Black then
- FImgArr[i].Canvas.Draw(0,0,Bitmap1);
- FImgArr[i].Refresh;
- end;
-
- // Must be called BEFORE everything else ...
- procedure TForm1.OthelloGameNew(Sender: TObject);
- begin
- self.Labelnewgame.caption := 'New Game Started';
- end;
-
- // Called at the end of the game
- procedure TForm1.OthelloGameEnd(Sender: TObject);
- begin
- self.wl.Caption := IntToStr(self.Othello.WhiteCount) ;
- self.bl.Caption := IntToStr(self.Othello.BlackCount) ;
- self.LabelPlayer.Caption := 'No move possible';
- self.Labelnewgame.Caption :='End of the Game';
- ShowMessage('End of Game ..');
- end;
-
- // Occurs when the users adds a disc while playing
- procedure TForm1.OthelloPlaceBegin(pt: TPoint; color: TDiscColor);
- var
- i : integer ;
- begin
- i := self.Othello.PointToSqareNumber(pt) ;
- if color = Black then
- FImgArr[i].Canvas.Draw(0,0,Bitmap1);
- if color = White then
- FImgArr[i].Canvas.Draw(0,0,Bitmap2);
- FImgArr[i].Refresh;
- end;
-
- // Occurs et the end of the place of a disc
- procedure TForm1.OthelloPlaceEnd(pt: TPoint; color: TDiscColor);
- begin
- if self.Othello.TurnToPlay = Black then self.LabelPlayer.Caption := 'Black to play' ;
- if self.Othello.TurnToPlay = White then self.LabelPlayer.Caption := 'White to play' ;
- self.wl.Caption := IntToStr(self.Othello.WhiteCount) ;
- self.bl.Caption := IntToStr(self.Othello.BlackCount) ;
- end;
-
- // Occurs if the disc placement is NOT correct
- procedure TForm1.OthelloPlaceError(pt: TPoint; color: TDiscColor);
- begin
- // Fired when the Square is not valid when played ..thru "PlaceDiscsInGame" proc.
- ShowMessage('Can not Play his !');
- end;
-
- // Draws the highlight square image for every possible move
- procedure TForm1.OthelloShowHintBegin(pt: TPoint; color: TDiscColor);
- var
- i : integer ;
- begin
- i := self.Othello.PointToSqareNumber(pt) ;
-
- if color = Black then
- FImgArr[i].Canvas.Draw(0,0,Bitmap1);
- if color = White then
- FImgArr[i].Canvas.Draw(0,0,Bitmap2);
- FImgArr[i].Refresh;
- end;
-
- // Changes back the Bitmap for every possible square
- procedure TForm1.OthelloShowHintEnd(pt: TPoint; color: TDiscColor);
- var
- i : integer ;
- begin
- i := self.Othello.PointToSqareNumber(pt) ;
- if color = Black then
- FImgArr[i].Canvas.Draw(0,0,Bitmap2);
- if color = White then
- FImgArr[i].Canvas.Draw(0,0,Bitmap1);
- FImgArr[i].Refresh;
- end;
-
- // Draws the Highlight bitmap for the possible squares
- procedure TForm1.OthelloShowPlayableBegin(pt: TPoint; color: TDiscColor);
- var
- i : integer ;
- begin
- i := self.Othello.PointToSqareNumber(pt) ;
- FImgArr[i].Canvas.Draw(0,0,Bitmap3);
- FImgArr[i].Refresh;
- end;
-
- // Draws back the normal empty image
- procedure TForm1.OthelloShowPlayableEnd(pt: TPoint; color: TDiscColor);
- var
- i : integer ;
- begin
- i := self.Othello.PointToSqareNumber(pt) ;
- FImgArr[i].Canvas.Draw(0,0,Bitmap0);
- FImgArr[i].Refresh;
- end;
-
- end.
|