this post was submitted on 22 Feb 2024
696 points (96.4% liked)

Programmer Humor

32469 readers
687 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
all 48 comments
sorted by: hot top controversial new old
[–] [email protected] 97 points 8 months ago (2 children)

Reminds me of how I made tic tac toe game for my first university assignment 🀣

[–] [email protected] 41 points 8 months ago (1 children)

That has a finite number of moves. And then you could possibly generate the code based on that finite set.

[–] [email protected] 42 points 8 months ago (1 children)

Chess technically has a finite number of moves. Although its a huge number and some have theorized its larger than the number of atoms in the known universe.

[–] [email protected] 7 points 8 months ago (2 children)

Technically it's infinite, but that's because of a stalemate. You can keep repeating the same moves over and over.

[–] [email protected] 13 points 8 months ago (2 children)

Stalemate is after 3 repeat moves, so no.

[–] [email protected] 10 points 8 months ago

It's not just repeated moves, a draw can be called if the board is in the same state 3 times at all during the game; if you get to the same position 3 times using different moves that still counts, even if it was a white move the first two times and a black move the third.

The game also ends after 50 moves with no captures or pawn moves so you can't play indefinitely by just avoiding those board states. Interestingly those two moves also make it impossible to return to a previous board state (pawns can't move backwards, extra pieces are never added) so if you're enforcing both rules in code you can safely discard previous board states every time you reset the move counter.

[–] [email protected] 4 points 8 months ago

But it’s a state machine, and you could easily contrive scenarios for infinite moves, without stalemate.

[–] [email protected] 6 points 8 months ago

*finite, I'm assuming autocorrect gotcha

[–] [email protected] 40 points 8 months ago

"hey, as long as it works..."

[–] [email protected] 87 points 8 months ago (2 children)

YandereDev coding chess.

Also who has a variable to store an input and decides to name it "player"?

[–] [email protected] 10 points 8 months ago

Thank you! That was driving me nuts.

[–] [email protected] 1 points 8 months ago

Someone editing a tutorial without knowing what they're doing

[–] [email protected] 60 points 8 months ago (2 children)
[–] [email protected] 27 points 8 months ago

Tldr Estimated total number of legal chess positions is (4.822 ± 0.028) * 10⁴⁴

[–] [email protected] 3 points 8 months ago* (last edited 8 months ago)

"John Tromp and Peter Γ–sterlund estimated the number of legal chess positions with a 95% confidence level at ( 4.822 Β± 0.028 ) Γ— 10^44, based on an efficiently computable bijection between integers and chess positions."

%95 confidenece level? Did they make a poll?

[–] [email protected] 47 points 8 months ago (1 children)

Is this the same person who coded the odd/even function?

[–] [email protected] 34 points 8 months ago* (last edited 8 months ago) (6 children)

I have a better odd/even function:

bool isEven( long long x ) {  
  if ( x < 0 ) x = -x;  
  if ( x == 1 )  
    return false;  
  if ( x == 2 )  
    return true;  
  return isEven( x - 2 );  
}

This will work for both negative numbers and arbitrarily large integers. I've tested it up to 26 but I'm pretty sure it will work up to infinity.

Though serious question: Why are these forums coded in such a way that they ignore single newlines? How hard is it to replace a newline with a br tag?

Edit: added two spaces, should look much better.

Edit2: just looks a bit better lol

Edit3: better now?

[–] [email protected] 18 points 8 months ago (1 children)

It’s Markdown syntax. You can actually format it nicely in a code block:

bool isEven( long long x ) {
  if ( x < 0 ) x = -x;
  if ( x == 1 )
    return false;
  if ( x == 2 )
    return true;
  return isEven( x - 2 );
}

