Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a finiteness test #5

Merged
merged 4 commits into from
Jun 8, 2023
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ Nessler 2022:
Starting hands: ---AJ--Q---------QAKQJJ-QK/-----A----KJ-K--------A---
There were 8344 turns
There were 1164 tricks
````
````

There are [alternate versions of the game](https://github.com/matthewmayer/beggarmypython/pull/5) that do not terminate, entering an infinite loop. For example the Italian game of Camicia uses a 40-card deck and no draw-4 cards (aces).

```
Carmicia
Starting hands: --Q------QJ----JK---/---Q---J-Q-KJ--K-K--
Game appears to be infinite, loop detected after 197 turns and 37 tricks
```
25 changes: 20 additions & 5 deletions beggarmypython/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ def penalty_value_of(card):
return values[card]

def play(hands,firstCardOnLeft=True,verbose=False):
(turns, tricks, finite) = calculate(hands,firstCardOnLeft,verbose)
if finite:
print("There were %d turns" % turns)
print("There were %d tricks\n" % tricks)
else:
print("Game appears to be infinite, loop detected after %d turns and %d tricks" % (turns, tricks))

def calculate(hands,firstCardOnLeft=True,verbose=False):
a,b = hands #hands are called a and b
print("Starting hands: %s/%s" % (a, b))
if not firstCardOnLeft:
Expand All @@ -12,7 +20,9 @@ def play(hands,firstCardOnLeft=True,verbose=False):
turns = 0
tricks = 0
player = 1 #alternates between 1 and -1
while a!="" and b!="": #game terminates when a or b's hands are empty
finite = True
visited = set() #keep the visited statuses
while a!="" and b!="" and finite: #game terminates when a or b's hands are empty
battle_in_progress = False
cards_to_play = 1
while cards_to_play>0: #current player may play up to cards_to_play cards
Expand Down Expand Up @@ -58,8 +68,13 @@ def play(hands,firstCardOnLeft=True,verbose=False):
player = player*-1

#print current status
status = "%s/%s/%s" % (a, b, stack)
if verbose:
print("%s/%s/%s" % (a, b, stack))

print("There were %d turns" % turns)
print("There were %d tricks\n" % tricks)
print(status)

#check for finiteness
if status in visited:
finite = False
else:
visited.add(status)
return (turns, tricks, finite)
3 changes: 3 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@

print("Nessler 2022:")
beggarmypython.play(('---AJ--Q---------QAKQJJ-QK','-----A----KJ-K--------A---'), verbose=False)

print("Carmicia")
beggarmypython.play(('--Q------QJ----JK---','---Q---J-Q-KJ--K-K--'))