Gerard Charles

Ranch Hand
+ Follow
since Jan 21, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
8
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
11
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Gerard Charles

Any thoughts on preferences / style of calling subclass method? Given:

Do ya'll prefer

or
or something else? (Animal is from an external class library and changing it is not a feasible option.)
4 years ago

Tim Holloway wrote:I would be very interested in how you make the determination that a tuple is "strongly typed". Of the 3 languages that I can think of that can return tuples: LISP (a tuple here is a list), Python, Perl - and I think I had a 4th, but it just fell out of my mind. None of those languages defines a tuple with strong typing or even fixed-length. A Python or Perl method can have multiple return statements and each statement could return a simple value, a tuple, or some other data type like an array or dictionary and no two of those return statements would be obliged to return the same sort of thing(s).

I haven't dug into Scala enough to say whether it defines fixed-type tuples, but if so, it's an outlier. Not that I'd complain. Just pointing out that no such assurances occur in most other popular languages that can return tuples.



FWIW, C++ defines strongly typed tuples. https://en.cppreference.com/w/cpp/utility/tuple
4 years ago


Why is that a not (!) ? I'd have thought we'd want the positive case there.
All models are wrong. Some models are useful.

A bishop is only a bishop when it's on the board. Otherwise it's a funny shaped object. It works for me to model it's movement in the Piece / Bishop class hierarchy. If you prefer to put Piece motion in a Board, go for it.

The solution I've outlined addresses the question the original poster asked. The linked page https://open.kattis.com/problems/chess is a different problem and would have a different solution.

I'm a working engineer. Engineering is about maximizing the ratio benefit / cost . The purpose of a social media post is to convey an idea; what my post is trying to convey is how to efficiently model the diagonals on a chessboard. The test cases were sufficient for me to be reasonably confident the code is working as intended.
4 years ago

Knute Snortum wrote:Looks good!

I think you can simplify the Diagonal class to one field, and use

Also, in the Piece class, I'd add after line 38:
I haven't tested any of this, though.



Don't see how Diagonals could work with a single field, since each square is on two diagonals.

Concur the IllegalArgumentException could be improved.
4 years ago
You don't have a coding problem.

You have a modeling problem. You've already identified the solution to "Can two bishops attack" translates to are they on the same diagonal?

So the question becomes -- how do you model the diagonal of a chess square? Assuming standard algebraic chess notation given in the link, if we increase both the column letter and the row number by one, we're on the same diagonal. Alternatively, if we increase the column letter and decrease row by one, we're on the same diagonal. So if the sum or difference of the row and column coordinates are the  same, the pieces are on the same diagonal.

The code code then like something like this:

Piece is an immutable class that models the row and column as integers. Representation seemed easier than using the row letter (character), but I could see storing the a to g letter instead of the column number.

Diagonals is a simple immutable return class for the diagonals method of Piece, which calculates the two diagonals as the sum and difference as the coordinates (line 60). Two diagonals intersect if either of the two values are equal.

Then Bishop becomes straightforward.


and a test case

4 years ago

Junilu Lacar wrote:One pair I worked with at Global Day of Coderetreat last Saturday had code like this:

That pair was actually working in Python but the code they had was essentially the same as that Java code.

My question to folks here is this: Would you see this code and think, "Oh, code smell..." or would you think it's a reasonable API?

I'd like to get some other opinions/perspectives before sharing what mine are.

Thanks!



Nope, doesn't smell at all.

Code can't "smell." It's conceptual, inorganic text that doesn't emit molecules. As near as I can tell, "smell" means "I don't like it / I wouldn't have done it this way." It's an opinionated, pejorative term that tells me the speaker is unable or unwilling to communicate in a professional manner.

As far as the method call, from looking at it, I have  pretty good idea what it's going to do. It mirrors the pattern found in the JDK graphics API (e.g. fillText("Cow", 42,54). If I have any doubt, I look at the declaration, because that's where functions are described in Java. Good IDEs will jump you there quickly and/or show the API in a hover. JetBrain's Intellij will fill in the parameter names for you.
5 years ago

Liutauras Vilda wrote:

Gerard Charles wrote:some of those neighborLocation values will be negative, but because the code calls worldWrapped they become positive. Logically, I'd expect setAlive to do the same thing.


This is what it does.


My bad, was looking at the old version.
5 years ago

