<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
round.py
(c) <NAME>  25 December, 2016

An object representing a single round in a game of cribbage between
two CribbagePlayers.
'''

from __future__ import absolute_import, print_function
import random
from cribbage.cards import card_tostring, cards_worth, make_deck, split_card
from cribbage.cribbage_score import is_legal_play, score_hand, score_play

class Round(object):
    '''
    A single round of cribbage in a game between two CribbagePlayers.
    '''

    def __init__(self, game, players, dealer_idx):
        '''
        Constructor.

        Arguments:
        - `players`:
        - `dealer_idx`:
        '''
        self.game = game
        self.players = players
        self.dealer_idx = dealer_idx
        # the deck of the current game round
        self.deck = None
        # the hands of the two players during the current game round
        self.hands = None
        # copy of self.hands
        self.orig_hands = None
        # the crib of the current game round
        self.crib = None
        # the card value of the "starter" or "cut" card for the
        # current game round
        self.starter_card = None
        # the player whose turn it currently is to play a card
        self.turn_idx = None
        # the array of cards that have been played already during the
        # current game round by the two players
        self.faceups = None
        # the same cards recorded in self.faceups, but linearised into
        # a single list, based on the time that the cards were played
        self.linear_play = None
        # during a round, we keep track of the last_player
        self.last_player = None
        # the cards played by both players during a round
        self.round_played = []
        # a flag that indicates whether the current sequence of the
        # current game round has gone to "Go" or not
        self.is_go = False
        # a flag that indicates whether the last player hit 31 or not
        # during the current sequence of the current game round
        self.flag_31 = False

    def deal(self, verbose=False):
        '''
        Shuffles and deals the cards for a single round of cribbage.

            The dealer shuffles and deals six cards to each player.
            Once the cards have been dealt, each player chooses four
            cards to retain, then discards the other two face-down to
            form the "crib", which will be used later by the dealer.
            At this point, each player's hand and the crib will
            contain exactly four cards. The player on the dealer's
            left cuts the deck and the dealer reveals the top card,
            called the "starter" or the "cut". If this card is a Jack,
            the dealer scores two points for "his heels."

        Returns True if the game is not over after the deal; False if
        the game is over (or was already over before the deal).

        Arguments:
        - `verbose`:
        '''
        if self.game.over:
            return False
        # create a new shuffled deck
        self.deck = make_deck()
        random.shuffle(self.deck)
        # deal out 6 cards to each player
        nondealer_hand = self.deck[:12][::2]
        dealer_hand = self.deck[:12][1::2]
        self.deck = self.deck[12:]
        # self.dealer_idx indicates the player who is dealer
        self.hands = ([nondealer_hand, dealer_hand] if self.dealer_idx
                      else [dealer_hand, nondealer_hand])
        if verbose:
            print('Dealing cards')
            self.print_state()
        # ask players to select cards to discard to crib
        self.crib = []
        for idx, player in enumerate(self.players):
            discard_idxs = player.discard(self.hands[idx], idx == self.dealer_idx)
            # sanity checking
            assert len(set(discard_idxs)) == 2
            assert all(0 <= i < 6 for i in discard_idxs)
            discard_idxs = set(discard_idxs)
            discards = [c for i, c in enumerate(self.hands[idx])
                        if i in discard_idxs]
            if verbose:
                print('Player {} discards:'.format(idx+1),
                      ' '.join(card_tostring(c) for c in sorted(discards)))
            self.crib.extend(discards)
            self.hands[idx] = [c for i, c in enumerate(self.hands[idx])
                               if i not in discard_idxs]
        # copy self.hands after discarding
        self.orig_hands = [x[:] for x in self.hands]
        if verbose:
            self.print_state()
        # randomly cut a card from the deck to serve as the "starter"
        self.starter_card = random.choice(self.deck)
        # check for "his nibs"
        starter_value, _starter_suit = split_card(self.starter_card)<commit_msg>cribbage/round.py: pylint cleanup<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
round.py
(c) <NAME>  25 December, 2016

An object representing a single round in a game of cribbage between
two CribbagePlayers.
'''

