codice:
unit trisU;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, StdCtrls, ExtCtrls, Menus, ExtDlgs;
type
posArray= array [1..3,1..3] of integer;
type
TfmMain = class(TForm)
StaticText1: TStaticText;
StaticText2: TStaticText;
MainMenu1: TMainMenu;
File1: TMenuItem;
New1: TMenuItem;
N1: TMenuItem;
Exit1: TMenuItem;
N3: TMenuItem;
Player1Avatar1: TMenuItem;
Player2Avatar1: TMenuItem;
Help1: TMenuItem;
About1: TMenuItem;
imAvatar1: TImage;
imAvatar2: TImage;
OPD: TOpenPictureDialog;
SPD: TSavePictureDialog;
etPlayer1: TEdit;
etPlayer2: TEdit;
tmTimer1: TTimer;
StaticText3: TStaticText;
lbTempo1: TLabel;
StaticText4: TStaticText;
lbTempo2: TLabel;
tmTimer2: TTimer;
btStart1: TButton;
btStart2: TButton;
lbCella1: TLabel;
lbCella5: TLabel;
lbCella6: TLabel;
lbCella9: TLabel;
lbCella8: TLabel;
lbCella7: TLabel;
lbCella4: TLabel;
lbCella2: TLabel;
lbCella3: TLabel;
btNewGame: TButton;
btResetScore: TButton;
rgPlayFirst: TRadioGroup;
gbPunteggio: TGroupBox;
lbX: TLabel;
lbMinus: TLabel;
lbO: TLabel;
lbXpun: TLabel;
lbOpun: TLabel;
lbColon: TLabel;
procedure Player1Avatar1Click(Sender: TObject);
procedure Player2Avatar1Click(Sender: TObject);
procedure tmTimer1Timer(Sender: TObject);
procedure tmTimer2Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure lbCella1Click(Sender: TObject);
procedure btNewGameClick(Sender: TObject);
procedure btResetScoreClick(Sender: TObject);
private
{ Private declarations }
Function RightStr(Stringa: string; Size: Word):string;
Function CheckWin(iPos: posArray): integer;
Procedure IniziaGioco;
Function GamePlay(xo_Move: integer): integer;
public
{ Public declarations }
end;
var
fmMain: TfmMain;
Xpos, Opos: posArray;
sPlaySign: String;
bGameOver: Boolean;
iMove: Integer;
iXScore: Integer;
iOScore: Integer;
implementation
{$R *.DFM}
function RightStr(Stringa: string; Size:word): string;
begin
if Size > Length(Stringa) then Size := Length(Stringa);
RightStr := Copy(Stringa, Length(Stringa)-Size+1, Size)
end;
procedure TfmMain.IniziaGioco;
var
i, j, k: integer;
begin
for i := 1 to 3 do
begin
for j := 1 To 3 do
begin
k:= (i - 1) * 3 + j - 1; // 0 .. 8
TLabel(FindComponent('lbCella' + IntToStr(k))).Caption := '';
XPos[i, j] := 0;
OPos[i][j] := 0;
end;
end;
if rgPlayFirst.ItemIndex= 0 then sPlaySign:='X';
if rgPlayFirst.ItemIndex= 1 then sPlaySign:='O';
bGameOver:= False;
iMove:=0;
end;
procedure TfmMain.FormCreate(Sender: TObject);
begin
iXScore:=0;
iOScore:=0;
IniziaGioco;
end;
function CheckWin(iPos: posArray): integer;
var
iScore: integer;
i,j: integer;
begin
Result:=-1;
//in rows?
iScore:=0;
for i:=1 to 3 do
begin
iScore:=0;
Inc(Result);
for j:=1 to 3 do Inc(iScore, iPos[i,j]);
if iScore =3 then Exit;
end;
//top-left bottom-right diagonal?
iScore := 0;
Inc(Result);
for i := 1 to 3 do Inc(iScore, iPos[i,i]);
if iScore = 3 then Exit;
//top-right bottom-left diagonal?
iScore := 0;
Inc(Result);
for i := 1 to 3 do Inc(iScore, iPos[i,4-i]);
if iScore = 3 then Exit;
//columns?
for i := 1 to 3 do
begin
iScore := 0;
Inc(Result);
for j := 1 to 3 do Inc(iScore, iPos[j,i]);
if iScore = 3 then Exit;
end;//for i
Result := -1;
end;
Function TfmMain.GamePlay(xo_Move: integer): integer;
var
x,y: 1..3;
iWin: integer;
begin
Result:=-1;
Inc(iMove);
x:= (xo_Move div 3) +1;
y:= (xo_Move mod 3) +1;
if sPlaySign ='0' then
begin
OPos[x,y]:=1;
iWin:= CheckWin(Opos);
end
else
begin
Xpos[x,y]:=1;
iWin:= CheckWin(Xpos);
end;
TLabel(FindComponent('lbCella' + IntToStr(xo_Move))).Caption:=sPlaySign;
Result:=iWin;
if iWin >=0 then
begin
bGameOver:= TRUE;
if sPlaySign ='X' then
begin
iXScore:=iXScore+1;
lbXpun.Caption:= IntToStr(iXScore);
end
else
begin
iOScore:=iOScore+1;
lbOpun.Caption:=IntToStr(iOScore);
end;
ShowMessage(sPlaySign +'-Wins!!');
end;
if (iMove=9) AND (bGameOver= FALSE) then
begin
ShowMessage('Pareggio!!');
bGameOver:= True;
end;
if sPlaySign = 'O' then sPlaySign:='X'
else sPlaySign := 'O';
end;
procedure TfmMain.lbCella1Click(Sender: TObject);
var
iWin:integer;
IndiceCella: 1..9;
begin
if bGameOver= TRUE then Exit;
if TLabel(Sender).Caption <>'' then
begin
ShowMessage('Cella Occupata!');
Exit;
end;
IndiceCella:= StrToInt(RightStr(TLabel(Sender).Name,1));
iWin:= GamePlay(IndiceCella);
end;
procedure TfmMain.btNewGameClick(Sender: TObject);
var
Res: integer;
begin
if bGameOver = False then
begin
Res:=MessageDlg('Finisci la partita corrente?',mtConfirmation,mbOkCancel, 0);
if Res= mrCancel then Exit;
end;
IniziaGioco;
end;
procedure TfmMain.btResetScoreClick(Sender: TObject);
begin
if MessageDlg('Azzero il punteggio?', mtConfirmation, mbOkCancel,0)= mrCancel then Exit;
iXScore:=0;
iOScore:=0;
lbXpun.Caption:= IntToStr(iXScore);
lbOpun.Caption:= IntToStr(iOScore);
end;
procedure TfmMain.Player1Avatar1Click(Sender: TObject);
begin
if OPD.Execute then //Si sceglie l'avatar del primo giocatore
imAvatar1.Picture.LoadFromFile(OPD.FileName); //carico l'avatar
if (imAvatar1.Picture.Width > 115) OR (imAvatar1.Picture.Height > 170) then
begin
if MessageDlg('Hai scelto un avatar troppo grande!,selezionalo di nuovo..',mtError,[mbRetry],0)= mrRetry then
Player1Avatar1Click(Sender);
end;
end;
procedure TfmMain.Player2Avatar1Click(Sender: TObject);
begin
if OPD.Execute then //Si sceglie l'avatar del primo giocatore
imAvatar2.Picture.LoadFromFile(OPD.FileName); //carico l'avatar
if (imAvatar1.Picture.Width > 115) OR (imAvatar1.Picture.Height > 170) then
begin
if MessageDlg('Hai scelto un avatar troppo grande!,selezionalo di nuovo..',mtError,[mbRetry],0)= mrRetry then
Player1Avatar1Click(Sender);
end;
end;
procedure TfmMain.tmTimer1Timer(Sender: TObject);
begin
//if il tempo del player1 è finito (=0) then
// begin
//Comincia a giocare il player2
//Si bloccano le mosse del Player1
//end
//else
// lbTempo1.Caption:=IntToStr(StrToInt(lbTempo1.Caption)-1);
end;
procedure TfmMain.tmTimer2Timer(Sender: TObject);
begin
//if il tempo del player2 è finito (=0) then
// begin
//Comincia a giocare il player1
//Si bloccano le mosse del Player2
//end
//else
// lbTempo2.Caption:=IntToStr(StrToInt(lbTempo2.Caption)-1);
end;
end.
Noti qualcosa che non va nella disposizione?