How to implement Man in Maze? - oop

i am thinking about design for such simple exercise as Man in Maze.
I would like to make Maze object, put some Man objects inside it and call solve() for each to get out of Maze.
Man should know nothing about Maze. It just can try to go and find way out.
Here is how i see it:
maze = Maze.new
man1 = Man.new
man2 = Man.new
man3 = Man.new
maze.put(man1,man2,man3)
maze.men.each do |man| { man.solve }
But how to implement body of these classes?
How can man know how to walk inside Maze unless I give Maze instance to him?
But if i do that:
maze.put(man1(maze),man3(maze),man2(maze))
What the reason to put men inside Maze if I can just give Maze instance to them?
This is what i don't understand and can't find elegant solution.

Think of it this way: what data belongs with each class? What methods belong with it to work with that data? This is object-orientation: the bundling of data and relevant code.
The Maze class doesn't need to know about any men. It's a maze. All it is are some walls, some structure. Build it as such, as if nothing outside of it exists. Make some methods to obtain information about the maze, such as whether there's a wall at some point, a corner, a junction...
The Man class is, in your case, conceived for a single purpose: find his way out of the maze. A Man therefore should have a position in the maze (part of his data) and some methods that help him look for the exit. Some depth-first search algorithm, for example.
Of course, you could have some collection of all men currently in a Maze embedded in the maze itself. But that's not necessary. You could use any other collection to hold the men and iterate over them.
maze = Maze.new
man1 = Man.new(maze)
...
men = Array.new
men[0] = man1
...
men.each do |man| {man.solve}

You can add a new class called Map. The maze can have a method called GetMap which returns the Map of the maze. This map can be used to check whether man is making the right moves.
So, map would be the common ground between maze and man. But this may not help you if you want to track the current location of the man inside the maze.

Related

Is there such thing as a global variable? (GDscript)

I'm trying to make a game but I need something like a global variable for it to work. Is there a way in GDscript that you can make a variable that works across all nodes with scripts. I need a button that makes you buy a gun which would set a variable to true and with that variable being true, you could equip the gun.
This question doesn't really fit this section where it says: What did you try and what were you expecting? so I'm just gonna skip it.
Is there a way in GDscript that you can make a variable that works across all nodes with scripts.
You could use an autoload.
Go to the Project menu, the Project Settings option, in the Autoloads tab… And there you can set a script, and give it a name. It will be available for every node in the scene tree.
For example, you can have a "globals.gd" script that looks like this:
extends Node
var has_gun := false
Then you make it an autoload with the name "Globals". So in any other script you can use it. For example:
extends Node
func _ready() -> void:
print(Globals.has_gun)
And yes, autoloads will stay there even if you change the current scene.
You might also be interested in the Signal bus (Event bus) pattern, which involves defining signals in an autoload, such that other scripts emit them or connect to them.
Technically the autoload is not a true global. As I said, the autoload will be available for the nodes in the scene tree.
This also means the autoload will not be available for a script that is not a Node (e.g. a Resource), or otherwise not in the scene tree.
Unless you use a hacky workaround to get a reference to the autoload, which I will not go into.
Instead, I will give you an alternative: resource based communication.
This time you create a "global_resource.gd" that look like this:
class_name GlobalResource
extends Resource
var has_gun := false
And then you can use the context menu on the FileSystem panel to create a new resource. When Godot asks you for the type, you select GlobalResource, and give it a name such as "globals.tres".
Then you can use it like this:
extends Node
const Globals := preload("res://globals.tres")
func _ready() -> void:
print(Globals.has_gun)
Everywhere you preload that same resource file ("globals.tres") you will get the same Resource instance. It does not matter if it is in the same scene, or if you changed the current scene.
And yes, this does not depend on the scene tree. And yes, you can put signals in there too.
For completeness sake, I'll also mention that there is another hacky workaround which involves defining a Dictionary or Array as const in a script with a class name. Since in Godot 3 const does not imply inmutable (don't count on this on Godot 4). That is how some people currently work around the lack of static variables. However, I believe you won't find it necessary.

In gym, how should we implement the environment's render method so that Monitor's produced videos are not black?

