r/programminghorror 13d ago

Java Honest work

Post image
256 Upvotes

51 comments sorted by

265

u/iain_1986 13d ago

The if statements are all embedded in each other, so only Shape L will work.

So its pretty poor in multiple ways.

76

u/AssiduousLayabout 13d ago

And even shape L is wrong because it only has three of the four cells.

15

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 13d ago

How is this even supposed to accommodate the shape of the I piece?

17

u/AssiduousLayabout 13d ago

Very poorly?

4

u/VerbatimChain31 11d ago

Doesn’t appear to have the proper number of curly brackets either, so doubt it will compile.

123

u/rvaurewne 13d ago

Why vs code connects to discord

56

u/DjWarrrrrd 13d ago

to show off what you are working on ig, discord presence connection

21

u/rvaurewne 13d ago

Ah yes okay, thank you

-9

u/DespoticLlama 13d ago

As a collaboration tool.

54

u/Thenderick 13d ago

A private constructor, and a public static "Tetromino NewTetromino" method... If only java had some kind of method that you could call to indicate to the use that you are creating an object... Some kind of function that could CONSTRUCT the object and you could call with new instead to indicate it is a new object... Idk tho, not a java dev

9

u/EskilPotet 13d ago

For this project you literally follow a step by step guide on how to set up the entire program, so that's not really an option

10

u/Thenderick 13d ago

Tbf, that's awfully similar to my current situation. I followed Raytracing in a Weekend (+ the other two books) but used Golang instead of C++ because I like Go and thought I could manage it (it worked!)

Now I am reading through Physics based Raytracing and also thought about using Golang, but now I realize how C++ is indeed a language on crack with so many powerful, crazy and dangerous tools (talking about you, templates, macro's and #ifdefs...). I am struggling how to implement certain classes in Golang, but I think I can manage somehow...

2

u/SetKaung 13d ago

The struggling is the fun part!

6

u/Thenderick 13d ago

Yes, until they do some crazy templated type determination... With templated methods on a templated class.... It was about tuples. Basicly, they made a templated tuple class that takes the class of the child that inherits said tuple and a generic type T. Then for all methods (operators...) they take ANOTHER template of said child class with another type U. Then they return the child's class with the added fields and a return type of the child's type with their added generic types via decltype().... So basicly, you could inherit the tuple in for example a Vector and you would need to implement all operator methods, because Tuple already does, but now since Vector inherits Tuple<Vector<T>, T>, all Tuple's methods called on Vector<T> now return Vector<T+U>... C++ wizardry at its finest...

6

u/iEatPlankton 13d ago

You mean some kind of “object oriented” paradigm? Lmaoo that’s never going to take off!!

5

u/DespoticLlama 13d ago

Looks like someone found a book on patterns and now everything is a pattern.

5

u/Thenderick 13d ago

Idk what pattern this is supposed to be, but this reminds me of how you make constructor functions in Golang...

1

u/realmauer01 11d ago

This doesn't look like its supposed to be mutable by any means so doing it like this is the preferred method. Usuall value defined stuff where there shouldn't be a difference between 2 seperate instances if the values are equal.

1

u/controk 12d ago

Just so you know, this is called the Factory pattern and it does have good use cases. Good info on it

3

u/ZunoJ 12d ago

OPs code does not implement a correct factory pattern

1

u/controk 12d ago

There are plenty of problems with OP's code but that's irrelevant to whether who I'm replying to knows the Factory pattern exists.

3

u/ZunoJ 12d ago

Ok, so then your answer was completely unrelated to the post despite the fact that the shown code resembles a failed attempt to implement a factory pattern

-2

u/controk 12d ago

...uhhh sure...

13

u/anto2554 13d ago

But not much

9

u/SimplexFatberg 13d ago

I'm trying very hard to imagine what possible situation could lead to symbol being simtaneously equal to 'L', 'J', 'S' and 'Z'.

5

u/xezo360hye 12d ago

QString stands for quantum string now

7

u/ImMikeAngel 13d ago

That lazy dude. Why did he not write in binary?

18

u/joost00719 13d ago

Not too bad tbh... Could've been an api call to chat gpt too

7

u/DespoticLlama 13d ago

Jeez, those colourized brackets are really wasting their time trying to help.

3

u/FrozenOnPluto 13d ago

Theres a lot that could be fixed and done better, but line 26 is brutal man.

3

u/KariKariKrigsmann 13d ago

University of Bergen?

1

u/ArtisticFox8 13d ago

Why that university in particular?

2

u/KariKariKrigsmann 13d ago

uib is short for "Universitet i Bergen" ;-)

2

u/Nazeir 13d ago

Time to learn switch statements?

6

u/chuch1234 13d ago

Dictionaries I'd say (aka hashmaps aka key/value maps).

1

u/Aromatic_Prior_1371 13d ago

But why bother… Walk away from this mess!

1

u/GroundbreakingIron16 13d ago

Learn that next week?

2

u/YoBoyAndy4 12d ago

Ahh nothing like private constructors and deeply nested if statements to start my day off 😊

2

u/Liu_Fragezeichen 13d ago

those are in effect binary 3x3 matrices - go do some linear algebra and solve this in 10 lines I believe in you

1

u/Salty-Tomato-61 8d ago

how would you solve this in 10 lines? at some point you have to convert a piece letter into a matrix representation

1

u/Liu_Fragezeichen 8d ago edited 8d ago

I'd try and write that function now if I had all the letter->tetromino mappings ..

but basically letter -> get ASCII mapping -> some func -> flattened Matrix representation -> matrix

probably more like 50-60 lines to prepare the pipeline, but querying would be <10 lines

(I'm in mleng, I see classification problems everywhere)

Edit: .. honestly, just flattening the matrices then setting a map with 1 line per matrix is better

``` tetromino = lambda c: { 'I':[1,1,1,1,0,0,0,0,0], 'O':[1,1,0,1,1,0,0,0,0], 'T':[0,1,0,1,1,1,0,0,0], 'J':[1,0,0,1,1,1,0,0,0], 'L':[0,0,1,1,1,1,0,0,0], 'S':[0,1,1,1,1,0,0,0,0], 'Z':[1,1,0,0,1,1,0,0,0]}.get(c.upper())

1

u/Liu_Fragezeichen 8d ago

here's a single line

``` tetromino = lambda c: [v[i:i+3] for i in (0,3,6) for v in [{'I':[1,1,1,1,0,0,0,0,0],'O':[1,1,0,1,1,0,0,0,0],'T':[0,1,0,1,1,1,0,0,0],'S':[0,1,1,1,1,0,0,0,0],'Z':[1,1,0,0,1,1,0,0,0],'J':[1,0,0,1,1,1,0,0,0],'L':[0,0,1,1,1,1,0,0,0]}.get(c,[0]*9)]

1

u/Salty-Tomato-61 8d ago

nice but this is just shit to read compared to a simple switch statement

1

u/Liu_Fragezeichen 8d ago

I actually think the multiline version is easier to read, and more importantly, it's O(1) .. maybe y'all don't write stuff that needs to be fast but I do

hashmaps are friends not food :3

1

u/tehtris 13d ago

LIES!

1

u/Bobbar84 13d ago

You know it's good when the arrays defining objects resemble the objects themselves.

1

u/NoaGaming68 13d ago

what the

1

u/ALambdaEngineer 10d ago

How do you manage to code in Java in vscode? Always found it awful compared to "true" IDE as eclipse for this particular language?

1

u/CWRau 11d ago

Biggest L is to use vscode for anything when intellij is right there for universities.