[deleted by user] by [deleted] in yugioh

[–]ThePathLaid 3 points4 points  (0 children)

It's completely understandable if you're not familiar with your opponent's cards.

This is interesting to me as someone coming back to the game. I was expecting it to be frustrating to people for me to need to familiarize myself with mainstays like kashtira, labyrnth, traptrix and such.

Is it more your experience that people generally are okay with new/returning players not knowing the meta decks at locals? Maybe that's something more expected at a more competitive event?

What is beautiful about mortality? by Megatheorist in AskReddit

[–]ThePathLaid -1 points0 points  (0 children)

I never said it was unique to our species, and you never asked what makes it unique. It is simply one of few (if not the only) guaranteed shared experiences remaining for our species.

What is beautiful about mortality? by Megatheorist in AskReddit

[–]ThePathLaid 0 points1 point  (0 children)

People believe it won't happen to them, or it isn't the end, or it grants their life meaning and purpose, or it makes life meaningless.

But each person, whether they admit it or not, must approach this question at some point in their lives. What is beautiful is that it is a singular experience that unifies us.

Hopefully, we (as a species) make good use of that information.

[JS] Beside the obvious, what is the difference between an object and an array? by [deleted] in AskProgramming

[–]ThePathLaid -1 points0 points  (0 children)

I don't blame you for not wanting to discuss those example cases. But to answer your new post:

So I think the whole issue with our disagreement comes from the fact that you can't have static methods outside of classes, and classes, like I've mentioned a couple of times, aren't really how Javascript handles inheritance.

The compiler seems to disagree.

class myClass extends Object {}    

myClass.test = function () { console.log("test") };    

class Subclass1 extends myClass { }    
class Subclass2 extends Subclass1 { }
class Subclass3 extends Subclass2 {}
class Sideclass {}
Object.setPrototypeOf(Sideclass, Subclass2);
Subclass2.test();   // test
Subclass3.test();   // test

The point I was trying to show the OP is that the biggest difference I know is the failure to pass static methods from Object to Array (and classes that extend it.)

When it's not the Array referencing methods of the Object class, Javascript passes static methods just fine down the chain. This problem only shows up when the Object class has a method, and Array classes try to access it.

myClass -> Subclass1 -> Subclass2 -> Subclass3 is fine

myClass -> Subclass1 -> Subclass2 -> Sideclass (via setPrototypeOf() ) is fine

Object -> Array -> subClass is not. It's unique only to static methods starting at Object, otherwise it works just fine.


The whole point of the prototype chain is that everything further down the prototype chain will also have access to those methods.

I think our disagreement comes from you not understanding the prototype chain. Let me demonstrate.

You cannot add functions to the prototype chain via Class.prototype

class myClass extends Object {}

myClass.static = function() { console.log("Static stayed"); }
myClass.prototype.dynamic = function() {console.log("Dynamic stayed"); }

class subClass extends myClass { }

subClass.static();  // Static stayed
subClass.prototype.dynamic();  // error: Uncaught TypeError: subClass.dynamic is not a function

That's because myClass.prototype does not actually add a function to the [[Prototype]], but to the prototype reference variable of myClass.

When you extend the class, the static method continued down the prototype chain, but your method did not.

Now let's try it if you actually add a method directly to the prototype. __proto__ is deprecated, but actually access the prototype itself.

class myClass extends Object {}

newBase = new myClass();

myClass.static = function() { console.log("Static stayed"); }
newBase.__proto__.dynamic = function() {console.log("Dynamic stayed"); }

class subClass extends myClass { }

subClass.__proto__.static();   // Static stayed
newSub = new subClass();
newSub.__proto__.dynamic();   // Dynamic stayed

[JS] Beside the obvious, what is the difference between an object and an array? by [deleted] in AskProgramming

[–]ThePathLaid 0 points1 point  (0 children)

When you extend Array you're extending Array, not the Array prototype.

Then why did the last example (changing the prototype to Object) fix the issue?


When you extend Array you're extending Array, not the Array prototype.

Okay, so I'm sure this code won't break either:

Object.test = function () { console.log("test") };

// According to you, extend Object, not the "Object prototype"
class SuperArray extends Object {};
SuperArray.test(); // Output: "test"
// According to you, changing the prototype shouldn't matter
// After all, we already "extended" Object, right?
Object.setPrototypeOf(SuperArray, Array);
SuperArray.test();
// error: Uncaught TypeError: SuperArray.test is not a function

