TheoNick

Vokabeltrainer - Delphi

Apr 11th, 2021 (edited)
737
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, ShellApi;
  8.  
  9. type
  10.   TfrmVTrainer = class(TForm)
  11.     rbnOne: TRadioButton;
  12.     rbnTwo: TRadioButton;
  13.     lblWordOne: TLabel;
  14.     lblWordTwo: TLabel;
  15.     btnGuess: TButton;
  16.     edtGuess: TEdit;
  17.     Panel1: TPanel;
  18.     Panel2: TPanel;
  19.     rbnLinear: TRadioButton;
  20.     rbnRandom: TRadioButton;
  21.     btnNext: TButton;
  22.     lblTries: TLabel;
  23.     lblTriesLabel: TLabel;
  24.     edtTries: TEdit;
  25.     btnDelete: TButton;
  26.     btnHelp: TButton;
  27.     Label1: TLabel;
  28.     btnChooseFile: TButton;
  29.     Panel3: TPanel;
  30.     rbnGKSYes: TRadioButton;
  31.     rbnGKSNo: TRadioButton;
  32.     Label2: TLabel;
  33.     procedure InitStuff;
  34.     procedure btnGuessClick(Sender: TObject);
  35.     procedure btnNextClick(Sender: TObject);
  36.     procedure btnDeleteClick(Sender: TObject);
  37.     procedure btnHelpClick(Sender: TObject);
  38.     procedure btnChooseFileClick(Sender: TObject);
  39.   private
  40.     { Private-Deklarationen }
  41.   public
  42.     { Public-Deklarationen }
  43.   end;
  44.  
  45. var
  46.   frmVTrainer: TfrmVTrainer;
  47.   intRandom, intTries: integer;
  48.   strCorrect, strFile: string;
  49.   strListe: TStringList;
  50.   bolInit: boolean;
  51.   myFile: TextFile;
  52.  
  53. implementation
  54.  
  55. {$R *.dfm}
  56.  
  57. procedure TfrmVTrainer.InitStuff;
  58. begin
  59.   //check if strFile allready assigned
  60.   if strFile = '' then strFile := 'vok.csv';
  61.   //loading words from file
  62.   strListe := TStringList.Create;
  63.   strListe.LoadFromFile(strFile);
  64.   //init various other stuff
  65.   bolInit := true;
  66.   intRandom := -1;
  67.   AssignFile(myFile, strFile);
  68. end;
  69.  
  70. procedure TfrmVTrainer.btnChooseFileClick(Sender: TObject);
  71. var opdFile: TOpenDialog;
  72. begin
  73.   opdFile := TOpenDialog.Create(self);
  74.   opdFile.InitialDir := GetCurrentDir;
  75.   opdFile.Filter := 'csv-Datei|*.csv|txt-Datei|*.txt';
  76.   if opdFile.Execute() then
  77.   begin
  78.     strFile := opdFile.FileName;
  79.     btnChooseFile.Caption := ExtractFileName(strFile);
  80.     bolInit := false;
  81.   end;
  82. end;
  83.  
  84. procedure TfrmVTrainer.btnDeleteClick(Sender: TObject);
  85. var intIndex: integer;
  86. begin
  87.   //delete entry from list
  88.   strListe.Delete(intRandom);
  89.  
  90.   //rewrite list to file
  91.   ReWrite(myFile);
  92.   for intIndex := 0 to strListe.Count-1 do
  93.     WriteLn(myFile, strListe[intIndex]);
  94.   CloseFile(myFile);
  95.  
  96.   //hide button
  97.   btnDelete.Visible := false;
  98. end;
  99.  
  100. procedure TfrmVTrainer.btnGuessClick(Sender: TObject);
  101. var bolCorrect: boolean;
  102. begin
  103.   //check if guess is correct
  104.   if rbnGKSYes.Checked then
  105.     if edtGuess.Text = strCorrect then bolCorrect := true
  106.     else bolCorrect := false
  107.   else if rbnGKSNo.Checked then
  108.     if Uppercase(edtGuess.Text) = Uppercase(strCorrect) then bolCorrect := true
  109.     else bolCorrect := false;
  110.  
  111.   if bolCorrect then
  112.   begin
  113.     //give congrats, handle buttons
  114.     lblWordTwo.Caption := 'Korrekt!';
  115.     lblTries.Caption := IntToStr(StrToInt(edtTries.Text)-intTries)+' Fehlversuche';
  116.     btnNext.Visible := true;
  117.     btnGuess.Visible := false;
  118.     edtTries.Visible := true;
  119.     lblTriesLabel.Visible := true;
  120.     btnDelete.Visible := true;
  121.   end
  122.   else
  123.   begin
  124.     lblWordTwo.Caption := 'Nope, try again';
  125.     intTries := intTries - 1;
  126.     lblTries.Caption := 'Tries: ' + IntToStr(intTries);
  127.     //check if all tries are out
  128.     if intTries = 0 then
  129.     begin
  130.       lblWordTwo.Caption := 'Das Wort wäre '+strCorrect+' gewesen.';
  131.       lblTries.Caption := 'All tries used';
  132.       btnNext.Visible := true;
  133.       btnGuess.Visible := false;
  134.       edtTries.Visible := false;
  135.       lblTriesLabel.Visible := false;
  136.     btnDelete.Visible := true;
  137.     end;
  138.   end;
  139. end;
  140.  
  141. procedure TfrmVTrainer.btnHelpClick(Sender: TObject);
  142. begin
  143.   ShellExecute(Application.Handle, 'open', PChar('notepad.exe'), PChar('readme.txt'), Nil, SW_NORMAL);
  144. end;
  145.  
  146. procedure TfrmVTrainer.btnNextClick(Sender: TObject);
  147. var strCurrent: string;
  148.     intPos: integer;
  149.     pwcMsg: PWideChar;
  150.     bolNext: boolean;
  151. begin
  152.   //run init-procedure if pressed for the first time
  153.   if not bolInit = true then InitStuff;
  154.  
  155.   //check if random or linear
  156.   if rbnRandom.Checked then
  157.   begin
  158.     intRandom := Random(strListe.Count);
  159.   end
  160.   else if rbnLinear.Checked then
  161.   begin
  162.     //check if all words are through
  163.     if intRandom = strListe.Count-1 then
  164.     begin
  165.       Application.MessageBox('Du hast alle Einträge durchgearbeitet!', 'Bravo!', MB_ICONINFORMATION or MB_OK);
  166.       intRandom := 0;
  167.     end
  168.     else
  169.     begin
  170.       intRandom := intRandom + 1;
  171.     end;
  172.   end;
  173.  
  174.   //check that file is not empty
  175.   if strListe.Count = 0 then
  176.   begin
  177.     pwcMsg := PWideChar('Keine Einträge in Datei gefunden.'+sLineBreak+'Datei wird bei nächstem Klick auf Next oder Neustart neu eingelesen.');
  178.     bolInit := false;
  179.     Application.MessageBox(pwcMsg, 'Achtung', MB_ICONERROR or MB_OK);
  180.   end;
  181.  
  182.   //assign words from stringlist
  183.   strCurrent := strListe[intRandom];
  184.   intPos := POS(';', strCurrent);
  185.   //check that entry contains semikolon
  186.   if intPos = 0 then
  187.   begin
  188.     intPos := POS(#9, strCurrent);
  189.     if intPos = 0 then
  190.     begin
  191.       pwcMsg := PWideChar('Kein Semikolon oder Tabstop an Stelle '+IntToStr(intRandom+1)+' gefunden.'+sLineBreak+'Überprüfen sie bitte die Datei '+strFile+'.'+sLineBreak+'Datei wird bei nächstem Klick auf Next oder Neustart neu eingelesen.');
  192.       bolInit := false;
  193.       bolNext := false;
  194.       Application.MessageBox(pwcMsg, 'Achtung', MB_ICONERROR or MB_OK);
  195.     end
  196.     else
  197.     begin
  198.       bolNext := true;
  199.     end;
  200.   end
  201.   else
  202.   begin
  203.     bolNext := true;
  204.   end;
  205.  
  206.   if bolNext then
  207.   begin
  208.     intTries := StrToInt(edtTries.Text);
  209.     lblTries.Caption := 'Tries: ' + IntToStr(intTries);
  210.     edtGuess.Text := '';
  211.     btnNext.Visible := false;
  212.     btnGuess.Visible := true;
  213.     edtTries.Visible := false;
  214.     lblTriesLabel.Visible := false;
  215.     btnDelete.Visible := false;
  216.   end;
  217.  
  218.   if rbnOne.Checked then
  219.   begin
  220.     lblWordOne.Caption := COPY(strCurrent, 1, intPos-1);
  221.     strCorrect := COPY(strCurrent, intPos+1, strCurrent.Length);
  222.   end
  223.   else if rbnTwo.Checked then
  224.   begin
  225.     strCorrect := COPY(strCurrent, 1, intPos-1);
  226.     lblWordOne.Caption := COPY(strCurrent, intPos+1, strCurrent.Length);
  227.   end;
  228. end;
  229. end.
RAW Paste Data