Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Collection of Basic Computer Games
#8
Sit back and relax while ReverseAI plays the Reverse Puzzle Game for you!
Code: (Select All)
_Title "Reverse Update with AI" 'b+ 2024-07-21 directions 7-22 with AI 7-22
' compare to Reverse here www.vintage-basic.net/games.html
' David Ahl's compilation of Basic Games and other stuff

'     Directions for Reverse Puzzle:

' Reverse goal is to get the given string of digits back in order from 1 to 9.
' Asking to reverse say 5 will swap first with 5th, 2nd with 4th and that leaves 3 unmoved.
' so   5 4 3 2 1 6 7 8 9 reverse 5
' gets 1 2 3 4 5 6 7 8 9 exactly what we are trying to achieve, you got it!
' reverse always starts on left and counts n to right, note: so reverse 1 does nothing.
'
' Another example from help in original code:
' 2 3 4 5 1 6 7 8 9
' reverse 4
' 5 4 3 2 1 6 7 8 9
' now just reverse 5
' 1 2 3 4 5 6 7 8 9 You got it!
'
' numbers printed on far left are just counting the number of times you use reverse.

Randomize Timer
Dim Shared c$
r$ = " 1 2 3 4 5 6 7 8 9"
c$ = r$
restart:
cnt = 0
fini% = Int(Rnd * 11) + 2
For i = 1 To fini%
    r% = Int(Rnd * 8) + 2
    c$ = reverse$(r%)
Next
Do
    Print cnt; " reverses", c$
    'Input "Enter how many to reverse (0 quits)"; n
    n = reverseAI%
    If n = 0 Then Print "Already done!": Exit Do
    Print "AI says reverse"; n
    c$ = reverse$(n)
    If c$ = r$ Then Print cnt, c$; " AI did it!": Print: Print: Exit Do Else cnt = cnt + 1
    _Delay 10
Loop
GoTo restart

Function reverse$ (n As Integer)
    For i = 1 To 2 * n Step 2
        b$ = Mid$(c$, i, 2) + b$
    Next
    reverse$ = b$ + Mid$(c$, 2 * n + 1)
End Function

Function reverseAI%
    ' from right to left find the first digit out of order
    Dim As Integer i, x, place
    For i = 9 To 2 Step -1
        x$ = Mid$(c$, i * 2, 1)
        If x$ <> _Trim$(Str$(i)) Then
            lookup$ = _Trim$(Str$(i))
            'Print "lookup$ "; lookup$
            Exit For
        End If
    Next
    place = InStr(c$, lookup$) / 2
    'Print "place"; place
    If place = 1 Then reverseAI% = Val(lookup$) Else reverseAI% = place
End Function

Slowed way down so humans might be able to follow moves AI makes.
b = b + ...
Reply


Messages In This Thread
Collection of Basic Computer Games - by eoredson - 06-16-2024, 03:03 AM
RE: Collection of Basic Computer Games - by bplus - 06-16-2024, 01:23 PM
RE: Collection of Basic Computer Games - by bplus - 07-21-2024, 07:19 PM
RE: Collection of Basic Computer Games - by bplus - 07-21-2024, 11:28 PM
RE: Collection of Basic Computer Games - by bplus - 07-22-2024, 05:41 PM
RE: Collection of Basic Computer Games - by bplus - 07-22-2024, 06:54 PM



Users browsing this thread: 5 Guest(s)