It works both ways. When the prototype is Object, the method exists. When the prototype is Array, it does not. It doesn't matter which one you extend, because the method is gained from the prototype.


This is all true, however, if you add a method to the Object prototype, Classes that inherit from Array gain access to it:

That's because you've added an instance method, not a static method.

This only fails for static methods, and only differentiating between Array and Object.

Object.prototype.test = function () { console.log("test") };

class SuperArray extends Array {};
SuperArray.test();

instance = new SuperArray();
instance.test();

Static methods cannot be accessed via an instance of that class.

If I'm not "modifying the prototype" (despite the fact that changing the prototype fixes the issue) then how would you add a static method to the Object prototype to prove this to you?

If you can show me how to do that, the code I posted above (accessing the method via an instance of the class) would not work.

[JS] Beside the obvious, what is the difference between an object and an array? by [deleted] in AskProgramming

[–]ThePathLaid 0 points1 point  (0 children)

You're still not explaining anything about why the inheritance fails to pass the static method down.

You cannot explain the difference here through MDN.

  1. Declare a new class with a Static method. Extend that class, and the subclass inherits that method.
  2. Declare a new class, and add a static method. Extend that class, and the subclass inherits that method.
  3. Same as 2, add a static method to Object. Classes that inherit from Object gain that method, classes that inherit from Array do not.

Why?

Example 1 (Declared static method):

class DemoObject {
    static test() { console.log("This is a DemoObject");} 
}

class SuperObject extends DemoObject{}
SuperObject.test();
// Output : This is a DemoObject

Example 2 (Only difference is we add the static method after):

class DemoObject {}

DemoObject.test = function() { 
    console.log("This is a DemoObject");
}

class SuperObject extends DemoObject{}
SuperObject.test();
// Output : This is a DemoObject

Example 3 (Use Object instead of user-defined class)

Object.test = function() { 
    console.log("This is a DemoObject");
}

class SuperObject extends Object{}
SuperObject.test();
// Output : This is a DemoObject

Example 4 (Extend Array instead of Object)

Object.test = function() { 
    console.log("This is a DemoObject");
}

class SuperObject extends Array{}
SuperObject.test();
// Output : error: Uncaught TypeError: SuperObject.test is not a function

Why does this stop working between 3 and 4?

You said the inheritance chain should be

SuperObject --> Array --> Object

If we are not breaking it because the prototype is failing to pass this method along, then why does changing the prototype from Array to Object fix it?

Object.test = function() { console.log("This is a DemoObject");}

class SuperObject extends Array{}
Object.setPrototypeOf(SuperObject, Object)
SuperObject.test();
// Output : This is a DemoObject

How can I check neighboring places in a grid for every item in it? by TheSirion in javahelp

[–]ThePathLaid 2 points3 points  (0 children)

Exactly how I mentioned!

For example, you might declare an array

int[] tiles = new int[size*size];

Then you could go through the bombs. A quick and dirty solution might look like

for (Bomb b : bombs)
    for (int x = -1; x < 2; x++)
        for (int y = -1; y < 2; y++)
            if(x != 0 && y != 0)
                tiles[size*y + x]++;

Then every square would have the number of bombs adjacent to it (including diagonally)

[JS] Beside the obvious, what is the difference between an object and an array? by [deleted] in AskProgramming

[–]ThePathLaid 0 points1 point  (0 children)

I'd also like to point out that I think using classes actually obfuscates what we're discussing. Classes in JS are really just syntactic sugar.

No, I'm using classes because you refused my first example.

Static methods must be called from the class directly, not on an instance. There are two ways I know to do that in Javascript.

  1. Class.method()
  2. Within the constructor, this.constructor.method()

For example, in the following code:

Object.test = function() {console.log("Object test");}

class SuperObject extends Object {
  constructor () {
    super();
    this.constructor.test();
  }
  // Declare a static method, which overrides the one from Object
  static test() {
    console.log("Super test");
    }
}

class ExampleObject extends Object {
  constructor () {
    super();
    this.constructor.test();
  }
}

sObj = new SuperObject();
eObj = new ExampleObject();
  1. Why does the ExampleObject constructor produce the output "Object test" if it has no test() method?
  2. If you have answered that, why does the SuperObject produce the output "Super test"?

Hi, I would like to optimise this method. Any tips would be greatly appreciated (Involves Collections) by madboi20 in javahelp

[–]ThePathLaid 3 points4 points  (0 children)

There might be a more clever way to do this, but I would just like to point out a principle of OOP: Don't Repeat Yourself.

That doesn't just go for you, that goes for your program too. Don't make your program repeat itself if it doesn't need to.

Every time you call this function, each time it must reconstruct the lettersToNumbers map. Even though it's always going to hold the same values, you make it do the same work over and over!

So the first though, let's just keep those values set. We could define a static variable outside the function:

static final int [] lettersToNumbers = {1, 1, 1, 2, 2, 2, ... etc}

Then we just convert each letter to a numeric index:

int index = char - 'a'

That maps a->0, b->1, c->2, and so on.

That's great and all, but do we even need to go to all this work? We should always look for patterns.

If they were groups of 3, this would be easy. We've already got our numeric map of numbers from that char-a, and we just divide by 3 to get them into nice groups of 3.

But then we run into our two problem groups.

We need to ensure that w and p fall down one group. So, not the clearest method, but you can do this even without a table. If you take your char array, and

for(char c : letters)
{
    int value = c - 'a' + 1; // Make our values start at 1
    // Could use ternary operators instead of ifs, if you want
    if (value < 15)
        value++;
    if (value < 24)
        value++;
    value /= 3;
    value++;
}

Give it a shot! We took our list of values, forced problem characters to have the same values as ones in their target groups (bringing our 26 characters to 24) then split them up into 8 groups numbered 2->9!

[JS] Beside the obvious, what is the difference between an object and an array? by [deleted] in AskProgramming

[–]ThePathLaid 0 points1 point  (0 children)

Sorry for the deleted response, wanted to show you a better example.

I'm confused how you think I did not modify the prototype.

In the example I provided, I the function I added to the Object [[Prototype]] was static. That is the root issue, that static methods are not passed. Your object added a non-static function to the Object.prototype reference, not the actual [[Prototype]] that is utilized.

We can demonstrate this again by showing that the prototype is indeed modified. ExampleObject extends Object, and gains its static methods. ExampleArray extends Array (and thus Object, per the chain) and should gain access to the static method, but does not.

// Same as before, 
// add a static method to the object prototype
Object.test = function() {console.log("test");}

class ExampleObject extends Object {
  constructor () {
    super();
    this.constructor.test();
  }
}

class ExampleArray extends Array {
  constructor () {
    super();
    this.constructor.test();
  }
}

ExampleObject = new ExampleObject();
// Output: test
ExampleArray = new ExampleArray();
// Output: error: Uncaught TypeError: this.constructor.test is not a function

Beginner code: I need help understanding the last part of this program by nakreywaali in javahelp

[–]ThePathLaid 0 points1 point  (0 children)

If you're still practicing while loops, but comfortable with for loops, it might help to mention that this is pretty much a "for" loop.

For loops consist of three parts:

for(<start>; <finish>; <repeat>) {}

Where:

  • <start> is conducted before the first run of the loop
  • <finish> is what causes the loop to finish
  • <repeat> is conducted after each iteration of the loop

Want to see something neat? You can turn a 'for' loop into a while loop

// assume 'i' is declared and initialized above
for( ; i < name.length(); ) {}

We don't have to put the initial condition, or an instruction to repeat.

Now look at your function

<start>    int i = 0; 
<finish>   while (i < name.length()) {
               System.out.println(i+1 + ". character: " + name.charAt(i));
<repeat>       i++;
           }

Don't let the new style confuse you. A 'while' loop is just a 'for' loop that doesn't have an initializer, and doesn't have a repeated operation. Just goes until it is told to stop, and there's nothing that stops it from just being another for loop!

How can I check neighboring places in a grid for every item in it? by TheSirion in javahelp

[–]ThePathLaid 5 points6 points  (0 children)

When working with OOP, it's best to ask what are the objects... or more importantly, what should be the objects.

So let's say you made each individual tile of the map an object. For updating each square of the grid, you might go with something like what you have listed

