variables defined by changing variables
Author |
Message |
chipanpriest

|
Posted: Thu Oct 25, 2012 6:39 pm Post subject: variables defined by changing variables |
|
|
Hi, I've ran into a problem with my program. I'm not sure if it's even solvable. I am working on remaking hangman. My program chooses a random word from a list of 20 words and has the user guess what it is. Everything works fine, the first time. I want to put my program in a loop so that the user can play multiple games of hangman without having to exit and restart the program. The problem is, I have procedures that require a variable that has a value. I'll show you some of my code:
Turing: |
open : f, "words.txt", get
for i : 1 .. 20
get : f, word (i )
end for
close : f
%--------------------------------------------------------------------------%
e := Rand.Int (1, 20)
var l : int := length (word (e ))
var letter : array 1 .. l of boolean
for i : 1 .. l
letter (i ) := false
end for
|
As you can see, variable l is defined by the length of the word. This is used later in the program to center the word. If I want the user to play multiple times, e would have to randomize in a loop. However, letter has an upper set to the length of the word. Is there a way I can reset the perimeters of that variable every time my program goes through the loop?
That isn't my only problem <insert sarcastic comment about me having problems here>. My program also contains many procedures, and some of these procedure depend on l. For example:
Turing: |
proc letters
for i : 1 .. l
if letter (i ) = true then
Font.Draw (word (e ) (i ), letterx (i ), 55, wordfont, 7)
end if
end for
end letters
|
If I try to put a procedure in a loop, turing yells at me. If I try to randomize e in a loop and set l as the length of word(e), my procedures yell at me about not having a value for the variables inside them.
Like I said, I'm not sure if my problems can be solved, but any help would be greatly appreciated. I added my file, but it may not be much help because the pictures and text file do not come with it, so basically you can read my code but not run it. I know I could have compressed the folder, but I'm feeling a little lazy right now, so tough luck, Chuck.
Description: |
|
 Download |
Filename: |
Rader4.t |
Filesize: |
4.15 KB |
Downloaded: |
109 Time(s) |
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Aange10

|
Posted: Thu Oct 25, 2012 6:44 pm Post subject: RE:variables defined by changing variables |
|
|
Quote: Is there a way I can reset the perimeters of that variable every time my program goes through the loop?
Do you know of flexible arrays?
Other than that, redefining the variable each time should solve the problem. Try having a procedure to do it. For example:
Turing: |
var l : int
var word_list : array 0.. 19 of string
var current_word : string
var letter : flexible array 0 .. 0 of boolean
proc initialize (randomInt : int)
l := randomInt
new letter, upper(l )
current_word := word_list (l )
end initialize
|
|
|
|
|
|
 |
chipanpriest

|
Posted: Thu Oct 25, 2012 7:03 pm Post subject: Re: RE:variables defined by changing variables |
|
|
Aange10 @ Thu Oct 25, 2012 6:44 pm wrote:
Do you know of flexible arrays?
Awesome! Thanks, it works now! I'll be looking forward to sharing this on the site!
|
|
|
|
|
 |
Raknarg

|
Posted: Fri Oct 26, 2012 8:29 am Post subject: RE:variables defined by changing variables |
|
|
Or lists. But that's totally unecessary atm.
|
|
|
|
|
 |
Aange10

|
Posted: Fri Oct 26, 2012 11:08 am Post subject: Re: RE:variables defined by changing variables |
|
|
Raknarg @ 26/10/2012, 7:29 am wrote: Or lists. But that's totally unecessary atm.
There are lists in Turing?
|
|
|
|
|
 |
TerranceN
|
Posted: Fri Oct 26, 2012 8:16 pm Post subject: Re: variables defined by changing variables |
|
|
Yep, but you have to make them yourself using pointers.
Here's a really basic implementation using cons cells.
Turing: |
type cell:
record
head: int
tail: ^cell
end record
% We need an empty value to denote the end of a list
var null: ^cell
new null
% We need a function to construct lists
fcn cons(head: int, tail: ^cell): ^cell
var newCell: ^cell
new newCell
newCell->head := head
newCell->tail := tail
result newCell
end cons
proc printList(lst: ^cell)
% if the list isn't empty print its head and recurse
if (lst ~= null) then
put lst->head
printList(lst->tail)
end if
end printList
var list: ^cell := cons(1, cons(2, cons(3, null)))
printList(list)
|
|
|
|
|
|
 |
Aange10

|
Posted: Sat Oct 27, 2012 12:18 pm Post subject: RE:variables defined by changing variables |
|
|
Oh a linked list. I was thinking more along the lines of like Python's lists.
|
|
|
|
|
 |
|
|