Je Devien Fou :P

Résolu
ff1061 Messages postés 13 Date d'inscription mercredi 16 septembre 2009 Statut Membre Dernière intervention 29 septembre 2009 - 27 sept. 2009 à 03:28
ff1061 Messages postés 13 Date d'inscription mercredi 16 septembre 2009 Statut Membre Dernière intervention 29 septembre 2009 - 29 sept. 2009 à 01:40
Bonjour...Je vais esseyer de faire sa court, j'ai un jeu de TicTacToe qui fonctionne tres bien lorsque je choisi l'option humain vs humain mais lorsque que je veux faire jouer quelqu'un contre l'ordinateur..j'ai de la difficulté a mettre l IA de l'ordinateur :P
En gros J'ai indiqué a l'ordinateur que si il y a un X au label 1 et label 2 il faut mettre un O au label 3 et ainsi de suite pour les autres ligne et colonnes. Mais lorsque que l'humain n'a aps de possibilité de victoire j'aimerais que l'ordinateur choissise un label vide au hasard et mettre un O. J'ai un bout de code qui devrais supposement le faire masi sa foire..En Passant desole j'ai vraiment beaucoup e ligne pour rien, je pourrais le faire beaucoup plus court masi ce n'est pas mon intention pour l'instant..j'aimerais qu'il fonctionne correctement donc éviter les commentaires si tu fais sa sa t'enleve beaucoup de linge...Mon Probleme est pour le coup au hasard . Je Vais mettre le code en entier pour que vous compreniez..desole j'ai pas encore tout commenté ET MERCI AUX PERSONNES QUI VONT PRENDRE LA PEINE DE TOUT LIRE :)
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'
  OptionJoueur : Integer;
  XorComp : 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;
   TBK : Integer;
   HasardCriss : Integer;
   a : Integer;
