codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
puts {Welcome to mastermind.} puts {A four digit code has been selected.} puts {Guess at the code, and you'll be given a black token for each digit in the correct place, and a white token for each digit that the code does contain, but is in the wrong place.} puts {Enter your guesses like: "4 5 6 7".\n} set code {} for {set i 0} {$i < 4} {incr i} { lappend code [expr int(rand() * 10)] } set numGuesses 0 while {1} { puts -nonewline {Guess: } flush stdout gets stdin guess if {![string match {[0-9] [0-9] [0-9] [0-9]} $guess]} { puts {Please guess in the format: "0 1 2 3"} flush stdout } else { incr numGuesses set black 0 set white 0 for {set i 0} {$i < 4} {incr i} { set c [lindex $guess $i] if {[lindex $code $i] == $c} { incr black } elseif {[lsearch $code $c] != -1} { incr white } } for {set i 0} {$i < $black} {incr i} {puts -nonewline B} for {set i 0} {$i < $white} {incr i} {puts -nonewline W} for {set i 0} {$i < 4 - ($black + $white)} {incr i} {puts -nonewline .} puts {} flush stdout if {$black == 4} break } } puts "Congratulations, you guessed correctly in just $numGuesses guesses."
Private
[
?
]
Run code
Submit