Question Help with codes!

brandon.c

New member
Joined
Jan 12, 2012
Messages
1
Programming Experience
Beginner
Hi guys,

It's my first post so forgive me if I've posted under the wrong section. Well, I just picked up VB.NET and was given a project and I'm stuck.

This is what I am suppose to create :

frm2.PNG

A number guessing game where 4 numbers will be randomly generated, and the player has to input any 4 random numbers 0 - 9. The text box will then show what numbers the player entered. "X" means wrong, "0" means correct and "P" means wrong position. Message box comes out, indicating the number of guesses the player took when he wins.

And this is what I've been doing until :

frm1.PNG

I have no idea how to position the "X", "0" and "P" into the text box when the numbers the player used matches the ones the computer generated. Tried Google and stuffs but nothing works. Any kind souls willing to help? Any help will be deeply appreciated! :rapture: My codes are below :


Public Class frmGuessingGame

' Declaration
Dim randomGenerator As New Random
Dim strInputValue As Integer
Dim strSummary As String
Dim intNum1, intNum2, intNum3, intNum4 As Integer
Dim X As String
Dim O As String
Dim P As String

Private Sub frmGuessingGame_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

' Random number generator

Randomize()

' Box 1
intNum1 = CStr(Int(Rnd() * 10))
txtBoxNo1.Text = intNum1

' Box 2
intNum2 = CStr(Int(Rnd() * 10))
txtBoxNo2.Text = intNum2

' Box 3
intNum3 = CStr(Int(Rnd() * 10))
txtBoxNo3.Text = intNum3

' Box 4
intNum4 = CStr(Int(Rnd() * 10))
txtBoxNo4.Text = intNum4

strSummary = "Numbers Entered : " + " 1st " + " 2nd " + " 3rd " + " 4th " + ControlChars.CrLf

End Sub

Private Sub btnGo_Click(sender As System.Object, e As System.EventArgs) Handles btnGo.Click

' Input value entered by player

If IsNumeric(txtBoxInputVal.Text) = False Then
MsgBox("Error: Input value must be a number", MsgBoxStyle.Critical, "Error Message")
Exit Sub
End If

If intNum1 = 1 Then

End If




End Sub
End Class
frm2.PNGfrm1.PNGfrm1.PNGfrm2.PNG
 
here's an idea...

hey Brandon

Positioning text in a textbox can be a bit of a pain. Using the + to join words wouldn't work. It only concatenates them. You need to use Control Characters to position them with spaces and newlines. Here's one site with some info that I came across. Know Dot Net - VB.NET Control Characters

I did a small version of your application just to test it out. I've attached the solution here so that you can see it more clearly. I gotta tell you, I have not completed the game. Since your question is on how to position text, I think I have answered that. Plus its 3 A.M here and I'm sleepy like hell! :)

Here's the code

VB.NET:
Public Class frmGuessingGame


    ' Declaration
    Dim randomGenerator As New Random
    Dim strInputValue As Integer
    Dim strSummary As String
    Dim intNum1, intNum2, intNum3, intNum4 As Integer
    Dim X As String
    Dim O As String
    Dim P As String
    Dim ico1, ico2, ico3, ico4 As String 'flags to represent the legend




    'here I have declared 2 constant variables for the control characters I'm using here.
    Const space As String = ControlChars.Tab  'a tab
    Const newline As String = ControlChars.CrLf  'a newline


    Private Sub frmGuessingGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        ' Random number generator
        Randomize()




        ' Box 1
        intNum1 = CStr(Int(Rnd() * 10))


        ' Box 2
        intNum2 = CStr(Int(Rnd() * 10))


        ' Box 3
        intNum3 = CStr(Int(Rnd() * 10))


        ' Box 4
        intNum4 = CStr(Int(Rnd() * 10))


        'I just put this label to see the random number that's generating.
        Label3.Text = intNum1.ToString + intNum2.ToString + intNum3.ToString + intNum4.ToString


    End Sub


    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click


        If IsNumeric(txtInput1.Text) = False Then
            MsgBox("Error: Input value must be a number", MsgBoxStyle.Critical, "Error Message")
            Exit Sub
        ElseIf IsNumeric(txtInput2.Text) = False Then
            MsgBox("Error: Input value must be a number", MsgBoxStyle.Critical, "Error Message")
            Exit Sub
        ElseIf IsNumeric(txtInput3.Text) = False Then
            MsgBox("Error: Input value must be a number", MsgBoxStyle.Critical, "Error Message")
            Exit Sub
        ElseIf IsNumeric(txtInput4.Text) = False Then
            MsgBox("Error: Input value must be a number", MsgBoxStyle.Critical, "Error Message")
            Exit Sub
        End If




        strInputValue = txtInput1.Text + txtInput2.Text + txtInput3.Text + txtInput4.Text




        txtDisplay1.Text = txtInput1.Text
        txtDisplay2.Text = txtInput2.Text
        txtDisplay3.Text = txtInput3.Text
        txtDisplay4.Text = txtInput4.Text


        txtInput1.Text = ""
        txtInput2.Text = ""
        txtInput3.Text = ""
        txtInput4.Text = ""




        If txtDisplay1.Text = intNum1 Then
            ico1 = "O"
        ElseIf txtDisplay1.Text <> intNum1 Then
            ico1 = "X"
        End If


        If txtDisplay2.Text = intNum2 Then
            ico2 = "O"
        ElseIf txtDisplay2.Text <> intNum2 Then
            ico2 = "X"
        End If


        If txtDisplay3.Text = intNum3 Then
            ico3 = "O"
        ElseIf txtDisplay3.Text <> intNum3 Then
            ico3 = "X"
        End If


        If txtDisplay4.Text = intNum4 Then
            ico4 = "O"
        ElseIf txtDisplay4.Text <> intNum4 Then
            ico4 = "X"
        End If


        'its a bit ugly but serves right. But if you can get an idea from this, you might be able to do better than this.
        strSummary = "Numbers Entered   : " & space & " 1st " & space & " 2nd " & space & " 3rd " & space & " 4th " & newline & newline & strInputValue & space & " ==> " & space & space & ico1 & space & ico2 & space & ico3 & space & ico4
        txtMain.Text = strSummary


    End Sub


End Class


mind you, I'm no expert in VB.NET either. I'm still learning too, just like you. Hope this helps. Cheers! :) if you have any doubts, just ask. I'll do my best to clear them out for you.
 
Last edited by a moderator:
Back
Top