Turn on thread page Beta
    • Thread Starter

    Hi, I have this macro to create and I have written most of it but it is far from perfect...I don't know what's wrong with it though. I'm just starting with VBA

    Exercise is
    Write a program for a guessing game played by two players. Hence read two numbers as input from the user.

    The program will now tell (Using MsgBox) to the second player how many digits are rightly placed and how many digits exist in the first number but are misplaced

    E.g. "There is 1 number well placed and 1 number misplaced"

    Keep going with the program telling the second player how many digits are placed/misplaced and ask the second player to give numbers until he finds the right one.

    If the 3 digits are in the right place the program stops by displaying "Bravo you have found, the number is ..." else the program requests another number from the second player until it finds the right number.

    I have this for now
    x = InputBox("player 1, enter an integer number")

    Do Until x = y
    y = InputBox("player 2, enter an integer number")
    For i = 1 To Len(x)
    a = Mid(x, i, 1)
    For j = 1 To Len(y)
    b = Mid(y, j, 1)

    If a = b And i = j Then
    count1 = count1 + 1
    ElseIf a = b And i <> j Then
    count2 = count2 + 1
    End If
    Next j
    Next i

    If count1 = 0 And count2 < 2 Then
    MsgBox ("There is 0 digit well placed and 0 digit misplaced")
    ElseIf count1 = 0 And count2 >= 2 Then
    MsgBox ("There is 0 digit well placed and" & count2 & " digits misplaced.")

    ElseIf x = y Then
    MsgBox "There are " & count1 & " digits well placed and 0 digits misplaced. Bravo, you have found the right number!"

    End If


    Apparently there's a problem with the line count1 = count1 + 1

    Thanks for your help
Submit reply
Turn on thread page Beta
Updated: February 2, 2017
Do you think parents should charge rent?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.