By using VB to write scissors game to tell the conditional statement

Source: Internet
Author: User
Tags end execution expression implement string
Terms and Conditions | This article is suitable for readers who are primarily readers and interested in VB introductory teaching. Conditional judgment is a Process Control statement that completes the main logical structure in computer programming. The IF Condition expression 1 then      [code part]elseif conditional Expression 2 then      ] is provided in VB [ Code section]else       [Code part]end IF

  Control process to program execution structure. The conditional expression determines the judgment of the execution condition. Here we give an example to illustrate the working method of the control flow.        This example uses VB to implement a scissors program to analyze conditional judgment statements. First, let's analyze the basic Scissors rules:1.       scissors consists of three basic punches (stone, scissors, cloth) 2.        Rules: Stone > Scissors scissors > Bubu > Stone then let's consider the implementation of the scissors game, first scissors by the competition between the player and the computer, The scissors of the computer we generate the random number of VB by random number rnd function can generate random numbers between 0~1 we implement the random number of "*3" by Int (Rnd () 1~3) +1. Expression (stone, scissors, cloth), while the player chooses the boxing record in the variable playerguess , and then we'll write the rules to implement it. For writing rules, we can apply the process control to the conditional judgment described above. Through the process control of conditional judgment we can do the following comparison, if the computer out (stone, scissors, cloth) One of the players out of (stone, scissors, cloth), then there are 9 kinds of combination rules, so here is the need to nest the conditions of judgment.   Dim guess as string    guesstimer.enabled = false    guess = randguess (Int (Rnd () * 3) + 1)     randlabel.caption = guess    If guess = "Stone" then     & nbsp;  If playerguess = "Rock" then             Titlelabel.caption = "player out ' stone ' computer out ' stone ' both sides flat"             Draw = Draw + 1        ElseIf playerguess = "Forfex" then     & nbsp;      titlelabel.caption = "player out ' cloth ' computer out ' stone ' player wins"              win = win + 1        Else ' playerguess= "Cloth"             titlelabel.caption = "player out ' scissors ' computer out ' stone ' computer win"              Own = Own + 1         End if    ElseIf guess = "Scissors" then        If playerguess = "Rock "then            titlelabel.caption =" player out ' stone ' computer out ' scissors ' player wins '             win = win + 1         ElseIf playerguess = "Forfex" THEN      &Nbsp;     titlelabel.caption = "player out ' scissors ' computer out ' scissors ' both sides flat"              Draw = Draw + 1        Else ' playerguess= ' Cloth '              titlelabel.caption = "player out ' cloth ' computer out ' scissors ' computer win"              Own = Own + 1        End If     Else ' guess= "cloth"         If playerguess = "Rock" then             titlelabel.caption = "player out ' stone ' computer out ' cloth ' computer win"              Own = Own + 1        ElseIf playerguess = "Forfex" then            TitleLabel.Caption = " Player out ' scissors ' computer out ' cloth ' player wins '           &nbsP Win = win + 1        Else ' playerguess= "Cloth"              titlelabel.caption = "player out ' cloth ' computer out ' cloth ' both sides flat"              Draw = Draw + 1        End if    End if    resultlabel.caption = "Win:" & Win & "Field" & "Ping:" & Draw & "field" & "Negative:" & Own & Amp Field

At the same time we count win, flat, negative field number.

The whole program:

Program Interface Design

Program Source code:

Private playerguess as Stringprivate randguess () as Stringprivate Win as Singleprivate Own as Singleprivate Draw as single

Private Sub clothcommand_click () playerguess = "Cloth" guesstimer.enabled = True titlelabel.caption = "player ready for ' cloth '" E nd Sub

Private Sub forfexcommand_click () playerguess = "Forfex" guesstimer.enabled = True titlelabel.caption = "Player ready for" scissors ' "End Sub

 private Sub Form_Load ()     ReDim randguess (3)     randguess (1) = "Stone"      randguess (2) = "Scissors"     randguess (3) = "Cloth"     resultlabel.caption = "Win:" & Win & "Field" & "Ping:" & Draw & "field" & "Negative:" & Own & "field"         Dim sum As single    sum = 0    for i = 1 to 12        sum = Sum + i    Next i    MsgBox sumend Sub

 private Sub Guesscommand_click ()     Dim guess as string    guesstimer.enabled = false    guess = randguess (Int (Rnd () * 3) + 1)     randlabel.caption = guess  & nbsp If guess = "stone" then        if playerguess = "Rock" then             titlelabel.caption = "player out ' stone ' computer out ' stone ' both sides flat"              Draw = Draw + 1        ElseIf playerguess = " Forfex "then            TitleLabel.Caption =" player out ' cloth ' computer out ' stone ' Player wins "            win = win + 1         Else ' playerguess= "Cloth"             Titlelabel.caption = "player out ' scissors ' computer out ' stone ' computer win"             Own = Own + 1        End if    ElseIf Guess = "Scissors" then        If playerguess = "Rock" then             titlelabel.caption = "player out ' stone ' computer out ' scissors ' player wins"              win = win + 1        ElseIf playerguess = "Forfex" then            titlelabel.caption = "player out ' scissors ' computer out ' scissors ' both sides flat"             Draw = Draw + 1         Else ' playerguess= "Cloth"             Titlelabel.caption = "player out ' cloth ' computer out ' scissors ' computer wins"             Own = Own + 1        End if    Else ' guess="Cloth"         If playerguess = "Rock" then             titlelabel.caption = "player out ' stone ' computer out ' cloth ' computer win"              Own = Own + 1        ElseIf playerguess = "Forfex" Then             titlelabel.caption = "player out ' scissors ' computer out ' cloth ' player wins"              win = win + 1         Else ' playerguess= "Cloth"             Titlelabel.caption = "player out ' cloth ' computer out ' cloth ' both sides flat"             Draw = Draw + 1        End if    end if    Resultlabel.caption = "Win:" & Win & "Field" & "Ping:" & Draw & "field" & "Negative:" & Own & "field" End Sub

Private Sub guesstimer_timer () randlabel.caption = Randguess (Int (Rnd () * 3) + 1) End Sub

Private Sub rockcommand_click () playerguess = "Rock" guesstimer.enabled = True titlelabel.caption = "player ready for ' stone '" En D Sub

Summarize:

This procedure is a simple and easy to understand the VB Getting Started program, through this procedure lets the just start friend in the interest to understand the program basic logic structure at the same time throws away the C machine complexity.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.