Ejemplo n.º 1
0
 def __init__(self, player1, player2):
     self._player1 = player1
     self._player1.hand = []
     self._player1.discard = []
     self._player1.played = []
     self._player2 = player2
     self._player2.hand = []
     self._player2.discard = []
     self._player2.played = []
     self._winner = None
     deck = Deck()
     deck.shuffle()
     while not deck.is_empty():
         player1.draw_card(deck)
         player2.draw_card(deck)
Ejemplo n.º 2
0
def main(): #Function that brings all other functions together in one place
    '''main function'''
    RULES = '''
    Basra Card Game:
        This game belongs to a category of card games called “fishing cards games”. 
        Each player in turn matches a card from their hand with one (or more) of those 
        lying face-up on the table and then takes them. 
        If the card which the player played out does not match one of the existing cards, 
        it stays on the table.
    To win, you have to collect more points.'''
    
    print(RULES) 
    D = Deck()
    p1_cards = [] # card in hands player1
    p2_cards = [] #card in hands player2
    t_cards = []   # card on the floor
    p1_pile = [] # for player1
    p2_pile = [] # for player2
    basra_1 = []
    basra_2 = []
    p1_turn = (0,2,4,6)
    p2_turn = (1,3,5,7)
    game = True
    game_quit = False
    
    answer = input("Would you like to play? y/Y or n/N?")
    while answer!='n':
        print(" ---------Start The game--------")
        D.shuffle()
        while game == True: #replace by the correct condition
            for rounds in range(6):
                if rounds == 0:
                    distribute_cards(D,p1_cards,p2_cards,t_cards,True)
                    print('Dealing the cards, 4 cards for each player, 4 cards on the table')
                    print('Cards left:  {}'.format(D.__len__()))
                    display_table(t_cards,p1_cards,p2_cards)
                else:
                    distribute_cards(D,p1_cards,p2_cards,t_cards,False)
                    print('')
                    print('------Start new round-----')
                    print('Dealing the cards, 4 cards for each player')
                    print('Cards left:  {}'.format(D.__len__()))
                    display_table(t_cards,p1_cards,p2_cards)
                for turn in range(8):
                    while turn in p1_turn:
                        card_index = input("Player {:d} turn: -> ".format(1))
                        if card_index == 'q':
                            game = False
                            game_quit = True
                            break
                        try:
                            play(p1_cards[int(card_index)],p1_cards,p1_pile,basra_1,t_cards)
                        except:
                            print('Please enter a valid card index, 0 <= index <= {:d}'.format(len(p1_cards)-1))
                        else:
                            display_table(t_cards,p1_cards,p2_cards)
                            break
                    if game_quit == True:
                        break
                    while turn in p2_turn:
                        card_index = input("Player {:d} turn: -> ".format(2))
                        if card_index == 'q':
                            game = False
                            game_quit = True
                            break
                        try:
                            play(p2_cards[int(card_index)],p2_cards,p2_pile,basra_2,t_cards)
                        except:
                            print('Please enter a valid card index, 0 <= index <= {:d}'.format(len(p2_cards)-1))
                        else:
                            display_table(t_cards,p1_cards,p2_cards)
                            break
                    if game_quit == True:
                        break
                turn = 0
                if t_cards != []:
                    if game == True:
                        if turn in p1_turn:
                            p1_pile.append(t_cards)
                        else:
                            p2_pile.append(t_cards)
                        t_cards = []
                        display_table(t_cards,p1_cards,p2_cards)
                if game == False:
                    break
            if D.is_empty() == True:
                display_table(t_cards,p1_cards,p2_cards)
                score = compute_score(p1_pile,p2_pile,basra_1,basra_2)
                print('player 1:  {}'.format(score[0]))
                print('player 2:  {}'.format(score[1]))
                print('')
                if score[0] > score[1]:
                    print('Player 1 is the winner')
                    game = False
                else:
                    print('Player 2 is the winner')
                    game = False
                    
# useful strings to match tests (put them in a loop to reprompt on bad input)
# print('Please enter a valid card index, 0 <= index <= {:d}'.format(len(p1_cards)-1))
# card_index = input("Player {:d} turn: -> ".format(turn))

# remember to clear the data structures before the next game
# deck.reset()
# p1_cards.clear() # card in hands player1
# p2_cards.clear() #card in hands player2
# t_cards.clear()   # card on the floor
# p1_pile.clear() # pile for player1
# p2_pile.clear() # pile for player2
# basra_1.clear() # basra for player1
# basra_2.clear() #basra for player2
        if game_quit == True:
            break
        answer = input("Would you like to play? y/Y or n/N? ")
        if answer.lower() == 'y':
            D.reset()
            p1_cards.clear() # card in hands player1
            p2_cards.clear() #card in hands player2
            t_cards.clear()   # card on the floor
            p1_pile.clear() # pile for player1
            p2_pile.clear() # pile for player2
            basra_1.clear() # basra for player1
            basra_2.clear() #basra for player2
    print('Thanks for playing. See you again soon.')
