Posted: 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
Raknarg
Posted: 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 endrecord
var grid :array1.. 50, 1.. 50of 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
Posted: 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
Posted: 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
Posted: 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
Posted: 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
Posted: 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
Posted: 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
Raknarg
Posted: 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.
Posted: 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
Posted: Fri May 17, 2013 3:52 pm Post subject: RE:Array questions
Absolutely you can
Nathan4102
Posted: Fri May 17, 2013 3:55 pm Post subject: RE:Array questions
I thought classes were only supported by OOP languages. I'll check em out soon!
Raknarg
Posted: 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 :pointerto word
new word, foo
foo -> set_s ("hi")
foo -> show_s
super redundant,. but yeah
Nathan4102
Posted: 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
Posted: 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.