Begin
     LblTempo := Tlabel(sender);  // Affection Des Clicks Sur Les Labels À Une Seule Variable
     NombreDeO := 1;
     TBK := 1;
     Case XorComp Of
          1 : Begin
                   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;
                   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;
                        If TBK = 1 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;
                        End;
                        If BlocOuCoup = 1 Then
                           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;

     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
                   If OptionJoueur = 2 then
                   Begin
                        If Not (LblTempo.Caption <> '') then
                        Begin
                             LblTempo.Caption := 'O';
                             XorO := 1;
                        End;
                   End;
          End;
     End;

     // Quoi Faire Si La Ligne 1 Est Gagnante
     If (Lbl1.Caption 'X') and (Lbl2.Caption 'X') and (Lbl3.Caption = 'X') then
     Begin
          Lbl1.Font.Color := clRed;
          Lbl2.Font.Color := clRed;
          Lbl3.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     // Quoi Faire Si La Ligne 2 Est Gagnante
     If (Lbl4.Caption 'X') and (Lbl5.Caption 'X') and (Lbl6.Caption = 'X') then
     Begin
          Lbl4.Font.Color := clRed;
          Lbl5.Font.Color := clRed;
          Lbl6.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     // Quoi Faire Si La Ligne 3 Est Gagnante
     If (Lbl7.Caption 'X') and (Lbl8.Caption 'X') and (Lbl9.Caption = 'X') then
     Begin
          Lbl7.Font.Color := clRed;
          Lbl8.Font.Color := clRed;
          Lbl9.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     // Quoi Faire Si La Colonne 1 Est Gagnante
     If (Lbl1.Caption 'X') and (Lbl4.Caption 'X') and (Lbl7.Caption = 'X') then
     Begin
          Lbl1.Font.Color := clRed;
          Lbl4.Font.Color := clRed;
          Lbl7.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     // Quoi Faire Si La Colonne 2 Est Gagnante
     If (Lbl2.Caption 'X') and (Lbl5.Caption 'X') and (Lbl8.Caption = 'X') then
     Begin
          Lbl2.Font.Color := clRed;
          Lbl5.Font.Color := clRed;
          Lbl8.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     // Quoi Faire Si La Colonne 3 Est Gagnante
     If (Lbl3.Caption 'X') and (Lbl6.Caption 'X') and (Lbl9.Caption = 'X') then
     Begin
          Lbl3.Font.Color := clRed;
          Lbl6.Font.Color := clRed;
          Lbl9.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     // Quoi Faire Si La Diagonale Gauche-Droite Est Gagnante
     If (Lbl1.Caption 'X') and (Lbl5.Caption 'X') and (Lbl9.Caption = 'X') then
     Begin
          Lbl1.Font.Color := clRed;
          Lbl5.Font.Color := clRed;
          Lbl9.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     // Quoi Faire Si La Diagonale Droite-Gauche Est Gagnante
     If (Lbl3.Caption 'X') and (Lbl5.Caption 'X') and (Lbl7.Caption = 'X') then
     Begin
          Lbl3.Font.Color := clRed;
          Lbl5.Font.Color := clRed;
          Lbl7.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     //Quoi Faire Si La Ligne 1 Et La Colonne 3 Est Gagnante
     If (Lbl1.Caption 'X') and (Lbl2.Caption 'X') and (Lbl3.Caption = 'X') and (Lbl6.Caption = 'X') and (Lbl9.Caption = 'X')then
     Begin
          Lbl1.Font.Color := clRed;
          Lbl2.Font.Color := clRed;
          Lbl3.Font.Color := clRed;
          Lbl6.Font.Color := clRed;
          Lbl9.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     //Quoi Faire Si La Ligne 1 Et La Colonne 1 Est Gagnante
     If (Lbl1.Caption 'X') and (Lbl2.Caption 'X') and (Lbl3.Caption = 'X') and (Lbl4.Caption = 'X') and (Lbl7.Caption = 'X')then
     Begin
          Lbl1.Font.Color := clRed;
          Lbl2.Font.Color := clRed;
          Lbl3.Font.Color := clRed;
          Lbl4.Font.Color := clRed;
          Lbl7.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     //Quoi Faire Si La Ligne 3 Et La Colonne 3 Est Gagnante
     If (Lbl7.Caption 'X') and (Lbl8.Caption 'X') and (Lbl3.Caption = 'X') and (Lbl6.Caption = 'X') and (Lbl9.Caption = 'X')then
     Begin
          Lbl7.Font.Color := clRed;
          Lbl8.Font.Color := clRed;
          Lbl3.Font.Color := clRed;
          Lbl6.Font.Color := clRed;
          Lbl9.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     //Quoi Faire Si La Ligne 3 Et La Colonne 1 Est Gagnante
     If (Lbl1.Caption 'X') and (Lbl4.Caption 'X') and (Lbl7.Caption = 'X') and (Lbl8.Caption = 'X') and (Lbl9.Caption = 'X')then
     Begin
          Lbl1.Font.Color := clRed;
          Lbl4.Font.Color := clRed;
          Lbl7.Font.Color := clRed;
          Lbl8.Font.Color := clRed;
          Lbl9.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     //Quoi Faire Si La Ligne 2 Et La Colonne 2 Est Gagnante
     If (Lbl2.Caption 'X') and (Lbl4.Caption 'X') and (Lbl5.Caption = 'X') and (Lbl6.Caption = 'X') and (Lbl8.Caption = 'X')then
     Begin
          Lbl2.Font.Color := clRed;
          Lbl4.Font.Color := clRed;
          Lbl5.Font.Color := clRed;
          Lbl6.Font.Color := clRed;
          Lbl8.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     // Quoi Faire Si La Ligne 1 Est Gagnante
     If (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.Enabled := True;
     End;

     // Quoi Faire Si La Ligne 2 Est Gagnante
     If (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.Enabled := True;

     End;

     // Quoi Faire Si La Ligne 3 Est Gagnante
     If (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.Enabled := True;
     End;

     // Quoi Faire Si La Colonne 1 Est Gagnante
     If (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.Enabled := True;
     End;

     // Quoi Faire Si La Colonne 2 Est Gagnante
     If (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.Enabled := True;
     End;

     // Quoi Faire Si La Colonne 3 Est Gagnante
     If (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.Enabled := True;
     End;

     // Quoi Faire Si La Diagonale Gauche-Droite Est Gagnante
     If (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.Enabled := True;
     End;

     // Quoi Faire Si La Diagonale Droite-Gauche Est Gagnante
     If (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.Enabled := True;
     End;

     //Quoi Faire Si La Ligne 1 Et La Colonne 3 Est Gagnante
     If (Lbl1.Caption 'O') and (Lbl2.Caption 'O') and (Lbl3.Caption = 'O') and (Lbl6.Caption = 'O') and (Lbl9.Caption = 'O')then
     Begin
          Lbl1.Font.Color := clRed;
          Lbl2.Font.Color := clRed;
          Lbl3.Font.Color := clRed;
          Lbl6.Font.Color := clRed;
          Lbl9.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     //Quoi Faire Si La Ligne 1 Et La Colonne 1 Est Gagnante
     If (Lbl1.Caption 'O') and (Lbl2.Caption 'O') and (Lbl3.Caption = 'O') and (Lbl4.Caption = 'O') and (Lbl7.Caption = 'O')then
     Begin
          Lbl1.Font.Color := clRed;
          Lbl2.Font.Color := clRed;
          Lbl3.Font.Color := clRed;
          Lbl4.Font.Color := clRed;
          Lbl7.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     //Quoi Faire Si La Ligne 3 Et La Colonne 3 Est Gagnante
     If (Lbl7.Caption 'O') and (Lbl8.Caption 'O') and (Lbl3.Caption = 'O') and (Lbl6.Caption = 'O') and (Lbl9.Caption = 'O')then
     Begin
          Lbl7.Font.Color := clRed;
          Lbl8.Font.Color := clRed;
          Lbl3.Font.Color := clRed;
          Lbl6.Font.Color := clRed;
          Lbl9.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     //Quoi Faire Si La Ligne 3 Et La Colonne 1 Est Gagnante
     If (Lbl1.Caption 'O') and (Lbl4.Caption 'O') and (Lbl7.Caption = 'O') and (Lbl8.Caption = 'O') and (Lbl9.Caption = 'O')then
     Begin
          Lbl1.Font.Color := clRed;
          Lbl4.Font.Color := clRed;
          Lbl7.Font.Color := clRed;
          Lbl8.Font.Color := clRed;
          Lbl9.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     //Quoi Faire Si La Ligne 2 Et La Colonne 2 Est Gagnante
     If (Lbl2.Caption 'O') and (Lbl4.Caption 'O') and (Lbl5.Caption = 'O') and (Lbl6.Caption = 'O') and (Lbl8.Caption = 'O')then
     Begin
          Lbl2.Font.Color := clRed;
          Lbl4.Font.Color := clRed;
          Lbl5.Font.Color := clRed;
          Lbl6.Font.Color := clRed;
          Lbl8.Font.Color := clRed;
          btnNewGame.Enabled := True;
     End;

     If btnNewGame.Enabled = True then      //Empêcher L'affichage De 'X' Ou De 'O' Lorsque La Partie Est Soit Terminé Ou Nulle
     Begin
          If Lbl1.Caption = '' then
             Lbl1.Visible := False;
          If Lbl2.Caption = '' then
             Lbl2.Visible := False;
          If Lbl3.Caption = '' then
             Lbl3.Visible := False;
          If Lbl4.Caption = '' then
             Lbl4.Visible := False;
          If Lbl5.Caption = '' then
             Lbl5.Visible := False;
          If Lbl6.Caption = '' then
             Lbl6.Visible := False;
          If Lbl7.Caption = '' then
             Lbl7.Visible := False;
          If Lbl8.Caption = '' then
             Lbl8.Visible := False;
          If Lbl9.Caption = '' then
             Lbl9.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
     Lbl1.Caption := '';
     Lbl2.Caption := '';
     Lbl3.Caption := '';
     Lbl4.Caption := '';
     Lbl5.Caption := '';
     Lbl6.Caption := '';
     Lbl7.Caption := '';
     Lbl8.Caption := '';
     Lbl9.Caption := '';
     Lbl1.Visible := False;
     Lbl2.Visible := False;
     Lbl3.Visible := False;
     Lbl4.Visible := False;
     Lbl5.Visible := False;
     Lbl6.Visible := False;
     Lbl7.Visible := False;
     Lbl8.Visible := False;
     Lbl9.Visible := False;
     Lbl1.Font.Color := clBlack;
     Lbl2.Font.Color := clBlack;
     Lbl3.Font.Color := clBlack;
     Lbl4.Font.Color := clBlack;
     Lbl5.Font.Color := clBlack;
     Lbl6.Font.Color := clBlack;
     Lbl7.Font.Color := clBlack;
     Lbl8.Font.Color := clBlack;
     Lbl9.Font.Color := clBlack;
     btnNewGame.Enabled := False;
     One_Player.Enabled := True;
     Two_Players.Enabled :=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;
     One_Player.Enabled := False;
     Two_Players.Enabled := False;
     Lbl1.Visible := True;
     Lbl2.Visible := True;
     Lbl3.Visible := True;
     Lbl4.Visible := True;
     Lbl5.Visible := True;
     Lbl6.Visible := True;
     Lbl7.Visible := True;
     Lbl8.Visible := True;
     Lbl9.Visible := True;
     OptionJoueur := 2;
End;

procedure TFormPrinc.One_PlayerClick(Sender: TObject);
Begin
     Lbl1.Visible := True;
     Lbl2.Visible := True;
     Lbl3.Visible := True;
     Lbl4.Visible := True;
     Lbl5.Visible := True;
     Lbl6.Visible := True;
     Lbl7.Visible := True;
     Lbl8.Visible := True;
     Lbl9.Visible := True;
     XorComp := 1;
     One_Player.Enabled := False;
     Two_Players.Enabled := False;


End;

End.

7 réponses

ff1061 Messages postés 13 Date d'inscription mercredi 16 septembre 2009 Statut Membre Dernière intervention 29 septembre 2009
28 sept. 2009 à 00:23
Oui Je savais deja tout sa...les possibilité de victoire sont deja inscrites, tu ne l'as surement pas lu car c'set tres long, ainsi, apres deux jours de travail intense, j'ai esseyé de tout plein de facon et je suiss rendu a CA :
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;
3
Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 10
27 sept. 2009 à 08:24
Aïe ça pique ! 90% de ton code est formé de redondances ! Essaye déjà de les supprimer, et ça sera déjà plus lisible et ainsi plus facile à corriger !

Cordialement, Bacterius !
0
ff1061 Messages postés 13 Date d'inscription mercredi 16 septembre 2009 Statut Membre Dernière intervention 29 septembre 2009
27 sept. 2009 à 20:36
Bon Voila J'ai essayé d'éditer mon premier message mais je n'y s pas arriver donc je vasi coller mon programme a nouvea ici mais il est maintenant beaucoup plus court...désolé pour la longueur du sujet :S Donc mon problème est de mettre un O au hasard (IA) lorsque le joueur n'a pas de possibilité de victoire, lors de l'exécution, je parvient a le faire une fois mais ensuite on dirait qu'il oubli de le faire et rien ne se passe.Il y a beaucoup moins de redondance mais je devais quand meem " expliquer" a l'ordinateur toutes les possibilité de victoire et de blocage du joueur. Merci
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.
0
Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 10
28 sept. 2009 à 00:11
Ca reste encore très très redondant ! Mais bon. Si ton problème consiste à placer un 'O' au hasard si l'humain n'est pas en mesure de gagner au prochain coup (quoique douteux pour une IA lol), alors il suffit de vérifier si l'humain ne va pas gagner (il n'a pas 2 pions alignés) :

XX-
---
---

X-X
---
---

-XX
---
---

X--
-X-
---

X--
---
--X

Et ainsi de suite (ou une combinaison de plusieurs de ces dessins, exemple le premier superposé au cinquième). Et si aucun de ces schémas est rencontré, alors on tire un nombre aléatoire entre 1 et 9 :

123
456
789

On en tire un jusqu'à ce qu'on tombe sur un nombre dont la case associée est vide.
Mais je suppose que tu savais déjà tout ça, donc à part le répéter je ne vois pas ce que je peux apporter d'autre ?

Cordialement, Bacterius !
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
ff1061 Messages postés 13 Date d'inscription mercredi 16 septembre 2009 Statut Membre Dernière intervention 29 septembre 2009
29 sept. 2009 à 01:34
C'est bau j'ai trouvé merci quand meme :P
0
Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 10
29 sept. 2009 à 01:38
Tu peux eventuellement dire où était le problème, pour ceux qui seraient confrontés au même dans le futur ?

Cordialement, Bacterius !
0
ff1061 Messages postés 13 Date d'inscription mercredi 16 septembre 2009 Statut Membre Dernière intervention 29 septembre 2009
29 sept. 2009 à 01:40
c'etait un arrangement de mes begin et end je crois... j'ai plutot été chanceux apres avoir essayé longtemps
0
Rejoignez-nous