Recent

Author Topic: More efficient way of doing this?  (Read 2551 times)

KappaHD

  • Newbie
  • Posts: 3
More efficient way of doing this?
« on: December 16, 2014, 10:00:49 am »
Code: [Select]
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.

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

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
Re: More efficient way of doing this?
« Reply #1 on: December 16, 2014, 11:51:53 am »
Maybe I just didn't understand what the program should do. I'd make it in the following way:

Code: [Select]
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;
somewhere at the quest start (i.e. a new question):
Code: [Select]
queststage:=1;
setradiobuttons;
and then if answer is correct then:
Code: [Select]
inc(score[queststage],100);if the answer is wrong then:
Code: [Select]
dec(score[queststage],100);
if queststage<3 then begin
  inc(queststage);
  setradiobuttons;
end;
or something like this.
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: More efficient way of doing this?
« Reply #2 on: December 16, 2014, 12:35:44 pm »
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?

 

TinyPortal © 2005-2018