Introduction:
Wasn’t yesterday Sunday?
What do you do to relax at home?
Weekend shopping, go out to see a movie…… This is your weekend.
Programmer’s weekend is lying corpse tang Shi lying corpse at home, occasionally work overtime work overtime, or follow a few friends at home pastime time dozen dozen mahjong, poker to play once!
Especially put long vacation [PS: there is no holiday, long vacation is the Chinese New Year] in my hometown, relatives in particular, seven aunts and eight aunts of a year very not easy to get together once, playing mahjong and poker this is a common thing, contact feelings this is the fastest way ~
Speaking of playing poker, we often is to play 244, fried golden flowers, three dozen a that is called what name again, let me think……
**** words true word poor, we that is dialect sa, I translate not to come over mandarin is call what, I estimate 240 you also did not understand is what, 23333~
Today, xiaobian is to take you to do a blackjack poker game!
Have big guy can optimize this code, do a delicate luxurious interface is good ~~
The body of the
Game rules: Blackjack, also known as black jack, is a game played by 2 to 6 players, using 52 cards except king and king. The player’s goal is to make the total number of cards in the hand not exceed 21 points and as large as possible. When playing a deck, one of each of the following cards (no Kings) :
(1) Initialize the number of players:
Def iniGame(): global playerCount, cards while(True): try: playerCount = int(input(' input: ') except ValueError: Print (' Invalid input! ') continue if playerCount < 2: print(' Player must be greater than 1! ') Continue else: break try: RPC = int(input(' Input number of cards: default to the number of players ')) except ValueError: print(' Default value already used! ') Deck = playerCount Print (' playerCount, ', The number of decks: ', RPC) Cards = getCards(RPC) # DeckCopy the code
(2)Player lists are built, and the computer plays against the player.
def createPlayerList(): global playerList playerList = [] for i in range(playerCount): playerList += [{'id': '', 'cards': [], 'score': 0}]. Copy () playerList[I]['id'] = 'computer' + STR (I +1) playerList[playerCount-1]['id'] = 'player' random.shuffle(playerList) # Rank each player randomlyCopy the code
(3)The beginning will set up 2 card play can see points.
Def gameStart(): print for I in range(playerCount): def gameStart(): print for I in range(playerCount): # deal(playerList[I]['cards'], cards, Print (playerList[I]['id'], print(playerList[I]['id'], print(I]['id'], GetCardName (playerList[I]['score']), 'score', playerList[I]['score']) time.sleep(1.5)Copy the code
(4) The game gives the computer and the player a third dark card in turn, which is invisible to others.
Def gamePlay(): for I in range(playerCount): print(' current ', playerList[I]['id']) if playerList[I]['id'] == 'playerList ': While (True): print(cardName (playerList[I]['cards']) _isDeal = input(' cards') (y/n) ') if _isDeal == 'y': Deal (playerList[I]['cards'], cards) print(' new card: ', getCardName(playerList[I]['cards'][-1])) # PlayerList [I]['score'] = getScore(playerList[I]['cards']) elif _isDeal == 'n': break else: print( While (True): if isDeal(playerList[I]['score']) == 1: Deal (playerList[I]['cards'], cards) print(' cards') PlayerList [I]['score'] = getScore(playerList[I]['cards']) else: print(' cards') ') break time. Sleep (1.5)Copy the code
(5) Random shuffle:
def getCards(decksNum): cardsList = ['Aa', 'Ab', 'Ac', 'Ad', 'Ka', 'Kb', 'Kc', 'Kd', 'Qa', 'Qb', 'Qc', 'Qd', 'Ja', 'Jb', 'Jc', 'Jd', '0a', '0b', '0c', '0d', '9a', '9b', '9c', '9d', '8a', '8b', '8c', '8d', '7a', '7b', '7c', '7d', '6a', '6b', '6c', '6d', '5a', '5b', '5c', '5d', '4a', '4b', '4c', '4d', '3a', '3b', '3c', '3d', '2a', '2b', '2c', '2d'] cardsList *= decksNum # shuffle(cardsList) # return cardsListCopy the code
(6)Set the brand dictionary:
CardNameDict = {' Aa ':' spades', 'Ab' : 'hearts A', 'Ac' : 'the plum blossom A', 'Ad' : 'party A', 'Ka' : 'spades K', 'Kb' : 'red peach K', 'Kc' : 'plum K', 'Kd: Qa ' 'party piece of K', ':' spades Q ', 'Qb' : 'red peach Q', 'Qc' : 'plum Q', 'Qd' : 'party piece of Q', 'Ja' : 'spades J', 'Jb:' red peach J ', 'Jc:' PAM ', 'Jd:' party piece of J ', '0 a' : 'spades 10', '0' b: 'red peach 10', '0 c' : 'the plum flower 10', '0 d' : 'party piece of 10', 'a' : 'spades 9', 'b' : 'red peach 9', 'c' : 'the plum blossom 9', '9 d' : 'party piece of 9', '8 a: 'spade 8', '8' b: 'red peach, 8', '8 c' : 'the plum blossom 8', '8 d' : 'party piece of 8', '7 a' : 'spades, 7', 7 'b' : 'hearts, 7', '7 c' : 'the plum blossom 7', '7 d' : '7', '6 a' : 'spades 6', '6 b: Red peach '6', '6 c' : 'the plum blossom 6', 6 'd' : 'party piece of 6', 'a' : 'spades 5', '5 b:' red peach 5 ', '5 c' : 'plum blossom 5', '5 d' : 'party piece of 5', 'a' : 'spades 4', '4 b' : 'hearts 4', '4 c' : 'plum 4', '4 d' : 'pieces of 4', 'aaa' : 'spades 3', '3' b: 'red peach 3', '3 c' : 'the plum blossom 3', '3 d' : 'pieces of 3', '2 a' : 'spades 2', '2 b' : 'red peach 2', '2 c' : 'plum 2', '2 d' : 'party piece of 2'}Copy the code
(7) Judging the outcome:
def showWinAndLose(): loserList = [] # [['id', score], ['id', score], ...] winnerList = [] # [['id', score], ['id', score], ...] winnerCount = 0 loserCount = 0 for i in range(playerCount): if playerList[i]['score'] > 21: Loserlist.append ([playerList[I]['id'], playerList[I]['score']]) else: WinnerList. Append ([playerList[I]['id'], playerList[I]['score']]) if len(winnerList) == 0: Print (' loserList ') for I in range(len(loserList)): Print (loserList[I][0], loserList[I][1]) elIf (len(loserList) == 0: print(loserList[I][0]) elif (len(loserList) == 0: X [1], reverse=True) # sort the list of winners by score value for I in range(len(winnerList)): = len(winnerList)-1: if winnerList[-i-1][1] == winnerList[-i-2][1]: loserCount = (i+2) else: if loserCount == 0: loserCount = 1 break else: loserCount = len(loserList) if loserCount == 1: loserList.append(winnerList.pop()) else: while(len(loserList) ! = loserCount): loserlist.append (winnerlist.pop ()) for I in range(len(winnerList)): = len(winnerList)-1: if winnerList[i][1] == winnerList[i+1][1]: winnerCount = (i+2) else: if winnerCount == 0: winnerCount = 1 break else: winnerCount = len(winnerList) while(len(winnerList) ! = winnerList.pop() print(' winnerList ') for I in range(len(winnerList)): Print (winnerList[I][0], winnerList[I][1]) print(' failure: ') for I in range(len(loserList)): Print (loserList[I][0], loserList[I][1]) else: print(loserList[I][0]) else: X [1], reverse=True) # sort the list of winners by the score value for I in range(len(winnerList)): = len(winnerList)-1: if winnerList[i][1] == winnerList[i+1][1]: winnerCount = (i+2) else: if winnerCount == 0: winnerCount = 1 break else: winnerCount = len(winnerList) while(len(winnerList) ! = winnerList.pop() print(' winnerList ') for I in range(len(winnerList)): Print (winnerList[I][0], winnerList[I][1]) print(' failure: ') for I in range(len(loserList)): print(loserList[i][0], loserList[i][1])Copy the code
Game effect: cough cough……. Feel this game to see luck also see courage!!
conclusion
Ha ha ha! Xiaobian game is relatively useless, you want to have a try? When bored, you can touch the fish and play soy sauce ~
Production is not easy, remember one key three even oh!! If you need this article complete code + image material.
Python novice installation package, free activation code, and more Python information is free!!