def testDealtHands(self): """Confirm that extraHands are added to the test_state hand_list""" test_state = ClientState() hand = [ Card(1, 'Spades'), Card(2, 'Clubs'), Card(3, 'Diamonds'), Card(4, 'Hearts'), Card(0, None) ] test_state.dealtHands([hand, hand]) self.assertEqual(test_state.hand_list, [hand]) self.assertEqual(test_state.hand_cards, hand)
def testSetup(self): """Confirm the state tracker initializes properly""" test_state = ClientState() self.assertEqual(test_state.turn_phase, 'inactive') self.assertEqual(test_state.hand_cards, []) self.assertEqual(test_state.hand_list, []) self.assertEqual(test_state.played_cards, {})
def testDiscardCards(self): """Confirm discardCards removes cards without playing them""" test_state = ClientState(ruleset=None) hand = [ Card(1, 'Spades'), Card(2, 'Clubs'), Card(3, 'Diamonds'), Card(4, 'Hearts'), Card(0, None) ] test_state.newCards(hand) test_state.discardCards([Card(1, 'Spades')]) self.assertEqual(test_state.played_cards, {}) hand.remove(Card(1, 'Spades')) self.assertEqual(test_state.hand_cards, hand) #Confirm can only discard cards actually in your hand with self.assertRaises(Exception): test_state.discardCards([Card(1, 'Spades')]) #Confirm can only discard correct number of cards with self.assertRaises(Exception): test_state.discardCards([Card(2, 'Clubs'), Card(3, 'Diamonds')])
def testNewCards(self): """Confirm newCards adds cards to hand""" test_state = ClientState(ruleset=None) wholeDeck = Card.getStandardDeck() test_state.newCards(wholeDeck) self.assertEqual(wholeDeck, test_state.hand_cards) drawn_cards = [Card(0, None), Card(0, None)] test_state.newCards(drawn_cards) self.assertEqual(Card.getJokerDeck(), test_state.hand_cards)
def testHandStatus(self): """Confirm that hand status information is ordered correctly""" test_state = ClientState(ruleset=None) #set turn phase test_state.turn_phase = 'TestPhase' #setup so that hand has 5 cards and there is a foot left to play hand = [ Card(1, 'Spades'), Card(2, 'Clubs'), Card(3, 'Diamonds'), Card(4, 'Hearts'), Card(0, None) ] test_state.dealtHands([hand, hand]) self.assertEqual(test_state.getHandStatus(), ['TestPhase', 5, 1])
def testPickupRulesCheck(self): """Confirm we check the discard pile size for pickups""" test_state = ClientState(ruleset=None) test_state.played_cards[1] = [ Card(1, 'Clubs'), Card(1, 'Clubs'), Card(1, 'Clubs') ] test_state.played_cards[4] = [ Card(4, 'Clubs'), Card(4, 'Clubs'), Card(0, None) ] prepared_cards = {} prepared_cards[5] = [Card(5, 'Clubs'), Card(5, 'Clubs')] #confirm too small pile disallowed test_state.discard_info = (Card(5, 'Hearts'), 6) with self.assertRaises(Exception): test_state.pickupPileRuleCheck(prepared_cards)
def RunClient(): # Comment out changes that are not compatible with server currently on host (= getting game from Server) if getattr(sys, 'frozen', False): os.chdir(sys._MEIPASS) """This is the launch point for the client. It sets up the various classes and starts the game loop. Steps -- (i) player provides host:port info, and connection to server established. (ii) clientState initialized (iii) controller initialized (iv) ruleset imported << this is section that I edited for this. Labeled changes: # oldServer. (v) player provides name (vi) game window created (vii) tableView and handView initialized (viii) playername confirmed with server and player_index found. (ix) main game loop """ # (i) Connect to server: host = str(input("Enter the host [xxxxx.net] ") or "xxxxx.net") port = str(input("Enter the port[8080] ") or "8080") connection.DoConnect((host, int(port))) # (ii)-(iv) initialize clientState and gameControl. Will get name of game from server ruleset = "tbd" # ruleset will be obtained from server. If wish to run in test mode than change "tbd" to "test" # oldServer - added next line and commented out gameControl.askForGame() ruleset = str( input("Enter the game - Liverpool or HandAndFoot [HandAndFoot]") or "HandAndFoot") clientState = ClientState(ruleset) gameControl = Controller(clientState) # gameControl.askForGame() # Ask server for name of game to be played. while clientState.ruleset == "tbd": connection.Pump() gameControl.Pump() sleep(0.001) clientState.importRules( clientState.ruleset ) # Have rec'd ruleset name from server, so import the rules. # gameControl.setName( ) # Ask the player their name, and confirm it is acceptable. playername = gameControl.getName() # Confirm server has player name. gameboard = CreateDisplay(playername) tableView = TableView(gameboard.display, clientState.ruleset) handView = HandView(gameControl, gameboard.display, clientState.ruleset) current_round = handView.round_index while (len(tableView.player_names) < 1) or (tableView.player_names.count('guest') > 0): # Note that if two people join with the same name almost simultaneously, then both might be renamed. gameboard.refresh() connection.Pump() gameControl.Pump() tableView.Pump() tableView.playerByPlayer(current_round) note = "Waiting for all current players to pick legit names. If wait seems too long, " \ "then it is possible you have the wrong server or port#..." gameboard.render(note) sleep(0.01) playername = gameControl.checkNames(tableView.player_names) # games with Shared_Board=True need player_index, first must insure that server is reporting correct name. # This can take a few cycles. if clientState.rules.Shared_Board: clientState.player_index = -99 while clientState.player_index == -99: try: clientState.player_index = tableView.player_names.index( playername) except Exception as err: note = "{0} waiting for name in player_names to update...".format( err) gameboard.refresh() connection.Pump() gameControl.Pump() tableView.Pump() tableView.playerByPlayer(current_round) gameboard.render(note) sleep(0.001) while True: # Primary game loop. this_round = handView.round_index gameboard.refresh() handView.nextEvent() connection.Pump() gameControl.Pump() tableView.Pump() tableView.playerByPlayer(this_round) if clientState.rules.Shared_Board: player_index = tableView.player_names.index(playername) visible_scards = tableView.visible_scards handView.update(player_index, len(tableView.player_names), visible_scards) else: handView.update() note = gameControl.note gameboard.render(note) sleep(0.001)
def testPlayCards(self): """Confirm playCards transfers cards from hand to visible""" test_state = ClientState(ruleset=None) hand = [ Card(1, 'Spades'), Card(2, 'Clubs'), Card(3, 'Diamonds'), Card(4, 'Hearts'), Card(4, 'Spades'), Card(0, None) ] test_state.newCards(hand) self.assertEqual(test_state.hand_cards, hand) #Confirm can't play cards we don't have (even if we have some) with self.assertRaises(Exception): test_state.playCards( {1: [Card(1, 'Spades'), Card(1, 'Spades'), Card(0, None)]}) #Confirm failed play didn't edit hand self.assertEqual(test_state.hand_cards, hand) #Confirm can't play illegal move with self.assertRaises(Exception): test_state.playCards({1: [Card(1, 'Spades')]}) #Confirm failed play didn't edit hand self.assertEqual(test_state.hand_cards, hand) #Confirm legal play is allowed and edits played cards and hand properly test_state.newCards([Card(1, 'Spades'), Card(0, None)]) test_state.playCards( {1: [Card(1, 'Spades'), Card(1, 'Spades'), Card(0, None)]}) self.assertEqual( test_state.played_cards, {1: [Card(1, 'Spades'), Card(1, 'Spades'), Card(0, None)]}) hand.remove(Card(1, 'Spades')) self.assertEqual(test_state.hand_cards, hand) #Confirm second play adds to the played cards properly test_state.playCards( {4: [Card(4, 'Hearts'), Card(4, 'Spades'), Card(2, 'Clubs')]}) self.assertEqual( test_state.played_cards, { 1: [Card(1, 'Spades'), Card(1, 'Spades'), Card(0, None)], 4: [Card(4, 'Hearts'), Card(4, 'Spades'), Card(2, 'Clubs')] }) hand.remove(Card(4, 'Hearts')) hand.remove(Card(4, 'Spades')) hand.remove(Card(2, 'Clubs')) self.assertEqual( test_state.hand_cards, [Card(3, 'Diamonds'), Card(0, None)])