Advertisement
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.ExtCtrls, Vcl.StdCtrls, jpeg, pngimage, StrUtils;
- type
- TfrmGalgenraten = class(TForm)
- imgImage: TImage;
- btnRaten: TButton;
- lblWort: TLabel;
- edtBS: TEdit;
- btnNext: TButton;
- lblW01: TLabel;
- lblW02: TLabel;
- lblW03: TLabel;
- lblW04: TLabel;
- lblW05: TLabel;
- lblW06: TLabel;
- lblW07: TLabel;
- lblW08: TLabel;
- lblW09: TLabel;
- lblW10: TLabel;
- Label1: TLabel;
- lblWins: TLabel;
- Label2: TLabel;
- lblLosses: TLabel;
- procedure Activate; override;
- procedure btnRatenClick(Sender: TObject);
- procedure btnNextClick(Sender: TObject);
- private
- { Private-Deklarationen }
- public
- { Public-Deklarationen }
- end;
- var
- frmGalgenraten: TfrmGalgenraten;
- strListe: TStringList;
- strWort: string;
- lblWrong: array[1..10] of TLabel;
- intWrongs: integer;
- implementation
- {$R *.dfm}
- procedure TfrmGalgenraten.Activate;
- begin
- inherited Activate;
- //loading words from file
- strListe := TStringList.Create;
- strListe.LoadFromFile('words.txt');
- //populating label-list
- lblWrong[1] := frmGalgenraten.lblW01;
- lblWrong[2] := frmGalgenraten.lblW02;
- lblWrong[3] := frmGalgenraten.lblW03;
- lblWrong[4] := frmGalgenraten.lblW04;
- lblWrong[5] := frmGalgenraten.lblW05;
- lblWrong[6] := frmGalgenraten.lblW06;
- lblWrong[7] := frmGalgenraten.lblW07;
- lblWrong[8] := frmGalgenraten.lblW08;
- lblWrong[9] := frmGalgenraten.lblW09;
- lblWrong[10] := frmGalgenraten.lblW10;
- //running first word
- btnNext.Click();
- end;
- procedure TfrmGalgenraten.btnNextClick(Sender: TObject);
- var intIndex: integer;
- begin
- //choosing word
- strWort := Uppercase(strListe[Random(strListe.Count)]);
- //writing encrypted word to label
- lblWort.Caption := '';
- for intIndex := 1 to Length(strWort) do
- begin
- lblWort.Caption := lblWort.Caption + '_ ';
- end;
- //clearing wrong-labels
- for intIndex := 1 to 10 do
- begin
- lblWrong[intIndex].Caption := '';
- end;
- //resetting other
- imgImage.Picture.LoadFromFile('img0.jpg');
- intWrongs := 1;
- edtBS.Text := '';
- edtBS.SetFocus;
- btnRaten.Visible := true;
- btnNext.Visible := false;
- end;
- procedure TfrmGalgenraten.btnRatenClick(Sender: TObject);
- var intIndex: integer;
- strGuessed, strChar: string;
- intPos: array of integer;
- bolOcur: boolean;
- pwcMsg: PWideChar;
- begin
- //gets guessed char
- strChar := (Uppercase(edtBS.Text));
- edtBS.Text := '';
- //gets positions of strChar in lblWort.Caption
- bolOcur := false;
- for intIndex := 1 to Length(strWort) do
- begin
- if strWort[intIndex] = strChar then
- begin
- SetLength(intPos, Length(intPos)+1);
- intPos[High(intPos)] := (intIndex*2)-1;
- bolOcur := true;
- end;
- end;
- //intPos := POS(strChar, strWort);
- //checks if strChar is not empty
- if (strChar = ' ') or (strChar = '') then
- begin
- Application.MessageBox('Kein Buchstabe im Feld', 'Achtung', MB_ICONERROR or MB_OK);
- end
- else
- begin
- //checks if all 10 tries are done
- if intWrongs < 11 then
- begin
- //checks if word contains char
- if bolOcur then
- begin
- //put letter into label
- for intIndex := 0 to Length(intPos)-1 do
- begin
- strGuessed := lblWort.Caption;
- delete(strGuessed, intPos[intIndex], 1);
- insert(strChar, strGuessed, intPos[intIndex]);
- lblWort.Caption := strGuessed;
- end;
- end
- else
- begin
- //cycle through images
- imgImage.Picture.LoadFromFile('img'+IntToStr(intWrongs)+'.jpg');
- //add letter to wrong ones
- lblWrong[intWrongs].Caption := strChar;
- intWrongs := intWrongs + 1;
- end;
- end
- else
- begin
- //lock guessing when 10 tries are out
- //messagebox
- pwcMsg := PWideChar('Sie haben versagt.'+sLineBreak+'Das Wort wäre '+strWort+' gewesen.');
- Application.MessageBox(pwcMsg, 'Naja', MB_ICONINFORMATION or MB_OK);
- lblLosses.Caption := IntToStr(StrToInt(lblLosses.Caption)+1);
- //reset buttons
- btnRaten.Visible := false;
- btnNext.Visible := true;
- end;
- if (not ContainsText(lblWort.Caption, '_')) and (btnRaten.Visible) then
- begin
- //messagebox
- pwcMsg := PWideChar('Sie haben das Wort erraten!'+sLineBreak+'Sie hatten '+IntToStr(intWrongs-1)+' Fehlversuche.');
- Application.MessageBox(pwcMsg, 'Glückwunsch', MB_ICONINFORMATION or MB_OK);
- lblWins.Caption := IntToStr(StrToInt(lblWins.Caption)+1);
- //reset buttons
- btnNext.Visible := true;
- btnRaten.Visible := false;
- end;
- end;
- edtBS.SetFocus;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement