Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/tclsh
- namespace path {::tcl::mathop ::tcl::mathfunc}
- proc lsum L {expr [join $L +]+0}
- proc random range {expr int(rand()*$range)}
- proc genAnsList {n_max lPair lAns} {
- upvar $lPair lPair1
- upvar $lAns lAns1
- set lPair {{0 0}}
- set lPair1 {{0 0}}
- for {set i 1} {[<= $i $n_max]} {incr i} {
- set q [/ [+ [sqrt 5] 1 ] 2]
- set a [int [* $q $i]]
- set b [+ $a $i]
- lappend lPair1 "$a $b"
- lappend lPair "$a $b"
- lappend lPair "$b $a"
- }
- set lPairSorted [lsort -integer -index 0 $lPair]
- for {set i 0} {[<= $i $n_max]} {incr i} {
- lappend lAns1 [lindex [lindex $lPairSorted $i] 1]
- }
- }
- proc turn {st lAns lPair} {
- set st_cur_sum [lsum $st]
- if {[== 0 $st_cur_sum]} {return {you win}}
- set a [lindex $st 0]
- set b [lindex $st 1]
- set c [abs [- $a $b]]
- set a1 [min $a [lindex $lAns $b]]
- set b1 [min $b [lindex $lAns $a]]
- set cPair1 [lindex $lPair $c]
- set a2 [random $a]
- set b2 [random $b]
- set c2 [random [+ 1 [min $a $b]]]
- set cPair2 [list [- $a $c2] [- $b $c2]]
- set st_next1 [list "$a $b1" [+ $a $b1]]
- set st_next2 [list "$a1 $b" [+ $a1 $b]]
- set st_next3 [list "$cPair1" [lsum $cPair1]]
- set lst_next [list $st_next1 $st_next2 $st_next3]
- set st_next [lindex [lsort -integer -index 1 $lst_next] 0]
- set st_next_sum [lindex $st_next 1]
- if {[== 0 $st_next_sum]} {return {i won}}
- if {[> $st_cur_sum $st_next_sum]} {
- return [lindex $st_next 0]
- } else {
- switch [random 3] {
- 0 {return "$a $b2"}
- 1 {return "$a2 $b"}
- 2 {return "$cPair2"}
- }
- }
- }
- puts {Enter heap sizes:}
- if {[< 0 [gets stdin state]]} {
- set n_max [lindex [lsort -integer $state] end]
- genAnsList $n_max lPair lAns
- set state [turn $state $lAns $lPair]
- puts $state
- while {[< 0 [gets stdin state]]} {
- set state [turn $state $lAns $lPair]
- puts $state
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement