Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
2048 Puzzle
#21
And now, we have 2048 - 32 LOC!!

Code: (Select All)
_Title "2048 - 32 LOC": DefLng A-Z: Randomize Timer: ReDim Shared B(15), Score, F: Screen _NewImage(400, 400, 32): _ScreenMove 450, 180: _PrintMode _KeepBackground: F = _LoadFont("arial.ttf", 28): _Font F: AddNewCell: AddNewCell '                              = Add 2 random cells to Board for starters ==
Do
5 Cls , _RGB(100, 100, 250): Color _RGB(255, 255, 255) ' == DrawBoard ==
For x = 0 To 3: For y = 0 To 3 ' == find power of 2 that each Board(x,y) is for bg& in tiles ==
If B(x + 4 * y) Then power = Log(B(x + 4 * y)) / Log(2) Else power = 0
bg& = _RGB32(255 - 17 * power): Line (x * 100 + 3, y * 100 + 3)-Step(100 - 3, 100 - 3), bg&, BF: Line (x * 100 + 3, y * 100 + 3)-Step(100 - 3, 0 - 3), &HFFFFFFFF, B ' == accent tile ==
If B(x + 4 * y) > 0 Then n$ = _Trim$(Str$(B(x + 4 * y))): ox = (100 - _PrintWidth(n$)) / 2: oy = (100 - _FontHeight(F)) / 2: Color &HFF000000: _PrintString (x * 100 + ox + 2, y * 100 + oy + 2), n$: Color &HFFFFFFFF: _PrintString (x * 100 + ox, y * 100 + oy), n$
Next: Next: _Title "2048 - " + "Score: " + Str$(Score): _Display
For y = 0 To 3: For x = 0 To 3 ' == Look for empty space or combine move ==
If B(y * 4 + x) = 0 Then 10 ' == yes a move is left, skip to 10 ==
If y < 3 Then If B(y * 4 + x) = B((y + 1) * 4 + x) Then 10 ' == a move left goto 10 ==
If x < 3 Then If B(y * 4 + x) = B(y * 4 + (x + 1)) Then 10 ' == a move left goto 10 ==
Next: Next
Line (75, 100)-(325, 300), _RGBA(0, 0, 0, 40), BF: s$ = "No More Moves!": _PrintString (75 + (250 - _PrintWidth(s$)) / 2, 166), s$: s$ = "Score:" + Str$(Score): _PrintString (75 + (250 - _PrintWidth(s$)) / 2, 266), s$: _Display: Beep: Exit Do
10 Do: k$ = InKey$: _Limit 30: Loop Until k$ <> "": ReDim t(15): jm = 0 ' == start temp board array t() and make sure only arrow keys processed ==

Select Case k$: Case Chr$(0) + "K": jm = 4: ks = 0: ke = 3: kstep = 1: km = 1: Case Chr$(0) + "M": jm = 4: ks = 3: ke = 0: kstep = -1: km = 1: Case Chr$(0) + "H": jm = 1: ks = 0: ke = 3: kstep = 1: km = 4: Case Chr$(0) + "P": jm = 1: ks = 3: ke = 0: kstep = -1: km = 4: End Select
If jm = 0 Then 5 ' == back to the drawing board ! ==
For j = 0 To 3 ' == outer loop is always same numbers but could be for x or y ==
If jm = 4 Then p = j * jm + ks Else If kstep = 1 Then p = j Else p = 12 + j
For k = ks To ke Step kstep: If B(j * jm + k * km) <> 0 Then Select Case t(p): Case B(j * jm + k * km): t(p) = t(p) + B(j * jm + k * km): Score = Score + t(p): p = p + kstep * km: Case 0: t(p) = B(j * jm + k * km): Case Else: p = p + kstep * km: t(p) = B(j * jm + k * km): End Select
Next k, j: For j = 0 To 15: B(j) = t(j): Next: AddNewCell: Loop Until _KeyDown(27): Sleep ' == Hold screen at final state until user presses a key ==
Sub AddNewCell ' == Insert new cell onto board in random unused blank ==
Dim temp(15), x, y, c, i, x1, y1
For y = 0 To 3: For x = 0 To 3 ' == save index of empty cells for random pick ==
If B(y * 4 + x) = 0 Then temp(c) = y * 4 + x: c = c + 1
Next: Next
If c > 0 Then ' == choose one place to make a number, convert index to row =y1, col =x1 ==
i = Int(Rnd * c): y1 = Int(temp(i) / 4): x1 = temp(i) Mod 4 ' == pick and convert index ==
If Rnd < .8 Then B(4 * y1 + x1) = 2 Else B(4 * y1 + x1) = 4 ' = more 2's, 80% than 4's, 20%
End If
End Sub

