How to resolve the algorithm Bulls and cows/Player step by step in the Tcl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bulls and cows/Player step by step in the Tcl programming language

Table of Contents

Problem Statement

Write a player of the Bulls and Cows game, rather than a scorer. The player should give intermediate answers that respect the scores to previous attempts. One method is to generate a list of all possible numbers that could be the answer, then to prune the list by keeping only those numbers that would give an equivalent score to how your last guess was scored. Your next guess can be any number from the pruned list. Either you guess correctly or run out of numbers to guess, which indicates a problem with the scoring.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bulls and cows/Player step by step in the Tcl programming language

Source code in the tcl programming language

package require struct::list
package require struct::set

proc scorecalc {guess chosen} {
    set bulls 0
    set cows 0
    foreach g $guess c $chosen {
	if {$g eq $c} {
	    incr bulls
	} elseif {$g in $chosen} {
	    incr cows
	}
    }
    return [list $bulls $cows]
}

# Allow override on command line
set size [expr {$argc ? int($argv) : 4}]

set choices {}
struct::list foreachperm p [split 123456789 ""] {
    struct::set include choices [lrange $p 1 $size]
}
set answers {}
set scores {}

puts "Playing Bulls & Cows with $size unique digits\n"
fconfigure stdout -buffering none
while 1 {
    set ans [lindex $choices [expr {int(rand()*[llength $choices])}]]
    lappend answers $ans
    puts -nonewline \
	"Guess [llength $answers] is [join $ans {}]. Answer (Bulls, cows)? "
    set score [scan [gets stdin] %d,%d]
    lappend scores $score
    if {$score eq {$size 0}} {
	puts "Ye-haw!"
	break
    }
    foreach c $choices[set choices {}] {
	if {[scorecalc $c $ans] eq $score} {
	    lappend choices $c
	}
    }
    if {![llength $choices]} {
	puts "Bad scoring? nothing fits those scores you gave:"
	foreach a $answers s $scores {
	    puts "  [join $a {}] -> ([lindex $s 0], [lindex $s 1])"
	}
	break
    }
}


  

You may also check:How to resolve the algorithm Towers of Hanoi step by step in the Pascal programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Rust programming language
You may also check:How to resolve the algorithm Pascal matrix generation step by step in the R programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the Logo programming language