Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
2048 Puzzle
#35
There -- I stripped off the 30 lines that I mentioned earlier. Tongue

Code: (Select All)
Dim Shared Grid(0 To 5, 0 To 5) As Integer
Screen _NewImage(480, 480, 32): _ScreenMove _Middle
_Title "Double Up by Steve": Randomize Timer: Color , 0
_Font _LoadFont("courbd.ttf", 32, "MONOSPACE")
GetNextNumber: GetNextNumber
Do
For x = 1 To 4: For y = 1 To 4
t$ = LTrim$(Str$(Grid(x, y))): If t$ = "0" Then t$ = ""
x1% = (x - 1) * 120: y1% = (y - 1) * 120
Line (x1% + 3, y1% + 3)-Step(117, 117), &HFF008888, BF
Line (x1% + 5, y1% + 5)-Step(115, 115), &HFF00FFFF, B
_PrintString (x1% + 60 - _PrintWidth(t$) \ 2, y1% + 44), t$
Next y, x
Select Case _KeyHit
Case 32: System
Case 19200: MoveLeftRight 1: GetNextNumber 'we hit a valid move key.
Case 18432: MoveUpDown 1: GetNextNumber ' Even if we don't move,
Case 20480: MoveUpDown -1: GetNextNumber ' get a new number.
Case 19712: MoveLeftRight -1: GetNextNumber
End Select
_Display: _Limit 30
Loop
Sub MoveUpDown (d As Integer)
s = 1: f = 4: If d < 0 Then Swap s, f
Do
moved = 0
For y = s To f Step d: For x = 1 To 4
If Grid(x, y) = 0 Then 'every point above this moves down
For j = y To f Step d
Grid(x, j) = Grid(x, j + d): moved = (Grid(x, j) <> 0)
Next
End If
Next x, y
If moved Then y = y + d 'recheck the same column
Loop Until Not moved
For y = s To f Step d: For x = 1 To 4
If Grid(x, y) <> 0 And Grid(x, y) = Grid(x, y + d) Then
Grid(x, y) = Grid(x, y) * 2
For j = y + d To f Step d: Grid(x, j) = Grid(x, j + d): Next
End If
Next x, y
End Sub
Sub MoveLeftRight (d)
s = 1: f = 4: If d < 0 Then Swap s, f
Do
moved = 0
For x = s To f Step d: For y = 1 To 4
If Grid(x, y) = 0 Then 'every point right of this moves left
For j = x To f Step d
Grid(j, y) = Grid(j + d, y): moved = (Grid(j, y) <> 0)
Next
End If
Next y, x
If moved Then x = x + d 'recheck the same row
Loop Until Not moved
For x = s To f Step d: For y = 1 To 4
If Grid(x, y) <> 0 And Grid(x, y) = Grid(x + d, y) Then
Grid(x, y) = Grid(x, y) * 2
For j = x + d To f Step d: Grid(j, y) = Grid(j + d, y): Next
End If
Next y, x
End Sub
Sub GetNextNumber
For x = 1 To 4: For y = 1 To 4: valid = (Grid(x, y) = 0): Next y, x
If valid Then
Do: x = _Ceil(Rnd * 4): y = _Ceil(Rnd * 4): Loop Until Grid(x, y) = 0
If Rnd < .8 Then Grid(x, y) = 2 Else Grid(x, y) = 4 ' < missed 4
End If
End Sub

And, every line is still less than 80 characters in width.
Reply


Messages In This Thread
2048 Puzzle - by Dav - 10-17-2024, 02:19 AM
RE: 2048 Puzzle - by FellippeHeitor - 10-17-2024, 02:42 AM
RE: 2048 Puzzle - by Dav - 10-17-2024, 02:48 AM
RE: 2048 Puzzle - by FellippeHeitor - 10-17-2024, 03:17 AM
RE: 2048 Puzzle - by bplus - 10-17-2024, 09:16 AM
RE: 2048 Puzzle - by Dav - 10-17-2024, 01:01 PM
RE: 2048 Puzzle - by bplus - 10-17-2024, 03:36 PM
RE: 2048 Puzzle - by Dav - 10-17-2024, 04:19 PM
RE: 2048 Puzzle - by bplus - 10-17-2024, 04:26 PM
RE: 2048 Puzzle - by SMcNeill - 10-17-2024, 05:05 PM
RE: 2048 Puzzle - by bplus - 10-17-2024, 05:15 PM
RE: 2048 Puzzle - by SMcNeill - 10-17-2024, 05:28 PM
RE: 2048 Puzzle - by Dav - 10-17-2024, 10:46 PM
RE: 2048 Puzzle - by bplus - 10-18-2024, 12:48 AM
RE: 2048 Puzzle - by Dav - 10-18-2024, 11:49 AM
RE: 2048 Puzzle - by bplus - 10-18-2024, 01:51 PM
RE: 2048 Puzzle - by bplus - 10-18-2024, 09:59 PM
RE: 2048 Puzzle - by Dav - 10-20-2024, 10:44 PM
RE: 2048 Puzzle - by bplus - 10-20-2024, 11:32 PM
RE: 2048 Puzzle - by bplus - 10-21-2024, 09:18 AM
RE: 2048 Puzzle - by SMcNeill - 10-21-2024, 10:19 AM
RE: 2048 Puzzle - by bplus - 10-22-2024, 11:37 AM
RE: 2048 Puzzle - by SMcNeill - 10-22-2024, 02:27 PM
RE: 2048 Puzzle - by SMcNeill - 10-22-2024, 02:39 PM
RE: 2048 Puzzle - by bplus - 10-22-2024, 03:26 PM
RE: 2048 Puzzle - by SMcNeill - 10-22-2024, 03:49 PM
RE: 2048 Puzzle - by SMcNeill - 10-22-2024, 03:36 PM
RE: 2048 Puzzle - by bplus - 10-22-2024, 03:38 PM
RE: 2048 Puzzle - by bplus - 10-22-2024, 04:37 PM
RE: 2048 Puzzle - by SMcNeill - 10-22-2024, 08:18 PM
RE: 2048 Puzzle - by bplus - 10-22-2024, 04:47 PM
RE: 2048 Puzzle - by SMcNeill - 10-23-2024, 03:44 AM
RE: 2048 Puzzle - by bplus - 10-23-2024, 10:32 AM
RE: 2048 Puzzle - by bplus - 10-23-2024, 12:40 PM
RE: 2048 Puzzle - by SMcNeill - 10-23-2024, 01:51 PM
RE: 2048 Puzzle - by bplus - 10-23-2024, 05:00 PM
RE: 2048 Puzzle - by bplus - 10-24-2024, 06:42 PM
RE: 2048 Puzzle - by Dav - 10-25-2024, 07:32 PM
RE: 2048 Puzzle - by bplus - 10-26-2024, 12:34 PM
RE: 2048 Puzzle - by Dav - 10-26-2024, 01:21 PM
RE: 2048 Puzzle - by bplus - 10-26-2024, 01:33 PM
RE: 2048 Puzzle - by bplus - 10-27-2024, 01:39 AM
RE: 2048 Puzzle - by bplus - 10-27-2024, 10:08 AM



Users browsing this thread: 15 Guest(s)