How can I get a variable using a variable?
Author |
Message |
Linksku
|
Posted: Tue May 31, 2011 3:56 pm Post subject: How can I get a variable using a variable? |
|
|
What is it you are trying to achieve?
I have a variable amount of variables. The variable are all flexible arrays. I wanted to use multidimensional arrays, but the second dimension isn't resizable. It's too hard to explain here, check the code.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
Turing: |
% What I tried doing, but doesn't work
% var playerx : flexible array 1 .. numplayers, 1 .. 1 of int
% ...
% new playerx, numplayers, turn
% What I'm trying to do
for i:1..numplayers
var playerx+i := flexible array 1 .. 1 of int
end for
% Basically creates the variables playerx1, playerx2, playerx3 ...
|
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zren
|
Posted: Tue May 31, 2011 5:03 pm Post subject: RE:How can I get a variable using a variable? |
|
|
Turing: |
var w, h : int := 10
var grid : flexible array 1 .. 0, 1 .. 0 of int
new grid, w, h % Set bounds (size)
|
You start out at 0, then set the size after.
Can I ask what you need this array for? |
|
|
|
|
|
Tony
|
Posted: Tue May 31, 2011 5:37 pm Post subject: RE:How can I get a variable using a variable? |
|
|
while the syntax allows for it, I'm not sure if that ever was actually implemented
flexible
Quote:
In the current implementation (1999), with a multi-dimensional array with a non-zero number of total elements, it is a run-time error to change any but the first dimension (unless one of the new upper bounds is one less than the corresponding lower bound, giving 0 elements in the array) as the algorithm to rearrange the element memory locations has not yet been implemented.
You _could_ have a flexible array of pointers, that point to more flexible arrays... but perhaps this can be solved with a different data-structure design. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Raknarg
|
Posted: Tue May 31, 2011 5:48 pm Post subject: RE:How can I get a variable using a variable? |
|
|
Nope, you can definitely resize multi-dimensional arrays. I've done it in the past; they probably forgot to make it impossible. |
|
|
|
|
|
Linksku
|
Posted: Tue May 31, 2011 6:41 pm Post subject: RE:How can I get a variable using a variable? |
|
|
I found this: http://compsci.ca/v3/viewtopic.php?t=14333
But it seems that I can only set the second dimension's size if one of the dimensions is 0. Is there a workaround for this?
@Zren: I'm making a game, and I need to know the x and y of everywhere the players have gone. Basically keeps track of the movements of each player. |
|
|
|
|
|
goroyoshi
|
Posted: Tue May 31, 2011 6:45 pm Post subject: RE:How can I get a variable using a variable? |
|
|
you dont need flexible arrays for tracking movement, just use an array or two for each player |
|
|
|
|
|
Zren
|
Posted: Tue May 31, 2011 7:24 pm Post subject: Re: How can I get a variable using a variable? |
|
|
You'll just end up with two multidemensional arrays for x and y. That's just adding to the problem. Learn about records. They are veeeery valuable. An example in your case would be something like:
Turing: |
type Move :
record
player, x, y : int
end record
var moves : flexible array 1 .. 0 of Move
% Register a new move
new moves, upper (moves ) + 1
var move : Move
move.player := 1
move.x := 234
move.y := 13209
|
I should probably explain this more, but SC waits... Ah here we go. Check this out: http://wiki.compsci.ca/index.php?title=Turing_Records_and_Types |
|
|
|
|
|
Linksku
|
Posted: Thu Jun 02, 2011 4:07 pm Post subject: Re: How can I get a variable using a variable? |
|
|
I do use records, but I can't add flexible arrays in records.
I have a variable number of players, anywhere from 1 to 4. That's why I wanted to know if I can use a variable in a variable. So that I can do something like:
code: | for i:1..numPlayer
currentPlayer:=player+i
end for |
It seems like the only possible solution is hardcoding. But I want to make the game as efficient as possible. My last (fully playable and mildly addicting) game was only 100 lines, formatted and without comments. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Thu Jun 02, 2011 4:36 pm Post subject: RE:How can I get a variable using a variable? |
|
|
player+i is obviously not a valid variable name, but you could have an array of players, and address individual elements by index. E.g.
code: |
currentPlayer := players(i)
|
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
|
|