def test_hand_get_score(cards, expected): """ A hand's best score. """ hand = Hand() hand.set_cards(cards) actual = hand.score() assert actual == expected
def test_hand_set_scores(cards, expected): """ A hand's cards score correctly. """ hand = Hand() hand.set_cards(cards) actual = hand.get_scores() assert actual == expected
def test_hand_natural(cards, expected): """ A hand is a natural. """ hand = Hand() hand.set_cards(cards) actual = hand.natural() assert actual == expected
def test_value_of_three_aces_in_hand(self): from src.hand import Hand testHand = Hand() # Generate three "ace" cards and add them to hand for card in range(0, 3): testHand.add_card(self.generateCard(value=11, face='A', is_ace=True)) # Three Aces should be create a hand value of 13 (as not to bust) self.assertEqual(testHand.get_hand_value(), 13)
def test_is_bust_fcn_when_hand_val_is_over_21(self): from src.hand import Hand testHand = Hand() # Generate three "king" cards and add them to hand for card in range(0,3): testHand.add_card(self.generateCard(value=10, face='K', is_ace=False)) # Value of hand is 30, so bust should be True self.assertTrue(testHand.is_bust())
def test_is_bust_fcn_when_hand_val_is_21(self): from src.hand import Hand testHand = Hand() # Generate a "king" and "ace" card and add them to hand testHand.add_card(self.generateCard(value=11, face='A', is_ace=False)) testHand.add_card(self.generateCard(value=10, face='K', is_ace=False)) # Value of hand is 21, so bust should be false self.assertFalse(testHand.is_bust())
def test_fcn_add_card_adds_card_to_hand(self): from src.hand import Hand testHand = Hand() tempCard = self.generateCard() testHand.add_card(card=tempCard) self.assertIn(tempCard, testHand.hand)
def test_is_bust_fcn_when_hand_val_is_under_21(self): from src.hand import Hand testHand = Hand() # Generate Two "3" cards and add them to hand for card in range(0, 2): testHand.add_card(self.generateCard(value=3, face=str(3), is_ace=False)) # Value of hand is 6, so bust should be false self.assertFalse(testHand.is_bust())
def __init__(self, start_ammount=100.00): self.hand1 = Hand() self.hand2 = Hand() self.bank = None # Convert to float if it isn't if not isinstance(start_ammount, float): self.bank = float(start_ammount) else: self.bank = start_ammount self.__is_hand_split__ = False
def test_number_of_cards(self): from src.hand import Hand import random testHand = Hand() # Get a random int between 1 and 10 numberOfCardsToGenerate = random.randint(1,10) # Generate random number cards and add them to hand for card in range(0, numberOfCardsToGenerate): testHand.add_card(self.generateCard()) # Number of cards in hand should be equal to numberOfCardsToGenerate self.assertEqual(testHand.get_number_of_cards(), numberOfCardsToGenerate)
class Player: def __init__(self, name, seed): self.name = name self.wallet = seed self.hand = Hand() def receive_card(self, card): self.hand.append_card(card) def get_name(self): return self.name def get_wallet(self): return self.wallet
def test_value_can_add(self): from src.hand import Hand import random testHand = Hand() # Get a random int between 1 and 10 randomInt1 = random.randint(1,10) randomInt2 = random.randint(1,10) valueOfHand = randomInt1 + randomInt2 testHand.add_card(self.generateCard(value=randomInt1, face=str(randomInt1), is_ace=False)) testHand.add_card(self.generateCard(value=randomInt2, face=str(randomInt2), is_ace=False)) self.assertEqual(testHand.get_hand_value(), valueOfHand)
def test_value_with_hidden_card(self): from src.hand import Hand testHand = Hand() # Generate Ace and 10 cards and add them to hand card1 = self.generateCard(value=11, face='A', is_ace=True) card2 = self.generateCard(value=10, face=str(10), is_ace=False) # hide the ace card1.hide() testHand.add_card(card1) testHand.add_card(card2) # Value should be 10 when the ace card is hidden self.assertEqual(testHand.get_hand_value(), 10)
def initialize_hand(self, dealer_seat): print('\n----------- Initializing new hand -----------') #self.debug_print_players(dealer_seat) print('deck size = ' + str(self.deck.deck_size)) players = self.initialize_players(dealer_seat) hand = Hand(self.table, self.deck, dealer_seat, players) return hand
def show_dialog(args): args['d'].set_is_visible(True) r = Card("Hall", CardType.ROOM, "Hall") w = Card("Revolver", CardType.WEAPON, "Revolver") s = Card("Colonel Mustard", CardType.SUSPECT, "Colonel Mustard") sug = Suggestion(r, w, s) hand = Hand([r, w]) args['d'].set_suggestion(sug) args['d'].set_player_hand(hand)
def test_value_with_single_ace_and_a_numeric(self): from src.hand import Hand testHand = Hand() # Generate Ace and King cards and add them to hand testHand.add_card(self.generateCard(value=11, face='A', is_ace=True)) testHand.add_card(self.generateCard(value=10, face=str(10), is_ace=False)) # One Ace and one King should be create a hand value of 21 self.assertEqual(testHand.get_hand_value(), 21)
def test_fcn_unhide_only_unhides_1_card_in_hand(self): from src.hand import Hand testHand = Hand() tempCards = [] tempCards.append(self.generateCard()) tempCards.append(self.generateCard(value=3, face=str(3), is_ace=False)) tempCards.append(self.generateCard(value=10, face='K', is_ace=False)) # Set hidden status on all temp cards for card in tempCards: card.hide() # add temp cards to deck for card in tempCards: testHand.add_card(card) # Unhide card testHand.unhide_card() numberOfHiddenCards = 0 # check number of hidden cards for card in testHand.hand: if card.is_hidden(): numberOfHiddenCards = numberOfHiddenCards + 1 self.assertEqual(numberOfHiddenCards, 2)
def test_face_and_numeric_not_equal(self): from src.hand import Hand testHand = Hand() # Generate Face card and add it to hand testHand.add_card(self.generateCard(value=10, face='J',is_ace=False)) # Generate Numeric card and add it to hand testHand.add_card(self.generateCard(value=4, face=str(4),is_ace=False)) self.assertFalse(testHand.first_two_cards_identical())
def test_output_is_correct(self): from src.hand import Hand testHand = Hand() # Add a King and a 7 testHand.add_card(self.generateCard(value=10, face='K', is_ace=False)) testHand.add_card(self.generateCard(value=7, face=str(7), is_ace=False)) outputShouldBe = "K 7 " self.assertEqual(testHand.get_printable_hand_output(), outputShouldBe)
def test_numeric_and_ace_not_equal(self): from src.hand import Hand testHand = Hand() # Generate Numeric card and add it to hand testHand.add_card(self.generateCard(value=9, face=str(9), is_ace=False)) # Generate Ace card and add it to hand testHand.add_card(self.generateCard(value=11, face='A', is_ace=True)) self.assertFalse(testHand.first_two_cards_identical())
def test_first_two_ace_cards_are_identical(self): from src.hand import Hand testHand = Hand() # Generate 2 cards and add them to hand for card in range(0, 2): testHand.add_card(self.generateCard(value=11, face='A',is_ace=True)) # Generate 3rd alt card testHand.add_card(self.generateCard(value=4, face=str(4),is_ace=False)) self.assertTrue(testHand.first_two_cards_identical())
def detect_keypoint(test_image, is_vis): body_estimation = Body('model/body_pose_model.pth') hand_estimation = Hand('model/hand_pose_model.pth') oriImg = cv2.imread(test_image) # B,G,R order # detect body # subset: n*20 array, n is the human_number in the index, 0-17 is the index in candidate, 18 is the total score, 19 is the total parts # candidate: m*4, m is the keypoint number in the image, [x, y, confidence, id] candidate, subset = body_estimation( oriImg ) # candidate: output the keypoints([25, 4]), x, y, score, keypoint_index canvas = copy.deepcopy(oriImg) canvas, bodypoints = util.draw_bodypose(canvas, candidate, subset) # detect hand hands_list = util.handDetect(candidate, subset, oriImg) all_hand_peaks = [] hand_personid_isleft = [] for x, y, w, is_left, person_id in hands_list: # cv2.rectangle(canvas, (x, y), (x+w, y+w), (0, 255, 0), 2, lineType=cv2.LINE_AA) # cv2.putText(canvas, 'left' if is_left else 'right', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) # if is_left: # plt.imshow(oriImg[y:y+w, x:x+w, :][:, :, [2, 1, 0]]) # plt.show() peaks = hand_estimation(oriImg[y:y + w, x:x + w, :]) peaks[:, 0] = np.where(peaks[:, 0] == 0, peaks[:, 0], peaks[:, 0] + x) peaks[:, 1] = np.where(peaks[:, 1] == 0, peaks[:, 1], peaks[:, 1] + y) # else: # peaks = hand_estimation(cv2.flip(oriImg[y:y+w, x:x+w, :], 1)) # peaks[:, 0] = np.where(peaks[:, 0]==0, peaks[:, 0], w-peaks[:, 0]-1+x) # peaks[:, 1] = np.where(peaks[:, 1]==0, peaks[:, 1], peaks[:, 1]+y) # print(peaks) all_hand_peaks.append(peaks) hand_personid_isleft.append([person_id, is_left]) # all_hand_peaks: [p, 21, 2] p is the hand number in the image # hand_personid_isleft: [p, 2] is_isleft, person_id all_hand_peaks = np.asarray(all_hand_peaks) hand_personid_isleft = np.asarray(hand_personid_isleft) canvas = util.draw_handpose(canvas, all_hand_peaks) if is_vis: plt.imshow(canvas[:, :, [2, 1, 0]]) plt.axis('off') plt.show() return bodypoints, all_hand_peaks, hand_personid_isleft
def test_output_hides_cards(self): from src.hand import Hand testHand = Hand() # Generate Ace and 10 cards and add them to hand testHand.add_card(self.generateCard(value=11, face='A', is_ace=True)) card2 = self.generateCard(value=10, face=str(10), is_ace=False) # hide the 10 card2.hide() testHand.add_card(card2) outputShouldBe = "A # " self.assertEqual(testHand.get_printable_hand_output(), outputShouldBe)
def test_fcn_add_card_adds_card_to_end_of_hand(self): from src.hand import Hand testHand = Hand() tempCard = self.generateCard() tempCard2 = self.generateCard(value=3, face=str(3), is_ace=False) testHand.add_card(card=tempCard) testHand.add_card(card=tempCard2) self.assertEqual(tempCard2, testHand.hand[-1])
def test_clear_hand_fcn(self): from src.hand import Hand testHand = Hand() # Generate 2 cards and add them to hand for card in range(0, 2): testHand.add_card(self.generateCard()) # Clear hand testHand.clear_hand() # Ensure hand is empty self.assertEqual(len(testHand.hand), 0)
def test_fcn_unhide_only_unhides_2nd_card_in_hand(self): from src.hand import Hand testHand = Hand() tempCards = [] tempCards.append(self.generateCard()) tempCards.append(self.generateCard(value=3, face=str(3), is_ace=False)) tempCards.append(self.generateCard(value=10, face='K', is_ace=False)) # Set hidden status of 2nd temp card tempCards[1].hide() # add temp cards to deck for card in tempCards: testHand.add_card(card) # Unhide card testHand.unhide_card() self.assertFalse(testHand.hand[1].is_hidden())
def __init__(self): super(pose_detector, self).__init__() self.hand_estimation = Hand('src/hand_pose_model.pth')
class Player(): def __init__(self, start_ammount=100.00): self.hand1 = Hand() self.hand2 = Hand() self.bank = None # Convert to float if it isn't if not isinstance(start_ammount, float): self.bank = float(start_ammount) else: self.bank = start_ammount self.__is_hand_split__ = False # Play Game def play(self, canSplit=False, canInsure=False): can_split_status = self.__can_split__() # Get printable play options options = self.get_play_options(canSplit=canSplit, canInsure=canInsure) print(options) # Get decision decision = self.get_decision() # Check decision is valid decision_valid = self.check_if_decision_valid( decision, canSplit=can_split_status, canInsure=False) if decision_valid: return decision else: print(" !!! Invalid Option !!!") time.sleep(0.5) raise ValueError("Invalid Input option") def get_play_options(self, canSplit=False, canInsure=False): output = "Pick an option:\n" output = output + "[1] Hit\n" output = output + "[2] Stand\n" if canSplit: output = output + "[3] Split\n" if canInsure: output = output + "[4] Insure\n" return output def check_if_decision_valid(self, decision, canSplit=False, canInsure=False): # Check valid if decision > 0 and decision < 5: if decision == 1 or decision == 2: return True # Check if user was given the option to split elif decision == 3 or decision == 4: if canSplit and decision == 3: return True elif canInsure and decision == 4: return True else: return False else: return False else: return False def get_decision(self): decision = input("Please select a number from the options above: ") return int(decision) # Bank Status def get_bank(self): return self.bank def bank_deposit(self, ammount): self.bank = self.bank + ammount # Add Cards def add_card(self, card1, card2=None): if self.__get_split_status__(): # Reject if 2nd card is not given when in split if card2: if not self.hand1.is_bust(): self.hand1.add_card(card1) if not self.hand2.is_bust(): self.hand2.add_card(card2) else: self.hand1.add_card(card1) # Printable Outputs for hand def get_hand_output(self): if self.__get_split_status__(): return self.__get_hand_printable_output_in_split__() else: return self.__get_hand_printable_output__() def __get_hand_printable_output_in_split__(self): hand1_output = self.__get_hand1_printable_output__(showHandNumber=True) hand2_output = self.__get_hand2_printable_output__(showHandNumber=True) output = hand1_output + hand2_output return output def __get_hand_printable_output__(self): hand1_output = self.__get_hand1_printable_output__( showHandNumber=False) output = hand1_output return output def __get_hand1_printable_output__(self, showHandNumber=False): hand_value = self.hand1.get_hand_value() hand_output = self.hand1.get_printable_hand_output() if showHandNumber: return self.__compose_printable_output__(hand_output, hand_value, 1) else: return self.__compose_printable_output__(hand_output, hand_value) def __get_hand2_printable_output__(self, showHandNumber=False): hand_value = self.hand2.get_hand_value() hand_output = self.hand2.get_printable_hand_output() if showHandNumber: return self.__compose_printable_output__(hand_output, hand_value, 2) else: return self.__compose_printable_output__(hand_output, hand_value) def __compose_printable_output__(self, hand_output, hand_value, handNumber=0): hand_value_str = str(hand_value) output = "\t" if handNumber == 0: output = output + "Hand: " else: output = output + "Hand_" + str(handNumber) + ": " output = output + hand_output + " (Value: " + hand_value_str + ") \n" return output # Get The hand Objects def __get_hand_obj__(self): return [self.hand1.hand, self.hand2.hand] # Hand Values def get_hand_value(self): if self.__get_split_status__(): return self.__get_hand_value_in_split__() else: return self.__get_hand1_value__() def __get_hand1_value__(self): return self.hand1.get_hand_value() def __get_hand_value_in_split__(self): value_hand_1 = self.hand1.get_hand_value() value_hand_2 = self.hand2.get_hand_value() return [value_hand_1, value_hand_2] # Split Functions def split(self): self.__set_split__() split1 = self.hand1.hand[0] split2 = self.hand1.hand[1] self.__clear_hand__() # self.hand.add_card(split1) # self.hand2.add_card(split2) self.add_card(card1=split1, card2=split2) def __get_split_status__(self): return self.__is_hand_split__ def __set_split__(self): self.__is_hand_split__ = True def __unset_split__(self): self.__is_hand_split__ = False def __can_split__(self): can_split = False # make sure we're not already in a split if not self.__get_split_status__(): # Check hand only has two cards if self.hand1.get_number_of_cards() == 2: # Check if first two cards are identical if self.hand1.first_two_cards_identical(): can_split = True return can_split # Check if hand is busted (over 21) def check_if_bust(self): bust = False # Bust in Split? if self.__get_split_status__(): if self.__check_hand1_bust__() and self.__check_hand2_bust__(): bust = True #Bust in normal else: if self.__check_hand1_bust__(): bust = True return bust def __check_hand1_bust__(self): return self.hand1.is_bust() def __check_hand2_bust__(self): return self.hand2.is_bust() # Reset Functions def reset(self): self.__clear_hand__() self.__unset_split__() def __clear_hand__(self): self.hand1.clear_hand() self.hand2.clear_hand()
import cv2 import matplotlib.pyplot as plt import copy import numpy as np from src import model from src import util from src.body import Body from src.hand import Hand body_estimation = Body('model/body_pose_model.pth') hand_estimation = Hand('model/hand_pose_model.pth') test_image = 'images/demo.jpg' oriImg = cv2.imread(test_image) # B,G,R order candidate, subset = body_estimation(oriImg) canvas = copy.deepcopy(oriImg) canvas = util.draw_bodypose(canvas, candidate, subset) # detect hand hands_list = util.handDetect(candidate, subset, oriImg) all_hand_peaks = [] for x, y, w, is_left in hands_list: # cv2.rectangle(canvas, (x, y), (x+w, y+w), (0, 255, 0), 2, lineType=cv2.LINE_AA) # cv2.putText(canvas, 'left' if is_left else 'right', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) # if is_left: # plt.imshow(oriImg[y:y+w, x:x+w, :][:, :, [2, 1, 0]]) # plt.show() peaks = hand_estimation(oriImg[y:y + w, x:x + w, :]) peaks[:, 0] = np.where(peaks[:, 0] == 0, peaks[:, 0], peaks[:, 0] + x)
import cv2 import matplotlib.pyplot as plt import copy import numpy as np from src import model from src import util from src.body import Body from src.hand import Hand body_estimation = Body('/home2/lgfm95/openposehzzone/model/body_pose_model.pth') hand_estimation = Hand('/home2/lgfm95/openposehzzone/model/hand_pose_model.pth') def main(oriImg): shape0 = oriImg.shape candidate, subset = body_estimation(oriImg) canvas = copy.deepcopy(oriImg) shape1 = canvas.shape canvas = util.draw_bodypose(canvas, candidate, subset) # detect hand hands_list = util.handDetect(candidate, subset, oriImg) all_hand_peaks = [] shape2 = canvas.shape for x, y, w, is_left in hands_list: # cv2.rectangle(canvas, (x, y), (x+w, y+w), (0, 255, 0), 2, lineType=cv2.LINE_AA) # cv2.putText(canvas, 'left' if is_left else 'right', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) # if is_left: # plt.imshow(oriImg[y:y+w, x:x+w, :][:, :, [2, 1, 0]]) # plt.show()