Liutauras Vilda wrote:

Gerard Charles wrote:because if the GUI's calculated location of a point is negative


Wondering how that could happen? Does it work like x and y axis where 0 is in a center? My model follows rows, columns idea starting from the upper left corner where 0's are.


Well, it happens in:

some of those neighborLocation values will be negative, but because the code calls worldWrapped they become positive. Logically, I'd expect setAlive to do the same thing.
5 years ago

Piet Souris wrote:We have arrived at a point where the discussion is hard to understand, if at all.

Anyway: what I was wondering: if incorporating some GUI makes it necessary to adjust the World code in places, then what is wrong with, or missing from, the design?



Well, probably easiest to run my proposed variant to see what the interface is doing first and then look at how GraphicDisplay is trying to allow CShape to place a pattern on the world.

But it wasn't necessary to adjust the World, nor is their necessarily anything wrong with the design -- I just playing around with merging two different designs. Personally I thought the question: Hey, world, what's the coordinates of the Location four to the left of this one? was a reasonable one to ask.
5 years ago

Liutauras Vilda wrote:

Gerard Charles wrote:It include's a drop down to include a couple gliders. The implementation of that needs to be able to add
locations modulo the grid, which is why the code is calling worldWrapped.


I don't think so. API does that already, you don't need to do that in a front-end. You just need to pass pattern's alive cells locations.

Gerard Charles wrote:The GUI intentionally messes with the world -- it allows the user to click on the grid and toggle cells on or off


Understand that, but since my nextGeneration() always construct a new world, for me it makes sense to have all in tact and don't rely on GUI implementor's vision. If GUI wants to change something, it can change through aliveAt(), deadAt(), that way I can assure that world is in consistent state. So getWorldMap() actually supposed to return immutable map.


Actually I didn't need a public worldWrapped since I added an "add" method, but that does need worldWrap, because if the GUI's calculated location of a point is negative (e.g. when placing a glider) the world API doesn't wrap the left side to the right (or top to bottom)-- actually fails silently due to the Math.abs in the implementation.



5 years ago

Liutauras Vilda wrote:
World.java Cell at(Location) as well as worldWrapped(Location) instead private became both public. For what reason?
...

2. Map<Location, Cell> getWorldMap(), and that would be a copy of original, so front end couldn't mess up with world's state.


The GUI intentionally messes with the world -- it allows the user to click on the grid and toggle cells on or off (apparently incorrectly with the incorrect deadAt).
It include's a drop down to include a couple gliders. The implementation of that needs to be able to add
locations modulo the grid, which is why the code is calling worldWrapped.

5 years ago

Liutauras Vilda wrote:

Gerard Charles wrote:I'm am curious as to your thought process in the static Location.of instead of simply having a public Location(int rowIndex, int columnIndex) constructor?


If you get a chance, read an Item 1 from Effective Java by Joshua Bloch. It has comprehensive chapter about that, advantanges and dis

However, my main reason was readability in this case. I really like that way, which seem to go in line what newer Java version follow as as naming convention, i.e.: List.of(...), Map.of(...).... Yes, new Location() is also fairly readable in this case.

One of other advantages could be, that I'm still left with an option to cache Location objects if I wanted to in a future, while habitual way to create object through constructor does not  give you such flexibility, whenever you write "new" you create a new object.

Don't want to list other advantages and repeat what is way better written in a book, but there are quite a few advantages over constructors.



Interesting read. (found a copy Effective Java). (Of course, we no longer have to specify all the types in generic creation, a development Bloch seemed to anticipate).

I'm just used to the standard Java library conventions (e.g. Point2D constructor), so that's what I'd expect to find in a "point" class.
5 years ago
So I was curious at to what it would take to connect my JavaFX front end to this implementation. (See pull request on github.) Fairly straightforward, except a a couple tweaks:
* I needed a deadAt method to mirror aliveAt.
* Had to make some methods public to set / access the state of the World.

I'm am curious as to your thought process in the static Location.of instead of simply having a public Location(int rowIndex, int columnIndex) constructor?
5 years ago

Junilu Lacar wrote:Names like WorldConsumer and CellConsumer are smelly.

Why not Consumer<World> or Consumer<Cell> instead?

See the difference?



Consumer is already defined in the JDK. Generics don't work with primitives.

What's the definition of smelly  in the context of code?
5 years ago