How are we supposed to implement the environment's render method in gym, so that Monitor's produced videos are not black (as they appear to me right now)? Or, alternatively, in which circumstances would those videos be black?
To give more context, I was trying to use the gym's wrapper Monitor. This wrapper writes (every once in a while, how often exactly?) to a folder some .json files and an .mp4 file, which I suppose represents the trajectory followed by the agent (which trajectory exactly?). How is this .mp4 file generated? I suppose it's generated from what is returned by the render method. In my specific case, I am using a simple custom environment (i.e. a very simple grid world/maze), where I return a NumPy array that represents my environment's current state (or observation). However, the produced .mp4 files are black, while the array clearly is not black (because I am also printing it with matplotlib's imshow). So, maybe Monitor doesn't produce those videos from the render method's return value. So, how exactly does Monitor produce those videos?
(In general, how should we implement render, so that we can produce nice animations of our environments? Of course, the answer to this question depends also on the type of environment, but I would like to have some guidance)
This might not be an exhaustive answer, but here's how I did.
First I added rgb_array to the render.modes list in the metadata dictionary at the beginning of the class.
If you don't have such a thing, add the dictionary, like this:
class myEnv(gym.Env):
""" blah blah blah """
metadata = {'render.modes': ['human', 'rgb_array'], 'video.frames_per_second': 2 }
...
You can change the desired framerate of course, I don't know if every framerate will work though.
Then I changed my render method. According to the input parameter mode, if it is rgb_array it returns a three dimensional numpy array, that is just a 'numpyed' PIL.Image() (np.asarray(im), with im being a PIL.Image()).
If mode is human, just print the image or do something to show your environment in the way you like it.
As an example, my code is
def render(self, mode='human', close=False):
# Render the environment to the screen
im = <obtain image from env>
if mode == 'human':
plt.imshow(np.asarray(im))
plt.axis('off')
elif mode == 'rgb_array':
return np.asarray(im)
So basically return an rgb matrix.
Looking at the gym source code, it seems there are other ways that work, but I'm not an expert in video rendering so for those other way I can't help.
Regarding your question "how often exactly [are the videos saved]?", I can point you to this link that helped me for that.
As a final side note, video saving with a gym Monitor wrapper does not work for a mis-indentation (as of today 30/12/20, gym version 0.18.0), if you want to solve it do like this guy did.
(I'm sorry if my English sometimes felt weird, feel free to harshly correct me)

VB.NET How do I pass parameters to a class?

Relatively new to coding and have taken up lots of small projects to help learn the basics, and I have now set myself a challenge of a "bigger" one. Essentially I want to recreate the Message Box but with my own styling and customisable elements.
I have got the basics in a class and created it, however I want the class to have two options.
1) load all the details from an XML file for the message, I have done this and that works.
2) I want it to be like the standard message box where you can pass in parameters.
My question is, How can I achieve number 2.
I have tried adding details into the Show/Load subs but no luck, the only way around it I can see is with properties but that would take too long.
I want to be something like the below.
classname.show("message","tittle",icon,"buttons",imagefile,"caption")
However alot of my code is done in the load method as opposed to show, so it needs to be visible / accessible there.
Any help / advice would be appreciated.
Properties are definitely the way to go. It also makes sense: Conceptually, the message being shown is a property of the message box.
Your Show method would look like this:
Public Shared Show(message As String, title As String, ...)
Dim box as New MyMessageBoxWindow()
box.Message = message
box.Title = title
...
box.ShowDialog()
End Sub
In the Load method of MyMessageBoxWindow, you access these properties and configure the UI elements.

How do I go to the nth method of the eclipse editor's file?

I need to write a function goToNthMethod(int n) to let the user jump to the nth method in the file being edited.
Ideas so far:
I imagine the ContentOutline reads its tree from some sort of IContentSource (made up) or something, if I can read from the same source, that would probably be cleaner. Does something like this exist?
Read the contents of the outline view, and maybe simulate a double click on one of the Outline view's entries. This is as far as I got before I realized I was in over my head:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IViewPart part = page.findView("org.eclipse.ui.views.ContentOutline");
ContentOutline outline = (ContentOutline)part;
PageBook pageBook = outline.book; // Doesn't work, book is private
Tree root = pageBook.currentPage; // Doesn't work, currentPage is private
String label = root.getLabel(); // Nothing like getLabel exists
Read the entire IDocument's contents, parse the java source code within, get the offsets in the file, and feed that to the editor.selectAndReveal method. However, parsing the java source code within is a massive task, so this approach probably won't work.
Use outline.getCurrentPage(), which is a JavaOutlinePage, but I can't seem to import that class. I'm guessing I need to pull in the entire JDT project to do that. This approach also means I'm tied to a specific language, when I want my goToNthMethod to be language agnostic.
Any ideas on how I can jump to the nth method? Thanks!
Some context: I'm integrating Dragon NaturallySpeaking with eclipse to be able to program with my voice. It's working well so far, but one tedious part is navigating around the file, which would be made easier if I could say "go to 8th method". In fact, just "go to 8th entry" to just go to the 8th row in the outline view would be sufficient. Any other ideas appreciated!

create a new object

I want to create a new object so as to instantiate and use it several times;
For example, if I want to create an object that has a label and a button inside, how do I? I created a new NSObject but inside it has nothing, then how do I make everything from scratch since there was a viewDidLoad for example (obviously, since it has a view)?
thanks!
Your questions lead me to think that you're really just starting out. There's nothing wrong with that, but rather than trying to summarize several megabytes of documentation in a few paragraphs, I'm just going to point you to the iOS Starting Point. I think that you're just trying to create a container that can hold other UI components? If so, use a UIView for that. However, don't jump in and try to get something specific done without first reading through some of the Getting Started documents -- you'll just end up back here, and we'll just point you back to the docs. You might like the Your First iOS Application guide, as that lets you get your feet wet but explains things along the way.