Recent

Author Topic: Problems making a Hangman game in Pascal  (Read 7378 times)

mathex14

  • Newbie
  • Posts: 4
Problems making a Hangman game in Pascal
« on: June 08, 2014, 03:44:07 pm »
Hi! i'm new here....
Here is the deal, im making a hangman game in pascal, but im with some problems, well, i want someone to tell me, how do i do that lines below, i mean like if the word has 5 letters then there would be 5 lines, i have made this using for, but the real problem is, to make a line become a letter, i was capable of making a code write the lines(however im not sure if its possible to change the lines to letters with it), i also made it appear a letter if digited, and if its on the word(if i put "m", when the word "monkey" is the word then "m" will appear, but not in the correct places and the lines continue there).
Here is the part of the code:

ch:=length(animals[vp]);
writeln('');
sentenca:= ' _ ';
for tam:=1 to ch do
begin
write(sentenca);
end;
writeln('');
word:= animals[vp];
read(try);
for tam:=1 to ch do
begin
if (word[tam] = tentativa) then
writeln(word[tam]);
end;
____________

I just dont know how to make the word appear where the line is, or if i should make the lines in a different way.... could someone please help me? maybe give me some tips of what should i use to fix the problem?
Sorry if my english is bad, it's not my first language.

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: Problems making a Hangman game in Pascal
« Reply #1 on: June 08, 2014, 04:08:11 pm »
the easiest way out is to use a string. Then use the fillchar procedure to fill it with the underscore character and use it as an array to replace the underscore with you letter. Easy and fast.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

mathex14

  • Newbie
  • Posts: 4
Re: Problems making a Hangman game in Pascal
« Reply #2 on: June 09, 2014, 03:55:14 am »
the easiest way out is to use a string. Then use the fillchar procedure to fill it with the underscore character and use it as an array to replace the underscore with you letter. Easy and fast.

Thanks for helping me Taazz, but im not very good at pascal yet, and i didnt understood what you said very well, i mean, could you explain a little better, i tried in her search for the fillchar procedure, and i used it and stuff, but i couldnt do what i needed, because i think i dont know the logic to substitute tha line("_") for the correspondent char, so if you could explain me in a more detailed it would be cool, and if someone else could help me as well,  i would appreciate :D

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: Problems making a Hangman game in Pascal
« Reply #3 on: June 09, 2014, 08:59:11 am »
Every string variable can be accessed as an array of characters eg
Code: [Select]
var
  MyVar : String;
begin
  MyVar := 'This is a test';
  MyVar[2] :='_';
  MyVar[3] :='_';
  writeln(MyVar);
end;

So if you have a string filled with underscores you can simple say MyString[12] := 'a'; changing the 12th underscore to a; after that you need to writeln the new data on the screen so the user can see what he got right if anything.
« Last Edit: June 09, 2014, 09:12:32 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

mathex14

  • Newbie
  • Posts: 4
Re: Problems making a Hangman game in Pascal
« Reply #4 on: June 09, 2014, 01:52:45 pm »
Every string variable can be accessed as an array of characters eg
Code: [Select]
var
  MyVar : String;
begin
  MyVar := 'This is a test';
  MyVar[2] :='_';
  MyVar[3] :='_';
  writeln(MyVar);
end;

So if you have a string filled with underscores you can simple say MyString[12] := 'a'; changing the 12th underscore to a; after that you need to writeln the new data on the screen so the user can see what he got right if anything.

I did it! Thanks a lot, one more question, to upgrade the doll(making his arms appear and stuff) and letters, i should use clrscr right?

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: Problems making a Hangman game in Pascal
« Reply #5 on: June 09, 2014, 03:17:54 pm »
If I recall correctly clrscr is part of the crt unit. if you use crt then you can use the gotoxy function and re write everything from X,Y just make sure that the strings you write will erase any left overs as well.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

mathex14

  • Newbie
  • Posts: 4
Re: Problems making a Hangman game in Pascal
« Reply #6 on: June 14, 2014, 08:19:46 pm »
If I recall correctly clrscr is part of the crt unit. if you use crt then you can use the gotoxy function and re write everything from X,Y just make sure that the strings you write will erase any left overs as well.

Thanks! I finished it! I really apreciate your help taazz! Again, sorry if my english is bad, its not my first language, anyway i finally did it, i used gotoxy like you said, and it worked! Thanks a lot! My problem is solved!

 

TinyPortal © 2005-2018