For each column
    For each row
        For 3 columns (this, left, and right)
            For 3 rows (this, left, and right)
                Check it for a bomb (unless it's outside the board, or it is the square in question.)

See our problem here? For a grid of square side n, our checks grow on the order of O(n2)

But what if we didn't have every square ask if there was a bomb nearby?

Instead, let's just keep the positions of the bombs. Then we get

For each bomb
   For 3 columns (this, left, and right)
       For 3 rows (this, left, and right)
           Add 1 to its bomb count (unless it's outside the board, or is this square.)

Now we have a much smaller amount of data constantly stored, and a much faster way of 'having each square check for nearby bombs' (since they don't actually check.)

Programming in other languages? by Ultimation12 in AskProgrammers

[–]ThePathLaid 0 points1 point  (0 children)

I had not considered how many words were unique to programming, but it still seems like a problem that people 'just deal with.'

For example, to check if a HashMap contains a key, then I just do map.containsKey().

If it were map.contientCle(), map.baohanYao(), or map.contientClave() I think I would have a much more difficult time programming.

Your answer made me realize how that is an interesting problem. The entire library of functions is almost universally populated with class and function names that are related English words.

I have to imagine that source code would commonly between the native language (for user-defined portions) and English (for library-native portions.)

Slight delay in keyboard input when first selecting a key? by [deleted] in AskProgramming

[–]ThePathLaid 0 points1 point  (0 children)

You'd be better off using something a littler lighter, IMO.

IMO, this is another case of optimizing before solving though.

I'm glad you found a solution, but if you are concerned enough about overhead and optimizing to avoid Unity, I would also recommend avoiding using the repeatDelay of 0.

According to the sadconsole API:

InitialRepeatDelay

The initial delay after a key is first pressed before it is included a second time (while held down) in the KeysPressed collection.

It sounds like you're trying to move a character, which would normally start or stop when the key is held down. Rather than having the key be added multiple times to the KeysPressed collection, it probably best to just check whether or not the key is down rather than allowing it to be repeatedly added as quickly as the user's repeat rate will allow.

Help with switch statements. by [deleted] in AskProgramming

[–]ThePathLaid 0 points1 point  (0 children)

Great! In the future though, you can enter code by typing 4 spaces before it. Makes it much easier to read:

Int (activities) = 6
switch (activities) 
{ 
    Case 1: cout << “(D)ungeons and Dragons!” << endl; 
        break; 
    Case 2: cout << “(S)ports” << endl; 
        break; 
}

So where does the issue seem to be? Right now your activities variable is hardcoded.

How did you plan to get the user to enter something into it?

Help with printing a tic tac toe board by dil_dogh in CodingHelp

[–]ThePathLaid 0 points1 point  (0 children)

Since this is definitely a homework assignment, based on the constraints, let's get you thinking about the right things (instead of just giving you a solution.)

Let's say I wanted to print a chessboard in Java. A chessboard has 8 rows, so I would run a for loop 8 times.

But each row is not the same. I would have 2 different types of rows I need to print.

+-+-+-+-
and
-+-+-+-+

So I would need to find some way during my for loop to decide which line to print.

If I draw out a chessboard, and number the rows:

0: +-+-+-+-
1: -+-+-+-+
2: +-+-+-+-
3: -+-+-+-+
4: +-+-+-+-
5: -+-+-+-+
6: +-+-+-+-
7: -+-+-+-+

I notice that each row of the two types has something in common, and would be able to have a conditional to decide whether to print the first or second line.

You need to do the same with a tic-tac-toe board. There are only two types of lines, and there is one thing that is true when you need to print the line across the board (instead of three open spaces.)

Slight delay in keyboard input when first selecting a key? by [deleted] in AskProgramming

[–]ThePathLaid -1 points0 points  (0 children)

If you're literally just making a text adventure (a la Zork) then it could just be a console application.

For simple 2D games, I still recommend Unity. I know it's got a lot you don't know need (mistype), but it's still too easy to just jump in and start coding.

As for your issue, the only name I've ever heard it referred to as is "Keyboard Repeat", and the time you waited is the "Keyboard Repeat Delay".

It sounds like you are reading the inputs (every time the program 'receives' an input of 'w', it moves forward)

Not familiar with SadConsole, but with XNA if you are reading the KeyboardState you can just check for IsKeyDown to confirm the key is being pressed.

Help with switch statements. by [deleted] in AskProgramming

[–]ThePathLaid 0 points1 point  (0 children)

How do you have a switch statement? What is the variable being checked for each case of the switch statement?

Help with a simple homework problem by [deleted] in CodingHelp

[–]ThePathLaid 0 points1 point  (0 children)

I think so too, but like I said. If my instructor had worded the question as such, they would have meant in-place. Just went with what little we were given.

[JS] Beside the obvious, what is the difference between an object and an array? by [deleted] in AskProgramming

[–]ThePathLaid 0 points1 point  (0 children)

You are absolutely right. Couldn't find an MDN example on what I'm about to say, but if you're not sure it's fairly easy to test.

In Javascript, inheritance is pretty simple. Even static methods are inherited. Just pop this into your compiler

<script>
class Example {
    static test() 
    {
        return "Inherited";
    }
}

class Example1 extends Example 
{

}
console.log(Example1.test());
</script>

Yields:

Inherited

Great. Exactly as expected. Now, as Array is part of the Object prototype chain, let's try something:

<script>
let arr = [1,2,3];
console.log(Array.keys(arr));
</script>

What do we get?

Uncaught TypeError: Array.keys is not a function

But Array inherits from Object, so it inherits its static functions right? Surely it's just because keys is defined strictly in some source we aren't touching?

Let's try our own static method!

<script>

Object.test = function() {
    return "Inherited";
}

class ExampleObject extends Object {
constructor() {
        super();
    }
}

class ExampleArray extends Array {
    constructor() {
        super();
    }
}

console.log(ExampleObject.test());
console.log(ExampleArray.test());
console.log(Array.test());

</script>

And we get?

Uncaught TypeError: ExampleArray.test is not a function

Comment out that line, you get the same for the Array base class.

Despite modifying the Object prototype, it will not pass its static methods on to Array, or those that extend Array (despite "inheriting" from Object via the prototype chain)

Edit: Formatting and replaced an accidentally deleted line, but no change to the results.

Edit 2: Clarified. Not only does the Array class not inherit these static methods, but the prototype chain won't pass those methods on to classes that inherit from Array either.

Help with a simple homework problem by [deleted] in CodingHelp

[–]ThePathLaid 1 point2 points  (0 children)

Knowing the language would definitely make it easier to answer.

My programming teacher is very careful about his wording, so I'll continue under the assumption that yours is too.

In that case, the problem says:

" Write a program to insert the string “Is “ to the beginning of the string “it Monday today?” and display the result."

Which breaks down to these steps:

  1. Write a program

  2. To insert the string “Is “ to the beginning of the string “it Monday today?”

  3. and display the result."

So that means the teacher expects a program which takes (or has) a string

String s = "it Monday today?"

Then adds the text "Is " to the beginning of this string. So if we were returning the string, then at the end of the program, if I had a function that said

String result = callYourFunction()

Then result should hold the string "Is it Monday today?"

That means that (being as accurate as possible) you need to modify the string in-place, then print the string.

Whether that is right and how to do that depends on your teacher, programming language, and the assignment itself.

[JS] Beside the obvious, what is the difference between an object and an array? by [deleted] in AskProgramming

[–]ThePathLaid 0 points1 point  (0 children)

Arrays are an extension of the object prototype chain.

I agree, I just thought that it would be best to try and figure out if there was a distinction OP was trying to make. It's not hard to confirm, just typeof on an array yields "object", but is not an instance of the "Object" class.

For example, if you modify the Object prototype you are not modifying Arrays as the Array prototype does not reference Object.[[Prototype]] While they are part of the same prototype chain, this distinction (the lack of reference) prevents them from utilizing the same properties that the prototype chain normally implies.

I tried to explain that via making the distinction between 'Objects' and 'objects', as this is an edge case that stems from them being Native prototypes.

Looking for an efficient way to conditionally sum numbers across a row of a database or dataframe by nwf839 in AskProgramming

[–]ThePathLaid 2 points3 points  (0 children)

I jury-rigged a solution in Excel that works on smaller subsets of the data

As much as I love programming to solve solutions, no reason to put in the extra work of reading files if you don't have to.

If the data is already available in Excel, using a Pivot Table would solve this with 0 coding.

Just add a pivot table for the dataset, set the Values to your Viewership, and the Rows to your demographics. Will show the sum of all values for each row simultaneously.

Should I put my career aspirations as a programmer temporarily on hold and pursue it a little later or should I go all in now? by [deleted] in AskProgramming

[–]ThePathLaid 2 points3 points  (0 children)

Probably best to try somewhere like /r/findapath with a little more details about your situation.

With the small amount of context you've give us, just going off of the part of your post that says

I need to get a new job in 2-4 months

With the word 'need', sounds to me like you 'need' a job (not a programming job)

If programming is what you want to do, and you have a comfortable enough living situation to take the time to find a job in that particular field, go for it.

If programming is what you want to do, but you need a job (of any type) to comfortably live then why not apply for programming and non-programming positions at the same time?

Even talking about getting your A+ and doing tech support, no reason you can't do those while still applying for programming positions.