我试图教自己用Tcl编程.(我想更熟悉语言以理解其他人的代码 - SCID国际象棋)我自己设定的任务是激励我学习Tcl是为了解决8皇后问题.我创建程序的方法是过度"原型化"解决方案.所以.我要在for循环中嵌套一个for循环,在第2行中持有q pos的第2行中的q pos
这是我的代码
set allowd 1
set notallowd 0
for {set r1p 1} {$r1p <= 8} {incr r1p } {
puts "1st row q placed at $r1p"
;# re-initialize r2 'free for q placemnt' array after every change of r1 q pos:
for {set i 1 } {$i <= 8} {incr i} { set r2($i) $allowd }
for { set r2($r1p) $notallowd ; set r2([eval $r1p-1]) $notallowd ;
set r2([eval $r1p+1]) $notallowd ; set r2p 1} {$r2p <= 8} {
incr r2p ;# end of 'next' arg of r2 forloop
}
;# commnd arg of r2 forloop placed below:
{puts "2nd row q placed at $r2p"
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我运行代码时,解释器正在使用致命错误中止:"错误的#args应该用于启动测试下一个命令.
我已经查看了几次我的代码并且看不到我错过了任何for循环参数.
在最后一个for循环中的命令之前的回车是什么让你.从Tcl手册页的第一个语法规则来看,"分号和换行符是命令分隔符,除非引用如下所述." 顺便说一句,你的评估应该是expr's.
这对我有用:
set allowd 1
set notallowd 0
for {set r1p 1} {$r1p <= 8} {incr r1p } {
puts "1st row q placed at $r1p"
;# re-initialize r2 'free for q placemnt' array after every change of r1 q pos:
for {set i 1 } {$i <= 8} {incr i} { set r2($i) $allowd }
for { set r2($r1p) $notallowd ; set r2([expr $r1p-1]) $notallowd ;
set r2([expr $r1p+1]) $notallowd ; set r2p 1} {$r2p <= 8} {
incr r2p ;# end of 'next' arg of r2 forloop
} {
# commnd arg of r2 forloop placed below:
puts "2nd row q placed at $r2p"
}
}
Run Code Online (Sandbox Code Playgroud)