[第十二关]24游戏

24 游戏测试一个人的心算。

任务

编写一个程序,随机选择并显示四个数字,每个数字从 1 ──► 9(含)开始,允许重复。

程序应提示玩家仅使用这些数字输入算术表达式,并且所有这四位数字都只使用一次。程序应检查然后评估表达式

目标是让玩家输入一个(在数字上)计算为24的表达式。

  • 只允许使用以下运算符/函数:乘法、除法、加法、减法
  • 除法应使用浮点或有理算术等来保留余数。
  • 如果使用中缀表达式求值器,则允许使用括号。
  • 不允许从提供的数字形成多个数字。(因此,当给出 1、2、2 和 1 时,12+12 的答案是错误的)。
  • 给定时的数字顺序不必保留。
笔记
  • 使用的表达式评估器的类型不是强制性的。例如,RPN评估器同样可以接受。
  • 任务不是让程序生成表达式,或者测试表达式是否可能。

 

相关任务
  • 24 游戏/解决

 

参考

参考源代码:

AutoExecute: 
    Title := "24 Game" 
    Gui, -MinimizeBox 
    Gui, Add, Text, w230 vPuzzle 
    Gui, Add, Edit, wp vAnswer 
    Gui, Add, Button, w70, &Generate 
    Gui, Add, Button, x+10 wp Default, &Submit 
    Gui, Add, Button, x+10 wp, E&xit 
 
 
ButtonGenerate: ; new set of numbers 
    Loop, 4 
        Random, r%A_Index%, 1, 9 
    Puzzle = %r1%, %r2%, %r3%, and %r4% 
    GuiControl,, Puzzle, The numbers are:  %Puzzle%  - Good luck! 
    GuiControl,, Answer ; empty the edit box 
    ControlFocus, Edit1 
    Gui, -Disabled 
    Gui, Show,, %Title% 
Return ; end of auto execute section 
 
 
ButtonSubmit: ; check solution 
    Gui, Submit, NoHide 
    Gui, +Disabled 
 
    ; check numbers used 
    RegExMatch(Answer, "(\d)\D+(\d)\D+(\d)\D+(\d)", $) 
    ListPuzzle := r1 "," r2 "," r3 "," r4 
    ListAnswer := $1 "," $2 "," $3 "," $4 
    Sort, ListPuzzle, D, 
    Sort, ListAnswer, D, 
    If Not ListPuzzle = ListAnswer { 
        MsgBox, 48, Error - %Title%, Numbers used!`n%Answer% 
        Goto, TryAgain 
    } 
 
    ; check operators used 
    StringReplace, $, $, +,, All 
    StringReplace, $, $, -,, All 
    StringReplace, $, $, *,, All 
    StringReplace, $, $, /,, All 
    StringReplace, $, $, (,, All 
    StringReplace, $, $, ),, All 
    Loop, 9 
        StringReplace, $, $, %A_Index%,, All 
    If StrLen($) > 0 
    Or InStr(Answer, "**") 
    Or InStr(Answer, "//") 
    Or InStr(Answer, "++") 
    Or InStr(Answer, "--") { 
        MsgBox, 48, Error - %Title%, Operators used!`n%Answer% 
        Goto, TryAgain 
    } 
 
    ; check result 
    Result := Eval(Answer) 
    If Not Result = 24 { 
        MsgBox, 48, Error - %Title%, Result incorrect!`n%Result% 
        Goto, TryAgain 
    } 
 
    ; if we are sill here 
    MsgBox, 4, %Title%, Correct solution! Play again? 
    IfMsgBox, Yes 
        Gosub, ButtonGenerate 
    Else 
        ExitApp 
Return 
 
 
TryAgain: ; alternative ending of routine ButtonSubmit 
    ControlFocus, Edit1 
    Gui, -Disabled 
    Gui, Show 
Return 
 
 
GuiClose: 
GuiEscape: 
ButtonExit: 
    ExitApp 
Return 
 
 
;--------------------------------------------------------------------------- 
Eval(Expr) { ; evaluate expression using separate AHK process 
;--------------------------------------------------------------------------- 
    ; credit for this function goes to AutoHotkey forum member Laszlo 
    ; http://www.autohotkey.com/forum/topic9578.html 
    ;----------------------------------------------------------------------- 
    static File := "24$Temp.ahk" 
 
    ; delete old temporary file, and write new 
    FileDelete, %File% 
    FileContent := "#NoTrayIcon`r`n" 
                .  "FileDelete, " File "`r`n" 
                .  "FileAppend, `% " Expr ", " File "`r`n" 
    FileAppend, %FileContent%, %File% 
 
    ; run AHK to execute temp script, evaluate expression 
    RunWait, %A_AhkPath% %File% 
 
    ; get result 
    FileRead, Result, %File% 
    FileDelete, %File% 
    Return, Result 
}

给TA捐赠
共{{data.count}}人
人已捐赠
AHKV1函数库图形图像

[opencv_ahk_lib]Opencv的ahk函数库

2022-2-17 16:43:56

AHKV1游戏

[第十三关]24游戏(解决)

2022-2-18 10:55:56

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索