Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Classes
#11
We assume that the player moves on a grid but in my case we need a list to save random objects and their coordinates-which are random too. -
Question-I don't know if I can do that-: For random coordinates can I make a function and just add it to all classes?
As for random objects can I use a secondary array like A=['triangle', 'sphere'] and using it to create the list??
Or is there a simpler way to make one function for both of them?
Reply
#12
(Feb-15-2019, 09:17 PM)Lonewolf Wrote: Question-I don't know if I can do that-: For random coordinates can I make a function and just add it to all classes?
yes, you would create a function, but don't add it to classes you have. If you had a Grid class, then it would belong there, but without it, better keep it as separate function. the function will depend on what type of list/grid you will have.

(Feb-15-2019, 09:17 PM)Lonewolf Wrote: As for random objects can I use a secondary array like A=['triangle', 'sphere'] and using it to create the list??

yes that's exactly what you would do. However, don't use list/tuple of strings, e.g. ['triangle', 'sphere'], but list of classes
obstacles = [Triangle, Sphere]
then you can use random.choice() to select what object to instantiate. If using python 3.6+ you can also use random.choices() to select all 12 obstacles at once.
(Feb-15-2019, 09:17 PM)Lonewolf Wrote: We assume that the player moves on a grid but in my case we need a list to save random objects and their coordinates-which are random too. -
you can take at least 3 different approaches:

populate just a list with 12 obstacles.

or

have a list with 11*29 elements, e.g. None. then populate randomly 12. i.e. replace None with an instance of (randomly selected) shapes - Triangle or Sphere


or

have a list of lists, i.e. list with 11 sub-lists. each sublists with 29 elements e.g. None. Then like in previous approach randomly populate 12 positions. That is close to what you were doing.

Of course you can always create a class and incorporate into it any of the above.

in the first approach you will need to check each pair of randomly selected coordinates against all previously selected pairs.
In the second and third approach you would just check if the respective element/slot is None (i.e. empty)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#13
i am getting what you say but teacher said to use randit.The thing is how to generate 2 variables at the same time (x,y).
if i had one variable x that should be something like
a=[]
for j in range(n):
      a.append(random.randint(1,12))
print('Randomised list is: ',a)
Same with y.
but i can't find a way to generate 2 variables at the same time since i can't write for example range(1-12,1-29)
Reply
#14
you don't need to generate them simultaneously. generate x (1, 11), then generate y (1, 29). choose randomly what shape. instantiate it, passing x and y and so on. Don't forget you have to check if the spot is empty.
So you go for a list of just 12 shapes, not list of lists?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#15
What i have to do is that:
part 1:
Set superclass and subclasses. Do not forget to keep information about the "penalty" that each different type of object will bring, and you should also keep the information specific for each type of object (dimensions). Set in all classes the appropriate methods for retrieving and storing a new value for each attribute of class
part 2:
Create a list where you will place 12 items.
- The creation of objects should be done with a repeating structure.
- The type of each new object will be determined in a random fashion.
For implementation, we will assume that all objects of the same type will have the same characteristics
For each new object, a pair of coordinates (x, y) will be randomly generated and will be checked if any other space object already exists at this point. If there is another object at this point, then the process of creating new, random coordinates will be repeated until a point is found in which there is no other space object. Once such a point has been found, the new space object will occupy it. –
You should be careful that the random co-ordinates produced are within the "grid ".
PART 3:
in order to be able to check the correctness of your implementation, you decide to add a printMessage () method to each of the subclasses you created in Question A. Considering that an object is at the coordinates (x, y), the method above should print the following message (of course, in the position of x, y, length, width, height, radius the actual values of each object must be printed):
- For triangle class object:
You hit triangle of size (x, y).
- For sphere class object:
You hit sphere of radius radius at (x, y).

Everything is a bit confusing!That's why i want to give up.
Reply
#16
by the way, normally in python indexes a 0-based. you may want to use same approach, e.g. x to be 0 to 10, not 1 to 11
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#17
(Feb-16-2019, 08:22 AM)Lonewolf Wrote: That's why i want to give up.
I was trying to help you, but if you want to give up - ok. have a nice weekend.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#18
(Feb-16-2019, 08:22 AM)buran Wrote: by the way, normally in python indexes a 0-based. you may want to use same approach, e.g. x to be 0 to 10, not 1 to 11
i know that it should start form 0..but who am i to disagree if teachers wants to start by 1?

as for the list of lists..it sounds complicated...Right now i can't even do the simplest things...A list of list is like "hell" for me!!

(Feb-16-2019, 08:25 AM)buran Wrote:
(Feb-16-2019, 08:22 AM)Lonewolf Wrote: That's why i want to give up.
I was trying to help you, but if you want to give up - ok. have a nice weekend.
i know that you want to help and thank you for that!!!!
but as you see all these ...they are like "alien stuff" for me.Classes and inheritance are very difficult

Sorry,if i said something bad.
But i can't understand something if i don't have help
Reply
#19
(Feb-16-2019, 08:55 AM)Lonewolf Wrote: Sorry,if i said something bad.
No you didn't say anything bad. But if you want to give up, I can't and don't want to force you. It just doesn't make sense to make you struggle.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#20
(Feb-16-2019, 09:02 AM)buran Wrote:
(Feb-16-2019, 08:55 AM)Lonewolf Wrote: Sorry,if i said something bad.
No you didn't say anything bad. But if you want to give up, I can't and don't want to force you. It just doesn't make sense to make you struggle.
the problenm is that i don't have someone to help me here.For example to show me how to generate the coordinates on this example and explain it to me.Like what you did in page 1 with setters and getters.
The only help i have in my home is that:
"Why are you crying?"
"Because i don't understand what i must do here..."
my older brother who knows python says "It's the easiest exercise ever and laughs!"
And then i am getting angrier and feel like the most stupid person in the world.i somehow know what i must do but i do not know how to do it.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020