Skip to content

Commit

Permalink
Add random game generator example and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
bennuttall committed Jan 2, 2017
1 parent cf56b84 commit a2039e0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
30 changes: 29 additions & 1 deletion README.md
@@ -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)
```
28 changes: 28 additions & 0 deletions random_game.py
@@ -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))

0 comments on commit a2039e0

Please sign in to comment.