' =============================================================================================
' Instructions briefly:
' Use arrow keys to move all numbers on board to a side, like numbers will combine and double
' Score is shown in title bar. ESC quits. Objective is to get a tile at 2048 but can go further.

'==============================================================================================
' Tracking bplus mods
' From Dav's 2048.BAS v1.01 Puzzle, OCT/2024 New for v1.01: Screen autoscales
' 2024-10-17 Dav's original 468 tons of blank lines for easy reading removed plus all that redundant
' Flash code and stuff, reduced to last paragraph of screening the Board. Added Option _Explicit and
' Dim lines for all variables not Dim'd for Option _Explicit. Now 356 LOC!

' Next dump rounded box and TEXT subs and replace with font and Line BF
' removed ss for screen shrinkage factor just used a 400 x 400 screen
' RBox replaced with standard Line
' OK now 257 lines about even with Steves
' OK 237 with combined DoRight DoLeft
' OK 215 line combined DoUp and DoDown
' OK 192 lines with reduction in picking a new place for next number
' OK 186 lines with reduction of RemainingMove
' OK 182 more edits of comments
' OK 174 no blank lines!
' Man I am looking at all those lines for color in DrawBoard.
' OK 158 lines.
' OK 154 4 lines off RemainingMove does it still work?
' OK 148 some more lines in UDArrow and LRArrow
' OK 142 double parking where it makes sense
' OK 138 subst IF Then for Select Case
' OK 131 sub DEFLNG A-Z for Option _Explicit remov DIM's
' OK the Double Parking is getting more serious
' OK 110 enough! 10/17/2024
' OK last night right after I posted 110, I immediately saw 2 obvious lines to take out
' WTH! in for a penny in for a 100, more judicious double/triple parking

' Rules for double/triple/more statements on one line by way of colon, I call Double Parking:
' + Premise: Avoid _code line extensions if at all possible, all lines < 100 chars nice too.
' + For sure, multiple assignments can go on one line specially if all are related.
' - When it comes to documenting varaibles DO NOT Double Park, following Terry's descriptive method.
' + For sure, multiple short Sub calls specially if all are related.
' + Doubling up on For loops specially for 2D array For y = 0 to ny : For x = 0 to nx ... Next: Next
' + Line 3 saved on Option _Explicit and combined with Random. I don't recommend starting
' without Option _Explicit because it comes in handy when name changes come ie to shorten
' names so everything fits in 100 chars across line. It should be a last step in reducing LOC.

' OK 97 LOC < 100 10/18/2024 am post
' OK 95 cut 2 but had to add one because line was too long for mult-assignments
' When adding comments I had to change Dav' temp Row() and Col() words. He or I had/have them
' reversed.
' 2024-10-19 94 LOC DrawBoard NOT needed in AddCell and main both I chose in main only.
' ============================================================================

' 2024-10-20 2048 - 1D Board combine all arrow handling subs into one, save LOC!
' OK 75 LOC
' 2024-10-21 2048 - 64 LOC
' subs and a function reinserted back into Main


The program now fits completely on a single QB64PE code screen for me. YAYS!! I win!
Reply
#22
That is so ugly! I know it's supposed to be a joke but Good God! I hate horizontal scrolling.

I got to say it again, I hate horizontal scrolling! Maybe as much as Steve hated MS yesterday, see that's your punishment for posting that shit. LOL!

The above example just proves it. Try to read that first line all the way across, you have to use the vertical scoll bar to get to the bottom to use the horizontal scroll then scroll up again to read the next section on same line if you don't lose your place... and repeat! You got me swearing...

but Steve wouild say that was the intentional message, no he wont now that I said it for him Wink

Anyway I am posting again so you all can compare uglyness versus beauty on the same page without having to jump back and forth in pages:

Up above ugly, down below beauty, disregarding LOC for a sec:
Code: (Select All)
_Title "2048 - 64 LOC" ' bplus 2024-10-21                 == instructions and code updates below ==
DefLng A-Z: Randomize Timer '                     == Basic Setup: Default Type and Random starts ==
ReDim Shared B(15), Score, F '                                == Globals Game Board, Score, Font ==
Screen _NewImage(400, 400, 32): _ScreenMove 450, 180 '                           == Screen Stuff ==
_PrintMode _KeepBackground: F = _LoadFont("arial.ttf", 28): _Font F '             == Print stuff ==
AddNewCell: AddNewCell '                              = Add 2 random cells to Board for starters ==
Do
    5 Cls , _RGB(100, 100, 250): Color _RGB(255, 255, 255) '                        == DrawBoard ==
    For x = 0 To 3: For y = 0 To 3 ' == find power of 2 that each Board(x,y) is for bg& in tiles ==
            If B(x + 4 * y) Then power = Log(B(x + 4 * y)) / Log(2) Else power = 0
            bg& = _RGB32(255 - 17 * power) '  == set shade grey, the higher the value the darker ==
            Line (x * 100 + 3, y * 100 + 3)-Step(100 - 3, 100 - 3), bg&, BF '       == draw tile ==
            Line (x * 100 + 3, y * 100 + 3)-Step(100 - 3, 0 - 3), &HFFFFFFFF, B ' == accent tile ==
            If B(x + 4 * y) > 0 Then '                          == label cells that aren't blank ==
                n$ = _Trim$(Str$(B(x + 4 * y))) '          == find offsets for centering in cell ==
                ox = (100 - _PrintWidth(n$)) / 2: oy = (100 - _FontHeight(F)) / 2
                Color &HFF000000: _PrintString (x * 100 + ox + 2, y * 100 + oy + 2), n$ '  shade ==
                Color &HFFFFFFFF: _PrintString (x * 100 + ox, y * 100 + oy), n$ ' =& print value ==
            End If
    Next: Next
    _Title "2048 - " + "Score: " + Str$(Score): _Display '                    ==  know the score ==
    For y = 0 To 3: For x = 0 To 3 '                     == Look for empty space or combine move ==
            If B(y * 4 + x) = 0 Then 10 '                      == yes a move is left, skip to 10 ==
            If y < 3 Then If B(y * 4 + x) = B((y + 1) * 4 + x) Then 10 '  == a move left goto 10 ==
            If x < 3 Then If B(y * 4 + x) = B(y * 4 + (x + 1)) Then 10 '  == a move left goto 10 ==
    Next: Next
    Line (75, 100)-(325, 300), _RGBA(0, 0, 0, 40), BF ' = OH NO! Game Over message box break the ==
    s$ = "No More Moves!": _PrintString (75 + (250 - _PrintWidth(s$)) / 2, 166), s$ '     == bad ==
    s$ = "Score:" + Str$(Score): _PrintString (75 + (250 - _PrintWidth(s$)) / 2, 266), s$ ' news ==
    _Display: Beep: Exit Do '                == Sleep after Do loop holds screen in final state. ==
    10 Do: k$ = InKey$: _Limit 30: Loop Until k$ <> "" '                         == wait for key ==
    ReDim t(15): jm = 0 '  == start temp board array t() and make sure only arrow keys processed ==
    If k$ = Chr$(0) + "K" Then jm = 4: ks = 0: ke = 3: kstep = 1: km = 1 '         == Left arrow ==
    If k$ = Chr$(0) + "M" Then jm = 4: ks = 3: ke = 0: kstep = -1: km = 1 '       == Right arrow ==
    If k$ = Chr$(0) + "H" Then jm = 1: ks = 0: ke = 3: kstep = 1: km = 4 '          == Up arrow  ==
    If k$ = Chr$(0) + "P" Then jm = 1: ks = 3: ke = 0: kstep = -1: km = 4 '        == Down arrow ==
    If jm = 0 Then 5 '                                            == back to the drawing board ! ==
    For j = 0 To 3 '                == outer loop is always same numbers but could be for x or y ==
        If jm = 4 Then p = j * jm + ks Else If kstep = 1 Then p = j Else p = 12 + j
        For k = ks To ke Step kstep '                             == inner loop, main processing ==
            If B(j * jm + k * km) <> 0 Then ' only handle cells with a value
                If t(p) = B(j * jm + k * km) Then ' == ah a matching tile! combine & update score =
                    t(p) = t(p) + B(j * jm + k * km): Score = Score + t(p): p = p + kstep * km
                ElseIf t(p) = 0 Then '                   == move board value into next open slot ==
                    t(p) = B(j * jm + k * km)
                Else '                                   == move board value into next open slot ==
                    p = p + kstep * km: t(p) = B(j * jm + k * km) '    == and update next p slot ==
                End If
            End If
        Next
    Next
    For j = 0 To 15: B(j) = t(j): Next: AddNewCell '       == Copy t() into B() array & Add cell ==
