Chinaunix首页 | 论坛 | 博客
  • 博客访问: 75760
  • 博文数量: 7
  • 博客积分: 1415
  • 博客等级: 上尉
  • 技术积分: 100
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-15 22:11
文章存档

2009年(2)

2008年(4)

2007年(1)

我的朋友

分类:

2009-07-25 21:43:16

#-------------------------------------------------------------
# 程序结束
#-------------------------------------------------------------
proc GameOver {} {
        global MineBody row col FmHead;

        for {set i 1} {$i <= $row} {incr i} {
                for {set j 1} {$j <= $col} {incr j} {
                        if {$MineBody($i,$j,status) == "OPENED"} {
                                continue;
                        } elseif {$MineBody($i,$j,val) == "*"} {
                                DisplayChanges $i $j $MineBody($i,$j,val) "OPENED" "RELIEF";
                        }
                }
        }

        CancelEvent;
        $FmHead.msg config -foreground red -text "游戏结束!";
}

#-------------------------------------------------------------
# 注销事件响应
#-------------------------------------------------------------
proc CancelEvent {} {
        global row col MineBody;

        for {set i 1} {$i <= $row} {incr i} {
                for {set j 1} {$j <= $col} {incr j} {
                        bind $MineBody($i,$j,NameOfBtn) <Button-1> "";
                        bind $MineBody($i,$j,NameOfBtn) <Button-2> "";
                        bind $MineBody($i,$j,NameOfBtn) <Button-3> "";
                        $MineBody($i,$j,NameOfBtn) config -state disable;
                }
        }
}

#-------------------------------------------------------------
# 响应鼠标中键
#-------------------------------------------------------------
proc ClickMidBtn {x y} {
        global MineBody row col;
        set flag 0;
        
        if {$MineBody($x,$y,status) != "OPENED"} {
                return;
        }

        for {set i [expr $x - 1]} {$i <= [expr $x + 1]} {incr i} {
                for {set j [expr $y - 1]} {$j <= [expr $y + 1]} {incr j} {
                        if {$MineBody($i,$j,val) == -1} {
                                continue;
                        } elseif {$MineBody($i,$j,status) == "SETFLAG"} {
                                incr flag;
                        }
                }
        }
        if {$flag != $MineBody($x,$y,val)} {
                return;
        }

        for {set i [expr $x - 1]} {$i <= [expr $x + 1]} {incr i} {
                for {set j [expr $y - 1]} {$j <= [expr $y + 1]} {incr j} {
                        if {$MineBody($i,$j,val) == -1} {
                                continue;
                        } elseif {$MineBody($i,$j,status) == "OPENED"} {
                                continue;
                        } elseif {$MineBody($i,$j,status) == "SETFLAG"} {
                                if {$MineBody($i,$j,val) != "*"} {
                                        DisplayChanges $i $j "×" "OPENED";
                                        GameOver;
                                } else {
                                        continue;
                                }
                        }

                        ClickLeftBtn $i $j;
                }
        }
}

#-------------------------------------------------------------
# 响应鼠标右键
#-------------------------------------------------------------
proc ClickRightBtn {x y} {
        global MineBody row col mines flags FmHead;
        
        if {$MineBody($x,$y,status) == "UNDO"} {
                DisplayChanges $x $y "▲" "SETFLAG";
                incr flags 1;
        } elseif {$MineBody($x,$y,status) == "SETFLAG"} {
                DisplayChanges $x $y "" "UNDO";
                incr flags -1;
        }

        $FmHead.minecnt config -text "剩余:[expr $mines - $flags] ";
}

#-------------------------------------------------------------
# 判断是否扫完所有的雷
#-------------------------------------------------------------
proc IsTaskOver {} {
        global MineBody row col FmHead;

        for {set i 1} {$i <= $row} {incr i} {
                for {set j 1} {$j <= $col} {incr j} {
                        if {$MineBody($i,$j,status) != "OPENED" && \
                                $MineBody($i,$j,val) != "*"} {
                                return;
                        }
                }
        }

        CancelEvent;
        $FmHead.msg config -foreground red -text "恭喜通关!";
}

#-------------------------------------------------------------
# Executing from here -->
#-------------------------------------------------------------

set FmHead [frame .fmhead];
set FmBody [frame .fmbody];
grid $FmHead;
grid $FmBody;

label $FmHead.lrow -text "行:";
entry $FmHead.erow -relief sunken -width 3;
label $FmHead.lcol -text "列:";
entry $FmHead.ecol -relief sunken -width 3;
button $FmHead.start -text "重新开始" -command "init";
message $FmHead.msg -width 100;
message $FmHead.minecnt -width 100;
grid $FmHead.minecnt $FmHead.start $FmHead.msg;
set MineBody(StartFlag) 0;
init;

阅读(1331) | 评论(1) | 转发(0) |
0

上一篇:TCL/TK脚本 扫雷(源码一)

下一篇:没有了

给主人留下些什么吧!~~

f.u.moon2010-01-05 10:32:10

厉害! 我最近也在mac下完成扫雷程序的编写. 嘿嘿.