from __future__ import absolute_import, print_function
import random
from cribbage.cards import card_tostring, cards_worth, make_deck, split_card
from cribbage.cribbage_score import is_legal_play, score_hand, score_play

class Round(object):
    '''
    A single round of cribbage in a game between two CribbagePlayers.
    '''

    def __init__(self, game, players, dealer_idx):
        '''
        Constructor.

        Arguments:
        - `players`:
        - `dealer_idx`:
        '''
        self.game = game
        self.players = players
        self.dealer_idx = dealer_idx
        # the deck of the current game round
        self.deck = None
        # the hands of the two players during the current game round
        self.hands = None
        # copy of self.hands
        self.orig_hands = None
        # the crib of the current game round
        self.crib = None
        # the card value of the "starter" or "cut" card for the
        # current game round
        self.starter_card = None
        # the player whose turn it currently is to play a card
        self.turn_idx = None
        # the array of cards that have been played already during the
        # current game round by the two players
        self.faceups = None
        # the same cards recorded in self.faceups, but linearised into
        # a single list, based on the time that the cards were played
        self.linear_play = None
        # during a round, we keep track of the last_player
        self.last_player = None
        # the cards played by both players during a round
        self.round_played = []
        # a flag that indicates whether the current sequence of the
        # current game round has gone to "Go" or not
        self.is_go = False
        # a flag that indicates whether the last player hit 31 or not
        # during the current sequence of the current game round
        self.flag_31 = False

    def deal(self, verbose=False):
        '''
        Shuffles and deals the cards for a single round of cribbage.

            The dealer shuffles and deals six cards to each player.
            Once the cards have been dealt, each player chooses four
            cards to retain, then discards the other two face-down to
            form the "crib", which will be used later by the dealer.
            At this point, each player's hand and the crib will
            contain exactly four cards. The player on the dealer's
            left cuts the deck and the dealer reveals the top card,
            called the "starter" or the "cut". If this card is a Jack,
            the dealer scores two points for "his heels."

        Returns True if the game is not over after the deal; False if
        the game is over (or was already over before the deal).

        Arguments:
        - `verbose`:
        '''
        if self.game.over:
            return False
        # create a new shuffled deck
        self.deck = make_deck()
        random.shuffle(self.deck)
        # deal out 6 cards to each player
        nondealer_hand = self.deck[:12][::2]
        dealer_hand = self.deck[:12][1::2]
        self.deck = self.deck[12:]
        # self.dealer_idx indicates the player who is dealer
        self.hands = ([nondealer_hand, dealer_hand] if self.dealer_idx
                      else [dealer_hand, nondealer_hand])
        if verbose:
            print('Dealing cards')
            self.print_state()
        # ask players to select cards to discard to crib
        self.crib = []
        for idx, player in enumerate(self.players):
            discard_idxs = player.discard(self.hands[idx], idx == self.dealer_idx)
            # sanity checking
            assert len(set(discard_idxs)) == 2
            assert all(0 <= i < 6 for i in discard_idxs)
            discard_idxs = set(discard_idxs)
            discards = [c for i, c in enumerate(self.hands[idx])
                        if i in discard_idxs]
            if verbose:
                print('Player {} discards:'.format(idx+1),
                      ' '.join(card_tostring(c) for c in sorted(discards)))
            self.crib.extend(discards)
            self.hands[idx] = [c for i, c in enumerate(self.hands[idx])
                               if i not in discard_idxs]
        # copy self.hands after discarding
        self.orig_hands = [x[:] for x in self.hands]
        if verbose:
            self.print_state()
        # randomly cut a card from the deck to serve as the "starter"
        self.starter_card = random.choice(self.deck)
        # check for "his nibs"
        starter_value = split_card(self.starter_card)[0]
{'fraction_non_alphanumeric': 0.07185752743839305, 'fraction_numerical': 0.005280596396769518, 'mean_word_length': 1.9978274363749224, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '608704', 'n_tokens_mistral': 2728, 'n_tokens_neox': 2577, 'n_words': 1044}