Online Examination System for all learner!!! coming soon..

Showing posts with label VB. Show all posts
Showing posts with label VB. Show all posts

VB 6 : Implementation of Tic-Tac-Toe game

Code:

Dim score1 As Integer, score2 As Integer
Dim cur As String

Private Sub Form_Load()
  ResetGame
  p2.Enabled = False
  cur = 1
End Sub

Private Sub img_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
    If img(Index).Picture = imgX.Picture Or img(Index).Picture = imgO.Picture Then 
    Exit Sub
    img(Index).Picture = Source.Picture
    img(Index).Tag = Source.Tag
    Dim winner As String
    If checkwin <> "no" Then
        If checkwin = "x" Then
            winner = "Player 1"
            score1 = score1 + 1
            s1.Caption = score1
        Else
            winner = "Player 2"
            score2 = score2 + 1
            s2.Caption = score2
        End If
        MsgBox "The winner is " & winner, vbInformation, "Winner"
        ResetGame
    End If
    If cur = 1 Then
        cur = 2
        p1.Enabled = False
        p2.Enabled = True
    Else
        cur = 1
        p1.Enabled = True
        p2.Enabled = False
    End If
End Sub

Private Sub ResetGame()
    For i = 0 To 8
        img(i).Tag = i
        img(i).Picture = Nothing
    Next i
   
End Sub

Private Function checkwin() As String
    Dim a1 As String, a2 As String, a3 As String, b1 As String, b2 As String, b3 As String, _
    c1 As String, c2 As String, c3 As String
   
    a1 = img(0).Tag
    a2 = img(1).Tag
    a3 = img(2).Tag
    b1 = img(3).Tag
    b2 = img(4).Tag
    b3 = img(5).Tag
    c1 = img(6).Tag
    c2 = img(7).Tag
    c3 = img(8).Tag
       
    If a1 <> "0" And a2 <> "1" And a3 <> "2" And _
        b1 <> "3" And b2 <> "4" And b3 <> "5" And _
        c1 <> "6" And c2 <> "7" And c3 <> "8" Then
        MsgBox "Game Draw!", vbInformation, "Game Over"
        ResetGame
    End If
   
       
    If a1 = a2 And a2 = a3 Then
        checkwin = a1
    ElseIf b1 = b2 And b2 = b3 Then
        checkwin = b1
    ElseIf c1 = c2 And c2 = c3 Then
        checkwin = c1
    ElseIf a1 = b1 And b1 = c1 Then
        checkwin = a1
    ElseIf a2 = b2 And b2 = c2 Then
        checkwin = a2
    ElseIf a3 = b3 And b3 = c3 Then
        checkwin = a3
    ElseIf a1 = b2 And b2 = c3 Then
        checkwin = a1
    ElseIf a3 = b2 And b2 = c1 Then
        checkwin = a3
    Else
        checkwin = "no"
    End If
End Function

Output:


 <<Previous Page                    Download Code                    Next Page>>

Related Programs:

  1. VB 6 : Tower of Hanoi
  2. VB 6 : Implementation of Tic-Tac-Toe game
  3. VB 6 :  Make Web Browser Application

VB 6.0 : Area and circumference of circle

VB 6.0 : Simple Calculator

  Design:

Fig-1:Object Arrangement

Code:

Private Sub Form_Load()
     Label1.Caption = "Enter 1st Number"
     Label2.Caption = "Enter 2nd Number"
     Label3.Caption = ""
     Label3.FontSize = 17
     Label4.Caption = "Result:"
     Text1.Text = ""
     Text2.Text = ""

     Option1.Caption = "ADD"
     Option2.Caption = "SUB"
     Option3.Caption = "MULT"
     Option4.Caption = "DIV"
End Sub

Private Sub Option1_Click()

    Label3.Caption = Val(Text1.Text) + Val(Text2.Text)
End Sub

Private Sub Option2_Click()

    Label3.Caption = Val(Text1.Text) - Val(Text2.Text)
End Sub

Private Sub Option3_Click()

    Label3.Caption = Val(Text1.Text) * Val(Text2.Text)
End Sub

Private Sub Option4_Click()

    Label3.Caption = Val(Text1.Text) / Val(Text2.Text)
End Sub 

Output:


Fig-2: First Output

Fig-3: Second Output

Related Videos:



Related VB 6 Example:


VB 6.0: Scientific Calculator

Scientific Calculator Created Using Visual Basic

This is a calculator that resembles a typical scientific calculator , albeit a simpler version. In our version, we have only included the trigonometric functions and the logarithmic functions. The reason of creating a simpler version of the calculator is to help users to learn the programming concepts in a gradual manner and not to confuse them especially those who are learning to program in Visual Basic.
To design the interface, we just to need to modify the interface of the basic calculator that we have created earlier using Visual Basic 6.  In this calculator, we have added five more buttons, they are Sin, Cos, Tan, Log and Ln.  The common trigonometric functions in Visual Basic 6 are Sin, Cos, Tan and Atn.
a) Sin is the function that computes the value of sine of an angle in radian.
b) Cos is the function that computes the value of cosine of an angle in radian.
c) Tan is the function that computes the value of tangent of an angle in radian.
d) Atn is the function that computes the value of arc tangent of an angle in radian.
Log computes the value of logarithm to base 10 whilst Ln computes the value of natural logarithm.
An angle in degree has to be converted to radian before it can be calculated by the above trigonometric functions. From high school mathematics, we know that π radian is equivalent to 180°; which means 1 radian is equivalent to π divided by 180. Therefore, in order to convert an angle x from degree to radian, we have to multiply x by (π/180). However, there is a small problem because it is rather difficult to obtain the precise value of π, fortunately, there is a way to do it in VB. First of all, we know that an arc tangent of 1 will return the value of 45° which is π/4 radian. So, to obtain the value of π, just multiply the arc tangent of 1 with 4. Now, we are ready to enter the code for all the above additional buttons.

For the Sin button, enter the following code:

Private Sub CmdSin_Click()
sinx = Round(Sin(displayValue * 4 * Atn(1) / 180), 4)
panel.Caption = sinx
End Sub
* The Round function is to correct the output to certain number of decimal places.
For the Cos button, enter the following code:
Private Sub CmdCos_Click()
cosx = Round(Cos(displayValue * 4 * Atn(1) / 180), 4)
panel.Caption = cosx
End Sub
For the Tan button, enter the following code:
Private Sub CmdTan_Click()
tanx = Round(Tan(displayValue * 4 * Atn(1) / 180), 4)
panel.Caption = tanx
End Sub
For the Ln button, enter the following code:
Private Sub CmdLn_Click()
panel.Caption = Log(displayValue)
End Sub
and for the Log button, enter the following code:
Private Sub CmdLog_Click()
panel.Caption = Log(displayValue) / Log(10)
End Sub
The interface is shown below:
Scientific Calculator created using Visual Basic 6
Scientific Calculator created using Visual Basic 6

C Programming Online Test 1

Following quiz provides Multiple Choice Questions (MCQs) related to C Programming Framework. You will have to read all the given answ...