Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, ShellApi;
- type
- TfrmVTrainer = class(TForm)
- rbnOne: TRadioButton;
- rbnTwo: TRadioButton;
- lblWordOne: TLabel;
- lblWordTwo: TLabel;
- btnGuess: TButton;
- edtGuess: TEdit;
- Panel1: TPanel;
- Panel2: TPanel;
- rbnLinear: TRadioButton;
- rbnRandom: TRadioButton;
- btnNext: TButton;
- lblTries: TLabel;
- lblTriesLabel: TLabel;
- edtTries: TEdit;
- btnDelete: TButton;
- btnHelp: TButton;
- Label1: TLabel;
- btnChooseFile: TButton;
- Panel3: TPanel;
- rbnGKSYes: TRadioButton;
- rbnGKSNo: TRadioButton;
- Label2: TLabel;
- procedure InitStuff;
- procedure btnGuessClick(Sender: TObject);
- procedure btnNextClick(Sender: TObject);
- procedure btnDeleteClick(Sender: TObject);
- procedure btnHelpClick(Sender: TObject);
- procedure btnChooseFileClick(Sender: TObject);
- private
- { Private-Deklarationen }
- public
- { Public-Deklarationen }
- end;
- var
- frmVTrainer: TfrmVTrainer;
- intRandom, intTries: integer;
- strCorrect, strFile: string;
- strListe: TStringList;
- bolInit: boolean;
- myFile: TextFile;
- implementation
- {$R *.dfm}
- procedure TfrmVTrainer.InitStuff;
- begin
- //check if strFile allready assigned
- if strFile = '' then strFile := 'vok.csv';
- //loading words from file
- strListe := TStringList.Create;
- strListe.LoadFromFile(strFile);
- //init various other stuff
- bolInit := true;
- intRandom := -1;
- AssignFile(myFile, strFile);
- end;
- procedure TfrmVTrainer.btnChooseFileClick(Sender: TObject);
- var opdFile: TOpenDialog;
- begin
- opdFile := TOpenDialog.Create(self);
- opdFile.InitialDir := GetCurrentDir;
- opdFile.Filter := 'csv-Datei|*.csv|txt-Datei|*.txt';
- if opdFile.Execute() then
- begin
- strFile := opdFile.FileName;
- btnChooseFile.Caption := ExtractFileName(strFile);
- bolInit := false;
- end;
- end;
- procedure TfrmVTrainer.btnDeleteClick(Sender: TObject);
- var intIndex: integer;
- begin
- //delete entry from list
- strListe.Delete(intRandom);
- //rewrite list to file
- ReWrite(myFile);
- for intIndex := 0 to strListe.Count-1 do
- WriteLn(myFile, strListe[intIndex]);
- CloseFile(myFile);
- //hide button
- btnDelete.Visible := false;
- end;
- procedure TfrmVTrainer.btnGuessClick(Sender: TObject);
- var bolCorrect: boolean;
- begin
- //check if guess is correct
- if rbnGKSYes.Checked then
- if edtGuess.Text = strCorrect then bolCorrect := true
- else bolCorrect := false
- else if rbnGKSNo.Checked then
- if Uppercase(edtGuess.Text) = Uppercase(strCorrect) then bolCorrect := true
- else bolCorrect := false;
- if bolCorrect then
- begin
- //give congrats, handle buttons
- lblWordTwo.Caption := 'Korrekt!';
- lblTries.Caption := IntToStr(StrToInt(edtTries.Text)-intTries)+' Fehlversuche';
- btnNext.Visible := true;
- btnGuess.Visible := false;
- edtTries.Visible := true;
- lblTriesLabel.Visible := true;
- btnDelete.Visible := true;
- end
- else
- begin
- lblWordTwo.Caption := 'Nope, try again';
- intTries := intTries - 1;
- lblTries.Caption := 'Tries: ' + IntToStr(intTries);
- //check if all tries are out
- if intTries = 0 then
- begin
- lblWordTwo.Caption := 'Das Wort wäre '+strCorrect+' gewesen.';
- lblTries.Caption := 'All tries used';
- btnNext.Visible := true;
- btnGuess.Visible := false;
- edtTries.Visible := false;
- lblTriesLabel.Visible := false;
- btnDelete.Visible := true;
- end;
- end;
- end;
- procedure TfrmVTrainer.btnHelpClick(Sender: TObject);
- begin
- ShellExecute(Application.Handle, 'open', PChar('notepad.exe'), PChar('readme.txt'), Nil, SW_NORMAL);
- end;
- procedure TfrmVTrainer.btnNextClick(Sender: TObject);
- var strCurrent: string;
- intPos: integer;
- pwcMsg: PWideChar;
- bolNext: boolean;
- begin
- //run init-procedure if pressed for the first time
- if not bolInit = true then InitStuff;
- //check if random or linear
- if rbnRandom.Checked then
- begin
- intRandom := Random(strListe.Count);
- end
- else if rbnLinear.Checked then
- begin
- //check if all words are through
- if intRandom = strListe.Count-1 then
- begin
- Application.MessageBox('Du hast alle Einträge durchgearbeitet!', 'Bravo!', MB_ICONINFORMATION or MB_OK);
- intRandom := 0;
- end
- else
- begin
- intRandom := intRandom + 1;
- end;
- end;
- //check that file is not empty
- if strListe.Count = 0 then
- begin
- pwcMsg := PWideChar('Keine Einträge in Datei gefunden.'+sLineBreak+'Datei wird bei nächstem Klick auf Next oder Neustart neu eingelesen.');
- bolInit := false;
- Application.MessageBox(pwcMsg, 'Achtung', MB_ICONERROR or MB_OK);
- end;
- //assign words from stringlist
- strCurrent := strListe[intRandom];
- intPos := POS(';', strCurrent);
- //check that entry contains semikolon
- if intPos = 0 then
- begin
- intPos := POS(#9, strCurrent);
- if intPos = 0 then
- begin
- 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.');
- bolInit := false;
- bolNext := false;
- Application.MessageBox(pwcMsg, 'Achtung', MB_ICONERROR or MB_OK);
- end
- else
- begin
- bolNext := true;
- end;
- end
- else
- begin
- bolNext := true;
- end;
- if bolNext then
- begin
- intTries := StrToInt(edtTries.Text);
- lblTries.Caption := 'Tries: ' + IntToStr(intTries);
- edtGuess.Text := '';
- btnNext.Visible := false;
- btnGuess.Visible := true;
- edtTries.Visible := false;
- lblTriesLabel.Visible := false;
- btnDelete.Visible := false;
- end;
- if rbnOne.Checked then
- begin
- lblWordOne.Caption := COPY(strCurrent, 1, intPos-1);
- strCorrect := COPY(strCurrent, intPos+1, strCurrent.Length);
- end
- else if rbnTwo.Checked then
- begin
- strCorrect := COPY(strCurrent, 1, intPos-1);
- lblWordOne.Caption := COPY(strCurrent, intPos+1, strCurrent.Length);
- end;
- end;
- end.
Add Comment
Please, Sign In to add comment