Loop Until _KeyDown(27) '                            == Escape quits before running out of moves ==
Sleep '                                   == Hold screen at final state until user presses a key ==
Sub AddNewCell '  == Insert new cell onto board in random unused blank ==
    Dim temp(15), x, y, c, i, x1, y1
    For y = 0 To 3: For x = 0 To 3 '                == save index of empty cells for random pick ==
            If B(y * 4 + x) = 0 Then temp(c) = y * 4 + x: c = c + 1
    Next: Next
    If c > 0 Then '      == choose one place to make a number, convert index to row =y1, col =x1 ==
        i = Int(Rnd * c): y1 = Int(temp(i) / 4): x1 = temp(i) Mod 4 '  == pick and convert index ==
        If Rnd < .8 Then B(4 * y1 + x1) = 2 Else B(4 * y1 + x1) = 4 ' = more 2's, 80% than 4's, 20%
    End If
End Sub

' =============================================================================================
' Instructions briefly:
' Use arrow keys to move all numbers on board to a side, like numbers will combine and double
' Score is shown in title bar. ESC quits. Objective is to get a tile at 2048 but can go further.

'==============================================================================================
' Tracking bplus mods
' From Dav's 2048.BAS v1.01 Puzzle, OCT/2024  New for v1.01: Screen autoscales
' 2024-10-17 Dav's original 468 tons of blank lines for easy reading removed plus all that redundant
' Flash code and stuff, reduced to last paragraph of screening the Board. Added Option _Explicit and
' Dim lines for all variables not Dim'd for Option _Explicit. Now 356 LOC!

' Next dump rounded box and TEXT subs and replace with font and Line BF
' removed ss for screen shrinkage factor just used a 400 x 400 screen
' RBox replaced with standard Line
' OK now 257 lines about even with Steves
' OK 237 with combined DoRight DoLeft
' OK 215 line combined DoUp and DoDown
' OK 192 lines with reduction in picking a new place for next number
' OK 186 lines with reduction of RemainingMove
' OK 182 more edits of comments
' OK 174 no blank lines!
' Man I am looking at all those lines for color in DrawBoard.
' OK 158 lines.
' OK 154 4 lines off RemainingMove does it still work?
' OK 148 some more lines in UDArrow and LRArrow
' OK 142 double parking where it makes sense
' OK 138 subst IF Then for Select Case
' OK 131 sub DEFLNG A-Z for Option _Explicit remov DIM's
' OK the Double Parking is getting more serious
' OK 110 enough! 10/17/2024
' OK last night right after I posted 110, I immediately saw 2 obvious lines to take out
' WTH! in for a penny in for a 100, more judicious double/triple parking

' Rules for double/triple/more statements on one line by way of colon, I call Double Parking:
' + Premise: Avoid _code line extensions if at all possible, all lines < 100 chars nice too.
' + For sure, multiple assignments can go on one line specially if all are related.
' - When it comes to documenting varaibles DO NOT Double Park, following Terry's descriptive method.
' + For sure, multiple short Sub calls specially if all are related.
' + Doubling up on For loops specially for 2D array For y = 0 to ny : For x = 0 to nx ... Next: Next
' + Line 3 saved on Option _Explicit and combined with Random. I don't recommend starting
'   without Option _Explicit because it comes in handy when name changes come ie to shorten
'   names so everything fits in 100 chars across line. It should be a last step in reducing LOC.

' OK 97 LOC < 100  10/18/2024  am post
' OK 95 cut 2 but had to add one because line was too long for mult-assignments
' When adding comments I had to change Dav' temp Row() and Col() words. He or I had/have them
' reversed.
' 2024-10-19 94 LOC DrawBoard NOT needed in AddCell and main both I chose in main only.
' ============================================================================

