|
|
|
|
@@ -28,20 +28,126 @@ As published in Basic Computer Games (1978):
|
|
|
|
|
Downloaded from Vintage Basic at
|
|
|
|
|
http://www.vintage-basic.net/games.html
|
|
|
|
|
|
|
|
|
|
#### Porting Notes
|
|
|
|
|
###How the computer deduces your guess.
|
|
|
|
|
|
|
|
|
|
in [#613](https://github.com/coding-horror/basic-computer-games/pull/613)
|
|
|
|
|
The computer takes the number of black pegs and white pegs that the user reports
|
|
|
|
|
and uses that information as a target. It then assumes its guess is the answer
|
|
|
|
|
and proceeds to compare the black and white pegs against all remaining possible
|
|
|
|
|
answers. For each set of black and white pegs it gets in these comparisons, if
|
|
|
|
|
they don't match what the user reported, then they can not be part of the solution.
|
|
|
|
|
This can be a non-intuitive assumption, so we'll walk through it with a three color,
|
|
|
|
|
three position example (27 possible solutions.)
|
|
|
|
|
|
|
|
|
|
Let's just suppose our secret code we're hiding from the computer is `BWB`
|
|
|
|
|
|
|
|
|
|
First let's point out the commutative property of comparing two codes for their
|
|
|
|
|
black and white pegs. A black peg meaning correct color and correct position, and
|
|
|
|
|
a white peg meaning correct color and wrong position. If the computer guesses
|
|
|
|
|
`RBW` then the black/white peg report is 0 black, 2 white. But if `RBW` is the
|
|
|
|
|
secret code and the computer guesses `BWB` the reporting for `BWB` is going to be
|
|
|
|
|
the same, 0 black, 2 white.
|
|
|
|
|
|
|
|
|
|
Now lets look at a table with the reporting for every possible guess the computer
|
|
|
|
|
can make while our secret code is `BWB`.
|
|
|
|
|
|
|
|
|
|
| Guess | Black | White | | Guess | Black | White | | Guess | Black | White |
|
|
|
|
|
|-------|-------|-------|-----|-------|-------|-------|-----|-------|-------|-------|
|
|
|
|
|
| BBB | 2 | 0 | | WBB | 1 | 2 | | RBB | 1 | 1 |
|
|
|
|
|
| BBW | 1 | 2 | | WBW | 0 | 2 | | RBW | 0 | 2 |
|
|
|
|
|
| BBR | 1 | 1 | | WBR | 0 | 2 | | RBR | 0 | 1 |
|
|
|
|
|
| BWB | 3 | 0 | | WWB | 2 | 0 | | RWB | 2 | 0 |
|
|
|
|
|
| BWW | 2 | 0 | | WWW | 1 | 0 | | RWW | 1 | 0 |
|
|
|
|
|
| BWR | 2 | 0 | | WWR | 1 | 0 | | RWR | 1 | 0 |
|
|
|
|
|
| BRB | 2 | 0 | | WRB | 1 | 1 | | RRB | 1 | 0 |
|
|
|
|
|
| BRW | 1 | 1 | | WRW | 0 | 1 | | RRW | 0 | 1 |
|
|
|
|
|
| BRR | 1 | 0 | | WRR | 0 | 1 | | RRR | 0 | 0 |
|
|
|
|
|
|
|
|
|
|
The computer has guessed `RBW` and the report on it is 0 black, 2 white. The code
|
|
|
|
|
used to eliminate other solutions looks like this:
|
|
|
|
|
|
|
|
|
|
`1060 IF B1<>B OR W1<>W THEN I(X)=0`
|
|
|
|
|
|
|
|
|
|
was changed to:
|
|
|
|
|
which says set `RBW` as the secret and compare it to all remaining solutions and
|
|
|
|
|
get rid of any that don't match the same black and white report, 0 black and 2 white.
|
|
|
|
|
So let's do that.
|
|
|
|
|
|
|
|
|
|
`1060 IF B1>B OR W1>W THEN I(X)=0`
|
|
|
|
|
Remember, `RBW` is pretending to be the secret code here. These are the remaining
|
|
|
|
|
solutions reporting their black and white pegs against `RBW`.
|
|
|
|
|
|
|
|
|
|
This was done because of a bug:
|
|
|
|
|
| Guess | Black | White | | Guess | Black | White | | Guess | Black | White |
|
|
|
|
|
|-------|-------|-------|-----|-------|-------|-------|-----|-------|-------|-------|
|
|
|
|
|
| BBB | 1 | 0 | | WBB | 1 | 1 | | RBB | 2 | 0 |
|
|
|
|
|
| BBW | 2 | 0 | | WBW | 2 | 0 | | RBW | 3 | 0 |
|
|
|
|
|
| BBR | 1 | 1 | | WBR | 1 | 2 | | RBR | 2 | 0 |
|
|
|
|
|
| BWB | 0 | 2 | | WWB | 0 | 2 | | RWB | 1 | 2 |
|
|
|
|
|
| BWW | 1 | 1 | | WWW | 1 | 0 | | RWW | 2 | 0 |
|
|
|
|
|
| BWR | 0 | 3 | | WWR | 1 | 1 | | RWR | 1 | 1 |
|
|
|
|
|
| BRB | 0 | 2 | | WRB | 0 | 3 | | RRB | 1 | 1 |
|
|
|
|
|
| BRW | 1 | 2 | | WRW | 1 | 1 | | RRW | 2 | 0 |
|
|
|
|
|
| BRR | 0 | 2 | | WRR | 0 | 2 | | RRR | 1 | 0 |
|
|
|
|
|
|
|
|
|
|
Originally, after guessing and getting feedback, the computer would look through every possible combination, and for all that haven't previously been marked as impossible it would check whether or not the black and white pins that that combination should get are not-equal to what its previous guess got and, if they are equal, the combination would be marked as possible, and if they aren't equal then the combination would be marked as impossible. This results in a bug where the computer eliminates the correct answer as a possible solution after the first guess, unless the first guess just happens to be correct.
|
|
|
|
|
Now we are going to eliminate every solution that **DOESN'T** matches 0 black and 2 white.
|
|
|
|
|
|
|
|
|
|
this was discussed in more detail in [issue #611](https://github.com/coding-horror/basic-computer-games/issues/611)
|
|
|
|
|
| Guess | Black | White | | Guess | Black | White | | Guess | Black | White |
|
|
|
|
|
|----------|-------|-------|-----|----------|-------|-------|-----|----------|-------|-------|
|
|
|
|
|
| ~~~BBB~~ | 1 | 0 | | ~~~WBB~~ | 1 | 1 | | ~~~RBB~~ | 2 | 0 |
|
|
|
|
|
| ~~~BBW~~ | 2 | 0 | | ~~~WBW~~ | 2 | 0 | | ~~~RBW~~ | 3 | 0 |
|
|
|
|
|
| ~~~BBR~~ | 1 | 1 | | ~~~WBR~~ | 1 | 2 | | ~~~RBR~~ | 2 | 0 |
|
|
|
|
|
| BWB | 0 | 2 | | WWB | 0 | 2 | | ~~~RWB~~ | 1 | 2 |
|
|
|
|
|
| ~~~BWW~~ | 1 | 1 | | ~~~WWW~~ | 1 | 0 | | ~~~RWW~~ | 2 | 0 |
|
|
|
|
|
| ~~~BWR~~ | 0 | 3 | | ~~~WWR~~ | 1 | 1 | | ~~~RWR~~ | 1 | 1 |
|
|
|
|
|
| BRB | 0 | 2 | | ~~~WRB~~ | 0 | 3 | | ~~~RRB~~ | 1 | 1 |
|
|
|
|
|
| ~~~BRW~~ | 1 | 2 | | ~~~WRW~~ | 1 | 1 | | ~~~RRW~~ | 2 | 0 |
|
|
|
|
|
| BRR | 0 | 2 | | WRR | 0 | 2 | | ~~~RRR~~ | 1 | 0 |
|
|
|
|
|
|
|
|
|
|
additionally, it's recommended that you have the computer elimate it's previous guess as possible unless that guess was correct. (the rust port does this)
|
|
|
|
|
That wipes out all but five solutions. Notice how the entire right column of solutions
|
|
|
|
|
is eliminated, including our original guess of `RBW`, therefore eliminating any
|
|
|
|
|
special case to specifically eliminate this guess from the solution set when we first find out
|
|
|
|
|
its not the answer.
|
|
|
|
|
|
|
|
|
|
Continuing on, we have the following solutions left of which our secret code, `BWB`
|
|
|
|
|
is one of them. Remember our commutative property explained previously.
|
|
|
|
|
|
|
|
|
|
| Guess | Black | White |
|
|
|
|
|
|-------|-------|-------|
|
|
|
|
|
| BWB | 0 | 2 |
|
|
|
|
|
| BRB | 0 | 2 |
|
|
|
|
|
| BRR | 0 | 2 |
|
|
|
|
|
| WWB | 0 | 2 |
|
|
|
|
|
| WRR | 0 | 2 |
|
|
|
|
|
|
|
|
|
|
So for its second pick, the computer will randomly pick one of these remaining solutions. Let's pick
|
|
|
|
|
the middle one, `BRR`, and perform the same ritual. Our user reports to the computer
|
|
|
|
|
that it now has 1 black, 0 whites when comparing to our secret code `BWB`. Let's
|
|
|
|
|
now compare `BRR` to the remaining five solutions and eliminate any that **DON'T**
|
|
|
|
|
report 1 black and 0 whites.
|
|
|
|
|
|
|
|
|
|
| Guess | Black | White |
|
|
|
|
|
|----------|-------|-------|
|
|
|
|
|
| BWB | 1 | 0 |
|
|
|
|
|
| ~~~BRB~~ | 2 | 0 |
|
|
|
|
|
| ~~~BRR~~ | 3 | 0 |
|
|
|
|
|
| ~~~WWB~~ | 0 | 1 |
|
|
|
|
|
| ~~~WRR~~ | 2 | 0 |
|
|
|
|
|
|
|
|
|
|
Only one solution matches and its our secret code! The computer will guess this
|
|
|
|
|
one next as it's the only choice left, for a total of three moves.
|
|
|
|
|
Coincidentally, I believe the expected maximum number of moves the computer will
|
|
|
|
|
make is the number of positions plus one for the initial guess with no information.
|
|
|
|
|
This is because it is winnowing down the solutions
|
|
|
|
|
logarithmically on average. You noticed on the first pass, it wiped out 22
|
|
|
|
|
solutions. If it was doing this logarithmically the worst case guess would
|
|
|
|
|
still eliminate 18 of the solutions leaving 9 (3<sup>2</sup>). So we have as
|
|
|
|
|
a guideline:
|
|
|
|
|
|
|
|
|
|
Log<sub>(# of Colors)</sub>TotalPossibilities
|
|
|
|
|
|
|
|
|
|
but TotalPossibilities = (# of Colors)<sup># of Positions</sup>
|
|
|
|
|
|
|
|
|
|
so you end up with the number of positions as a guess limit. If you consider the
|
|
|
|
|
simplest non-trivial puzzle, two colors with two positions, and you guess BW or
|
|
|
|
|
WB first, the most you can logically deduce if you get 1 black and 1 white is
|
|
|
|
|
that it is either WW, or BB which could bring your total guesses up to three
|
|
|
|
|
which is the number of positions plus one. So if your computer's turn is taking
|
|
|
|
|
longer than the number of positions plus one to find the answer then something
|
|
|
|
|
is wrong with your code.
|