コード例 #1
0
ファイル: testDeck.py プロジェクト: jasemichael/bingo_card
class TestDeck(unittest.TestCase):
    def setUp(self):
        self.deck = Deck(cardSize=7, cardCount=3, numberMax=100)
        self.deck1 = Deck(0, 0, 0)

    def test_getCardCount(self):
        self.assertNotEqual(self.deck.getCardCount(), 0)
        self.assertEqual(self.deck.getCardCount(), 3)
        self.assertEqual(self.deck1.getCardCount(), 0)

    def test_getCard(self):
        self.assertIsNotNone(self.deck.getCard(1))
        self.assertIsNone(self.deck.getCard(0))
        self.assertIsNone(self.deck1.getCard(1))

        # This requires the students to name their card array __m_cards.
        # .. It also accesses a private member which may not be preferred.
        self.assertIs(self.deck._Deck__m_cards[0], self.deck.getCard(1))
        self.assertIs(self.deck._Deck__m_cards[1], self.deck.getCard(2))
        self.assertIs(self.deck._Deck__m_cards[2], self.deck.getCard(3))
        self.assertIsNot(self.deck._Deck__m_cards[0], self.deck.getCard(2))
        self.assertIsNot(self.deck._Deck__m_cards[1], self.deck.getCard(3))
        self.assertIsNot(self.deck._Deck__m_cards[2], self.deck.getCard(1))
コード例 #2
0
class UserInterface():
    def __init__(self):
        self.__m_currentDeck = None


    def run(self):
        """Present the main menu to the user and repeatedly prompt for a valid command"""
        print("Welcome to the Bingo! Deck Generator\n")
        menu = Menu("Main")
        menu.addOption("C", "Create a new deck")
        
        keepGoing = True
        while keepGoing:
            command = menu.show()
            if command == "C":
                self.__createDeck()
            elif command == "X":
                keepGoing = False


    def __createDeck(self):
        """Command to create a new Deck"""
        # TODO: Get the user to specify the card size, max number, and number of cards
        size = self.__getNumberInput("Card Size: ")
        while size < 3 or size > 15:
            print("That is not a valid size!\n")
            size = self.__getNumberInput("Card Size: ")

        max_num = self.__getNumberInput("\nMax Number: ")
        while max_num < 2*size*size or max_num > 4*size*size:
            print("That is not a valid max number!\n")
            max_num = self.__getNumberInput("Max Number: ")

        num_cards = self.__getNumberInput("\nNumber of Cards: ")
        while num_cards < 3 or num_cards > 10000:
            print("That is not a valid number of cards!\n")
            num_cards = self.__getNumberInput("Number of Cards: ", )
        # TODO: Create a new deck
        self.__m_currentDeck = Deck(size, num_cards, max_num)
        # TODO: Display a deck menu and allow use to do things with the deck
        self.__deckMenu()


    def __deckMenu(self):
        """Present the deck menu to user until a valid selection is chosen"""
        menu = Menu("Deck")
        menu.addOption("P", "Print a card to the screen")
        menu.addOption("D", "Display the whole deck to the screen")
        menu.addOption("S", "Save the whole deck to a file")

        keepGoing = True
        while keepGoing:
            command = menu.show()
            if command == "P":
                self.__printCard()
            elif command == "D":
                print()
                self.__m_currentDeck.print()
            elif command == "S":
                self.__saveDeck()
            elif command == "X":
                keepGoing = False


    def __printCard(self):
        """Command to print a single card"""
        cardToPrint = self.__getNumberInput("Id of card to print: ")#, 1, self.__m_currentDeck.getCardCount())
        if cardToPrint > 0 and cardToPrint <= self.__m_currentDeck.getCardCount():
            print()
            self.__m_currentDeck.print(idx=cardToPrint)
        else:
            print("Can't print card! Invalid card ID!")


    def __saveDeck(self):
        """Command to save a deck to a file"""
        fileName = self.__getStringInput("Enter output file name: ")
        if fileName != "":
            # TODO: open a file and pass to currentDeck.print()
            outputStream = open(fileName, 'w')
            self.__m_currentDeck.print(outputStream)
            outputStream.close()
            print("Done!")

    def __getNumberInput(self, prompt):
        return int(input(prompt))

    def __getStringInput(self, prompt):
        return input(prompt)