Rosetta Code Challenges
#1
The posts in this thread are from Rosetta Code Challenges.

You are free to post improvements.

Better IMHO is less LOC (Lines Of Code) but try to hold off on using so many colons on a line: none is perfect, one or 2 reasonable, 10 ridiculous! = too much)
_____________________________________________________________________________________________

Ken was asking about this today.

Bulls and cows: http://rosettacode.org/wiki/Bulls_and_cows
Code: (Select All)
_Title "Bulls and Cows" ' found at Rosetta for Qbasic, copy 2019-01-31

'challenge is to develope AI player for this game

DefInt A-Z

Dim secret As String
Dim guess As String
Dim c As String
Dim bulls, cows, guesses, i

Randomize Timer
Do While Len(secret) < 4
    c = Chr$(Int(Rnd * 10) + 48)
    If InStr(secret, c) = 0 Then secret = secret + c
Loop

guesses = 0
Do
    Input "Guess a 4-digit number with no duplicate digits: "; guess
    guess = LTrim$(RTrim$(guess))
    If Len(guess) = 0 Then Exit Do

    If Len(guess) <> 4 Or Val(guess) = 0 Then
        Print "** You should enter 4 numeric digits!"
        GoTo looper
    End If

    bulls = 0: cows = 0: guesses = guesses + 1
    For i = 1 To 4
        c = Mid$(secret, i, 1)
        If Mid$(guess, i, 1) = c Then
            bulls = bulls + 1
        ElseIf InStr(guess, c) Then
            cows = cows + 1
        End If
    Next i
    Print bulls; " bulls, "; cows; " cows"

    If guess = secret Then
        Print "You won after "; guesses; " guesses!"
        Exit Do
    End If
    looper:
Loop



[Image: Bulls-and-cows.png]
b = b + ...
Reply


Messages In This Thread
Rosetta Code Challenges - by bplus - 04-26-2022, 09:17 PM
RE: Rosetta Code Challenges - by SierraKen - 04-27-2022, 04:36 AM
RE: Rosetta Code Challenges - by bplus - 04-27-2022, 01:38 PM
RE: Rosetta Code Challenges - by bplus - 10-30-2022, 04:41 AM
RE: Rosetta Code Challenges - by bplus - 10-31-2022, 05:40 PM
RE: Rosetta Code Challenges - by SpriggsySpriggs - 10-31-2022, 07:24 PM
RE: Rosetta Code Challenges - by bplus - 10-31-2022, 09:09 PM
RE: Rosetta Code Challenges - by MasterGy - 10-31-2022, 09:33 PM
RE: Rosetta Code Challenges - by bplus - 10-31-2022, 09:43 PM
RE: Rosetta Code Challenges - by bplus - 11-01-2022, 02:07 AM
RE: Rosetta Code Challenges - by bplus - 11-01-2022, 12:19 PM
RE: Rosetta Code Challenges - by bplus - 11-05-2022, 04:57 AM
RE: Rosetta Code Challenges - by CharlieJV - 11-05-2022, 03:10 PM
RE: Rosetta Code Challenges - by bplus - 11-05-2022, 04:02 PM



Users browsing this thread: 1 Guest(s)