' 2024-10-20 2048 - 1D Board combine all arrow handling subs into one, save LOC!
' OK 75 LOC
' 2024-10-21 2048 - 64 LOC
' subs and a function reinserted back into Main

Now regarding LOC, it seems to me, intuitively, there is a usefulness in this practice of reducing code to essentials. Maybe I am wrong. Steve is very good at it one of the reasons I respect his skills, I was hoping to see him join in with a real effort but he is busy this time of year as reported yesterday.

What is the lesson, what can I get from this little session? I am tempted to go back to my editor and forbid any line over 100 characters. I could save myself all that trouble attempting to allow an exception to 100 characters with a horizontal scroll. But it would surely be missed, even by me. But like the colon, it should be used judiciously. I even started a set of guidelines for us of colon. Opps! I called it rules, that does not work here in Basic, rules are for oneself and those who are into monologues. Wink
b = b + ...
Reply
#23
(10-22-2024, 11:37 AM)bplus Wrote: You got me swearing...

but Steve wouild say that was the intentional message, no he wont now that I said it for him Wink

That was the intentional message.  Big Grin

Look at your code in reply #20 (https://qb64phoenix.com/forum/showthread...&pid=29316) -- there's very few lines which aren't multiple statements tossed onto a single line, just to claim that it's a low LOC count.  When you start cramming 2 to 10 lines of code on a single line, there's no longer any use in trying to use LOC as a indiciation of the code's "worthiness".

Let me take a second to point out some ugly programming, which has been crammed together to produce one line of code:

Code: (Select All)
10 Do: k$ = InKey$: _Limit 30: Loop Until k$ <> "" '                         == wait for key ==

Now, that's 4 lines of code and a label all crammed together just to say, "SEE, my line count is low!"  /BLARF!!

Let's translate this to something which is much better.  Are you ready?

Code: (Select All)
k$ = Input$(1)

Now, doing something like this, would be a valid exercise in reducing lines of code, to me.  You've taken multiple keywords and simplified them down to one simple processs. 

And yet, by cramming all the crap together on one line, it invalidates the change there.  The orginal was one line of code.  The change is one line of code.  By simply using line of code as a metric to judge the code, there's no difference in the two methods of coding.

Quote:Now regarding LOC, it seems to me, intuitively, there is a usefulness in this practice of reducing code to essentials. Maybe I am wrong. Steve is very good at it one of the reasons I respect his skills, I was hoping to see him join in with a real effort but he is busy this time of year as reported yesterday.

What is the lesson, what can I get from this little session? I am tempted to go back to my editor and forbid any line over 100 characters. I could save myself all that trouble attempting to allow an exception to 100 characters with a horizontal scroll. But it would surely be missed, even by me. But like the colon, it should be used judiciously. I even started a set of guidelines for us of colon. Opps! I called it rules, that does not work here in Basic, rules are for oneself and those who are into monologues. Wink

To me, the lesson for line count should break down to:  *Minimal or no use of colons when counting lines of code.*

Sometimes, it makes sense to keep things together on a single line.

IF x > 2 then a = a + 1: b = b - 1: c = c / 4

^ That use makes sense to me.

Code: (Select All)
    Next: Next

Use of crap like the above is just arbitrarily cramming crap together to reduce line count.  It does nothing towards "reducing code to essentials".  It's just cramming multiple statements on one line -- and one can't even argue that it's a personal STYLE.  At least they can't when you also have this a few lines above:

Code: (Select All)
        Next
    Next

I defy anyone to say that stuff like this is "reducing code to essentials":
Code: (Select All)
If jm = 4 Then p = j * jm + ks Else If kstep = 1 Then p = j Else p = 12 + j

That's just obfuscating of code to once again arbitrarily reduce line count.  It's ugly.  It's hard to follow.  It's just a TRICK to try and manipulate the number of lines in a program, without doing anything to actually tighten up the program or improve performance.  It's not "reducing code to essentials".  It's just cramming as much crap on one line as possible, without having to scroll the screen.



And when it comes to " I hate horizontal scrolling! ", that's entirely dependent upon the user's monitor, IDE font size, and other factors.  The 64 LOC count of bplus's requires horizontal scrolling for me, but that's because I've been running in half screen mode with the IDE here lately, and I tend to use a large font for easiest readability with these old, tired eyes.  I don't have 120+ characters that I can display on the screen without horizontal scrolling currently.  Last I checked, my IDE has a limit of around 80 characters before horizontal scrolling occurs.

