From 1b879a998a981d53bd20f5c95e01cd5d9be3bf45 Mon Sep 17 00:00:00 2001 From: Paul-Louis NECH Date: Sat, 4 Apr 2020 14:16:30 +0200 Subject: [PATCH] feat: Basic model + tests --- model/__init__.py | 0 model/animals.py | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ model/data.py | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ test/test_data.py | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 238 insertions(+) create mode 100644 model/__init__.py create mode 100644 model/animals.py create mode 100644 model/data.py create mode 100644 requirements.txt create mode 100644 test/test_data.py diff --git a/model/__init__.py b/model/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/model/__init__.py diff --git a/model/animals.py b/model/animals.py new file mode 100644 index 0000000..a25fb2d --- /dev/null +++ b/model/animals.py @@ -0,0 +1,93 @@ +import random + + +def random_animal_name(): + names = ["Aardvark", "Abyssinian", "Adelie Penguin", "Affenpinscher", "Afghan Hound", "African Bush Elephant", + "African Civet", "African Clawed Frog", "African Forest Elephant", "African Palm Civet", "African Penguin", + "African Tree Toad", "African Wild Dog", "Ainu Dog", "Airedale Terrier", "Akbash", "Akita", + "Alaskan Malamute", "Albatross", "Aldabra Giant Tortoise", "Alligator", "Alpine Dachsbracke", + "American Bulldog", "American Cocker Spaniel", "American Coonhound", "American Eskimo Dog", + "American Foxhound", "American Pit Bull Terrier", "American Staffordshire Terrier", + "American Water Spaniel", "Amur Leopard", "Anatolian Shepherd Dog", "Angelfish", "Ant", "Anteater", + "Antelope", "Appenzeller Dog", "Arctic Fox", "Arctic Hare", "Arctic Wolf", "Armadillo", "Asian Elephant", + "Asian Giant Hornet", "Asian Palm Civet", "Asiatic Black Bear", "Australian Cattle Dog", + "Australian Kelpie Dog", "Australian Mist", "Australian Shepherd", "Australian Terrier", "Avocet", + "Axolotl", "Aye Aye " "Baboon", "Bactrian Camel", "Badger", "Balinese", "Banded Palm Civet", + "Bandicoot", "Barb", "Barn Owl", "Barnacle", "Barracuda", "Basenji Dog", "Basking Shark", "Basset Hound", + "Bat", "Bavarian Mountain Hound", "Beagle", "Bear", "Bearded Collie", "Bearded Dragon", "Beaver", + "Bedlington Terrier", "Beetle", "Bengal Tiger", "Bernese Mountain Dog", "Bichon Frise", "Binturong", + "Bird", "Birds Of Paradise", "Birman", "Bison", "Black Rhinoceros", "Black Russian Terrier", + "Black Widow Spider", "Bloodhound", "Blue Lacy Dog", "Blue Whale", "Bluetick Coonhound", "Bobcat", + "Bolognese Dog", "Bombay", "Bongo", "Bonobo", "Booby", "Border Collie", "Border Terrier", + "Bornean Orang-utan", "Borneo Elephant", "Boston Terrier", "Bottlenose Dolphin", "Boxer Dog", + "Boykin Spaniel", "Brazilian Terrier", "Brown Bear", "Budgerigar", "Buffalo", "Bull Mastiff", "Bull Shark", + "Bull Terrier", "Bulldog", "Bullfrog", "Bumble Bee", "Burmese", "Burrowing Frog", "Butterfly", + "Butterfly Fish", "Caiman", "Caiman Lizard", "Cairn Terrier", "Camel", "Canaan Dog", "Capybara", "Caracal", + "Carolina Dog", "Cassowary", "Cat", "Caterpillar", "Catfish", "Cavalier King Charles Spaniel", "Centipede", + "Cesky Fousek", "Chameleon", "Chamois", "Cheetah", "Chesapeake Bay Retriever", "Chicken", "Chihuahua", + "Chimpanzee", "Chinchilla", "Chinese Crested Dog", "Chinook", "Chinstrap Penguin", "Chipmunk", "Chow Chow", + "Cichlid", "Clouded Leopard", "Clown Fish", "Clumber Spaniel", "Coati", "Cockroach", "Collared Peccary", + "Collie", "Common Buzzard", "Common Frog", "Common Loon", "Common Toad", "Coral", "Cottontop Tamarin", + "Cougar", "Cow", "Coyote", "Crab", "Crab-Eating Macaque", "Crane", "Crested Penguin", "Crocodile", + "Cross River Gorilla", "Curly Coated Retriever", "Cuscus", "Cuttlefish", "Dachshund", "Dalmatian", + "Darwin's Frog", "Deer", "Desert Tortoise", "Deutsche Bracke", "Dhole", "Dingo", "Discus", + "Doberman Pinscher", "Dodo", "Dog", "Dogo Argentino", "Dogue De Bordeaux", "Dolphin", "Donkey", "Dormouse", + "Dragonfly", "Drever", "Duck", "Dugong", "Dunker", "Dusky Dolphin", "Dwarf Crocodile", "Eagle", "Earwig", + "Eastern Gorilla", "Eastern Lowland Gorilla", "Echidna", "Edible Frog", "Egyptian Mau", "Electric Eel", + "Elephant", "Elephant Seal", "Elephant Shrew", "Emperor Penguin", "Emperor Tamarin", "Emu", + "English Cocker Spaniel", "English Shepherd", "English Springer Spaniel", "Entlebucher Mountain Dog", + "Epagneul Pont Audemer", "Eskimo Dog", "Estrela Mountain Dog", "Falcon", "Fennec Fox", "Ferret", + "Field Spaniel", "Fin Whale", "Finnish Spitz", "Fire-Bellied Toad", "Fish", "Fishing Cat", "Flamingo", + "Flat Coat Retriever", "Flounder", "Fly", "Flying Squirrel", "Fossa", "Fox", "Fox Terrier", + "French Bulldog", "Frigatebird", "Frilled Lizard", "Frog", "Fur Seal", "Galapagos Penguin", + "Galapagos Tortoise", "Gar", "Gecko", "Gentoo Penguin", "Geoffroys Tamarin", "Gerbil", "German Pinscher", + "German Shepherd", "Gharial", "Giant African Land Snail", "Giant Clam", "Giant Panda Bear", + "Giant Schnauzer", "Gibbon", "Gila Monster", "Giraffe", "Glass Lizard", "Glow Worm", "Goat", + "Golden Lion Tamarin", "Golden Oriole", "Golden Retriever", "Goose", "Gopher", "Gorilla", "Grasshopper", + "Great Dane", "Great White Shark", "Greater Swiss Mountain Dog", "Green Bee-Eater", "Greenland Dog", + "Grey Mouse Lemur", "Grey Reef Shark", "Grey Seal", "Greyhound", "Grizzly Bear", "Grouse", "Guinea Fowl", + "Guinea Pig", "Guppy", "Hammerhead Shark", "Hamster", "Hare", "Harrier", "Havanese", "Hedgehog", + "Hercules Beetle", "Hermit Crab", "Heron", "Highland Cattle", "Himalayan", "Hippopotamus", "Honey Bee", + "Horn Shark", "Horned Frog", "Horse", "Horseshoe Crab", "Howler Monkey", "Human", "Humboldt Penguin", + "Hummingbird", "Humpback Whale", "Hyena", "Ibis", "Ibizan Hound", "Iguana", "Impala", "Indian Elephant", + "Indian Palm Squirrel", "Indian Rhinoceros", "Indian Star Tortoise", "Indochinese Tiger", "Indri", + "Insect", "Irish Setter", "Irish WolfHound", "Jack Russel", "Jackal", "Jaguar", "Japanese Chin", + "Japanese Macaque", "Javan Rhinoceros", "Javanese", "Jellyfish", "Kakapo", "Kangaroo", + "Keel Billed Toucan", "Killer Whale", "King Crab", "King Penguin", "Kingfisher", "Kiwi", "Koala", + "Komodo Dragon", "Kudu", "Labradoodle", "Labrador Retriever", "Ladybug", "Leaf-Tailed Gecko", "Lemming", + "Lemur", "Leopard", "Leopard Cat", "Leopard Seal", "Leopard Tortoise", "Liger", "Lion", "Lionfish", + "Little Penguin", "Lizard", "Llama", "Lobster", "Long-Eared Owl", "Lynx", "Macaroni Penguin", "Macaw", + "Magellanic Penguin", "Magpie", "Maine Coon", "Malayan Civet", "Malayan Tiger", "Maltese", "Manatee", + "Mandrill", "Manta Ray", "Marine Toad", "Markhor", "Marsh Frog", "Masked Palm Civet", "Mastiff", "Mayfly", + "Meerkat", "Millipede", "Minke Whale", "Mole", "Molly", "Mongoose", "Mongrel", "Monitor Lizard", "Monkey", + "Monte Iberia Eleuth", "Moorhen", "Moose", "Moray Eel", "Moth", "Mountain Gorilla", "Mountain Lion", + "Mouse", "Mule", "Neanderthal", "Neapolitan Mastiff", "Newfoundland", "Newt", "Nightingale", + "Norfolk Terrier", "North American Black Bear", "Norwegian Forest", "Numbat", "Nurse Shark", "Ocelot", + "Octopus", "Okapi", "Old English Sheepdog", "Olm", "Opossum", "Orang-utan", "Ostrich", "Otter", "Oyster", + "Pademelon", "Panther", "Parrot", "Patas Monkey", "Peacock", "Pekingese", "Pelican", "Penguin", "Persian", + "Pheasant", "Pied Tamarin", "Pig", "Pika", "Pike", "Pink Fairy Armadillo", "Piranha", "Platypus", + "Pointer", "Poison Dart Frog", "Polar Bear", "Pond Skater", "Poodle", "Pool Frog", "Porcupine", "Possum", + "Prawn", "Proboscis Monkey", "Puffer Fish", "Puffin", "Pug", "Puma", "Purple Emperor", "Puss Moth", + "Pygmy Hippopotamus", "Pygmy Marmoset", "Quail", "Quetzal", "Quokka", "Quoll", "Rabbit", "Raccoon", + "Raccoon Dog", "Radiated Tortoise", "Ragdoll", "Rat", "Rattlesnake", "Red Knee Tarantula", "Red Panda", + "Red Wolf", "Red-handed Tamarin", "Reindeer", "Rhinoceros", "River Dolphin", "River Turtle", "Robin", + "Rock Hyrax", "Rockhopper Penguin", "Roseate Spoonbill", "Rottweiler", "Royal Penguin", "Russian Blue", + "Sabre-Toothed Tiger", "Saint Bernard", "Salamander", "Sand Lizard", "Saola", "Scorpion", "Scorpion Fish", + "Sea Dragon", "Sea Lion", "Sea Otter", "Sea Slug", "Sea Squirt", "Sea Turtle", "Sea Urchin", "Seahorse", + "Seal", "Serval", "Sheep", "Shih Tzu", "Shrimp", "Siamese", "Siamese Fighting Fish", "Siberian", + "Siberian Husky", "Siberian Tiger", "Silver Dollar", "Skunk", "Sloth", "Slow Worm", "Snail", "Snake", + "Snapping Turtle", "Snowshoe", "Snowy Owl", "Somali", "South China Tiger", "Spadefoot Toad", "Sparrow", + "Spectacled Bear", "Sperm Whale", "Spider Monkey", "Spiny Dogfish", "Sponge", "Squid", "Squirrel", + "Squirrel Monkey", "Sri Lankan Elephant", "Staffordshire Bull Terrier", "Stag Beetle", "Starfish", + "Stellers Sea Cow", "Stick Insect", "Stingray", "Stoat", "Striped Rocket Frog", "Sumatran Elephant", + "Sumatran Orang-utan" "Sumatran Rhinoceros", "Sumatran Tiger", "Sun Bear", "Swan", "Tang", + "Tapanuli Orang-utan", "Tapir", "Tarsier", "Tasmanian Devil", "Tawny Owl", "Termite", "Tetra", + "Thorny Devil", "Tibetan Mastiff", "Tiffany", "Tiger", "Tiger Salamander", "Tiger Shark", "Tortoise", + "Toucan", "Tree Frog", "Tropicbird", "Tuatara", "Turkey", "Turkish Angora", "Uakari", "Uguisu", + "Umbrellabird", "Vampire Bat", "Vervet Monkey", "Vulture", "Wallaby", "Walrus", "Warthog", "Wasp", + "Water Buffalo", "Water Dragon", "Water Vole", "Weasel", "Welsh Corgi", "West Highland Terrier", + "Western Gorilla", "Western Lowland Gorilla", "Whale Shark", "Whippet", "White Faced Capuchin", + "White Rhinoceros", "White Tiger", "Wild Boar", "Wildebeest", "Wolf", "Wolverine", "Wombat", "Woodlouse", + "Woodpecker", "Woolly Mammoth", "Woolly Monkey", "Wrasse", "X-Ray Tetra", "Yak", + "Yellow-Eyed Penguin" "Yorkshire Terrier", "Zebra", "Zebra Shark", "Zebu", "Zonkey", "Zorse"] + return random.choice(names) \ No newline at end of file diff --git a/model/data.py b/model/data.py new file mode 100644 index 0000000..e44fbda --- /dev/null +++ b/model/data.py @@ -0,0 +1,102 @@ +from collections import defaultdict +from dataclasses import dataclass +from enum import Enum +from random import shuffle, randrange +from typing import List, Dict + +from model.animals import random_animal_name + + +class Value(Enum): + Ace = 1 + Two = 2 + Three = 3 + Four = 4 + Five = 5 + Six = 6 + Seven = 7 + Eight = 8 + Nine = 9 + Ten = 10 + Jack = 11 + Queen = 12 + King = 13 + + +class Color(Enum): + Hearts = "♥" + Spades = "♠" + Clubs = "♣" + Diamonds = "♦" + + +@dataclass(frozen=True) +class Card: + value: Value + color: Color + + def __str__(self) -> str: + return f"{self.color.value} {self.value.name} {self.color.value}" + + +class Deck: + def __init__(self): + self.cards = [Card(v, c) for v in Value for c in Color] + self.defausse = [] + + def __len__(self): + return len(self.cards) + + def random_card(self) -> Card: + if not self.cards: + self.reset() + + card = self.cards.pop(randrange(len(self.cards))) + self.defausse.append(card) + return card + + def reset(self): + self.cards.extend(self.defausse) + self.defausse.clear() + shuffle(self.cards) + + +class Player: + name: str = random_animal_name() + hand: List[Card] = [] + + def give(self, card: Card): + self.hand.append(card) + + +class Game: + def __init__(self, deck: Deck = Deck(), players=None): + if players is None: + players = [] + + self.deck: Deck = deck + self.players: List[Player] = players + self.defeats: Dict[Player, int] = defaultdict(lambda: 0) + + def new_game(self): + self.deck.reset() + while len(self.players) > 1: + self.new_turn() + winner = self.players[0] + print(f"Game over - Player {winner.name} wins with {len(winner.hand)} cards!") + pass + + def new_turn(self): + # Distribution + shuffle(self.deck) + for p in self.players: + p.give(self.deck.random_card()) + + # Tour + for p in self.players: + announce = p.announce(current_bet) + + + + + # TODO: Put next first player first of list \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f536c03 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +fastapi==0.53.2 +uvicorn==0.11.3 diff --git a/test/test_data.py b/test/test_data.py new file mode 100644 index 0000000..ee37cb8 --- /dev/null +++ b/test/test_data.py @@ -0,0 +1,41 @@ +from unittest import TestCase + +from model.data import Deck, Player, Card, Value, Color + + +class TestDeck(TestCase): + + def setUp(self) -> None: + super().setUp() + self.deck = Deck() + + def testInitDeck(self): + self.assertEqual(52, len(self.deck), "52 cards") + self.assertEqual(52, len(set(self.deck.cards)), "unique cards") + + def testRandomCard(self): + cards = [] + for i in range(len(self.deck)): + cards.append(self.deck.random_card()) + + self.assertEqual(52, len(set(cards)), "draw each card once") + + +class TestPlayer(TestCase): + + def setUp(self) -> None: + super().setUp() + self.player = Player() + + def testHand(self): + self.assertEqual(0, len(self.player.hand), "Begin no cards") + self.assertTrue(len(self.player.name), "Has a name") + + self.player.give(Card(Value.Ace, Color.Hearts)) + + self.assertEqual(1, len(self.player.hand), "Gave one card") + self.assertEqual(Value.Ace, self.player.hand[0].value, "Is Ace") + self.assertEqual(Color.Hearts, self.player.hand[0].color, "of Hearts") + + def testDefeats(self): + pass -- libgit2 0.27.0