Jump to content
m_pell_98037

If Then dilema

Recommended Posts

Compiles, but program has bugs.  I'm sure it's the if's.  Perhaps I should use subs.  HLP Please.

 

procedure TDialogs3Form.Image52Click(Sender: TObject);
var
  B: TBitmap;
  i: Integer;
begin
  i := 0;
  Current_Card := Image52.Tag;                    // Tag number provides index number into btn array below to get card name.
  Tries := StrToInt(Clicks.Text) + 1;            // Keep score
  Clicks.Text := IntToStr(tries);
  B := TBitMap.Create;
  Imagelist1.GetBitmap(Current_Card, B);
  Image52.Picture.Bitmap.Assign(B);
  B.Free;

  inc(Card_Selected);

  if (Card_Selected = 1) then
      First_Card_Tag :=  Current_Card;
      First_Card_Image := TImage(Components);
      Card1_Selected_Name := Btn[Current_Card].Text ;
      Edit4.Text := Card1_Selected_Name;
      Label53.Caption := 'Pick Another Card';

    if Card_Selected = 2 then
           Card2_Selected_Name := Btn[Current_Card].Text ;
           Edit4.Text := Card1_Selected_Name;
           Edit1.Text := Card2_Selected_Name;
           Sleep( 5000 );
           Card_Selected := 0;

     if Card1_Selected_Name = Card2_Selected_Name then
             Edit4.Text := '';
             Edit1.Text := '';
             Label53.Caption := ' Match';
             Image52.Visible := False;                     // Make 2nd Card invisble
             First_Card_Image.Visible := False;           // Make 1st Card invisble

       if Card1_Selected_Name <> Card2_Selected_Name then
           Edit4.Text := '';
           Edit1.Text := '';
           Label53.Caption := 'Select 1st Card';
             B := TBitMap.Create;
             Imagelist1.GetBitmap(0, B);                  // Card back
             Image52.Picture.Bitmap.Assign(B);
             B := TBitMap.Create;
             Imagelist1.GetBitmap(0, B);                  // Card back
             First_Card_Image.Picture.Bitmap.Assign(B);
             B.Free;
end;
 

Share this post


Link to post
Guest

your code is "crazy"!!! but it is yours!

  • my bet is that all rest is very confuse, too!
  • ALWAYS USE SEPARATE IT in little pieces or use "// comment" to determine parts for help you

 

var // for test your code
  Image52            : TImage;
  Current_Card       : integer;
  Tries              : integer;
  Clicks             : TEdit;
  ImageList1         : TImageList;
  Card_Selected      : integer;
  First_Card_Tag     : integer;
  First_Card_Image   : TImage;
  Card1_Selected_Name: string;
  Btn                : array [0 .. 1] of TEdit;
  ComponentsXXXX     : TComponent;
  Edit4              : TEdit;
  Label53            : TLabel;
  Card2_Selected_Name: string;
  Edit1              : TEdit;

procedure TForm1.Button1Click(Sender: TObject);
var
  B: TBitmap;
  i: integer;
begin
  i            := 0;
  Current_Card := Image52.Tag;               // Tag number provides index number into btn array below to get card name.
  Tries        := StrToInt(Clicks.Text) + 1; // Keep score
  Clicks.Text  := IntToStr(Tries);
  //
  B := TBitmap.Create; // if any "exception" ... how do it stay?
  try
    //
    B := nil;
    ImageList1.GetBitmap(Current_Card, B);
    Image52.Picture.Bitmap.Assign(B);
    //
    inc(Card_Selected);
    //
    if (Card_Selected = 1) then
      First_Card_Tag := Current_Card;
    //
    First_Card_Image    := TImage(ComponentsXXXX);
    Card1_Selected_Name := Btn[Current_Card].Text;
    Edit4.Text          := Card1_Selected_Name;
    Label53.Caption     := 'Pick Another Card';
    //
    if Card_Selected = 2 then
      Card2_Selected_Name := Btn[Current_Card].Text;
    //
    Edit4.Text := Card1_Selected_Name;
    Edit1.Text := Card2_Selected_Name;
    //
    Sleep(5000);
    //
    Card_Selected := 0;
    //
    if Card1_Selected_Name = Card2_Selected_Name then
      Edit4.Text := '';
    //
    Edit1.Text               := '';
    Label53.Caption          := ' Match';
    Image52.Visible          := False; // Make 2nd Card invisble
    First_Card_Image.Visible := False; // Make 1st Card invisble
    //
    if Card1_Selected_Name <> Card2_Selected_Name then
      Edit4.Text := '';
    //
    Edit1.Text      := '';
    Label53.Caption := 'Select 1st Card';
    //
    B := nil;
    //
    ImageList1.GetBitmap(0, B); // Card back
    Image52.Picture.Bitmap.Assign(B);
    //
    B := nil;
    //
    ImageList1.GetBitmap(0, B); // Card back
    First_Card_Image.Picture.Bitmap.Assign(B);
  finally
    B.Free;
  end;
end;

 

hug

Share this post


Link to post
6 hours ago, m_pell_98037 said:

Compiles, but program has bugs.

We don't even know what it is supposed to do.

First of all layout your code correctly so that the structure is apparent.

 

Share this post


Link to post
Guest

@m_pell_98037 Have you forget to use begin..end for block code (multiline) ? is that what did you mean with subs ? 

  if firststatement then
  begin
    // multi line of code, that will be executed only when firststatement is true
  end
  else if secondstatement then
  begin

  end
  else if thirdstatement then
  begin

  end
  else
  begin

  end;

 

