#-------------------------------------------------------------
# 程序结束
#-------------------------------------------------------------
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;
|