Skip to content

Instantly share code, notes, and snippets.

@dlamblin
Last active February 13, 2024 23:08
Show Gist options
  • Save dlamblin/b0b970e410ca7410b45c279eab637242 to your computer and use it in GitHub Desktop.
Save dlamblin/b0b970e410ca7410b45c279eab637242 to your computer and use it in GitHub Desktop.
Sophie's Python practice games

Sophie's Python practice games

In order to practice Python, Sophie worked on some Python that implements a game.

Deck shuffle

The first practice was to figure out how to take an ordered list of cards and shuffle that list like a deck of cards. After quickly figuring out a way to shuffle cards and then display the mixed up deck, she worked on making a guessing game out of it.

This deck_shuffle was written using the Windows 11 installation of Python 3.12.1 and its IDLE shell and editor.

----------------------------------------------------------------------++--------

"""
Named deck_shuffle, but includes a card suit guessing game.
In order to practice Python, we started with a set of cards and
implemented a way to shuffle them. Then we thought of a game we could
implement. Two player Go Fish would be tricky as keeping each player's
hand secret was difficult to implement. As this was implemented to run
in IDLE Shell and we weren't sure how to clear the screen between turns
we thought hot-seat players would too easily scroll up to view other
players' information. Therefore a no-knowledge guessing game was added.
Two players take turns guessing all 52 cards' suits of a shuffled deck.
Good scores make 13 or better correct guesses if card counting.
Some basic color support was added when run in IDLE.
"""
import random
from sys import stdout
suits = {'spade':'♠', 'heart':'♥', 'diamond':'♦', 'club':'♣'}
suit_color = {'♠':'black', '♥':'red', '♦':'blue', '♣':'green'}
cards = ['A♠','A♥','A♦','A♣',
'K♠','K♥','K♦','K♣',
'Q♠','Q♥','Q♦','Q♣',
'J♠','J♥','J♦','J♣',
'2♠','2♥','2♦','2♣',
'3♠','3♥','3♦','3♣',
'4♠','4♥','4♦','4♣',
'5♠','5♥','5♦','5♣',
'6♠','6♥','6♦','6♣',
'7♠','7♥','7♦','7♣',
'8♠','8♥','8♦','8♣',
'9♠','9♥','9♦','9♣',
'10♠','10♥','10♦','10♣',]
# The above was provided to Sophie to shuffle for use in a game.
# Her code follow here to the next comment about providing color.
def main():
"""
This is a two player guessing game where each player guesses the
suit of the cards in a whole shuffled deck.
"""
setup_colors()
print_color('Welcome to the suit guessing game.')
print_color('Player 1, make 52 guesses!', color='green')
guess_game(shuffle(cards.copy()))
print_color("\n\t\t✨----✨\n", color='black')
print_color('Player 2, make 52 guesses!', color='green')
guess_game(shuffle(cards.copy()))
def shuffle(deck):
"""Returns a shuffled deck and modifies passed in deck."""
return _sophies_shuffle(deck)
def _sophies_shuffle(deck):
shuffled = []
for i in range(len(deck)-1 ,-1, -1):
card = deck.pop(random.randint(0, i))
shuffled.append(card)
return shuffled
def display(deck):
"Prints out all the cards, prompting for enter in between each."
for card in deck:
print("It's the " + card + '!')
_ = input()
def guess_game(deck):
"Runs a suit guessing game of the full deck."
p = 0
buess = 0
for card in deck:
guess = ''
while guess not in ['heart', 'diamond', 'spade', 'club']:
guess = input('Guess the suit!(heart/diamond/spade/club) '
).lower().strip()
print_color("It was the ", end='', color='black')
print_color(card, end='', color=suit_color[card[-1]])
if card[-1] == suits[guess]:
p += 1
print_color('! You were ', end='')
print_color('correct', end='', color='green')
print_color('! :D')
else:
print_color('. You were ', end='')
print_color('wrong', end='', color='darkred')
print_color('. :(')
buess += 1
print_color("That was guess", str(buess), sep='\t', color="purple")
print_color("Player score is", str(p), sep='\t', color="inverse")
# The following code was provided for Sophie to use a bit of color in the game
OUT_MODE = None
colors = {}
def setup_colors():
"""Tries to setup the colors for IDLE Shell."""
global OUT_MODE, colors
try:
# If this doesn't throw, it may mean we're running in IDLE Shell
OUT_MODE = stdout.shell
colors = { # These colors depend on IDLE style
'black2': 'SYNC',
'black': 'stdin',
'purple': 'BUILTIN',
'green': 'STRING',
'black3': 'console',
'darkred': 'COMMENT',
'blue': 'stdout',
'black4': 'TODO',
'red': 'stderr',
'inverse': 'hit',
'blue2': 'DEFINITION',
'orange': 'KEYWORD',
'redred': 'ERROR',
'greybar': 'sel', # Do not use, it's temporary
}
except AttributeError:
print("Did not set up colors. Colors are available in IDLE shell.")
def print_color(*args, sep=' ', end='\n', color='black'):
"Prints with color if OUT_MODE is the IDLE Shell"
if OUT_MODE and color in colors:
_ = OUT_MODE.write(sep.join(args) + end, colors[color])
else:
print(*args, sep=sep, end=end)
def test_colors():
"Displays colors in IDLE to confirm they are sensible."
if not OUT_MODE:
setup_colors()
for key, value in colors.items():
print_color('This is', value, color=key)
# The following code wasn't provdied to Sophie. It tests shuffle methods
def _python_shuffle(deck):
random.shuffle(deck)
return deck
def _fisher_yates_shuffle(deck):
for i in range(len(deck)-1, -1, -1):
j = random.randint(0, i)
if j < i:
deck[i], deck[j] = deck[j], deck[i]
return deck
def _fisher_yates_index_shuffle(deck):
idx = list(range(len(deck)))
for i in range(idx[-1], -1, -1):
j = random.randint(0, i)
if j != i:
idx[i], idx[j] = idx[j], idx[i]
return list(map(lambda i: deck[i], idx))
def _swapsalot_shuffle(deck):
size = len(deck) - 1
for _ in range(size + 1):
i = random.randint(0, size)
j = random.randint(0, size)
if j != i:
deck[i], deck[j] = deck[j], deck[i]
return deck
def _slicing_shuffle(deck):
size = len(deck) - 2
for _ in range(int(size/2 + 1)):
i = random.randint(1, size)
deck = deck[i:] + deck[:i]
return deck
def _measure_shuffle(deck, orig):
"Calculates the quality of the shuffle"
if len(orig) != len(deck) or set(orig) != set(deck):
raise ValueException('Parameters are not a shuffling of each other')
count = len(deck)
return sum(
map(lambda i, c: abs(deck.index(c) - i) / max(i, count - i + 1),
range(0, count),
orig
)
)
def test_shuffles():
"Times different shuffle methods."
from timeit import timeit
for func in (_sophies_shuffle,
_fisher_yates_shuffle,
_fisher_yates_index_shuffle,
_swapsalot_shuffle,
_slicing_shuffle,
_python_shuffle):
print('Testing', func.__name__, flush=True)
quality = []
print('\tDone in', str(timeit(
lambda: quality.append(
_measure_shuffle(
func(cards.copy()), cards
)
),
number=50000
)), end='')
print('\tQuality averaged:', str(sum(quality)/len(quality)))
if __name__ == '__main__':
# test_colors()
# test_shuffles()
main()
PS C:\Users\dlamb> py.exe '.\Documents\Python for Sophie\deck_shufle.py'
Did not set up colors. Colors are available in IDLE shell.
Welcome to the suit guessing game.
Player 1, make 52 guesses!
Guess the suit!(heart/diamond/spade/club) club
It was the 2♦. You were wrong. :(
That was guess 1
Guess the suit!(heart/diamond/spade/club) club
It was the J♦. You were wrong. :(
That was guess 2
Guess the suit!(heart/diamond/spade/club) club
It was the 10♠. You were wrong. :(
That was guess 3
Guess the suit!(heart/diamond/spade/club) club
It was the 10♦. You were wrong. :(
That was guess 4
Guess the suit!(heart/diamond/spade/club) club
It was the 7♥. You were wrong. :(
That was guess 5
Guess the suit!(heart/diamond/spade/club) club
It was the 4♣! You were correct! :D
That was guess 6
Guess the suit!(heart/diamond/spade/club) club
It was the 7♠. You were wrong. :(
That was guess 7
Guess the suit!(heart/diamond/spade/club) club
It was the J♠. You were wrong. :(
That was guess 8
Guess the suit!(heart/diamond/spade/club) club
It was the 8♦. You were wrong. :(
That was guess 9
Guess the suit!(heart/diamond/spade/club) club
It was the 10♣! You were correct! :D
That was guess 10
Guess the suit!(heart/diamond/spade/club) club
It was the 5♥. You were wrong. :(
That was guess 11
Guess the suit!(heart/diamond/spade/club) club
It was the Q♥. You were wrong. :(
That was guess 12
Guess the suit!(heart/diamond/spade/club) club
It was the 6♦. You were wrong. :(
That was guess 13
Guess the suit!(heart/diamond/spade/club) club
It was the 3♠. You were wrong. :(
That was guess 14
Guess the suit!(heart/diamond/spade/club) club
It was the A♣! You were correct! :D
That was guess 15
Guess the suit!(heart/diamond/spade/club) club
It was the 2♣! You were correct! :D
That was guess 16
Guess the suit!(heart/diamond/spade/club) club
It was the A♠. You were wrong. :(
That was guess 17
Guess the suit!(heart/diamond/spade/club) club
It was the K♦. You were wrong. :(
That was guess 18
Guess the suit!(heart/diamond/spade/club) club
It was the 8♣! You were correct! :D
That was guess 19
Guess the suit!(heart/diamond/spade/club) club
It was the 4♠. You were wrong. :(
That was guess 20
Guess the suit!(heart/diamond/spade/club) club
It was the 6♠. You were wrong. :(
That was guess 21
Guess the suit!(heart/diamond/spade/club) club
It was the 9♣! You were correct! :D
That was guess 22
Guess the suit!(heart/diamond/spade/club) club
It was the 2♠. You were wrong. :(
That was guess 23
Guess the suit!(heart/diamond/spade/club) club
It was the A♥. You were wrong. :(
That was guess 24
Guess the suit!(heart/diamond/spade/club) club
It was the 5♦. You were wrong. :(
That was guess 25
Guess the suit!(heart/diamond/spade/club) club
It was the 8♠. You were wrong. :(
That was guess 26
Guess the suit!(heart/diamond/spade/club) club
It was the 6♣! You were correct! :D
That was guess 27
Guess the suit!(heart/diamond/spade/club) club
It was the 3♦. You were wrong. :(
That was guess 28
Guess the suit!(heart/diamond/spade/club) club
It was the 5♠. You were wrong. :(
That was guess 29
Guess the suit!(heart/diamond/spade/club) club
It was the 7♣! You were correct! :D
That was guess 30
Guess the suit!(heart/diamond/spade/club) club
It was the 8♥. You were wrong. :(
That was guess 31
Guess the suit!(heart/diamond/spade/club) club
It was the 2♥. You were wrong. :(
That was guess 32
Guess the suit!(heart/diamond/spade/club) club
It was the K♠. You were wrong. :(
That was guess 33
Guess the suit!(heart/diamond/spade/club) club
It was the 3♣! You were correct! :D
That was guess 34
Guess the suit!(heart/diamond/spade/club) club
It was the 10♥. You were wrong. :(
That was guess 35
Guess the suit!(heart/diamond/spade/club) club
It was the 4♥. You were wrong. :(
That was guess 36
Guess the suit!(heart/diamond/spade/club) club
It was the 9♠. You were wrong. :(
That was guess 37
Guess the suit!(heart/diamond/spade/club) club
It was the Q♦. You were wrong. :(
That was guess 38
Guess the suit!(heart/diamond/spade/club) club
It was the Q♠. You were wrong. :(
That was guess 39
Guess the suit!(heart/diamond/spade/club) club
It was the J♥. You were wrong. :(
That was guess 40
Guess the suit!(heart/diamond/spade/club) club
It was the K♥. You were wrong. :(
That was guess 41
Guess the suit!(heart/diamond/spade/club) club
It was the 3♥. You were wrong. :(
That was guess 42
Guess the suit!(heart/diamond/spade/club) club
It was the 9♥. You were wrong. :(
That was guess 43
Guess the suit!(heart/diamond/spade/club) club
It was the A♦. You were wrong. :(
That was guess 44
Guess the suit!(heart/diamond/spade/club) club
It was the 9♦. You were wrong. :(
That was guess 45
Guess the suit!(heart/diamond/spade/club) club
It was the 4♦. You were wrong. :(
That was guess 46
Guess the suit!(heart/diamond/spade/club) club
It was the 5♣! You were correct! :D
That was guess 47
Guess the suit!(heart/diamond/spade/club) club
It was the 7♦. You were wrong. :(
That was guess 48
Guess the suit!(heart/diamond/spade/club) club
It was the Q♣! You were correct! :D
That was guess 49
Guess the suit!(heart/diamond/spade/club) club
It was the 6♥. You were wrong. :(
That was guess 50
Guess the suit!(heart/diamond/spade/club) club
It was the K♣! You were correct! :D
That was guess 51
Guess the suit!(heart/diamond/spade/club) club
It was the J♣! You were correct! :D
That was guess 52
Player score is 13
✨----✨
Player 2, make 52 guesses!
Guess the suit!(heart/diamond/spade/club) club
It was the K♠. You were wrong. :(
That was guess 1
Guess the suit!(heart/diamond/spade/club) club
It was the 2♥. You were wrong. :(
That was guess 2
Guess the suit!(heart/diamond/spade/club) club
It was the 4♠. You were wrong. :(
That was guess 3
Guess the suit!(heart/diamond/spade/club) club
It was the Q♣! You were correct! :D
That was guess 4
Guess the suit!(heart/diamond/spade/club) club
It was the A♦. You were wrong. :(
That was guess 5
Guess the suit!(heart/diamond/spade/club) club
It was the 7♠. You were wrong. :(
That was guess 6
Guess the suit!(heart/diamond/spade/club) club
It was the J♦. You were wrong. :(
That was guess 7
Guess the suit!(heart/diamond/spade/club) club
It was the 3♦. You were wrong. :(
That was guess 8
Guess the suit!(heart/diamond/spade/club) club
It was the Q♥. You were wrong. :(
That was guess 9
Guess the suit!(heart/diamond/spade/club) club
It was the 5♥. You were wrong. :(
That was guess 10
Guess the suit!(heart/diamond/spade/club) club
It was the 10♣! You were correct! :D
That was guess 11
Guess the suit!(heart/diamond/spade/club) club
It was the K♥. You were wrong. :(
That was guess 12
Guess the suit!(heart/diamond/spade/club) club
It was the 8♠. You were wrong. :(
That was guess 13
Guess the suit!(heart/diamond/spade/club) club
It was the 2♦. You were wrong. :(
That was guess 14
Guess the suit!(heart/diamond/spade/club) club
It was the 5♦. You were wrong. :(
That was guess 15
Guess the suit!(heart/diamond/spade/club) club
It was the K♣! You were correct! :D
That was guess 16
Guess the suit!(heart/diamond/spade/club) club
It was the 3♠. You were wrong. :(
That was guess 17
Guess the suit!(heart/diamond/spade/club) club
It was the 9♦. You were wrong. :(
That was guess 18
Guess the suit!(heart/diamond/spade/club) club
It was the 7♣! You were correct! :D
That was guess 19
Guess the suit!(heart/diamond/spade/club) club
It was the 5♠. You were wrong. :(
That was guess 20
Guess the suit!(heart/diamond/spade/club) club
It was the 8♥. You were wrong. :(
That was guess 21
Guess the suit!(heart/diamond/spade/club) club
It was the J♥. You were wrong. :(
That was guess 22
Guess the suit!(heart/diamond/spade/club) club
It was the Q♠. You were wrong. :(
That was guess 23
Guess the suit!(heart/diamond/spade/club) club
It was the 3♣! You were correct! :D
That was guess 24
Guess the suit!(heart/diamond/spade/club) spade
It was the 4♦. You were wrong. :(
That was guess 25
Guess the suit!(heart/diamond/spade/club) spade
It was the 8♦. You were wrong. :(
That was guess 26
Guess the suit!(heart/diamond/spade/club) spade
It was the 3♥. You were wrong. :(
That was guess 27
Guess the suit!(heart/diamond/spade/club) spade
It was the A♠! You were correct! :D
That was guess 28
Guess the suit!(heart/diamond/spade/club) spade
It was the 9♥. You were wrong. :(
That was guess 29
Guess the suit!(heart/diamond/spade/club) spade
It was the 4♣. You were wrong. :(
That was guess 30
Guess the suit!(heart/diamond/spade/club) spade
It was the 10♦. You were wrong. :(
That was guess 31
Guess the suit!(heart/diamond/spade/club) spade
It was the 6♣. You were wrong. :(
That was guess 32
Guess the suit!(heart/diamond/spade/club) spade
It was the Q♦. You were wrong. :(
That was guess 33
Guess the suit!(heart/diamond/spade/club) spade
It was the 10♥. You were wrong. :(
That was guess 34
Guess the suit!(heart/diamond/spade/club) spade
It was the J♠! You were correct! :D
That was guess 35
Guess the suit!(heart/diamond/spade/club) spade
It was the 7♦. You were wrong. :(
That was guess 36
Guess the suit!(heart/diamond/spade/club) spade
It was the K♦. You were wrong. :(
That was guess 37
Guess the suit!(heart/diamond/spade/club) spade
It was the 6♦. You were wrong. :(
That was guess 38
Guess the suit!(heart/diamond/spade/club) spade
It was the 8♣. You were wrong. :(
That was guess 39
Guess the suit!(heart/diamond/spade/club) spade
It was the 5♣. You were wrong. :(
That was guess 40
Guess the suit!(heart/diamond/spade/club) spade
It was the 7♥. You were wrong. :(
That was guess 41
Guess the suit!(heart/diamond/spade/club) spade
It was the A♥. You were wrong. :(
That was guess 42
Guess the suit!(heart/diamond/spade/club) spade
It was the 6♠! You were correct! :D
That was guess 43
Guess the suit!(heart/diamond/spade/club) spade
It was the 9♠! You were correct! :D
That was guess 44
Guess the suit!(heart/diamond/spade/club) spade
It was the 10♠! You were correct! :D
That was guess 45
Guess the suit!(heart/diamond/spade/club) spade
It was the 9♣. You were wrong. :(
That was guess 46
Guess the suit!(heart/diamond/spade/club) spade
It was the A♣. You were wrong. :(
That was guess 47
Guess the suit!(heart/diamond/spade/club) club
It was the J♣! You were correct! :D
That was guess 48
Guess the suit!(heart/diamond/spade/club) club
It was the 6♥. You were wrong. :(
That was guess 49
Guess the suit!(heart/diamond/spade/club) club
It was the 2♠. You were wrong. :(
That was guess 50
Guess the suit!(heart/diamond/spade/club) spade
It was the 2♣. You were wrong. :(
That was guess 51
Guess the suit!(heart/diamond/spade/club) spade
It was the 4♥. You were wrong. :(
That was guess 52
Player score is 11
PS C:\Users\dlamb>
@dlamblin
Copy link
Author

Example of deck_shuffle.py game run in IDLE (file named deck_shuffle_run_idle_eg.png)
deck_shuffle_run_eg_idle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment