#!/usr/bin/tclsh #--- (+ ab) #--- , , (a + b) namespace path {::tcl::mathop ::tcl::mathfunc} #--- proc lsum L {expr [join $L +]+0} proc random range {expr int(rand()*$range)}
set n_max 20 ;# for {set i 1} {[<= $i $n_max]} {incr i} { set q [/ [+ [sqrt 5] 1 ] 2] ;# " " set a [int [* $q $i]] ;#i- a set b [+ $a $i] ;#ie b lappend lPair1 "$a $b" ;# lPair1 a b }
puts lPair1; # ; , ,
#--- : #----st - , #----lAns - #----lPair - , #-------------- proc turn {st lAns lPair} { set st_cur_sum [lsum $st] ;# #--- , 0 0, #--- , if {[== 0 $st_cur_sum]} {return {you win}} set a [lindex $st 0] ;# set b [lindex $st 1] ;# #---lindex - , #--- 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 } }
Source: https://habr.com/ru/post/141029/
All Articles