Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Variable name from a string
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
maxudaskin




PostPosted: Sun Jun 08, 2008 1:09 pm   Post subject: Variable name from a string

Is it possible to check a variable using it's name as a string?

code:
%Declaration Statments
var square_id : string
var attacked : boolean := false
% Player Squares
var a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 : int := 0 % 0 is empty, 1 is hit, 2 is miss
var b1, b2, b3, b4, b5, b6, b7, b8, b9, b10 : int := 0
var c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 : int := 0
var d1, d2, d3, d4, d5, d6, d7, d8, d9, d10 : int := 0
var e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 : int := 0
var f1, f2, f3, f4, f5, f6, f7, f8, f9, f10 : int := 0
var g1, g2, g3, g4, g5, g6, g7, g8, g9, g10 : int := 0
var h1, h2, h3, h4, h5, h6, h7, h8, h9, h10 : int := 0
var i1, i2, i3, i4, i5, i6, i7, i8, i9, i10 : int := 0
var j1, j2, j3, j4, j5, j6, j7, j8, j9, j10 : int := 0

% Opponent Squares
var oa1, oa2, oa3, oa4, oa5, oa6, oa7, oa8, oa9, oa10 : int := 0
var ob1, ob2, ob3, ob4, ob5, ob6, ob7, ob8, ob9, ob10 : int := 0
var oc1, oc2, oc3, oc4, oc5, oc6, oc7, oc8, oc9, oc10 : int := 0
var od1, od2, od3, od4, od5, od6, od7, od8, od9, od10 : int := 0
var oe1, oe2, oe3, oe4, oe5, oe6, oe7, oe8, oe9, oe10 : int := 0
var of1, of2, of3, of4, of5, of6, of7, of8, of9, of10 : int := 0
var og1, og2, og3, og4, og5, og6, og7, og8, og9, og10 : int := 0
var oh1, oh2, oh3, oh4, oh5, oh6, oh7, oh8, oh9, oh10 : int := 0
var oi1, oi2, oi3, oi4, oi5, oi6, oi7, oi8, oi9, oi10 : int := 0
var oj1, oj2, oj3, oj4, oj5, oj6, oj7, oj8, oj9, oj10 : int := 0

% X Y to Square ID - Inputs XY, Outputs the Square ID for processing
procedure xySquareID (xin : int, yin : int)
    if xin >= 2 and xin < 27 then
        square_id := "a"
    elsif xin >= 27 and xin < 52 then
        square_id := "b"
    elsif xin >= 52 and xin < 77 then
        square_id := "c"
    elsif xin >= 77 and xin < 102 then
        square_id := "d"
    elsif xin >= 102 and xin < 127 then
        square_id := "e"
    elsif xin >= 127 and xin < 152 then
        square_id := "f"
    elsif xin >= 152 and xin < 177 then
        square_id := "g"
    elsif xin >= 177 and xin < 202 then
        square_id := "h"
    elsif xin >= 202 and xin < 227 then
        square_id := "i"
    elsif xin >= 227 and xin < 252 then
        square_id := "j"
    end if

    if yin >= 2 and yin < 27 then
        square_id := square_id + "10"
    elsif yin >= 27 and yin < 52 then
        square_id := square_id + "9"
    elsif yin >= 52 and yin < 77 then
        square_id := square_id + "8"
    elsif yin >= 77 and yin < 102 then
        square_id := square_id + "7"
    elsif yin >= 102 and yin < 127 then
        square_id := square_id + "6"
    elsif yin >= 127 and yin < 152 then
        square_id := square_id + "5"
    elsif yin >= 152 and yin < 177 then
        square_id := square_id + "4"
    elsif yin >= 177 and yin < 202 then
        square_id := square_id + "3"
    elsif yin >= 202 and yin < 227 then
        square_id := square_id + "2"
    elsif yin >= 227 and yin < 252 then
        square_id := square_id + "1"
    end if
end xySquareID

% Check if Already Attacked
procedure checkAttacked (squ_id:string)
   
end checkAttacked




Max Canada
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Jun 08, 2008 1:31 pm   Post subject: RE:Variable name from a string

yes, if that variable is a Hash / HashMap. Which is an Array with a string used for an index.

Arrays should be sufficient for your game.
Turing:

% Player Squares
var player_squares : array 1 .. 10, 1 .. 10 of int

% Opponent Squares
var opponent_squares : array 1 .. 10, 1 .. 10 of int

% initialize everything to 0
% this is also an example of how to use arrays

for current_row : 1 .. 10
   for current_column : 1 .. 10
      player_squares(current_row)(current_column) := 0
      opponent_squares(current_row)(current_column) := 0
   end for
end for


More on arrays, in the Turing Walkthrough
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
maxudaskin




PostPosted: Sun Jun 08, 2008 4:01 pm   Post subject: Re: Variable name from a string

Do you mean

code:

for current_row : 1 .. 10
   for current_column : 1 .. 10
      player_squares(current_row,current_column) := 0
      opponent_squares(current_row,current_column) := 0
   end for
end for


or is what you wrote correct?
Tony




PostPosted: Sun Jun 08, 2008 4:17 pm   Post subject: RE:Variable name from a string

... you are right, I meant player_squares(current_row,current_column)

I don't actually code in Turing, or even have it available.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
maxudaskin




PostPosted: Sun Jun 08, 2008 5:07 pm   Post subject: Re: RE:Variable name from a string

Tony @ Sun Jun 08, 2008 4:17 pm wrote:
... you are right, I meant player_squares(current_row,current_column)

I don't actually code in Turing, or even have it available.


Java?
syntax_error




PostPosted: Sun Jun 08, 2008 5:18 pm   Post subject: RE:Variable name from a string

no as far am I am aware he is a ruby admirer, nonetheless the code was just of syntactically, not a huge mistake, you did get the idea of what you should be doing no?
Tony




PostPosted: Sun Jun 08, 2008 5:21 pm   Post subject: RE:Variable name from a string

I don't like Java, because Java sucks.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: