How to resolve the algorithm Bulls and cows/Player step by step in the ALGOL 68 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Bulls and cows/Player step by step in the ALGOL 68 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 ALGOL 68 programming language
Source code in the algol programming language
# This breaks a unique code of `n' pegs and `m' colours you think of. #
INT pegs = 4, colours = 6;
MODE LIST = FLEX [1 : 0] COMBINATION,
COMBINATION = [pegs] COLOUR,
COLOUR = INT;
OP +:= = (REF LIST u, COMBINATION v) REF LIST:
# Add one combination to a list. #
([UPB u + 1] COMBINATION w; w[ : UPB u] := u; w[UPB w] := v; u := w);
PROC gen = (REF COMBINATION part, INT peg) VOID:
# Generate all unique [colours!/(colours-pegs)!] combinations. #
IF peg > pegs
THEN all combs +:= part
ELSE FOR i TO colours
DO IF BOOL unique := TRUE;
FOR j TO peg - 1 WHILE unique
DO unique := part[j] ~= i
OD;
unique
THEN part[peg] := i;
gen (part, peg + 1)
FI
OD
FI;
LIST all combs;
gen (LOC COMBINATION, 1);
PROC break code = (LIST sieved) VOID:
# Present a trial and sieve the list with the entered score. #
CASE UPB sieved + 1
IN # No elements. # printf ($l"Inconsistent scores"l$),
# One element. # printf (($l"Solution is "4(xd)l$, sieved[1]))
OUT printf (($l4(dx)$, sieved[1]));
# Read the score as a sequence of "c" and "b". #
INT col ok := 0, pos ok := 0, STRING z := "";
WHILE z = ""
DO read ((z, new line))
OD;
FOR i TO UPB z
DO (z[i] = "c" | col ok |: z[i] = "b" | pos ok) +:= 1
OD;
(pos ok = pegs | stop);
# Survivors are combinations with score as entered. #
LIST survivors;
FOR i FROM 2 TO UPB sieved
DO INT col ok i := 0, pos ok i := 0;
FOR u TO pegs
DO FOR v TO pegs
DO IF sieved[1][u] = sieved[i][v]
THEN (u = v | pos ok i | col ok i) +:= 1
FI
OD
OD;
(col ok = col ok i AND pos ok = pos ok i | survivors +:= sieved[i])
OD;
# Solution must be among the survivors. #
break code (survivors)
ESAC;
break code (all combs)
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm List comprehensions step by step in the Rust programming language
You may also check:How to resolve the algorithm Quickselect algorithm step by step in the jq programming language
You may also check:How to resolve the algorithm System time step by step in the BASIC programming language
You may also check:How to resolve the algorithm Tau function step by step in the S-BASIC programming language