If BlocOuCoup = 1 Then Begin Repeat HasardCriss := Random(9) + 1 ; If TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(HasardCriss))).Caption = '' then TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(HasardCriss))).Caption := 'O'; Until TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(HasardCriss))).Caption = 'O'; End;
unit UPrincipale; { Auteur : Jean-Frédéric Faust 24 Septembre 2009} interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TFormPrinc = class(TForm) btnNewGame: TButton; Lbl1: TLabel; Lbl2: TLabel; Lbl3: TLabel; Lbl4: TLabel; Lbl5: TLabel; Lbl6: TLabel; Lbl7: TLabel; Lbl8: TLabel; Lbl9: TLabel; Shape1: TShape; Shape2: TShape; Shape3: TShape; Shape4: TShape; One_Player: TButton; Two_Players: TButton; procedure Lbl1Click(Sender: TObject); procedure btnNewGameClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Two_PlayersClick(Sender: TObject); procedure One_PlayerClick(Sender: TObject); private { Déclarations privées } public { Déclarations publiques } end; var FormPrinc: TFormPrinc; XorO : Integer; // Variable Permettant De Choisir Entre Le 'X' Ou Le 'O' XorComp : Integer; OptionJoueur : Integer; b : Integer; implementation {$R *.DFM} procedure TFormPrinc.Lbl1Click(Sender: TObject); // Procedure Regroupant Tous Les Clicks Sur Les Labels Var LblTempo : TLabel; // Variable Utilisé Pour Regrouper Tous Les Clicks Des Labels NombreDeO : Integer; HasardDebut : Integer; BlocOuCoup : Integer; HasardCriss : Integer; a : Integer; Begin LblTempo := Tlabel(sender); // Affection Des Clicks Sur Les Labels À Une Seule Variable NombreDeO := 1; Case XorComp Of 1 : Begin // Éviter La Double Possibilité De Gagner If Not (LblTempo.Caption <> '') then LblTempo.Caption := 'X'; If ((Lbl1.Caption 'X') Or (Lbl3.Caption 'X') Or (Lbl7.Caption = 'X') Or (Lbl9.Caption = 'X')) then Begin Lbl5.Caption := 'O'; XorComp := 2; End; // Si Autre , Choisir Un Label Au Hasard Et Mettre UN 'O' If (Lbl2.Caption 'X') Or (Lbl4.Caption 'X') Or (Lbl5.Caption = 'X') Or (Lbl6.Caption = 'X') Or (Lbl8.Caption = 'X') then Begin Repeat HasardDebut := Random(9) + 1 ; If TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(HasardDebut))).Caption = '' then TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(HasardDebut))).Caption := 'O'; Until TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(HasardDebut))).Caption = 'O'; XorComp := 2; End; End; 2 : Begin If (LblTempo.Caption = '') then Begin LblTempo.Caption := 'X'; XorComp := 2; NombreDeO := 1; BlocOuCoup := 1; // Définition des coups a bloquer pour l'ordinateur // // // // // If True then Begin If (Lbl1.Caption 'X') And (Lbl2.Caption 'X') And (NombreDeO = 1) then Begin Lbl3.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl1.Caption 'X') And (Lbl3.Caption 'X') And (NombreDeO = 1) then Begin Lbl2.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl3.Caption 'X') And (Lbl2.Caption 'X') And (NombreDeO = 1) then Begin Lbl1.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2;; End; If (Lbl4.Caption 'X') And (Lbl5.Caption 'X') And (NombreDeO = 1) then Begin Lbl6.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl4.Caption 'X') And (Lbl6.Caption 'X') And (NombreDeO = 1) then Begin Lbl5.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl5.Caption 'X') And (Lbl6.Caption 'X') And (NombreDeO = 1) then Begin Lbl4.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl7.Caption 'X') And (Lbl8.Caption 'X') And (NombreDeO = 1) then Begin Lbl9.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl7.Caption 'X') And (Lbl9.Caption 'X') And (NombreDeO = 1) then Begin Lbl8.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl8.Caption 'X') And (Lbl9.Caption 'X') And (NombreDeO = 1) then Begin Lbl8.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl1.Caption 'X') And (Lbl4.Caption 'X') And (NombreDeO = 1) then Begin Lbl7.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl1.Caption 'X') And (Lbl7.Caption 'X') And (NombreDeO = 1) then Begin Lbl4.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl4.Caption 'X') And (Lbl7.Caption 'X') And (NombreDeO = 1) then Begin Lbl1.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl2.Caption 'X') And (Lbl5.Caption 'X') And (NombreDeO = 1) then Begin Lbl8.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl2.Caption 'X') And (Lbl8.Caption 'X') And (NombreDeO = 1) then Begin Lbl5.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl5.Caption 'X') And (Lbl8.Caption 'X') And (NombreDeO = 1) then Begin Lbl2.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl3.Caption 'X') And (Lbl6.Caption 'X') And (NombreDeO = 1) then Begin Lbl9.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl3.Caption 'X') And (Lbl9.Caption 'X') And (NombreDeO = 1) then Begin Lbl6.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl6.Caption 'X') And (Lbl9.Caption 'X') And (NombreDeO = 1) then Begin Lbl3.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl3.Caption 'X') And (Lbl5.Caption 'X') And (NombreDeO = 1) then Begin Lbl7.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl3.Caption 'X') And (Lbl7.Caption 'X') And (NombreDeO = 1) then Begin Lbl5.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl5.Caption 'X') And (Lbl7.Caption 'X') And (NombreDeO = 1) then Begin Lbl3.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl1.Caption 'X') And (Lbl5.Caption 'X') And (NombreDeO = 1) then Begin Lbl9.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl1.Caption 'X') And (Lbl9.Caption 'X') And (NombreDeO = 1) then Begin Lbl5.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; If (Lbl5.Caption 'X') And (Lbl9.Caption 'X') And (NombreDeO = 1) then Begin Lbl1.Caption := 'O'; NombreDeO := 2; BlocOuCoup := 2; End; // Si aucune possibilité de gagner pour l'humain, Choisir Un label au hasard// // // // // If BlocOuCoup = 1 Then Begin For a := 1 to 50 do HasardCriss := (Random(9) + 1); If (TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(HasardCriss))).Caption = '') then Begin (TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(HasardCriss))).Caption := 'O'); a := 50 End Else Inc(a); End; End; End; End; End; // Section Humain Contre Humain // // // // Case XorO Of // Choix Entre Le 'X' OU Le 'O' 1 : Begin If Not (LblTempo.Caption <> '') then Begin LblTempo.Caption := 'X'; XorO := 2; End; End; 2 : Begin Begin If OptionJoueur = 2 then Begin If Not (LblTempo.Caption <> '') then Begin LblTempo.Caption := 'O'; XorO := 1; End; End; End; End; End; // Quoi Faire Si La Ligne 1 Est Gagnante If ((Lbl1.Caption 'X') and (Lbl2.Caption 'X') and (Lbl3.Caption = 'X')) Or ((Lbl1.Caption = 'O') and (Lbl2.Caption = 'O') and (Lbl3.Caption = 'O')) then Begin Lbl1.Font.Color := clRed; Lbl2.Font.Color := clRed; Lbl3.Font.Color := clRed; btnNewGame.Visible := True; End; // Quoi Faire Si La Ligne 2 Est Gagnante If (Lbl4.Caption 'X') and (Lbl5.Caption 'X') and (Lbl6.Caption = 'X') Or ((Lbl4.Caption = 'O') and (Lbl5.Caption = 'O') and (Lbl6.Caption = 'O')) then Begin Lbl4.Font.Color := clRed; Lbl5.Font.Color := clRed; Lbl6.Font.Color := clRed; btnNewGame.Visible := True; End; // Quoi Faire Si La Ligne 3 Est Gagnante If (Lbl7.Caption 'X') and (Lbl8.Caption 'X') and (Lbl9.Caption = 'X') Or ((Lbl7.Caption = 'O') and (Lbl8.Caption = 'O') and (Lbl9.Caption = 'O')) then Begin Lbl7.Font.Color := clRed; Lbl8.Font.Color := clRed; Lbl9.Font.Color := clRed; btnNewGame.Visible := True; End; // Quoi Faire Si La Colonne 1 Est Gagnante If (Lbl1.Caption 'X') and (Lbl4.Caption 'X') and (Lbl7.Caption = 'X') Or ((Lbl1.Caption = 'O') and (Lbl4.Caption = 'O') and (Lbl7.Caption = 'O')) then Begin Lbl1.Font.Color := clRed; Lbl4.Font.Color := clRed; Lbl7.Font.Color := clRed; btnNewGame.Visible := True; End; // Quoi Faire Si La Colonne 2 Est Gagnante If (Lbl2.Caption 'X') and (Lbl5.Caption 'X') and (Lbl8.Caption = 'X') Or ((Lbl2.Caption = 'O') and (Lbl5.Caption = 'O') and (Lbl8.Caption = 'O')) then Begin Lbl2.Font.Color := clRed; Lbl5.Font.Color := clRed; Lbl8.Font.Color := clRed; btnNewGame.Visible := True; End; // Quoi Faire Si La Colonne 3 Est Gagnante If (Lbl3.Caption 'X') and (Lbl6.Caption 'X') and (Lbl9.Caption = 'X') Or ((Lbl3.Caption = 'O') and (Lbl6.Caption = 'O') and (Lbl9.Caption = 'O')) then Begin Lbl3.Font.Color := clRed; Lbl6.Font.Color := clRed; Lbl9.Font.Color := clRed; btnNewGame.Visible := True; End; // Quoi Faire Si La Diagonale Gauche-Droite Est Gagnante If (Lbl1.Caption 'X') and (Lbl5.Caption 'X') and (Lbl9.Caption = 'X') Or ((Lbl1.Caption = 'O') and (Lbl5.Caption = 'O') and (Lbl9.Caption = 'O')) then Begin Lbl1.Font.Color := clRed; Lbl5.Font.Color := clRed; Lbl9.Font.Color := clRed; btnNewGame.Visible := True; End; // Quoi Faire Si La Diagonale Droite-Gauche Est Gagnante If (Lbl3.Caption 'X') and (Lbl5.Caption 'X') and (Lbl7.Caption = 'X') Or (Lbl3.Caption = 'O') and (Lbl5.Caption = 'O') and (Lbl7.Caption = 'O') then Begin Lbl3.Font.Color := clRed; Lbl5.Font.Color := clRed; Lbl7.Font.Color := clRed; btnNewGame.Visible := True; End; If btnNewGame.Visible = True then //Empêcher L'affichage De 'X' Ou De 'O' Lorsque La Partie Est Soit Terminé Ou Nulle Begin FormPrinc.Color := clBtnFace; For b := 1 to 9 do If TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(b))).Caption = '' then TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(b))).Visible := False; End; // Ligne De Code Permmettant À Savoir Si La Partie Est Nulle If (Lbl1.Caption <> '') and (Lbl2.Caption <> '') and (Lbl3.Caption <> '') and (Lbl4.Caption <> '') and (Lbl5.Caption <> '') and (Lbl6.Caption <> '') and (Lbl7.Caption <> '') and (Lbl8.Caption <> '') and (Lbl9.Caption <> '') then btnNewGame.Enabled := True; End; procedure TFormPrinc.btnNewGameClick(Sender: TObject); //Procédure Qui Commence Une Nouvelle Partie Begin For b := 1 to 9 do TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(b))).Caption := '' ; TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(b))).Visible := False; TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(b))).Font.Color := clBlack; btnNewGame.Visible := False; One_Player.Visible := True; Two_Players.Visible :=True; End; procedure TFormPrinc.FormCreate(Sender: TObject); //Procédure Permettant De Décider Si C'est Le 'X' Ou Le 'O' Qui Commence Begin Randomize; End; procedure TFormPrinc.Two_PlayersClick(Sender: TObject); Begin XorO := 1; XorComp := 3; One_Player.Visible := False; Two_Players.Visible := False; FormPrinc.Color := clGreen; OptionJoueur := 2; For b := 1 to 9 do TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(b))).Visible := True; End; procedure TFormPrinc.One_PlayerClick(Sender: TObject); Begin XorComp := 1; One_Player.Visible := False; Two_Players.Visible := False; FormPrinc.Color := clBlue; OptionJoueur := 1; For b := 1 to 9 do TLabel(FormPrinc.FindComponent('Lbl' + IntToStr(b))).Visible := True; End; End.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question