I am attempting to write a Hangman game using OOP. I have it done using procedural programming. I am not looking to make it a two person game yet. What Objects and methods would you all use?
I was thinking
newWord = class(tObject)
word : string,
definition : string,
typeOfSpeach : string,
function setWords : wordString;
function setDefination : wordString // need 4 other definations, plus the correct one
function setTypeOfSpeach;
It will be getting words and definitions form a 5000 text file.
I am also having issues with the same letter being able to be pressed, I suspect that once I am using objects that will clear up.
Here is what I have that works as long as I only type a correct letter once..
I also would like to get away from using global variables
unit main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls,
Vcl.Imaging.jpeg, strutils;
type
Thangman = class(TForm)
Button1: TButton;
RadioGroup1: TRadioGroup;
Panel1: TPanel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure RadioGroup1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
aLabel : TLabel;
hangman: Thangman;
wordList : tStringList;
definitionList : tStringList;
partsOfSpeachList : tStringList;
wordANDdefinition : tStringList;
partsOfSpeach : string;
word : string;
definition : string;
pressed : byte;
lettersGuessed : array of String;
correctDefinition : string;
Q : byte;
J : byte;
implementation
{$R *.dfm}
procedure deleteDashes();
var
i: Integer;
begin
hangman.RadioGroup1.Items.Clear;
for i := hangman.ComponentCount - 1 downto 0 do
begin
if hangman.Components[i] is tLabel then
begin
hangman.Components[i].Free;
end;
end;
end;
procedure createDashes();
var
// q : byte;
x : byte;
// xx : byte;
begin
// xx := 0;
for X := 1 to length(word) do
begin
aLabel := TLabel.Create(hangman);
aLabel.Name := 'letter_'+IntToStr(X);
aLabel.Parent := hangman;
aLabel.Visible := True;
if word[X] = '-' then
begin
aLabel.Caption := '-';//IntToStr(X);
inc(Q);
end
else
begin
aLabel.Caption := '_';//IntToStr(X);
end;
alabel.Font.Size := 11;
alabel.Width := 16;
aLabel.Height := 30;
aLabel.Top := 10;
aLabel.Left := 16*X+((hangman.clientwidth div 2)-(word.Length*11));
alabel.Transparent := false;
aLabel.Alignment := taRightJustify;
end;
end;
function getWords() : integer;
var
wordNumber : integer;
I : byte;
//defs : array[0..4] of string;
cnt : byte;
randomIndex : byte;
defs : tStringList;
// wordLength : byte;
begin
Q := 0;
J := 0;
defs := tStringList.Create;
//get word, definition, and parts of speach
wordNumber := random(wordList.Count-1);
word := wordList[wordNumber];
definition := definitionList[wordNumber];
partsOfSpeach := partsOfSpeachList[wordNumber];
// wordLength := word.Length;
hangman.Caption := 'The word is '+IntToStr(word.Length)+ ' letters long '+
'and is a'+partsOfSpeach;
// ShowMessage(word);
result := wordNumber;
correctDefinition := definition;
// showMessage(correctDefinition+' '+definition);
defs.Add(definition); //correct definition
for I := 0 to 3 do
begin
defs.Add(definitionList[random(wordList.Count-1)]);
end;
for cnt := 0 to -1 + defs.count do
begin
randomIndex := random(-cnt + defs.Count);
defs.Exchange(cnt, cnt + randomIndex);
end;
for cnt := 0 to defs.Count -1 do
begin
hangman.RadioGroup1.Items.Add(defs[cnt]);
end;
// showMessage(word);
// hangman.RadioGroup1.Items.Count
end;
procedure Thangman.Button1Click(Sender: TObject);
begin
pressed := 0;
deleteDashes();
getWords();
createDashes();
end;
procedure Thangman.FormCreate(Sender: TObject);
var
firstSplit : integer;
secondSplit : integer;
I : integer;
begin
KeyPreview := True;
wordANDdefinition := tStringList.Create;
definitionList := tStringList.Create;
wordList := tStringList.create;
partsOfSpeachList := tStringList.Create;
wordANDdefinition.LoadFromFile(getcurrentdir+'\newwordlist.txt');
for I := 0 to wordANDdefinition.Count-1 do
begin
firstSplit := pos('|', wordANDdefinition[I]);
secondSplit := pos('^', wordANDdefinition[I]);
word := wordANDdefinition[I];
definition := wordANDdefinition[I];
partsOfSpeach := wordANDdefinition[I];
delete(word,firstSplit,length(word)-firstSplit+1);
definition := copy(definition,secondSplit+1,definition.Length);
partsOfSpeach := copy(partsOfSpeach,firstSplit+1,(secondSplit-1)-firstSplit);
wordList.Add(word);
definitionList.Add(definition);
partsOfSpeachList.Add(partsOfSpeach);
end;
word := wordList[getWords()];
createDashes();
wordANDdefinition.Clear();
end;
procedure Thangman.FormKeyPress(Sender: TObject; var Key: Char);
var
I : byte;
begin
if CharinSet(Key,['A'..'Z', 'a'..'z']) then
begin
for I := 1 to length(word) do
begin
if word[I] = lowerCase(Key) then
begin
inc(Q);
TLabel(Components[I+2]).Caption := UpCase(Key);
end
end;
// exclude(key);
end
else
begin
showMessage('Please only use letters A-Z');
end;
if Q = length(word) then
begin
KeyPreview := False;
RadioGroup1.Visible := true;
end;
Panel1.Caption := UpCase(Key)+' '+Panel1.Caption;
// showmessage(IntToStr(J)+' '+IntToStr(length(lettersGuessed)));
end;
procedure Thangman.RadioGroup1Click(Sender: TObject);
begin
if radiogroup1.Items[radiogroup1.ItemIndex] = correctDefinition then
begin
ShowMessage('Correct');
KeyPreview := true;
RadioGroup1.Visible := false;
button1.Click;
Panel1.Caption := ' ';
end else
begin
ShowMessage('Try again');
end;
end;
end.