首页 > 开发 > 综合 > 正文

举例通过VB编写猜拳游戏来讲述条件语句

2024-07-21 02:30:06
字体:
来源:转载
供稿:网友

本文适合读者主要是入门读者和对vb入门教学有兴趣的朋友。条件判断是计算机编程中完成主要逻辑结构的流程控制语句。在vb中提供了if 条件表达式1 then      [代码部分]elseif 条件表达式2 then       [代码部分]else       [代码部分]end if

 的控制流程来控制程序的执行结构。条件表达式决定了执行条件的判断。这里我们来举个例子说明该控制流程的工作方法。       本例通过用vb实现一个猜拳程序来分析条件判断语句。首先我们先来分析一下基本的猜拳规则:1.       猜拳包括三个基本的拳(石头,剪刀,布)2.       胜负规则:石头>剪刀 剪刀>布 布>石头然后我们来考虑猜拳游戏的实现,首先猜拳是由选手和电脑之间的比赛进行的,电脑的猜拳我们通过随机数来生成vb的随机输函数rnd函数可以生成0~1之间的随机数我们通过int(rnd()*3)+1来实现生成【1~3】的随机数,表示(石头,剪刀,布),而选手选择的拳记录在变量中playerguess,然后我们再来编写规则来实现。对于编写规则我们就可以运用到上面所介绍的条件判断的流程控制了。通过条件判断的流程控制我们可以做如下比较,如果电脑出(石头,剪刀,布)的一种,选手出的(石头,剪刀,布)的一种,那么就有9种组合规则,因此这里就需要嵌套的条件判断。  dim guess as string    guesstimer.enabled = false    guess = randguess(int(rnd() * 3) + 1)    randlabel.caption = guess    if guess = "石头" then        if playerguess = "rock" then            titlelabel.caption = "玩家出'石头' 电脑出'石头' 双方平"            draw = draw + 1        elseif playerguess = "forfex" then            titlelabel.caption = "玩家出'布' 电脑出'石头' 玩家赢"            win = win + 1        else 'playerguess="cloth"            titlelabel.caption = "玩家出'剪刀' 电脑出'石头' 电脑赢"            own = own + 1        end if    elseif guess = "剪刀" then        if playerguess = "rock" then            titlelabel.caption = "玩家出'石头' 电脑出'剪刀' 玩家赢"            win = win + 1        elseif playerguess = "forfex" then            titlelabel.caption = "玩家出'剪刀' 电脑出'剪刀' 双方平"            draw = draw + 1        else 'playerguess="cloth"            titlelabel.caption = "玩家出'布' 电脑出'剪刀' 电脑赢"            own = own + 1        end if    else 'guess="布"        if playerguess = "rock" then            titlelabel.caption = "玩家出'石头' 电脑出'布' 电脑赢"            own = own + 1        elseif playerguess = "forfex" then            titlelabel.caption = "玩家出'剪刀' 电脑出'布' 玩家赢"            win = win + 1        else 'playerguess="cloth"            titlelabel.caption = "玩家出'布' 电脑出'布' 双方平"            draw = draw + 1        end if    end if    resultlabel.caption = "赢:" & win & "场" & "平:" & draw & "场" & "负:" & own & "场"

 同时我们统计赢,平,负的场数。

 

整个程序设计:

程序界面设计

程序源代码:

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 = "玩家准备出'布'"end sub

 private sub forfexcommand_click()    playerguess = "forfex"    guesstimer.enabled = true    titlelabel.caption = "玩家准备出'剪刀'"end sub

 private sub form_load()    redim randguess(3)    randguess(1) = "石头"    randguess(2) = "剪刀"    randguess(3) = "布"    resultlabel.caption = "赢:" & win & "场" & "平:" & draw & "场" & "负:" & own & "场"        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    if guess = "石头" then        if playerguess = "rock" then            titlelabel.caption = "玩家出'石头' 电脑出'石头' 双方平"            draw = draw + 1        elseif playerguess = "forfex" then            titlelabel.caption = "玩家出'布' 电脑出'石头' 玩家赢"            win = win + 1        else 'playerguess="cloth"            titlelabel.caption = "玩家出'剪刀' 电脑出'石头' 电脑赢"            own = own + 1        end if    elseif guess = "剪刀" then        if playerguess = "rock" then            titlelabel.caption = "玩家出'石头' 电脑出'剪刀' 玩家赢"            win = win + 1        elseif playerguess = "forfex" then            titlelabel.caption = "玩家出'剪刀' 电脑出'剪刀' 双方平"            draw = draw + 1        else 'playerguess="cloth"            titlelabel.caption = "玩家出'布' 电脑出'剪刀' 电脑赢"            own = own + 1        end if    else 'guess="布"        if playerguess = "rock" then            titlelabel.caption = "玩家出'石头' 电脑出'布' 电脑赢"            own = own + 1        elseif playerguess = "forfex" then            titlelabel.caption = "玩家出'剪刀' 电脑出'布' 玩家赢"            win = win + 1        else 'playerguess="cloth"            titlelabel.caption = "玩家出'布' 电脑出'布' 双方平"            draw = draw + 1        end if    end if    resultlabel.caption = "赢:" & win & "场" & "平:" & draw & "场" & "负:" & own & "场"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 = "玩家准备出'石头'"end sub

 总结:

       本程序是一个简单易懂的vb入门程序,通过该程序使刚起步的朋友在兴趣中了解程序的基本逻辑结构同时抛开了c的机器复杂性。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表