Ejemplo n.º 3
0
class Solitaire:
    """
    Patience class representing a game of Golf Solitaire.

    This game has 7 columns and 5 cards in each column,
    but the methods should work with other valid values as well.
    """

    columns = 7
    cards_in_column = 5

    def __init__(self):
        """
        Constructor, do the setup here.

        After setup with Solitaire.columns = 7, Solitaire.cards_in_column = 5
        You should have:
        self.tableau -> 7 columns of cards with 5 cards in each column
        self.stock -> 16 cards
        self.waste -> 1 card
        """
        self.deck = Deck()
        self.tableau = []
        self.waste = []
        self.stock = []
        self.deck.shuffle_deck()

        for col in range(Solitaire.columns):
            column = []
            # Deal cards to each column in range of given nr.
            for cards in range(Solitaire.cards_in_column):
                column.append(self.deck.deal_card())
            self.tableau.append(column)
        self.waste.append(self.deck.deal_card())

        while not self.deck.is_empty():
            self.stock.append(self.deck.deal_card())

    def can_move(self, card) -> bool:
        """
        Validate if a card from the tableau can be moved to the waste pile.

        The card must be last in the column list and adjacent by rank
        to the topmost card of the waste pile (last in waste list).
        Example: 8 is adjacent to 7 and 9. Ace is only adjacent to 2.
        King is only adjacent to Queen.
        """
        if card.rank is not None:
            for i in self.tableau:
                if len(i) > 0:
                    if card == i[-1]:
                        return card.rank - 1 == self.waste[
                            -1].rank or card.rank + 1 == self.waste[-1].rank
        return False

    def move_card(self, col: int):
        """
        Move a card from the tableau to the waste pile.

        Does not validate the move.
        :param col: index of column
        """
        if len(self.tableau[col]) > 0:
            card = self.tableau[col].pop()
            self.waste.append(card)

    def deal_from_stock(self):
        """
        Deal last card from stock pile to the waste pile.

        If the stock is empty, do nothing.
        """
        if len(self.stock) > 0:
            self.waste.append(self.stock.pop())

    def has_won(self) -> bool:
        """Check for the winning position - no cards left in tableau."""
        for i in self.tableau:
            if len(i) != 0:
                return False
        return True

    def last_cards(self):
        """Return list of last cards in the tableau."""
        return [
            self.tableau[i][-1] for i in range(len(self.tableau))
            if len(self.tableau[i]) != 0
        ]

    def has_lost(self) -> bool:
        """
        Check for the losing position.

        Losing position: no cards left in stock and no possible moves.
        """
        return len(self.stock) == 0 and len(
            [card for card in self.last_cards() if self.can_move(card)]) == 0

    def print_game(self):
        """
        Print the game.

        Assumes:
        Card(decorated=True) by default it is already set to True
        self.tableau -> a list of lists (each list represents a column of cards)
        self.stock -> a list of Card objects that are in the stock
        self.waste_pile -> a list of Card objects that are in the waste pile

        You may modify/write your own print_game.
        """
        print(f" {'    '.join(list('0123456'))}")
        print('-' * 34)
        print("\n".join([
            (" ".join((map(str, x))))
            for x in (zip_longest(*self.tableau, fillvalue="    "))
        ]))
        print()
        print(
            f"Stock pile: {len(self.stock)} card{'s' if len(self.stock) != 1 else ''}"
        )
        print(f"Waste pile: {self.waste[-1] if self.waste else 'Empty'}")

    @staticmethod
    def rules():
        """Print the rules of the game."""
        print("Rules".center(40, "-"))
        print(
            dedent("""
                Objective: Move all the cards from each column to the waste pile.

                A card can be moved from a column to the waste pile if the
                rank of that card is one higher or lower than the topmost card
                of the waste pile. Only the first card of each column can be moved.

                You can deal cards from the stock to the waste pile.
                The game is over if the stock is finished and
                there are no more moves left.

                The game is won once the tableau is empty.

                Commands:
                  (0-6) - integer of the column, where the topmost card will be moved
                  (d) - deal a card from the stock
                  (r) - show rules
                  (q) - quit
                  """))

    def play(self):
        """
        Play a game of Golf Solitaire.

        Create the game loop here.
        Use input() for player input.
        Available commands are described in rules().
        """
        allowed_moves = ["0", "1", "2", "3", "4", "5", "6"]
        self.deck.shuffle_deck()
        while True:
            self.print_game()
            if self.has_won():
                print("You won.")
                break
            if self.has_lost():
                print("You lost.")
                break
            command = input()
            if command == "r":
                self.rules()
                continue
            if command in allowed_moves:
                cmd = int(command)
                if cmd < Solitaire.columns:
                    if self.can_move(self.tableau[cmd][-1]) and len(
                            self.tableau[cmd]) != 0:
                        self.move_card(cmd)
                continue
            if command == "d":
                self.deal_from_stock()
                continue
            if command == "q":
                break
            else:
                print("Wrong input " + str(command))
                continue