Forum > Beginners

More efficient way of doing this?

(1/1)

KappaHD:

--- Code: ---unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, crt,
  LCLType, MMSystem;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    procedure Button1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  Vraag: array[1..2] of string = ('Vraag: Wat is zwart', 'Vraag: wat is Bruin');
  score: array[1..3] of integer = (1000, 1000, 1000);
  UI: string;
  Naam: array[1..3] of string;
  error: integer;
    x: integer;

implementation

{$R *.lfm}

{ TForm1 }
procedure TForm1.FormActivate(Sender: TObject);
   RadioButton1.Checked:= true;
   RadioButton2.Enabled:= false;
   RadioButton3.Enabled:= false;
   // Vraag 1 wordt gepresenteerd en heeft een fout en een goed optie
   writeln(Vraag[1]);
   read(UI);
   If (UI= ('Zwart')) or (UI= ('zwart')) then begin
      writeln('Goed zo!');
      write('<Druk op een knop om door te gaan...>');
      Readkey;
      ClrScr;
      // Score wordt toegevoegd per speler als ie aan de beurt is
      If radiobutton1.checked= true then begin
         score[1] := score[1] + 100 ;
         readln;
         end
       else If radiobutton2.checked= true then begin
         score[2] := score[2] + 100 ;
         readln;
      end
      else If radiobutton3.checked= true then begin
         score[3] := score[3] + 100 ;
         readln;
      end
      end



   else if (UI<> ('Zwart')) or (UI<> ('zwart'))  then  begin
      readln;
      writeln('Jammer!');
      write('<Druk op een knop om door te gaan...>');
      Readkey;
      ClrScr;
      If radiobutton1.checked= true then begin
         radiobutton2.Enabled:= true ;
         radiobutton1.checked:= false;
         radiobutton2.checked:= true ;
         radionbutton1.enabled:= false;
         score[1] := score[1] - 100 ;
      end
      else If radiobutton2.checked= true then begin
         radiobutton3.Enabled:= true ;
         radiobutton2.checked:= false;
         radiobutton3.checked:= true ;
         radionbutton2.enabled:= false;
         score[2] := score[2] - 100 ;

      end
      else If radiobutton3.checked= true then begin
      radiobutton1.Enabled:= true ;
      radiobutton3.checked:= false;
      radiobutton1.checked:= true ;
      radionbutton3.enabled:= false;
         score[3] := score[3] - 100 ;
      end             
   end;




end;
end.
--- End code ---

What it should do is pretty easy.  At the beginning only Radiobutton 1 should be enabled, however if it answer is wrong then RadioButton2 should be enabled and checked. But the other buttons should be disabled, otherwise you could just change the state of the radiobutton. Which shouldn't be possible.

Eugene Loza:
Maybe I just didn't understand what the program should do. I'd make it in the following way:


--- Code: ---var queststage:integer;

...
procedure setradiobuttons;
begin
 radiobutton1.checked:= 1=queststage;
 radiobutton1.enabled:= 1=queststage;
 radiobutton2.checked:= 2=queststage;
 radiobutton2.enabled:= 2=queststage;
 radiobutton3.checked:= 3=queststage;
 radiobutton3.enabled:= 3=queststage;
// I'm not sure if I understood what you want to do in this part correctly.
end;
--- End code ---
somewhere at the quest start (i.e. a new question):

--- Code: ---queststage:=1;
setradiobuttons;
--- End code ---
and then if answer is correct then:

--- Code: ---inc(score[queststage],100);
--- End code ---
if the answer is wrong then:

--- Code: ---dec(score[queststage],100);
if queststage<3 then begin
  inc(queststage);
  setradiobuttons;
end;
--- End code ---
or something like this.

howardpc:
Why don't you use a TRadioGroup?
It's already programmed by experts and fully debugged.
Then you could just write one or two lines of code which immediately do what you want, instead of scores of lines of dubious spaghetti which need other people to debug them?

Navigation

[0] Message Index

Go to full version