Computer Science Canada

Array questions

Author:  Nathan4102 [ 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

Author:  Raknarg [ 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)

Author:  Nathan4102 [ 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.

Author:  Raknarg [ 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

Author:  Raknarg [ 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?

Author:  Nathan4102 [ 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.

Author:  Raknarg [ 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

Author:  Nathan4102 [ 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.

Author:  Raknarg [ 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

Author:  Nathan4102 [ 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??

Author:  Raknarg [ Fri May 17, 2013 3:52 pm ]
Post subject:  RE:Array questions

Absolutely you can

Author:  Nathan4102 [ 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!

Author:  Raknarg [ 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

Author:  Nathan4102 [ 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?

Author:  Raknarg [ 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

Author:  Nathan4102 [ Fri May 17, 2013 4:09 pm ]
Post subject:  RE:Array questions

Oh, now I see how classes can be really useful. I'll probably dive into this stuff when I learn Java sometime soon hopefully. Thanks for the advice and examples!

Author:  Raknarg [ Fri May 17, 2013 4:12 pm ]
Post subject:  RE:Array questions

IF you ever get to that stage, you should look this up: http://processing.org/

It's simple like turing and it's structured very similarly, but it has the power and syntax of Java.

Learn turing first though

Author:  Nathan4102 [ Fri May 17, 2013 4:16 pm ]
Post subject:  RE:Array questions

Well I'm going to need to learn Java too, since my co-op in 8 months will be using Java, plus my computers course in the fall will be in Java, so I'd like to get a bit of a head start before that class starts. The questions isn't IF I get to that stage, it's WHEN. Wink

Author:  Raknarg [ Fri May 17, 2013 4:17 pm ]
Post subject:  RE:Array questions

Well in that case, you're probably better off actually learning Java rather than something close to java Razz

also yay 1000 posts

Author:  Nathan4102 [ Fri May 17, 2013 4:44 pm ]
Post subject:  RE:Array questions

That's what I was thinking, and gratz! I'll catch up to ya soon!

Author:  Panphobia [ Fri May 17, 2013 8:45 pm ]
Post subject:  Re: Array questions

This here was one of my major OOP assignments in grade 12 compsci, it isn't that hard but its just to give a feel on what it looks like
code:
ICS4U ? Object Orientation Assignment Part 2 ? Novel Knights and Wicked Wizards

The world contains two types of humans: knights and wizards. Humans can be represented in the world as small squares. Knights are blue and Wizards are red. The relationship between Humans, Knights and Wizards can be described by the following hierarchy:



All humans can perform certain actions as described by the following interface:
https://lh3.googleusercontent.com/3tZdinTjzSV7s4YBNvBN7KmSlDf-cyXYDeC3Xm1ohLrNwEycpjUmBTWDT61j_DxIKqeyV2REeICImUdeK_xNaudNlVW25kI8l9WpsBi-vyD5ZvU5MgM
code:


Part 1:
Write an abstract Human class that implements the Human interface above. You?ll need to include the interface in your project. Your abstract Human should conform to the following class summary box:


Class:

Human



Private Instance Variables:

String name, int age, double height, int health, int xPos, int yPos



Public Instance Methods:

constructor

void move(int x, int y)

int getXPos

int getYPos

int getHealth

boolean setHealth

String toString

String getName

void setName

int getAge

boolean setAge

double getHeight

boolean setHeight

void draw (Pen p)



Public Class Variables:

double LOW_HEIGHT, double HIGH_HEIGHT, int LOW_AGE, int HIGH_AGE, int LOW_HEALTH, int HIGH_HEALTH, int NUM_HUMANS



Public Class Methods:

int getNumHumans

void setNumHumans

String getAgeRules

String getHeightRules

String getHealthRules


Part 2:
Implement the Knight and Wizard classes as described in the class summary boxes below.
Class:

Wizard



Private Instance Variables:

int magicka



Public Instance Methods:

constructor

void draw(Pen p)

void castSpell

String toString

boolean setMagicka (int m)

int  getMagicka

boolean equals(Wizard w)

Wizard clone



Public Class Variables:

int LOW_MAGICKA, int HIGH_MAGICKA



Public Class Methods:

String getMagickaRules

Class:

Knight



Private Instance Variables:

String horseName



Public Instance Methods:

constructor

String toString

void draw(Pen p)

void setHorse (String n)

String getHorseName

boolean equals(Knight k)

Knight clone



Public Class Variables:

none



Public Class Methods:

none

Part 3:
Write a test program World, which:
        0. Declares 4 Humans.
        1. Instantiates a Knight
        2. Instantiates a Wizard  and moves it.
        3. Clones the Knight  and changes its name
        4. Checks if the Knight clone is not equal to the original Knight, and if so:
                a) Moves the clone
        5. Clones the Wizard
        6. Checks if the Wizard clone is equal to the original Wizard, and if so:
                a) Moves the clone
        7. Draws both Knights and both Wizards
        8. Prints the total number of Humans living in this World.
        9. Prints a description of all Humans to the console.




Evaluation:
Marks

Part 1:

Abstract Human Class:

Private Instance Variables:

Public Instance Methods:

Public Class Variables:

Public Class Methods:
                             



/3 TI

/7 TI

/2 TI

/2 TI

Part 2:

Knight Class Implementation:

Wizard Class Implementation:


/4 A

/5 A




Part 3:

Declare Humans 

Instantiate Knight and Wizard   

Clone?s Knight and Wizard

Checks and Moves Clones

Draws and Prints World Info     

Header, Commenting 



Challenge:

Draw actual Knights and Wizards!


/1 K

/1 K

/4 K

/4 K

/2 C

/4 C









HumanInterface

Human (abstract)

Knight (concrete)

Wizard (concrete)

Implements

Author:  Nathan4102 [ Fri May 17, 2013 9:56 pm ]
Post subject:  RE:Array questions

It looks like they've written half the thing for you! Interesting though, thanks for posting.


: