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

Username:   Password: 
 RegisterRegister   
 Array questions
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nathan4102




PostPosted: Fri May 17, 2013 3:08 pm   Post subject: Array questions

I'm making a minesweeper game for my summative, and I'm kind of stuck on something. What would be the most efficient way to store data for each individual cell(Click/Notclicked, number displayed, contains mine/doesn't)? I've taken a look at records, but i'm not sure if it'll work with what I need. Another possibility is a 2d array of strings, with each string containing info for the cell that could be dissected when needed, but if there's a better way, I don't want to get into all that string manipulation stuff.
Thanks!
Nathan
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Fri May 17, 2013 3:15 pm   Post subject: RE:Array questions

Sure, array of records would do just the trick.

For instance, you could do this:

Turing:

%Lets say you had a 50x50 grid

type gridData :
    record
        clicked, hasMine : boolean
        display : string
    end record
   
var grid : array 1 .. 50, 1 .. 50 of gridData


Lets say you wanted to see if the cell 45, 30 is clicked. You can call the variable there by calling:

grid (45, 30).clicked

So records in this case would be very useful. Not to mention, it's good to learn because it helps you understand the use of classes later (which are super useful, which you should aslo learn at some point soon)
Nathan4102




PostPosted: Fri May 17, 2013 3:19 pm   Post subject: RE:Array questions

Oh, sweet! I didn't think records would work with arrays, but if they do, I'll use them. I'll learn classes next year when I need them in ICS, for now, I'll stick with procedural programming.
Raknarg




PostPosted: Fri May 17, 2013 3:26 pm   Post subject: RE:Array questions

gridData is a type the same way int or real are types, so they can be used in the same ways. You can result gridData, or take it in as a parameter if you want to
Raknarg




PostPosted: Fri May 17, 2013 3:28 pm   Post subject: RE:Array questions

You don't have to wait btw. Do you code stuff for fun on your own time?
Nathan4102




PostPosted: Fri May 17, 2013 3:32 pm   Post subject: RE:Array questions

Man, I wish I knew about records earlier. I could have used them in some old projects. I usually have a couple projects going for fun, but my main focus is on schoolwork.
Raknarg




PostPosted: Fri May 17, 2013 3:34 pm   Post subject: RE:Array questions

That happens all the time. Once i learned array, once I learned records, once I learned classes, or random algorithms
Nathan4102




PostPosted: Fri May 17, 2013 3:41 pm   Post subject: RE:Array questions

I'd like to learn classes, but I haven't seen any practical use of them where they are superior to procedures/functions! I tried learning about them once, but all this Dog.Bark and Dog.Walk stuff seemed pointless to me.
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Fri May 17, 2013 3:47 pm   Post subject: RE:Array questions

It's useful if you have things that are actual objects. They can have all their own variables, and do all their calculations within the class.

For instance, have you ever tried making a button in turing? It's a tedious process, especially if you have more than one button. Hoever, you can create a class that handles everything for you. You want to know if the button is clicked? The class will perform calculations that are in the class, and call button -> is_clicked. You're done.

Want to draw the button? button -> draw

What if you have an entire page full of buttons? JUst call a procedure to initialize them all, and you're done. It's easy, and clean, and very useful.

You can check this out if you want: http://compsci.ca/v3/viewtopic.php?t=30797
Nathan4102




PostPosted: Fri May 17, 2013 3:50 pm   Post subject: RE:Array questions

Buttons are a pain in turing... I guess classes do have a use, but you can't use classes in turing, can you??
Raknarg




PostPosted: Fri May 17, 2013 3:52 pm   Post subject: RE:Array questions

Absolutely you can
Nathan4102




PostPosted: Fri May 17, 2013 3:55 pm   Post subject: RE:Array questions

Surprised I thought classes were only supported by OOP languages. I'll check em out soon!
Raknarg




PostPosted: Fri May 17, 2013 3:56 pm   Post subject: RE:Array questions

Turing:

class word
     export set_s, show_s
     
     var s : string
     
     proc set_s (s_ : string)
         s := s_
     end set_s
     
     proc show_s
         put s
     end show_s
end word

var foo : pointer to word

new word, foo

foo -> set_s ("hi")

foo -> show_s


super redundant,. but yeah
Nathan4102




PostPosted: Fri May 17, 2013 4:02 pm   Post subject: RE:Array questions

That's pretty neat... and confusing! Is that foo part necessary? or can you not call Word.set_s without it?
Raknarg




PostPosted: Fri May 17, 2013 4:06 pm   Post subject: RE:Array questions

You can't do that. Class is pretty similar to the type thing we talked about earlier. This is like a template for what you want each pointer to hold (a pointer is a variable that uses the class, btw). We can set 100 variables to this class, and they'd all have the same functions, but we could make them different.

Turing:

class word
     export set_s, show_s
     
     var s : string
     
     proc set_s (s_ : string)
         s := s_
     end set_s
     
     proc show_s
         put s
     end show_s
end word

var word1, word2 : pointer to word

new word, word1
new word, word2

word1 -> set_s ("hello ")
word2 -> set_s ("world")

word1 -> show_s
word2 -> show_s
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 2  [ 22 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: