Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 196 additions & 0 deletions 01 Acey Ducey/python/aceyducey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
########################################################
#
# Acey Ducey
#
# From: BASIC Computer Games (1978)
# Edited by David Ahl
#
# "This is a simulation of the Acey Ducey card game.
# In the game, the dealer (the computer) deals two
# cards face up. You have an option to bet or not to
# bet depending on whether or not you feel the next
# card dealt will have a value between the first two.
#
# "Your initial money is set to $100. The game keeps
# going on until you lose all your money or interrupt
# the program.
#
# "The original BASIC program author was Bill Palmby
# of Prairie View, Illinois."
#
# Python port by Jeff Jetton, 2019
#
########################################################


import random

# "You may alter [the following statement] if you want
# to start with more or less than $100."
DEFAULT_BANKROLL = 100

# functions
def deal_card_num():
return random.randint(0, 12)

def get_card_name(n):
cardNames = (" 2", " 3", " 4", " 5", " 6", \
" 7", " 8", " 9", " 10", "Jack", \
"Queen", "King", "Ace")
return(cardNames[n])

def display_bankroll(b):
if bankroll > 0:
print("You now have %s dollars\n"%b)



# Display initial title and instructions
print("\n Acey Ducey Card Game")
print("Creative Computing Morristown, New Jersey")
print("\n\n")
print("Acey-Ducey is played in the following manner")
print("The dealer (computer) deals two cards face up")
print("You have an option to bet or not bet depending")
print("on whether or not you feel the card will have")
print("a value between the first two.")
print("If you do not want to bet, input a 0")


# Loop for series of multiple games
keep_playing = True
while keep_playing:

# Initialize bankroll at start of each game
bankroll = DEFAULT_BANKROLL
display_bankroll(bankroll)

# Loop for a single round. Repeat until out of money.
while bankroll > 0:

# Deal out dealer cards
print("Here are your next two cards")
dealer1 = deal_card_num()
# If the cards match, we redeal 2nd card until they don't
dealer2 = dealer1
while dealer1 == dealer2:
dealer2 = deal_card_num()
# Organize the cards in order if they're not already
if (dealer1 >= dealer2):
(dealer1, dealer2) = (dealer2, dealer1) # Ya gotta love Python!
# Show dealer cards to the player
# (use card name rather than internal number)
print(get_card_name(dealer1))
print(get_card_name(dealer2) + "\n")

# Get and handle player bet choice
bet_is_valid = False
while not bet_is_valid:
curr_bet = input("What is your bet? ")
try:
curr_bet = int(curr_bet)
except:
# Bad input? Just loop back up and ask again...
pass
else:
if curr_bet == 0:
bet_is_valid = True
print("Chicken!!\n")
elif curr_bet > bankroll:
print("Sorry, my friend but you bet too much")
print("You have only %s dollars to bet\n" % bankroll)
else:
# Deal player card
bet_is_valid = True
player = deal_card_num()
print(get_card_name(player))

# Did we win?
if player > dealer1 and player < dealer2:
print("You win!!!")
bankroll += curr_bet
else:
print("Sorry, you lose")
bankroll -= curr_bet

# Update player on new bankroll level
display_bankroll(bankroll)

# End of loop for a single round

print("\n\nSorry, friend but you blew your wad")
player_response = input("Try again (yes or no) ")
if player_response.lower() == "yes":
print()
else:
keep_playing = False

# End of multiple game loop

print("OK Hope you had fun\n")


########################################################
#
# Porting notes:
#
# The original BASIC version had a variable named N
# that was initialized to 100 and then never used.
# Maybe it did something in feature that was edited
# out of the final version used in the book?
#
# The original program simply generated random numbers
# for each card. It did not simulate a true card deck,
# where the dealing of a card eliminates it from the
# deck and reduces the chances of the same value
# being drawn. This "infinite deck" logic (or "deal,
# with replacement after") has NOT been changed.
#
# In the interests of historical fidelity, the bug
# in the original BASIC listing that let you input a
# negative bet value has been faithfully reproduced.
# This lets the player lose money when they win and
# earn money when they lose! :-)
#
#
# Ideas for Modifications
#
# Give the user the ability to quit the game, perhaps
# by typing "quit" instead of making a bet. Provide a
# final assement based on how much of the original
# bankroll they have left.
#
# Or have the game run for a set number of rounds or
# until a certain bankroll goal is attained.
#
# Implement an "ante"--a set amount the player has to
# bet each time rather than having the option to lay
# out entirely.
#
# See "porting notes" above about negative bet values.
# How would you fix this?
#
# When the player "chickens out", show them what the
# next card would've been and point out whether they
# made a good or bad decision.
#
# In what situations are the odds of winning high
# enough to justify making a bet? Create a cheat mode
# where the program identifies these situations and
# lets the player know.
#
# Change the card dealing to simulate deals from a
# single deck (or a user-selectable number of decks).
#
# Implement a two-player mode where players take turns
# betting (or both bet on the same dealer cards and
# get their own player card dealt).
#
########################################################







201 changes: 201 additions & 0 deletions 05 Bagels/python/bagels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
######################################################################
#
# 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

MAX_GUESSES = 20



def print_rules():
print("\nI am thinking of a three-digit number. Try to guess")
print("my number and I will give you clues as follows:")
print(" PICO - One digit correct but in the wrong position")
print(" FERMI - One digit correct and in the right position")
print(" BAGELS - No digits correct")



def pick_number():
# Note that this returns a list of individual digits
# as separate strings, not a single integer or string
numbers = [i for i in range(10)]
random.shuffle(numbers)
num = numbers[0:3]
num = [str(i) for i in num]
return num



def get_valid_guess():
valid = False
while not valid:
guess = input(f"Guess # {guesses} ? ")
guess = guess.strip()
# Guess must be three characters
if len(guess) == 3:
# And they should be numbers
if guess.isnumeric():
# And the numbers must be unique
if len(set(guess)) == 3:
valid = True
else:
print("Oh, I forgot to tell you that " +
"the number I have in mind")
print("has no two digits the same.")
else:
print("What?")
else:
print("Try guessing a three-digit number.")

return guess



def build_result_string(num, guess):

result = ""

# Correct digits in wrong place
for i in range(2):
if num[i] == guess[i+1]:
result += "PICO "
if num[i+1] == guess[i]:
result += "PICO "
if num[0] == guess[2]:
result += "PICO "
if num[2] == guess[0]:
result += "PICO "

# Correct digits in right place
for i in range(3):
if num[i] == guess[i]:
result += "FERMI "

# Nothing right?
if result == "":
result = "BAGELS"

return result




######################################################################


# Intro text
print("\n Bagels")
print("Creative Computing Morristown, New Jersey")
print("\n\n")

# Anything other than N* will show the rules
response = input("Would you like the rules (Yes or No)? ")
if len(response) > 0:
if response.upper()[0] != 'N':
print_rules()
else:
print_rules()

games_won = 0
still_running = True
while still_running:

# New round
num = pick_number()
num_str = ''.join(num)
guesses = 1

print("\nO.K. I have a number in mind.")
guessing = True
while guessing:

guess = get_valid_guess()

if guess == num_str:
print("You got it!!!\n")
games_won += 1
guessing = False
else:
print(build_result_string(num, guess))
guesses += 1
if guesses > MAX_GUESSES:
print("Oh well")
print(f"That's {MAX_GUESSES} guesses. " +
"My number was " + num_str)
guessing = False


valid_response = False
while not valid_response:
response = input("Play again (Yes or No)? ")
if len(response) > 0:
valid_response = True
if response.upper()[0] != "Y":
still_running = False


if games_won > 0:
print(f"\nA {games_won} point Bagels buff!!")

print("Hope you had fun. Bye.\n")



######################################################################
#
# Porting Notes
#
# The original program did an unusually good job of validating the
# player's input (compared to many of the other programs in the
# book). Those checks and responses have been exactly reproduced.
#
#
# Ideas for Modifications
#
# It should probably mention that there's a maximum of MAX_NUM
# guesses in the instructions somewhere, shouldn't it?
#
# Could this program be written to use anywhere from, say 2 to 6
# digits in the number to guess? How would this change the routine
# that creates the "result" string?
#
######################################################################







Loading