You do that by adding ``` above and below it. To force single line breaks, you can terminate your sentences with two spaces, or a backslash.

[–] [email protected] 4 points 8 months ago

Ah thank you, the editor in connect isn't great and doesn't have the markdown guide or buttons for the preformatted block, so I couldn't find the character combo to use it (and never would have guessed that lol). Also it keeps giving an error when I edit, though the edit is going through.

[–] [email protected] 10 points 8 months ago* (last edited 8 months ago) (1 children)

Is this meant to be a joke or is it intended to be a serious solution?

Asking for someone who lacks a sense of humor.

Ok, fine, I'm asking for me. That person is me.

[–] [email protected] 13 points 8 months ago (1 children)

I want to see how you'd reply if I said it was serious. So let's go with that. This is the best way to determine if a number is even in c/c++.

[–] [email protected] 7 points 8 months ago* (last edited 8 months ago) (1 children)

The reply would have been return x % 2 == 0, or if you wanted it to be less readable return !(x&1).

But if you were going for a way that is subtly awful or expensive, just do a regex match on "[02468]$". You don't get a stack overflow with larger numbers but I struggle to think of a plausible bit of code that consumes more unnnecessary cycles than that...

[–] [email protected] 4 points 8 months ago (1 children)

The less readable one is faster, though I'd be surprised if the compiler doesn't generate the same code for both of those options with optimizations enabled.

I like the regex one as another unnecessarily complicated way of doing it. It also involves a string conversion.

The way that was being hinted at before my reply was a large series of if statements: is it 2? 4? 6? 8? And so on. It's plausible in that I could see a beginner programmer using that method. And honestly, knowing how people can get pigeon holed into looking at a problem from some weird angle but still having the determination to figure it out, most solutions are plausible. Often when they get pointed in a better direction, it's not so much a case of them learning something new as it is a facepalm kinda moment where they feel embarrassed at not seeing that. I've done it myself many times lol.

It's always funny seeing someone use the wrong tool for the job but still getting the job done, even when it's me.

[–] [email protected] 3 points 8 months ago (1 children)

Godbolt to the rescue! So gcc 13.2 certainly does produce the same code, though a lot of other versions and compilers do it slightly differently. Surprisingly, clang doesn't optimise this and uses idiv for the modulo version.

[–] [email protected] 1 points 8 months ago

Awesome, thank you for that link. I should have guessed this was a thing but it hadn't even occurred to me.

And yeah, it is surprising that clang doesn't treat mod power of 2 as a special case. It looks like I can't type in code on mobile, does clang handle divide by power of 2 as a idiv or shift?

[–] [email protected] 9 points 8 months ago

I love the use of recursion. Slick.

[–] [email protected] 8 points 8 months ago (1 children)

For large numbers, you might get a stack overflow. Depends on whether the compiler recognizes that it's a tail recursion and then handles it appropriately.

As for why Lemmy ignores single newlines, that's because it uses Markdown for its comment syntax, which happens to handle newlines that way. If you want a single newline to display, you have to add two spaces at the end of the line.

Here, you really would have wanted to use a code block, though, which you can create with backticks

```
like
so.
```

Normally, this would display "like so." in a monospace font and handle newlines as you expected:

like
so.
[–] [email protected] 9 points 8 months ago* (last edited 8 months ago) (1 children)

Just need to add some code that saves the base stack pointer, detects when the stack is about to run out, then saves the current number, resets the stack to the original pointer but now with the current number as x and continues on to fix the stack overflow issue. You don't even have to restore the stack when it's all unwinding!

The integer overflow issue would be more complicated to solve except it isn't really an issue because chopping off the upper bits doesn't affect the eveness of a value.

Oh and I thought of a quick way to verify the result of the algorithm. It should give the same result as this expression that you can use in your test suite:

!(x & 1)

Edit: um, new formatting problem... And that amp shows up in the edit box so it might even keep expanding.

[–] [email protected] 8 points 8 months ago (1 children)

The ampersand thing is a known bug in Lemmy. It has to do with input sanitation, as I understand.

[–] [email protected] 6 points 8 months ago

Yeah looks like it's sanitizing both on submit and when displaying.

[–] [email protected] 5 points 8 months ago

Some quirk of this version of Markdown I think.
Try putting two spaces at the end of a line before your single newline.

[–] [email protected] 38 points 8 months ago* (last edited 8 months ago)

I've seen someone code that way. Not since high school, but that's a way that some people think coding works when they start out writing code.

This person was trying to write a game in (trigger warning: nostalgia) QBasic and had it drawing kindof a Pacman kind of character. And in pseudocode basically what he was doing was:

// Draw character with mouth open at (100, 100)
moveCursorTo(100, 100)
drawLineFromCursorAndMoveCursor(116, 100)
drawLineFromCursorAndMoveCursor(108, 108)
drawLineFromCursorAndMoveCursor(116, 116)
drawLineFromCursorAndMoveCursor(100, 116)
drawLineFromCursorAndMoveCursor(100, 100)

// Wait for half a second.
sleepSeconds(0.5)

// Draw character with mouth closed at (101, 100)
moveCursorTo(101, 100)
drawLineFromCursorAndMoveCursor(109, 100)
drawLineFromCursorAndMoveCursor(117, 108)
drawLineFromCursorAndMoveCursor(109, 116)
drawLineFromCursorAndMoveCursor(101, 116)
drawLineFromCursorAndMoveCursor(101, 100)

// Wait for half a second.
sleepSeconds(0.5)

// Draw character with mouth open at (102, 100)
moveCursorTo(102, 100)
drawLineFromCursorAndMoveCursor(118, 100)
drawLineFromCursorAndMoveCursor(110, 108)
drawLineFromCursorAndMoveCursor(118, 116)
drawLineFromCursorAndMoveCursor(110, 116)
drawLineFromCursorAndMoveCursor(102, 100)

// Wait for half a second.
sleepSeconds(0.5)

...

He hadn't gotten to the point of working in user controls. (Like "change direction to 'up' when user presses the 'up' key" or whatever.) And understandably had no idea how that would work if/when he got that far.

[–] [email protected] 38 points 8 months ago (1 children)

What no theory does to a mfer

[–] [email protected] 1 points 8 months ago

Something my DE needs to understand

[–] [email protected] 13 points 8 months ago

And now they'll have to fix their board, for it is wrong. h3 is a white square.

[–] [email protected] 13 points 8 months ago

what if you write an algorithm that will produce the necessary code for each possible move, so you don't have to type them all manually

[–] [email protected] 13 points 8 months ago

Reminds me of when I downloaded Tic Tac Toe for my graphing calculator in high school. It wasn't this verbose, but the original author copy pasted the logic for each turn. Even I knew this was awful, so I refactored it to use a loop.

[–] [email protected] 8 points 8 months ago

Non-monospaced font for programming. Ya'll know this is satire.

[–] [email protected] 7 points 8 months ago (1 children)

Btw, is there a cli chess game with such characters as symbols?

[–] [email protected] 4 points 8 months ago* (last edited 8 months ago)

I imagine there are a few out there, but unicode has chess symbols so it's certainly easy enough to do if you have a language, font, and terminal that supports it.

[–] [email protected] 7 points 8 months ago

When I was learning c++ in high school, I made a working 10x10 implementation of Conway's Life before I had learned about arrays.

Even then I didn't do it like this. Just had 200 variables for the front and back buffers, and one big function with individual checks for each grid coord variable to check it's neighbors.

[–] [email protected] 4 points 8 months ago

What no theory does to a mfer

[–] [email protected] 3 points 8 months ago

Reminds me of my lottery number randomizer in VB 3.0 I calculated 10 rows and checked each input box against the others without any loops, and then each row was just copy pasted. 🀦

[–] [email protected] 3 points 8 months ago

someone should have started with ~~tingo~~ i mean bingo

[–] [email protected] 3 points 8 months ago

If it works, it works.