If it's going to scroll with the 64-LOC of code, then why not just cram more crap together onto a line and reduce the LOC even further?



Honestly, I think when using lines of code as a metric to guage a set of code, then that code should be organized in a natural and normal style of coding.

I like to see when stuff like this:

Code: (Select All)
10 Do: k$ = InKey$: _Limit 30: Loop Until k$ <> "" '                         == wait for key ==

Can be reduced down to:

Code: (Select All)
k$ = Input$(1)

This is a legitimate reduction to try and reduce a set of code down to its essentials.

Cramming crap together on the same line doesn't do that.  It's not reducing your code; it's only obfuscating it.

And -- to ME -- that's the truly UGLY code.  Tongue
Reply
#24
See if this doesn't do the same thing as the ugly IF ELSE IF ELSE line that you've got crammed together all nasty-like above.

Instead of:
Code: (Select All)
If jm = 4 Then p = j * jm + ks Else If kstep = 1 Then p = j Else p = 12 + j

Wouldn't this be the same?

Code: (Select All)
If jm = 4 Then p = j * jm + ks Else p = 12 + j + (kstep = 1) * 12

One IF. One ELSE. No tossing multiple lines of IF into the same statement just to reduce lines of code.

^ This, I would count as a valid reduction of coding to try and reduce it to the essentials. It's not just an exercise in seeing how much one can cram on a line, to artifically keep line count low. Wink
Reply
#25
Some good points for sure!

I totally forgot about Input$(1) and am kicking myself for it.
That's what I know and respect from the amazing Steve Smile

I do disagree on two lines for Next when one will do, specially for 2D array on game board of graphic, just wasting space. I am a not forbidding colons; as you said and I said, makes sense for multiple assignments and I want to include multiple one word statements.

In Reply above, #24, I agree that first line is crap and I confess I was pushing it to get lines from like 65 to the cute 64. I don't think your alternate is much clearer, IMHO.

We could debate all day what maximum amount of characters per line might work best to avoid horizontal scrolling, so ehh to 80 Smile with 100 chars you have a better chance of fitting comments on same line as code. I say a big YEA! to that.

I think how I got from < 100 to 75 was more artfully done. But here's is a good thing I found getting to 64 lines:

Getting rid of 2 lines for Function definition /End Function, which you can do anytime you call a sub or function only once in main code, this was slightly ineffecient:
Code: (Select All)
Function RemaingMove '   == Can player still make a move? ==
    For y = 0 To 3: For x = 0 To 3 '                     == Look for empty space or combine move ==
            If B(y * 4 + x) = 0 Then RemaingMove = 1 '                     == yes a move is left ==
            If y < 3 Then If B(y * 4 + x) = B((y + 1) * 4 + x) Then RemaingMove = 1 '  move left ==
            If x < 3 Then If B(y * 4 + x) = B(y * 4 + (x + 1)) Then RemaingMove = 1 '  move left ==
    Next: Next
End Function
You could : Exit Function after each of 3 RemainMove = 1 to get back to it in main faster (but who would really notice) but that means more colons crap.

To this:
Code: (Select All)
For y = 0 To 3: For x = 0 To 3 '                     == Look for empty space or combine move ==
            If B(y * 4 + x) = 0 Then 10 '                      == yes a move is left, skip to 10 ==
            If y < 3 Then If B(y * 4 + x) = B((y + 1) * 4 + x) Then 10 '  == a move left goto 10 ==
            If x < 3 Then If B(y * 4 + x) = B(y * 4 + (x + 1)) Then 10 '  == a move left goto 10 ==
    Next: Next
Which actually did function more efficiently using the old fashioned THEN (GOTO) line_number and no colon crap.
b = b + ...
Reply
#26
Instead of: 

Code: (Select All)
NEXT: NEXT

Why not:

Code: (Select All)
NEXT x, y

??  Wink
Reply
#27
OK forgot about that one too Smile
b = b + ...
Reply
#28
(10-22-2024, 03:26 PM)bplus Wrote: Some good points for sure!

I totally forgot about Input$(1) and am kicking myself for it.
That's what I know and respect from the amazing Steve Smile

I do disagree on two lines for Next when one will do, specially for 2D array on game board of graphic, just wasting space. I am a not forbidding colons; as you said and I said, makes sense for multiple assignments and I want to include multiple one word statements.

