How to resolve the algorithm Bulls and cows step by step in the QB64 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bulls and cows step by step in the QB64 programming language

Table of Contents

Problem Statement

Bulls and Cows   is an old game played with pencil and paper that was later implemented using computers.

Create a four digit random number from the digits   1   to   9,   without duplication. The program should:

The score is computed as:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bulls and cows step by step in the QB64 programming language

Source code in the qb64 programming language

Const MaxDigit = 4, Min = 1, Max = 9
Dim As String NumberToGuess, NumberToTest, Newdigit, Result
Dim As Integer Counter, Number
Randomize Timer
Do
    Counter = Counter + 1
    Newdigit = _Trim$(Str$(Int(Rnd * Max) + Min))
    If InStr(NumberToGuess, Newdigit) = 0 Then NumberToGuess = NumberToGuess + Newdigit Else Counter = Counter - 1
Loop While Counter < MaxDigit
Print NumberToGuess  'debug output
Do While NumberToGuess <> NumberToTest
    Input "Please enter your guess of 4 digits... ", Number
    NumberToTest = _Trim$(Str$(Number))
    If NumberToGuess <> NumberToTest Then
        Result = ""
        For Counter = 1 To 4 Step 1
            Newdigit = Mid$(NumberToTest, Counter, 1)
            If InStr(NumberToGuess, Newdigit) - Counter = 0 Then
                Result = Result + " Bull "
            ElseIf InStr(NumberToGuess, Newdigit) > 0 Then
                Result = Result + " Cow "
            End If
        Next Counter
        Print NumberToTest, Result
    Else
        Print "You Win!"
    End If
Loop

  

You may also check:How to resolve the algorithm Reflection/List methods step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the KSI programming language
You may also check:How to resolve the algorithm Align columns step by step in the Arturo programming language
You may also check:How to resolve the algorithm Perfect totient numbers step by step in the AWK programming language
You may also check:How to resolve the algorithm Search a list step by step in the S-lang programming language