hidrau
asked on
I need some help to complite my game word with richtext
Hello Guys.
I really need a help from you.
I am trying to make a scrambled word game and there are some things that I would like to do
and I don't have any ideia how to do. Some things I got but it isn't work so fine so far. If you could
check and give your opinion , it will be well acceptable.
Well, Let me explain the game:
I have two RxRichEdit one of them is gonna have the scrambled phrase and the other one is gonna have the
correct phrase. So, you have a scrambled word like that in the first rxrichedit:
Mother working father today but home at me with is my my.
The student is gonna listen the correct phrase and must put all the scrambled word on the correct way, see an
example:
My father is working today but my mother is at home with me
Well, How it works:
In the first rxrichedit you must click on a word, when the student makes a doble clicked,
the function will get the word and will put it in the second rxrichedit, creating the correct phrase
listened. Sometimes he/she can make any mistake and the word must come back to the first rxrichedit, taking off
the word from the second rxrichedit. After all finish the student will correct the phrase.
Well, this step was done until here, sometimes it didn't work fine and I haven't figured out why so far, so,
I would like you to take a look and help me.
Second: There is another thing that must be done and I don't have any ideia how to make it, it is.
Imagine this phrase:
"work here is a professional good she"
"She can because"
As you can see, it is missing a word between because and can that is WORK, well, this work must be inserted this
way: You have to click on the word from first richedit, this way, you will get it, with your mouse you have to
insert the word between the two words that is can and because.
It is not be a good solution working only with double click because if you have a long phrase and something missed
almost at the beginning, you would have to take off all the words until the correct word to start imputing all the
words again, so with the you have a good save time.
That's all I need to do
I thanks in advance
Alexandre
Follow below the code was make until now .
**************************
Function VisibleLines(RE: TRxRichEdit): integer;
Var
Oldfont:HFont;
DC :THandle;
TM :TTextMetric;
TheRect:TRect;
begin
DC := GetDC(RE.Handle);
try
OldFont := SelectObject(DC, RE.Font.Handle);
try
GetTextMetrics(DC, TM);
RE.Perform(EM_GETRECT, 0, longint(@TheRect));
Result:=(TheRect.Bottom - TheRect.Top) div (TM.tmHeight + TM.tmExternalLeading);
finally
SelectObject(DC, Oldfont);
end;
finally
ReleaseDC(RE.Handle, DC)
end
end;
procedure TFSCRAMBLEDWORD.Button1Cli
var
NoLines, X:Integer;
begin
RodaBaseDados;
Edit1.Text :=StringReplace(TQ.FieldBy
RxRichEdit1.Lines.Clear;
RxRichEdit2.Lines.Clear;
RxRichEdit1.Lines.Add(Emba
NoLines:=VisibleLines(RxRi
for X:=1 to NoLines do RxRichEdit1.Lines.Insert(0
end;
procedure TFSCRAMBLEDWORD.RodaBaseDa
begin
With TQ do
Begin
Close;
Sql.Clear;
Sql.Add('SELECT CP_CODIGO, CP_FRASE_US FROM CADPAL');
Sql.Add('ORDER BY RND(CP_CODIGO)');
Open;
End;
end;
procedure TFSCRAMBLEDWORD.RxRichEdit
Var
Pos : Integer;
NoLines, X:Integer;
Txt, Str : String;
begin
Txt := Trim(RxRichEdit2.Text);
Txt := StringReplace(txt,chr(10),
Txt := StringReplace(txt,chr(13),
Txt := TrimLeft(Txt);
Txt := txt + ' ' + Trim(RxRichEdit1.SelText);
RxRichEdit2.Lines.Clear;
RxRichEdit2.Lines.Append(T
NoLines:=VisibleLines(RxRi
For X:=1 to NoLines do RxRichEdit2.Lines.Insert(0
str := RxRichEdit1.Text;
Str := StringReplace(Str,chr(10),
Str := StringReplace(Str,chr(13),
Str := TrimLeft(Str);
// Str := TrimRight(Str);
Txt := RxRichEdit1.SelText;
Txt := StringReplace(Str, txt,'',[]);
RxRichEdit1.Lines.Clear;
RxRichEdit1.Lines.Append(T
NoLines:=VisibleLines(RxRi
for X:=1 to NoLines do RxRichEdit1.Lines.Insert(0
End;
Function TFSCRAMBLEDWORD.EmbaralhaF
var
lr : integer;
lS : TStringlist;
lout : string;
begin
Randomize;
ls:=TStringlist.create;
ls.commatext:= Str;
While ls.count>0 do
Begin
lr:=Random(ls.count);
lout:=lout+ ' '+ls[lr];
ls.Delete(lr);
End;
Result := lout;
freeandNil(ls);
End;
procedure TFSCRAMBLEDWORD.Button2Cli
Var
Txt : String;
begin
Txt := RxRichEdit2.Text;
Txt := StringReplace(txt,chr(10),
Txt := StringReplace(txt,chr(13),
If TrimLeft(Txt) = Edit1.Text Then ShowMessage('OK')
end;
procedure TFSCRAMBLEDWORD.RxRichEdit
Var
Pos : Integer;
NoLines, X:Integer;
Txt, Str : String;
begin
Txt := Trim(RxRichEdit1.Text);
Txt := StringReplace(txt,chr(10),
Txt := StringReplace(txt,chr(13),
Txt := TrimLeft(Txt);
Txt := txt + ' ' + Trim(RxRichEdit2.SelText);
RxRichEdit1.Lines.Clear;
RxRichEdit1.Lines.Append(T
NoLines:=VisibleLines(RxRi
For X:=1 to NoLines do RxRichEdit1.Lines.Insert(0
str := RxRichEdit2.Text;
Str := StringReplace(Str,chr(10),
Str := StringReplace(Str,chr(13),
Str := TrimLeft(Str);
// Str := TrimRight(Str);
Txt := RxRichEdit2.SelText;
Txt := StringReplace(Str, txt,'',[]);
RxRichEdit2.Lines.Clear;
RxRichEdit2.Lines.Append(T
NoLines:=VisibleLines(RxRi
for X:=1 to NoLines do RxRichEdit2.Lines.Insert(0
ASKER
Hello pcsentinel
Thanks for taking a look at my problem, but I don't know if you read the lines in my first post. I need to work with a mouse.
( As you can see, it is missing a word between because and can that is WORK, well, this work must be inserted this
way: You have to click on the word from first richedit, this way, you will get it, with your mouse you have to
insert the word between the two words that is can and because.
It is not be a good solution working only with double click because if you have a long phrase and something missed
almost at the beginning, you would have to take off all the words until the correct word to start imputing all the
words again, so with the you have a good save time. )
I thanks in advance
Alexandre
Thanks for taking a look at my problem, but I don't know if you read the lines in my first post. I need to work with a mouse.
( As you can see, it is missing a word between because and can that is WORK, well, this work must be inserted this
way: You have to click on the word from first richedit, this way, you will get it, with your mouse you have to
insert the word between the two words that is can and because.
It is not be a good solution working only with double click because if you have a long phrase and something missed
almost at the beginning, you would have to take off all the words until the correct word to start imputing all the
words again, so with the you have a good save time. )
I thanks in advance
Alexandre
Ok try this
the user can reposition then cursor in the second edit box and that is where the word from the first box is inserted. If the user clicks within a word in the second box then this is invalid and the cursor moves to the end.
*********************UNIT CODE********************** ***
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdFTP, Word97, OleServer, ExtCtrls, IdAntiFreezeBase, IdAntiFreeze;
type
TForm1 = class(TForm)
Edit1: TEdit;
lblSentence: TLabel;
Edit2: TEdit;
Button1: TButton;
lblDone: TLabel;
procedure Edit1DblClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Edit2DblClick(Sender: TObject);
procedure Edit2Click(Sender: TObject);
procedure Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
FStore: TStringlist;
procedure UpdateEdit1;
function Scramble(Str: String): String;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function TForm1.Scramble(Str: String): String;
var
lr : integer;
lS : TStringlist;
lout : string;
begin
Randomize;
ls:=TStringlist.create;
ls.commatext:= Str;
while ls.count>0 do
begin
lr:=Random(ls.count);
lout:=lout+ ' '+ls[lr];
ls.Delete(lr);
end;
Result := lout;
freeandNil(ls);
end;
procedure TForm1.UpdateEdit1;
var
li: integer;
begin
Edit1.Text:='';
for li:=0 to pred(FStore.Count) do
begin
if Integer(FStore.Objects[li] )=0 then
begin
if li>0 then
Edit1.Text:=Edit1.Text+' ';
Edit1.Text:=Edit1.Text+FSt ore[li];
end;
end;
Edit1.Text:=Trim(Edit1.Tex t);
end;
procedure TForm1.Edit1DblClick(Sende r: TObject);
begin
edit2.Seltext:=Trim(edit1. seltext)+' ';
edit2.Text:=Trim(edit2.Tex t)+' ';
edit2.SelStart:=Length(Edi t2.Text);
FStore.Objects[FStore.Inde xOf(Trim(e dit1.selte xt))]:=poi nter(1);
UpdateEdit1;
if Trim(Edit2.Text)=Trim(lblS entence.Ca ption) then
lblDone.Caption:='Correct'
else
lblDone.Caption:='InCorrec t';
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FStore:=TStringlist.Create ;
end;
procedure TForm1.Button1Click(Sender : TObject);
var
li: integer;
begin
Edit1.Text:='';
FStore.CommaText:=Scramble (lblSenten ce.Caption );
for li:=0 to pred(FStore.Count) do
FStore.Objects[li]:=pointe r(0);
UpdateEdit1;
end;
procedure TForm1.Edit2DblClick(Sende r: TObject);
begin
if edit2.sellength>0 then
begin
FStore.Objects[FStore.Inde xOf(Trim(e dit2.selte xt))]:=poi nter(0);
Edit2.SelText:='';
UpdateEdit1;
end;
Edit2.Text:=Trim(Edit2.Tex t)+' ';
edit2.SelStart:=Length(Edi t2.Text);
if Trim(Edit2.Text)=Trim(lblS entence.Ca ption) then
lblDone.Caption:='Correct'
else
lblDone.Caption:='InCorrec t';
end;
procedure TForm1.Edit2Click(Sender: TObject);
begin
//first check
if edit2.SelStart>0 then
begin
if (copy(edit2.text,edit2.sel start,1)<> ' ') and (copy(edit2.text,edit2.sel start+1,1) <>' ')then
edit2.selstart:=length(edi t2.text);
end;
//adjust insert pos
if (edit2.SelStart>0) and (copy(edit2.text,edit2.sel start,1)<> ' ') then
edit2.selstart:=edit2.sels tart+1;
//recheck
if edit2.SelStart>0 then
begin
if (copy(edit2.text,edit2.sel start,1)<> ' ') and (copy(edit2.text,edit2.sel start+1,1) <>' ')then
edit2.selstart:=length(edi t2.text);
end;
end;
procedure TForm1.Edit2KeyDown(Sender : TObject; var Key: Word;
Shift: TShiftState);
begin
Key:=0;
end;
end.
************************** ********FO RM CODE********************** ***
object Form1: TForm1
Left = 111
Top = 169
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object lblSentence: TLabel
Left = 144
Top = 72
Width = 83
Height = 13
Caption = 'A day on the farm'
end
object lblDone: TLabel
Left = 56
Top = 256
Width = 42
Height = 13
Caption = 'Incorrect'
end
object Edit1: TEdit
Left = 52
Top = 76
Width = 225
Height = 21
TabOrder = 0
OnDblClick = Edit1DblClick
end
object Edit2: TEdit
Left = 48
Top = 208
Width = 229
Height = 21
TabOrder = 1
OnClick = Edit2Click
OnDblClick = Edit2DblClick
OnKeyDown = Edit2KeyDown
end
object Button1: TButton
Left = 412
Top = 68
Width = 75
Height = 25
Caption = 'Load'
TabOrder = 2
OnClick = Button1Click
end
end
regards
the user can reposition then cursor in the second edit box and that is where the word from the first box is inserted. If the user clicks within a word in the second box then this is invalid and the cursor moves to the end.
*********************UNIT CODE**********************
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdFTP, Word97, OleServer, ExtCtrls, IdAntiFreezeBase, IdAntiFreeze;
type
TForm1 = class(TForm)
Edit1: TEdit;
lblSentence: TLabel;
Edit2: TEdit;
Button1: TButton;
lblDone: TLabel;
procedure Edit1DblClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Edit2DblClick(Sender: TObject);
procedure Edit2Click(Sender: TObject);
procedure Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
FStore: TStringlist;
procedure UpdateEdit1;
function Scramble(Str: String): String;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function TForm1.Scramble(Str: String): String;
var
lr : integer;
lS : TStringlist;
lout : string;
begin
Randomize;
ls:=TStringlist.create;
ls.commatext:= Str;
while ls.count>0 do
begin
lr:=Random(ls.count);
lout:=lout+ ' '+ls[lr];
ls.Delete(lr);
end;
Result := lout;
freeandNil(ls);
end;
procedure TForm1.UpdateEdit1;
var
li: integer;
begin
Edit1.Text:='';
for li:=0 to pred(FStore.Count) do
begin
if Integer(FStore.Objects[li]
begin
if li>0 then
Edit1.Text:=Edit1.Text+' ';
Edit1.Text:=Edit1.Text+FSt
end;
end;
Edit1.Text:=Trim(Edit1.Tex
end;
procedure TForm1.Edit1DblClick(Sende
begin
edit2.Seltext:=Trim(edit1.
edit2.Text:=Trim(edit2.Tex
edit2.SelStart:=Length(Edi
FStore.Objects[FStore.Inde
UpdateEdit1;
if Trim(Edit2.Text)=Trim(lblS
lblDone.Caption:='Correct'
else
lblDone.Caption:='InCorrec
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FStore:=TStringlist.Create
end;
procedure TForm1.Button1Click(Sender
var
li: integer;
begin
Edit1.Text:='';
FStore.CommaText:=Scramble
for li:=0 to pred(FStore.Count) do
FStore.Objects[li]:=pointe
UpdateEdit1;
end;
procedure TForm1.Edit2DblClick(Sende
begin
if edit2.sellength>0 then
begin
FStore.Objects[FStore.Inde
Edit2.SelText:='';
UpdateEdit1;
end;
Edit2.Text:=Trim(Edit2.Tex
edit2.SelStart:=Length(Edi
if Trim(Edit2.Text)=Trim(lblS
lblDone.Caption:='Correct'
else
lblDone.Caption:='InCorrec
end;
procedure TForm1.Edit2Click(Sender: TObject);
begin
//first check
if edit2.SelStart>0 then
begin
if (copy(edit2.text,edit2.sel
edit2.selstart:=length(edi
end;
//adjust insert pos
if (edit2.SelStart>0) and (copy(edit2.text,edit2.sel
edit2.selstart:=edit2.sels
//recheck
if edit2.SelStart>0 then
begin
if (copy(edit2.text,edit2.sel
edit2.selstart:=length(edi
end;
end;
procedure TForm1.Edit2KeyDown(Sender
Shift: TShiftState);
begin
Key:=0;
end;
end.
**************************
object Form1: TForm1
Left = 111
Top = 169
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object lblSentence: TLabel
Left = 144
Top = 72
Width = 83
Height = 13
Caption = 'A day on the farm'
end
object lblDone: TLabel
Left = 56
Top = 256
Width = 42
Height = 13
Caption = 'Incorrect'
end
object Edit1: TEdit
Left = 52
Top = 76
Width = 225
Height = 21
TabOrder = 0
OnDblClick = Edit1DblClick
end
object Edit2: TEdit
Left = 48
Top = 208
Width = 229
Height = 21
TabOrder = 1
OnClick = Edit2Click
OnDblClick = Edit2DblClick
OnKeyDown = Edit2KeyDown
end
object Button1: TButton
Left = 412
Top = 68
Width = 75
Height = 25
Caption = 'Load'
TabOrder = 2
OnClick = Button1Click
end
end
regards
ASKER
Hi pcsentinel
I did understand what you have made, but I think we aren't talking about the same thing, see if you understood what I need.
FIRS EDIT1
is working here working and there my sister
SECOND EDIT2
She is here with my father.
She is _______ here with my father
The user forgot putting the word "working" in that blank space,(of course, there will not be any blank space, it is an example) , if the user click on the word "working" this word is gonna be placed at the last word, because of that I need to get the word with mouse and drag it at the right position that is between is and here.
Thanks
Alexandre
I did understand what you have made, but I think we aren't talking about the same thing, see if you understood what I need.
FIRS EDIT1
is working here working and there my sister
SECOND EDIT2
She is here with my father.
She is _______ here with my father
The user forgot putting the word "working" in that blank space,(of course, there will not be any blank space, it is an example) , if the user click on the word "working" this word is gonna be placed at the last word, because of that I need to get the word with mouse and drag it at the right position that is between is and here.
Thanks
Alexandre
hello hidrau, , I think that I understand what you want, a way to "Mouse Drag" a seleted word from one RichEdit to another richEdit. . .
I looked at your code and I have some questions about it
1. . Why are you using the RxRichEdit component, does it have some special property that you need ? ? (I do not see any in your code) .
and I am NOT familar with the RxRichEdit. .
2. Why do you need the - VisibleLines(RE: TRxRichEdit): function ? ? and why do you have these lines of code -
NoLines:=VisibleLines(RxRi chEdit1) div 2;
for X:=1 to NoLines do RxRichEdit1.Lines.Insert(0 , ' ');
3. Did you know that a TRichEdit has the ability to drag the selected Text to another Rich Edit
and it will drop the text in to that Rich Edit where ever you release the mouse?
I looked at your code and I have some questions about it
1. . Why are you using the RxRichEdit component, does it have some special property that you need ? ? (I do not see any in your code) .
and I am NOT familar with the RxRichEdit. .
2. Why do you need the - VisibleLines(RE: TRxRichEdit): function ? ? and why do you have these lines of code -
NoLines:=VisibleLines(RxRi
for X:=1 to NoLines do RxRichEdit1.Lines.Insert(0
3. Did you know that a TRichEdit has the ability to drag the selected Text to another Rich Edit
and it will drop the text in to that Rich Edit where ever you release the mouse?
ASKER
Hello slick812
Ok, let me explain.
1) I use the rxrichedit or richedit because I need to make other things that I didn't relate here. But the example could be with edit, after that I will make the changes to richedit.
2) Visiblelines is a function only to align the text in richedit vertically. It counts how many lines there is in the richedit and put the sentence in the middle of it.
3) I know that I can select the text, but how could I transfer the selected text to other richedit at the right place, I don't know.
This is the problem, pcsentinel has helped me a lot, he gives me an example using edits.
Thanks
Alexandre
Ok, let me explain.
1) I use the rxrichedit or richedit because I need to make other things that I didn't relate here. But the example could be with edit, after that I will make the changes to richedit.
2) Visiblelines is a function only to align the text in richedit vertically. It counts how many lines there is in the richedit and put the sentence in the middle of it.
3) I know that I can select the text, but how could I transfer the selected text to other richedit at the right place, I don't know.
This is the problem, pcsentinel has helped me a lot, he gives me an example using edits.
Thanks
Alexandre
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I had much better success if I used two TStringGrids for this "Game" , instead of two TRichEdits
private
{ Private declarations }
aryWords: Array of String;
PlayGame: Boolean;
procedure SetGrids(const Text: String);
function WordsCorrect: Boolean;
procedure TForm1.SetGrids(const Text: String);
var
hold: String;
i, j, tw, maxWidth: Integer;
pStart, pNext: PChar;
aryWd: Array of String;
begin
if Length(Text) < 3 then Exit;
hold := Text+' ';
tw := Length(hold)+1;
i := 1;
while i < tw do
if hold[i] = ' ' then
Inc(i) else Break;
if i = tw then Exit;
SetLength(aryWords, 0);
Dec(i);
pStart := PChar(hold)+i;
pNext := StrScan(pStart,' ');
while pNext <> nil do
begin
if pNext <> pStart then
begin
SetLength(aryWords, Length(aryWords)+1);
SetString(aryWords[High(ar yWords)],p Start, pNext-pStart);
end;
pStart := pNext+1;
pNext := StrScan(pStart,' ');
end;
if Length(aryWords) < 2 then
begin
SetLength(aryWords, 0);
Exit;
end;
setLength(aryWd, Length(aryWords));
for i := 0 to High(aryWords) do
aryWd[i] := aryWords[i];
Randomize;
for i := 0 to High(aryWords) do
begin
hold := aryWd[i];
j := Random(High(aryWd));
aryWd[i] := aryWd[j];
aryWd[j] := hold;
end;
StringGrid1.ColCount := Length(aryWd);
StringGrid1.RowCount := 1;
StringGrid2.ColCount := StringGrid1.ColCount;
StringGrid2.RowCount := 1;
maxWidth :=6;
for i := 0 to High(aryWd) do
begin
tw := StringGrid1.Canvas.TextWid th(aryWd[i ]);
if tw > maxWidth then
maxWidth := tw;
StringGrid1.Cells[i, 0] := aryWd[i];
StringGrid2.Cells[i, 0] := '';
end;
StringGrid1.DefaultColWidt h := maxWidth+6;
StringGrid2.DefaultColWidt h := maxWidth+6;
StringGrid1.Col := 0;
StringGrid1.Row := 0;
StringGrid2.Col := 0;
StringGrid2.Row := 0;
PlayGame := True;
end;
procedure TForm1.sbut_NewWordsClick( Sender: TObject);
begin
SetGrids('Please try and place these words into the correct order');
end;
procedure TForm1.StringGrid1MouseDow n(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
if PlayGame then
StringGrid1.BeginDrag(Fals e,4);
end;
procedure TForm1.StringGrid1StartDra g(Sender: TObject;
var DragObject: TDragObject);
begin
DragObject := nil;
end;
procedure TForm1.StringGrid2DragOver (Sender, Source: TObject; X,
Y: Integer; State: TDragState; var Accept: Boolean);
begin
if Source = StringGrid1 then Accept := True;
end;
function TForm1.WordsCorrect: Boolean;
var
i: Integer;
begin
Result := False;
for i := 0 to High(aryWords) do
if aryWords[i] <> StringGrid2.Cells[i,0] then Exit;
PlayGame := False;
Result := True;
ShowMessage('GOOD WORK ! ! All the words are correct');
end;
procedure TForm1.StringGrid2DragDrop (Sender, Source: TObject; X,
Y: Integer);
var
aRow, aCol: Integer;
begin
if Source = StringGrid1 then
begin
Y := 6;
if X > StringGrid2.CellRect(Strin gGrid2.Col Count-1, 0).Right then
X := StringGrid2.CellRect(Strin gGrid2.Col Count-1, 0).Right;
StringGrid2.MouseToCell(X, Y,aCol,aRo w);
if aCol < 0 then Exit;
StringGrid2.Cells[aCol,aRo w] := StringGrid1.Cells[StringGr id1.Col, StringGrid1.Row];
WordsCorrect;
end;
end;
procedure TForm1.StringGrid1DblClick (Sender: TObject);
var
i: Integer;
begin
if not PlayGame then Exit;
for i := 0 to StringGrid2.ColCount-1 do
if StringGrid2.Cells[i, 0] = '' then
begin
StringGrid2.Cells[i, 0] := StringGrid1.Cells[StringGr id1.Col, StringGrid1.Row];
WordsCorrect;
break;
end;
end;
private
{ Private declarations }
aryWords: Array of String;
PlayGame: Boolean;
procedure SetGrids(const Text: String);
function WordsCorrect: Boolean;
procedure TForm1.SetGrids(const Text: String);
var
hold: String;
i, j, tw, maxWidth: Integer;
pStart, pNext: PChar;
aryWd: Array of String;
begin
if Length(Text) < 3 then Exit;
hold := Text+' ';
tw := Length(hold)+1;
i := 1;
while i < tw do
if hold[i] = ' ' then
Inc(i) else Break;
if i = tw then Exit;
SetLength(aryWords, 0);
Dec(i);
pStart := PChar(hold)+i;
pNext := StrScan(pStart,' ');
while pNext <> nil do
begin
if pNext <> pStart then
begin
SetLength(aryWords, Length(aryWords)+1);
SetString(aryWords[High(ar
end;
pStart := pNext+1;
pNext := StrScan(pStart,' ');
end;
if Length(aryWords) < 2 then
begin
SetLength(aryWords, 0);
Exit;
end;
setLength(aryWd, Length(aryWords));
for i := 0 to High(aryWords) do
aryWd[i] := aryWords[i];
Randomize;
for i := 0 to High(aryWords) do
begin
hold := aryWd[i];
j := Random(High(aryWd));
aryWd[i] := aryWd[j];
aryWd[j] := hold;
end;
StringGrid1.ColCount := Length(aryWd);
StringGrid1.RowCount := 1;
StringGrid2.ColCount := StringGrid1.ColCount;
StringGrid2.RowCount := 1;
maxWidth :=6;
for i := 0 to High(aryWd) do
begin
tw := StringGrid1.Canvas.TextWid
if tw > maxWidth then
maxWidth := tw;
StringGrid1.Cells[i, 0] := aryWd[i];
StringGrid2.Cells[i, 0] := '';
end;
StringGrid1.DefaultColWidt
StringGrid2.DefaultColWidt
StringGrid1.Col := 0;
StringGrid1.Row := 0;
StringGrid2.Col := 0;
StringGrid2.Row := 0;
PlayGame := True;
end;
procedure TForm1.sbut_NewWordsClick(
begin
SetGrids('Please try and place these words into the correct order');
end;
procedure TForm1.StringGrid1MouseDow
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
if PlayGame then
StringGrid1.BeginDrag(Fals
end;
procedure TForm1.StringGrid1StartDra
var DragObject: TDragObject);
begin
DragObject := nil;
end;
procedure TForm1.StringGrid2DragOver
Y: Integer; State: TDragState; var Accept: Boolean);
begin
if Source = StringGrid1 then Accept := True;
end;
function TForm1.WordsCorrect: Boolean;
var
i: Integer;
begin
Result := False;
for i := 0 to High(aryWords) do
if aryWords[i] <> StringGrid2.Cells[i,0] then Exit;
PlayGame := False;
Result := True;
ShowMessage('GOOD WORK ! ! All the words are correct');
end;
procedure TForm1.StringGrid2DragDrop
Y: Integer);
var
aRow, aCol: Integer;
begin
if Source = StringGrid1 then
begin
Y := 6;
if X > StringGrid2.CellRect(Strin
X := StringGrid2.CellRect(Strin
StringGrid2.MouseToCell(X,
if aCol < 0 then Exit;
StringGrid2.Cells[aCol,aRo
WordsCorrect;
end;
end;
procedure TForm1.StringGrid1DblClick
var
i: Integer;
begin
if not PlayGame then Exit;
for i := 0 to StringGrid2.ColCount-1 do
if StringGrid2.Cells[i, 0] = '' then
begin
StringGrid2.Cells[i, 0] := StringGrid1.Cells[StringGr
WordsCorrect;
break;
end;
end;
ASKER
Thanks Friend, I am gonna check this option.
*******************UNIT CODE**********************
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
lblSentence: TLabel;
Edit2: TEdit;
Button1: TButton;
lblDone: TLabel;
procedure Edit1DblClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Edit2DblClick(Sender: TObject);
private
FStore: TStringlist;
procedure UpdateEdit1;
function Scramble(Str: String): String;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function TForm1.Scramble(Str: String): String;
var
lr : integer;
lS : TStringlist;
lout : string;
begin
Randomize;
ls:=TStringlist.create;
ls.commatext:= Str;
while ls.count>0 do
begin
lr:=Random(ls.count);
lout:=lout+ ' '+ls[lr];
ls.Delete(lr);
end;
Result := lout;
freeandNil(ls);
end;
procedure TForm1.UpdateEdit1;
var
li: integer;
begin
Edit1.Text:='';
for li:=0 to pred(FStore.Count) do
begin
if Integer(FStore.Objects[li]
begin
if li>0 then
Edit1.Text:=Edit1.Text+' ';
Edit1.Text:=Edit1.Text+FSt
end;
end;
Edit1.Text:=Trim(Edit1.Tex
end;
procedure TForm1.Edit1DblClick(Sende
begin
if edit1.sellength>0 then
begin
if Edit2.Text<>'' then
Edit2.Text:=Edit2.Text+' ';
edit2.text:=edit2.text+Tri
FStore.Objects[FStore.Inde
UpdateEdit1;
end;
if Trim(Edit2.Text)=Trim(lblS
lblDone.Caption:='Correct'
else
lblDone.Caption:='InCorrec
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FStore:=TStringlist.Create
end;
procedure TForm1.Button1Click(Sender
var
li: integer;
begin
Edit1.Text:='';
FStore.CommaText:=Scramble
for li:=0 to pred(FStore.Count) do
FStore.Objects[li]:=pointe
UpdateEdit1;
end;
procedure TForm1.Edit2DblClick(Sende
begin
if edit2.sellength>0 then
begin
FStore.Objects[FStore.Inde
Edit2.SelText:='';
UpdateEdit1;
end;
Edit2.Text:=Trim(Edit2.Tex
if Trim(Edit2.Text)=Trim(lblS
lblDone.Caption:='Correct'
else
lblDone.Caption:='InCorrec
end;
end.
************************FO
object Form1: TForm1
Left = 1273
Top = 106
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object lblSentence: TLabel
Left = 144
Top = 72
Width = 83
Height = 13
Caption = 'A day on the farm'
end
object lblDone: TLabel
Left = 56
Top = 256
Width = 42
Height = 13
Caption = 'Incorrect'
end
object Edit1: TEdit
Left = 52
Top = 128
Width = 225
Height = 21
TabOrder = 0
OnDblClick = Edit1DblClick
end
object Edit2: TEdit
Left = 48
Top = 208
Width = 229
Height = 21
TabOrder = 1
OnDblClick = Edit2DblClick
end
object Button1: TButton
Left = 412
Top = 68
Width = 75
Height = 25
Caption = 'Load'
TabOrder = 2
OnClick = Button1Click
end
end
hope this helps
regards