Scid  4.6.5
move.tcl
Go to the documentation of this file.
1 # Copyright (C) 2009-2015 Fulvio Benini
2 #
3 # This file is part of Scid (Shane's Chess Information Database).
4 #
5 # Scid is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation.
8 #
9 # Scid is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with Scid. If not, see <http://www.gnu.org/licenses/>.
16 
17 ### move.tcl
18 ### Functions for moving within a game.
19 
20 namespace eval ::move {}
21 
22 proc ::move::drawVarArrows {} {
23  if {! $::showVarArrows || $::autoplayMode} { return 0}
24  if {[winfo exists .serGameWin]} { return 0}
25 
26  set bDrawArrow 0
27  set varList [sc_var list UCI]
28 
29  if {$varList != ""} {
30  set move [sc_game info nextMoveUCI]
31  if {$move != ""} { set varList [linsert $varList 0 $move]}
32  foreach { move } $varList {
33  set bDrawn 0
34  set sq_start [ ::board::sq [ string range $move 0 1]]
35  set sq_end [ ::board::sq [ string range $move 2 3]]
36  foreach mark $::board::_mark(.main.board) {
37  if { [lindex $mark 0] == "arrow" } {
38  if {[lindex $mark 1] == $sq_start && [lindex $mark 2] == $sq_end} {
39  set bDrawn 1
40  break
41  }
42  }
43  }
44  if {! $bDrawn } { set bDrawArrow 1; break}
45  }
46  }
47 
48  return $bDrawArrow
49 }
50 
51 proc ::move::showVarArrows {} {
52  set move [sc_game info nextMoveUCI]
53  if {$move != ""} {
54  set sq_start [ ::board::sq [ string range $move 0 1]]
55  set sq_end [ ::board::sq [ string range $move 2 3]]
56  ::board::mark::add ".main.board" "arrow" $sq_start $sq_end "green"
57  }
58  set varList [sc_var list UCI]
59  foreach { move } $varList {
60  set sq_start [ ::board::sq [ string range $move 0 1]]
61  set sq_end [ ::board::sq [ string range $move 2 3]]
62  ::board::mark::add ".main.board" "arrow" $sq_start $sq_end "blue"
63  }
64 }
65 
66 proc ::move::Start {} {
67  if {[info exists ::playMode] && [eval "$::playMode moveStart"] == 0} {
68  return
69  }
70  sc_move start
73 }
74 
75 proc ::move::End {} {
76  if {[info exists ::playMode] && [eval "$::playMode moveEnd"] == 0} {
77  return
78  }
79  sc_move end
82 }
83 
84 proc ::move::ExitVar {} {
85  if {[sc_var level] == 0 } { return 0; }
86  if {[info exists ::playMode] && [eval "$::playMode moveExitVar"] == 0} {
87  return
88  }
89  sc_var exit;
92 }
93 
94 proc ::move::Back {{count 1}} {
95  if {[sc_pos isAt start]} { return}
96  if {[sc_pos isAt vstart]} { ::move::ExitVar; return}
97  if {[info exists ::playMode] && [eval "$::playMode moveBack"] == 0} {
98  return
99  }
100 
101  sc_move back $count
102 
103  if {$count == 1} {
104  updateBoard -animate
106  } else {
108  }
109 
110  if {[sc_pos isAt vstart]} { sc_var exit}
111 
113 }
114 
115 proc ::move::Forward {{count 1}} {
116  if {[sc_pos isAt end] || [sc_pos isAt vend]} { return}
117  if {[info exists ::playMode] && [eval "$::playMode moveForward"] == 0} {
118  return
119  }
120 
121  set bArrows [::move::drawVarArrows]
122 
123  set move [sc_game info next]
124  if {$count == 1} {
125  if {[sc_var count] != 0 && ! $::autoplayMode && $::showVarPopup} {
126  showVars
127  set bArrows $::showVarArrows
128  } else {
129  if {! $bArrows} { sc_move forward}
130  }
131 
132  # Animate and speak this move:
133  updateBoard -animate
135  } else {
136  if {! $bArrows} { sc_move forward $count}
138  }
139  if {$bArrows} { ::move::showVarArrows}
140 }
141 
142 #Follow the main line or enter a variation
143 #Return 0 for main line, 1 for 1st variation, etc...
144 proc ::move::Follow {{moveUCI}} {
145  if {$moveUCI != "null"} {
146  set moveUCI2 "[string range $moveUCI 2 3][string range $moveUCI 0 1][string range $moveUCI 4 end]"
147  } else {
148  set moveUCI2 "0000"
149  }
150  set varList [sc_var list UCI]
151  set varList [linsert $varList 0 "[sc_game info nextMoveUCI]"]
152  set i -1
153  foreach {move} $varList {
154  if { [ string compare -nocase $moveUCI $move] == 0 || \
155  [ string compare -nocase $moveUCI2 $move] == 0 } {
156  if {$i == -1} {
157  sc_move forward
158  } else {
159  sc_var moveInto $i
160  }
161  return 1
162  }
163  incr i
164  }
165  return 0
166 }
167