Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add random game generator example and update readme
- Loading branch information
1 parent
cf56b84
commit a2039e0
Showing
2 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,35 @@ | ||
# Uno | ||
|
||
Some Python code to model the card game Uno | ||
Some unit tested Python code to model the card game Uno | ||
|
||
## Rules | ||
|
||
https://en.wikipedia.org/wiki/Uno_(card_game)#Official_rules | ||
|
||
## Usage | ||
|
||
An example auto-generated game of 5 players: | ||
|
||
```python | ||
from uno import UnoGame, COLORS | ||
import random | ||
|
||
game = UnoGame(5) | ||
|
||
while game.is_active: | ||
player = game.current_player | ||
player_id = player.player_id | ||
if player.can_play(game.current_card): | ||
for i, card in enumerate(player.hand): | ||
if game.current_card.playable(card): | ||
if card.color == 'black': | ||
new_color = random.choice(COLORS) | ||
else: | ||
new_color = None | ||
print("Player {} played {}".format(player, card)) | ||
game.play(player=player_id, card=i, new_color=new_color) | ||
break | ||
else: | ||
print("Player {} picked up".format(player)) | ||
game.play(player=player_id, card=None) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from uno import UnoGame, COLORS | ||
import random | ||
|
||
players = random.randint(2, 15) | ||
game = UnoGame(players) | ||
|
||
print("Starting a {} player game".format(players)) | ||
|
||
count = 0 | ||
while game.is_active: | ||
count += 1 | ||
player = game.current_player | ||
player_id = player.player_id | ||
if player.can_play(game.current_card): | ||
for i, card in enumerate(player.hand): | ||
if game.current_card.playable(card): | ||
if card.color == 'black': | ||
new_color = random.choice(COLORS) | ||
else: | ||
new_color = None | ||
print("Player {} played {}".format(player, card)) | ||
game.play(player=player_id, card=i, new_color=new_color) | ||
break | ||
else: | ||
print("Player {} picked up".format(player)) | ||
game.play(player=player_id, card=None) | ||
|
||
print("{} player game - {} cards played".format(players, count)) |