In Reply above, #24, I agree that first line is crap and I confess I was pushing it to get lines from like 65 to the cute 64. I don't think your alternate is much clearer, IMHO.

We could debate all day what maximum amount of characters per line might work best to avoid horizontal scrolling, so ehh to 80 Smile with 100 chars you have a better chance of fitting comments on same line as code. I say a big YEA! to that.

I think how I got from < 100 to 75 was more artfully done. But here's is a good thing I found getting to 64 lines:

Getting rid of 2 lines for Function definition /End Function, which you can do anytime you call a sub or function only once in main code, this was slightly ineffecient:
Code: (Select All)
Function RemaingMove '   == Can player still make a move? ==
    For y = 0 To 3: For x = 0 To 3 '                     == Look for empty space or combine move ==
            If B(y * 4 + x) = 0 Then RemaingMove = 1 '                     == yes a move is left ==
            If y < 3 Then If B(y * 4 + x) = B((y + 1) * 4 + x) Then RemaingMove = 1 '  move left ==
            If x < 3 Then If B(y * 4 + x) = B(y * 4 + (x + 1)) Then RemaingMove = 1 '  move left ==
    Next: Next
End Function
You could : Exit Function after each of 3 RemainMove = 1 to get back to it in main faster (but who would really notice) but that means more colons crap.

To this:
Code: (Select All)
    For y = 0 To 3: For x = 0 To 3 '                     == Look for empty space or combine move ==
            If B(y * 4 + x) = 0 Then 10 '                      == yes a move is left, skip to 10 ==
            If y < 3 Then If B(y * 4 + x) = B((y + 1) * 4 + x) Then 10 '  == a move left goto 10 ==
            If x < 3 Then If B(y * 4 + x) = B(y * 4 + (x + 1)) Then 10 '  == a move left goto 10 ==
    Next: Next
Which actually did function more efficiently using the old fashioned THEN (GOTO) line_number and no colon crap.


And you can consoldate all those lines down into one statement, since they're all doing the same thing:

Code: (Select All)
If (B(y * 4 + x) = 0) _ORELSE (y < 3 _Andalso B(y * 4 + x) = B((y + 1) * 4 + x)) _ORELSE _
(x < 3 _Andalso B(y * 4 + x) = B(y * 4 + (x + 1))) Then RemaingMove = 1 ' move left ==


Now question: Is the above one line of code, or two lines of code since I continued it with the underscore _ so it doesn't go past that 100 character limit?
Reply
#29
That is one line of code for which I'd gladly exchange the 3 easier to understand lines from which it came.

I wouldn't take it even if I was at 66 lines and was trying to get to 64.
b = b + ...
Reply
#30
Here's something interesting, ...maybe Smile
Code: (Select All)
Sub AddNewCell '  == Insert new cell onto board in random unused blank ==
    Dim temp(15), x, y, c, i, x1, y1
    For y = 0 To 3: For x = 0 To 3 '                == save index of empty cells for random pick ==
            If B(y * 4 + x) = 0 Then temp(c) = y * 4 + x: c = c + 1
    Next: Next
    If c > 0 Then '      == choose one place to make a number, convert index to row =y1, col =x1 ==
        i = Int(Rnd * c): y1 = Int(temp(i) / 4): x1 = temp(i) Mod 4 '  == pick and convert index ==
        If Rnd < .8 Then B(4 * y1 + x1) = 2 Else B(4 * y1 + x1) = 4 ' = more 2's, 80% than 4's, 20%
    End If
End Sub

I tried this with straight index to B()
Code: (Select All)
Sub AddNewCell '  == Insert new cell onto board in random unused blank ==
    Dim temp(15), x, y, c, i, x1, y1
    For i = 0 To 15 '  == save index of empty cells for random pick ==
        If B(i) = 0 Then temp(c) = i: c = c + 1
    Next
    If c > 0 Then '      == choose one place to make a number, convert index to row =y1, col =x1 ==
        i = Int(Rnd * c) '  == pick and convert index ==
        If Rnd < .8 Then B(i) = 2 Else B(i) = 4 ' = more 2's, 80% than 4's, 20%
    End If
End Sub

That didn't work, it was crazy / interesting trying to figure out why!
b = b + ...
Reply




Users browsing this thread: 7 Guest(s)