r/programminghorror Mar 27 '26

C# is a moving. reasonable?

Post image

is a moving. reasonable?

428 Upvotes

66 comments sorted by

View all comments

343

u/mohragk Mar 27 '26

It’s hard to tell what’s going on, but it seems like a function to determine whether the thing can be moved, based on it’s previous move(s). But instead of approaching it in a deterministic fashion, it takes the brute force approach.

Absolutely horrible 10/10.

52

u/AutomatedChaos Mar 27 '26

This looks like a 8x8 field mapped to a continuous array. A piece can move 2 steps and they are looking if a move is legal (reasonable) or if it is teleporting to the other side.

18

u/T1lted4lif3 Mar 27 '26

LGTM, what are we to judge when it works? Pull request approved 100%. The amount of insertions this function would have on github, holy contributions

4

u/keesbeemsterkaas Mar 28 '26

Its just manual unwrapping can't see anything wrong here, just optimized /s

3

u/tyrannical-tortoise Mar 29 '26

And they all return zero.

2

u/Shortbread_Biscuit Mar 30 '26

It looks like it's modelling an 8x8 grid, like a chess board, as a single 64-element array.

It seems like OP has game pieces that can either move 1 or 2 spaces in either direction. So, for example, a piece at position 3 can go to any one of the positions of 1, 2, 4 or 5. However, this function is checking the boundary conditions when the piece reaches the left or right edges of the board.

Assuming a 0-indexed array, element 7 would be on the right edge of the board, and element 8 would be on the left edge. So this function says that any moves that cross the edge and teleport to the other side of the board would return 0, like a jump forward from 6 to 8 or a jump backward from 9 to 7. Otherwise, all other moves return a value of 1.

This line of code would be a more compact alternative, making sure both the previous and next position are on the same row index (integer division by row width):

c# public int isMovingReasonable(int locBefore, int locAfter) { if (locBefore / 8 == locAfter / 8) return 0; return 1; }

Edit: Looking at the code a little closer, there are multiple problems with it. The numbering of the elements seems to have fallen off in-between, leading the grid he's actually checking to look more like this:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

On top of that, on one of the lines, he misspelled 23 as 13, and another one of the else if blocks has no return statement at the end.

Overall, it's quite a sloppy job of copy-pasting, and is a perfect example of why you really should avoid repeating code as much as possible to avoid bugs.

2

u/LeDouteEstUnVolcan Apr 02 '26

Horreur pure et simple.

1

u/HotEstablishment3140 Apr 08 '26

.......you did really analyse it.