Share this post


Link to post
17 hours ago, m_pell_98037 said:

Perhaps I should use subs.

If subroutines mean procedures and functions also lists arrays and what not.   Marco Cantu has some free books out there.  Delphi has help built in plus source.

 

 

 

For now take a Tshape and drop it in the design window.   Use the F11 key to change the Brush and Pen Properties this makes a nice card back.  

In code window find Shape1: TShape; Hold down the control<key>to underline TShape then cllick on it.  This coding allows the Tshape to be made. Study how the paint works with pen and brush. 

 

Calendar1: TCalendar;  Is another one that shows how change properties and a grid as well.  You not needing to make a control now just examine the plumbing.      

 

 

Share this post


Link to post

It is going to be Solitaire Concentration game.

The Try Finally compiled as did K's   end else if then begin, suggestion.

I'm ready to start stepping into program.  Drat!

 

I usually start out the long hard way to get something running and then refine.

Sometimes I get bit when chunks are to big. Still knocking out cobb webs since Delphi 2.

 

I sure appreciate the help.  At least now things compile.

 

I thought there was a way to bundle a whole project for export.  Did that go away.

Might be easier if you see whole ball of wax.

 

Thanks

Michael Pell

Share this post


Link to post
Quote

I thought there was a way to bundle a whole project for export.  Did that go away.

Might be easier if you see whole ball of wax.

No need for that. Its your program and by doing it yourself will allow you to get higher up the hill. 

 

promote isplaying, isgameover,isplayer2turn booleans to enumeration of Tsolcon  = (scPlaying, scGameover, scNewGameQ, scDeal)

 

gamestatus:Tsolcon; global

 

set gamestatus := scNewGameQ

 

In a timer event place a case statement

 

Case gameStatus of

...

scNewGameQ:  begin gamestatus := scDeal; // next timer event the code in scDeal will be called

                          end;

scDeal: //calls procedure dealCards  

 

end;   

 

Other events Buttonclick  simply change gamestatus  the timer's event acts on the status change 

 

   

    

Share this post


Link to post

I concur.  Just thought I'd throw it out there.

Sounds like you've tried this, so I saved your post (as well as others) in project folder.

I'll glean something from it later, if you don't mind and its there so i did.

Thanks again

Michael Pell  -> Knowledge is to be shared. imho

Share this post


Link to post
Quote

Sounds like you've tried this, 

Actually in VB I made video poker game.   The enumerations allow say washing machine to be emulated when combined with a case statement.   Each enum needs to be in order in the case statement.  As you add more features you just insert additional enum.  Also consider the scPlaying Its' ordinal value is zero so its first in case statement.  More importantly it helps laying out the program. Until you get around to making the play routine routine it just passes through waiting for change in game status.  I'll fab up a working example in week in week or two. A key or button click would change status then call procedure with case statement.      

 

Pascal with Excellence quotes Alexander Pope--A little learning is a dangerous thing, drink deep, or taste not.   // I try to share knowledge so people need not read the documentation or hit a barn when learning to fly.  :classic_smile:     

Edited by Pat Foley
Add emu

Share this post


Link to post

Help from the community was indispensable.  I didn't just read posts, I tried them. Which got me down the path.

You know I just have to replace broken code with something that works perfectly.

 

 

procedure TDialogs3Form.Image52Click(Sender: TObject);
var
  B: TBitmap;
  i: Integer;
begin
  i := 0;
  Current_Card := Image52.Tag;                    // Tag number provides index number into btn array below to get card name.
  Tries := StrToInt(Clicks.Text) + 1;            // Keep score
  Clicks.Text := IntToStr(tries);
  B := TBitMap.Create;
    Imagelist1.GetBitmap(Current_Card, B);
    Image52.Picture.Bitmap.Assign(B);
  B.Free;

  inc(Card_Selected);

  if (Card_Selected = 1) then
     begin
        First_Card_Tag :=  Current_Card;
        Card1_Selected_Name := Btn[Current_Card].Text ;
        nr := (Sender as TImage).Name; // nr : string;
        First_Card_Image := (Dialogs3Form.Findcomponent(nr) as TImage);  // Saving the 1st of two Image clicks.
        Label53.Caption := 'Pick Another Card';
     end
   else if Card_Selected = 2 then
      begin
           Card2_Selected_Name := Btn[Current_Card].Text ;
           Label53.Caption := '2nd Card';
           ShowTime; //=ShowMessage                                                                                                                                       // Call this Dialog procedure to break out of Image10Click procedure, so card face displays.
      end;                                                                                                                                                                                    //(otherwise 2nd card face won't display at all), and you can look at the pair of cards as long Message dialog is up.

    if Card_Selected = 2 then
    if Card1_Selected_Name = Card2_Selected_Name then
      begin
             Label53.Caption := ' Match';                  // Msg to player
             Image52.Visible := False;                     // Make 2nd Card invisble
             First_Card_Image.Visible := False;            // Make 1st Card invisble
             Card_Selected := 0;
      end;
       if Card_Selected = 2 then
       if Card1_Selected_Name <> Card2_Selected_Name then
      begin
           Label53.Caption := 'Select 1st Card';          // Msg to player
             B := TBitMap.Create;
             Imagelist1.GetBitmap(0, B);                  // Display card backside
             Image52.Picture.Bitmap.Assign(B);
             Imagelist1.GetBitmap(0, B);                  // Display card backside
             First_Card_Image.Picture.Bitmap.Assign(B);
             B.Free;
           Card_Selected := 0;
      end;
end;
 

 

Thanks again for the community help

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×