21是一个两人游戏,游戏是通过选择一个数字(1、2或3 )添加到运行总数中来进行的。
游戏由选择的数字导致运行总数恰好 达到21的玩家获胜。
运行总计从零开始。一名玩家将成为电脑。
玩家交替提供要添加到运行总数中的数字。
-
任务
编写一个计算机程序,它将:
-
- 做提示(或提供按钮菜单),
- 检查错误并显示适当的错误消息,
- 做加法(将所选数字添加到运行总数中),
- 显示运行总计,
- 为玩家提供退出/退出/暂停/停止/关闭程序的机制,
- 当有获胜者时发出通知,并且
- 确定谁先走(可能是随机选择或用户选择,或者可以在游戏开始时指定)。
参考代码:
Gui, font, S16
Gui, add, Radio, vRadioC , 点心
Gui, add, Radio, vRadioE x+0 Checked, 简单
Gui, add, Radio, vRadioH x+0, 困难
Gui, add, text, xs vT, 总和 : 00
Gui, add, text, xs vComputer, 电脑出牌 00
Gui, add, text, xs Section, 玩家 1
loop, 3
Gui, add, button, x+5 vPlayer1_%A_Index% gTotal, % A_Index
Gui, add, button, xs vReset gReset, 重置
Gui show,, 21 游戏
Winners := [1,5,9,13,17,21]
gosub, reset
return
;-----------------------------------
reset:
total := 0
GuiControl,, T, % "总和 : " SubStr("00" Total, -1)
GuiControl,, Computer, % "电脑等待"
Loop 3
GuiControl, Enable, % "Player1_" A_Index
Random, rnd, 0, 1
if rnd
{
Loop 3
GuiControl, Disable, % "Player1_" A_Index
gosub ComputerTurn
}
return
;-----------------------------------
Total:
Added := SubStr(A_GuiControl, 9,1)
Total += Added
GuiControl,, T, % "总和 : " SubStr("00" Total, -1)
if (total >= 21)
{
MsgBox % "玩家 1 赢"
gosub reset
return
}
Loop 3
GuiControl, Disable, % "Player1_" A_Index
gosub, ComputerTurn
return
;-----------------------------------
ComputerTurn:
Gui, Submit, NoHide
Sleep 500
if RadioE
{
if (total < 13)
RadioC := true
else
RadioH := true
}
if RadioC
{
Random, Deal, 1, 3
total += Deal
}
if RadioH
{
for i, v in Winners
if (total >= v)
continue
else
{
Deal := v - total
if Deal > 3
Random, Deal, 1, 3
total += Deal
break
}
}
GuiControl,, T, % "总和 : " SubStr("00" Total, -1)
GuiControl,, Computer, % "电脑出牌 " Deal
if (total=21)
{
MsgBox 电脑赢
gosub, reset
}
else
Loop 3
GuiControl, Enable, % "Player1_" A_Index
return
可以
感谢分享,试用一下
积分不够,回复来凑