mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Python: Add tests and type annotations
This commit is contained in:
@@ -1,35 +1,33 @@
|
||||
######################################################################
|
||||
#
|
||||
# Bagels
|
||||
#
|
||||
# From: BASIC Computer Games (1978)
|
||||
# Edited by David H. Ahl
|
||||
#
|
||||
# "In this game, the computer picks a 3-digit secret number using
|
||||
# the digits 0 to 9 and you attempt to guess what it is. You are
|
||||
# allowed up to twenty guesses. No digit is repeated. After
|
||||
# each guess the computer will give you clues about your guess
|
||||
# as follows:
|
||||
#
|
||||
# PICO One digit is correct, but in the wrong place
|
||||
# FERMI One digit is in the correct place
|
||||
# BAGELS No digit is correct
|
||||
#
|
||||
# "You will learn to draw inferences from the clues and, with
|
||||
# practice, you'll learn to improve your score. There are several
|
||||
# good strategies for playing Bagels. After you have found a good
|
||||
# strategy, see if you can improve it. Or try a different strategy
|
||||
# altogether and see if it is any better. While the program allows
|
||||
# up to twenty guesses, if you use a good strategy it should not
|
||||
# take more than eight guesses to get any number.
|
||||
#
|
||||
# "The original authors of this program are D. Resek and P. Rowe of
|
||||
# the Lawrence Hall of Science, Berkeley, California."
|
||||
#
|
||||
#
|
||||
# Python port by Jeff Jetton, 2019
|
||||
#
|
||||
######################################################################
|
||||
"""
|
||||
Bagels
|
||||
|
||||
From: BASIC Computer Games (1978)
|
||||
Edited by David H. Ahl
|
||||
|
||||
"In this game, the computer picks a 3-digit secret number using
|
||||
the digits 0 to 9 and you attempt to guess what it is. You are
|
||||
allowed up to twenty guesses. No digit is repeated. After
|
||||
each guess the computer will give you clues about your guess
|
||||
as follows:
|
||||
|
||||
PICO One digit is correct, but in the wrong place
|
||||
FERMI One digit is in the correct place
|
||||
BAGELS No digit is correct
|
||||
|
||||
"You will learn to draw inferences from the clues and, with
|
||||
practice, you'll learn to improve your score. There are several
|
||||
good strategies for playing Bagels. After you have found a good
|
||||
strategy, see if you can improve it. Or try a different strategy
|
||||
altogether and see if it is any better. While the program allows
|
||||
up to twenty guesses, if you use a good strategy it should not
|
||||
take more than eight guesses to get any number.
|
||||
|
||||
"The original authors of this program are D. Resek and P. Rowe of
|
||||
the Lawrence Hall of Science, Berkeley, California."
|
||||
|
||||
|
||||
Python port by Jeff Jetton, 2019
|
||||
"""
|
||||
|
||||
|
||||
import random
|
||||
|
||||
@@ -1,5 +1,35 @@
|
||||
from bagels import build_result_string
|
||||
import io
|
||||
from _pytest.capture import CaptureFixture
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
|
||||
from bagels import build_result_string, main, pick_number
|
||||
|
||||
|
||||
def test_build_result_string() -> None:
|
||||
build_result_string(["a", "b", "c"], "abc")
|
||||
|
||||
|
||||
def test_pick_number() -> None:
|
||||
picked = pick_number()
|
||||
assert len(picked) == 3
|
||||
for el in picked:
|
||||
assert el in "0123456789"
|
||||
|
||||
|
||||
def test_main(monkeypatch: MonkeyPatch, capsys: CaptureFixture) -> None:
|
||||
# Succeed
|
||||
round_1 = "Y\n4444\nabc\n444\n456\n145\n321\n123"
|
||||
|
||||
# Fail after 20 guesses
|
||||
round_2 = (
|
||||
"666\n132\n321\n312\n132\n213\n678\n678\n678\n678\n678\n"
|
||||
"678\n678\n678\n678\n678\n678\n678\n678\n678\n678\nNo"
|
||||
)
|
||||
monkeypatch.setattr("sys.stdin", io.StringIO(f"{round_1}\nYES\n{round_2}"))
|
||||
monkeypatch.setattr("bagels.pick_number", lambda: ["1", "2", "3"])
|
||||
main()
|
||||
captured = capsys.readouterr()
|
||||
assert "Would you like the rules" in captured.out
|
||||
assert "I have a number in mind" in captured.out
|
||||
assert "My number was" in captured.out
|
||||
assert "Hope you had fun." in captured.out
|
||||
|
||||
Reference in New Issue
Block a user