Hire Experts For Answers
Order NowRelated Study Services
- Homework Answers
- Coursework writing help
- Term paper writing help
- Writing Help
- Paper Writing Help
- Research paper help
- Thesis Help
- Dissertation Help
- Case study writing service
- Capstone Project Writing Help
- Lab report Writing
- Take my online class
- Take my online exam
- Do my test for me
- Do my homework
- Do my math homework
- Online Assignment Help
- Do my assignment
- Essay Writing Help
- Write my college essay
- Write my essay for me
DESCRIPTION
Posted
Modified
Viewed
17
We need to finish a python project, its a card game and we lack of time. We have 13 hours left and it is difficult for us to finish it. We start with Python this year
This order does not have tags, yet.
Attachments
hw1_card_game_v2_toStudent.py
#@title
# HW1 : Card Game v2
# ID Firstname Lastname
import time
import random
def generate_deck(n_cards, n_shuffles):
print('Shuffle', end='')
deck = ''
for suit in 'CDHS':
for face in 'A23456789TJQK':
deck += '|' + face + suit + '|'
deck = cut(deck, random.randint(0, n_cards))
deck = shuffle(deck)
time.sleep(0.1)
print('.', end='')
print()
return deck[:4*n_cards]
def play(n_cards, n_in_hand):
print('Start a card game with', n_cards, 'cards.')
deck = generate_deck(n_cards, 20)
p1, deck = deal_n_cards(deck, n_in_hand)
p2, deck = deal_n_cards(deck, n_in_hand)
players = [p1, p2]
table_cards, deck = deal_n_cards(deck, 1)
fail = False
turn = 0
while True:
show_game_status(deck, table_cards)
show_table_cards(table_cards)
show_player_cards(players[turn], turn+1)
k = select_card_number(players[turn])
n_player_cards = len(players[turn])//4
valid = (k != 0)
if valid:
cards = players[turn]
card = peek_kth_card(cards, k)
print(' Playing :', card)
valid_left = eq_suit_or_value(card, table_cards[:4])
valid_right = eq_suit_or_value(card, table_cards[-4:])
valid = (valid_left or valid_right)
if valid:
if valid_right:
table_cards += card
else: # valid_left:
table_cards = card + table_cards
players[turn] = remove_kth_card(cards, k)
fail = False
if not valid:
print(' ** Invalid **')
if len(deck) == 0:
if fail: break
fail = True
if len(deck) > 0:
print(' >> get a new card')
card, deck = deal_n_cards(deck, 1)
players[turn] = card + players[turn]
show_player_cards(players[turn], turn + 1)
if len(players[turn]) == 0:
break
turn = (turn + 1) % len(players)
if len(deck) == 0:
print('\n** No more cards **')
print('*'*20)
if len(deck) == 0 and \
len(players[0]) == len(players[1]):
print('Draw!!!')
elif len(players[0]) < len(players[1]):
print('Player # 1 win!!!')
else:
print('Player # 2 win!!!')
def eq_suit_or_value(card1, card2):
return card1[1] == card2[1] or \
card1[2] == card2[2]
def show_game_status(deck, table):
print('-'*40)
print('Deck remaining: {} | On table : {}'.format(len(deck)//4, len(table)//4))
def show_player_cards(cards, k):
print(' Player #', k, ':', cards)
def input_int(prompt):
while True:
try:
return int(input(prompt))
except:
return 0
#pass
def select_card_number(player):
nc = len(player)//4
k = input_int(' Select card # (1-'+str(nc)+') : ')
if not (1 <= k <= nc):
k = nc
return k
#----------------------------------------------------------------------------------
# TODO:
# Complete the body of the function.
# If your program is correct in each, the game will be played until game over.
# If any of you code is wrong you will see the test messages about each of the
# testcase that the output is not as expected.
#----------------------------------------------------------------------------------
def peek_kth_card(cards, k):
# cards is a string storing cards (may be one card) e.g., "|2H||4S||TD||AC|"
# k is an integer indicating card number that is interested (leftmost is 1)
# TODO:
# assign the variable the_kth_card to store a string that represents
# the kth card in cards
# Example:
# cards stores "|2H||4S||TD||AC|", k is 2
# the_kth_card ---> "|4S|"
"""
>>> peek_kth_card("|2H||4S||TD||AC|", 1)
'|2H|'
>>> peek_kth_card("|2H||4S||TD||AC|", 2)
'|4S|'
>>> peek_kth_card("|2H||4S||TD||AC|", 3)
'|TD|'
>>> peek_kth_card("|2H||4S||TD||AC|", 4)
'|AC|'
"""
the_kth_card = '|**|'
return the_kth_card
#----------
def remove_kth_card(cards, k):
# cards is a string storing cards (may be one card) e.g., "|2H||4S||TD||AC|"
# k is an integer indicating card number that is interested (leftmost is 1)
# TODO:
# assign the variable new_card to be the same as cards but removing the kth card,
# the original cards remain unchanged
# Example:
# cards stores "|2H||4S||TD||AC|", k is 2
# new_cards ---> "|2H||TD||AC|"
"""
>>> remove_kth_card('|2H||4S||TD||AC|', 1)
'|4S||TD||AC|'
>>> remove_kth_card('|2H||4S||TD||AC|', 2)
'|2H||TD||AC|'
>>> remove_kth_card('|2H||4S||TD||AC|', 3)
'|2H||4S||AC|'
>>> remove_kth_card('|2H||4S||TD||AC|', 4)
'|2H||4S||TD|'
"""
new_cards = '|**|'
return new_cards
#----------
def deal_n_cards(deck, n):
# deck is a string storing cards (may be one card) e.g. "|2H||4S||TD||AC||AD||AS|"
# n is an integer indicates the number of card to be dealed from the deck (n leftmost cards)
# TODO: assign variable cards to store n leftmost cards from deck
# assign variable new_deck to be the same as deck which remove n leftmost cards
# Note that, n always <= number of cards in deck
# Example: deck stores "|2H||4S||TD||AC|", n stores 3
# cards ---> "|2H||4S||TD|"
# new_deck ---> "|AC|"
"""
>>> deal_n_cards('|2H||4S||TD||AC|', 1)
('|2H|', '|4S||TD||AC|')
>>> deal_n_cards('|2H||4S||TD||AC|', 3)
('|2H||4S||TD|', '|AC|')
>>> deal_n_cards('|2H||4S||TD||AC|', 4)
('|2H||4S||TD||AC|', '')
>>> deal_n_cards('|2H||4S||TD||AC|', 5)
('|2H||4S||TD||AC|', '')
"""
cards = ''
new_deck = ''
return cards, new_deck
#----------
def cut(deck, m):
# deck is a string storing cards, e.g. "|2H||4S||TD||AC||3H||4H||5H||6H||7H||8H|"
# m is an integer for m leftmost cards of deck (0 <= m <= number of cards in deck)
# TODO:
# assign variable new_deck to store string result from move m leftmost cards
# of deck append to the right of deck
# Example:
# deck stores "|2H||3H||4H||5H||6H|", m stores 3
# new_deck ---> "|5H||6H||2H||3H||4H|"
"""
>>> cut('|2H||3H||4H||5H||6H|', 1)
'|3H||4H||5H||6H||2H|'
>>> cut('|2H||3H||4H||5H||6H|', 3)
'|5H||6H||2H||3H||4H|'
>>> cut('|2H||3H||4H||5H||6H|', 4)
'|6H||2H||3H||4H||5H|'
"""
new_deck = '|**||**|'
return new_deck
#----------
def shuffle(deck):
# deck is a string storing cards, e.g. "|2H||4S||TD||AC||3H||4H||5H||6H||7H||8H|"
# TODO:
# assign variable new_deck to store cards taken from left half and right
# half of deck one card at a time
# Example: when the number of cards is odd
# deck stores "|2H||3H||4H||5H||JS||QS||KS|"
# new_deck ---> "|2H||JS||3H||QS||4H||KS||5H|"
# when the number of cards is even
# deck stores "|2H||3H||4H||5H||JS||QS||KS||AS|"
# new_deck ---> "|2H||JS||3H||QS||4H||KS||5H||AS|"
"""
>>> shuffle('|2H||3H||4H||5H||JS||QS||KS|')
'|2H||JS||3H||QS||4H||KS||5H|'
>>> shuffle('|2H||3H||4H||5H||JS||QS||KS||AS|')
'|2H||JS||3H||QS||4H||KS||5H||AS|'
"""
new_deck = '|**|'
return new_deck
#----------
def show_table_cards(cards):
# cards is a string storing cards (may be one card) e.g. "|2H||4S||TD||AC|"
# the maximum number of cards to be display on the table is 5
# TODO:
# If the table is large enough to display all the cards, show them all.
# When there are more cards to be displayed than the table,
# shows 2 leftmost, and 2 rightmost cards and show .... in the middle
# this function return nothing
"""
>>> show_table_cards("|2H|")
-----------
Table: |2H|
-----------
>>> show_table_cards("|2H||3H||4H||5H|")
-----------------------
Table: |2H||3H||4H||5H|
-----------------------
>>> show_table_cards("|2H||3H||4H||5H||6H|")
---------------------------
Table: |2H||3H||4H||5H||6H|
---------------------------
>>> show_table_cards("|2H||3H||4H||5H||6H||7H|")
---------------------------
Table: |2H||3H|....|6H||7H|
---------------------------
>>> show_table_cards("|2H||3H||4H||5H||6H||7H||8H|")
---------------------------
Table: |2H||3H|....|7H||8H|
---------------------------
"""
cards_to_show = '|$$|'
border = '-'*5
print(border)
print('Table:', cards_to_show)
print(border)
#-------------------------------------------------
if __name__ == "__main__":
import doctest
doctest.testmod()
#-------------------------------------------------
"""### Play the game"""
n_cards = 15
initial_hand_size = 5
play(n_cards, initial_hand_size)
__MACOSX/._hw1_card_game_v2_toStudent.py
PROGRAMMING ASSIGNMENT #1
CARD GAME
A deck of card has 52 cards combined in 4 suits, spades(ª), hearts(©), diamonds(¨), and clubs(§). Each
suit has 13 cards with number 2 to 10, J(ack), Q(ueen), K(ing), and A(ce).
This homework simulates a simple card game. To represent a card, we decide to use string begins with |,
follows by card's value, card's suit, and ends with |.
• card's value is a single alphabet: A, 2, 3, 4, 5, 6, 7, 8, 9, T (for ten), J, Q, and K
• card's suit is a single alphabet: S, H, D, C for spades, hearts, diamonds, and clubs respectively.
A string "|2H||4S||TD||AC|" represents 4 cards: 2 of hearts, 4 of spades, ten of diamonds, and ace of
clubs, respectively.
GAME RULES
PREPARING:
This is a two-player card game. The game starts with cutting and shuffling a deck for a number of times,
then deal 5 cards to both players, then put a card on the table face up.
PLAYING:
Player 1 and 2 take turn to play by choosing a valid card to put next to the card on the table.
• In a turn, a player selects a card to be played from his hand. If selecting a non-existing card, the
rightmost card in hand will be selected by default.
o A valid card is a card that has the same card's value or card's suit as the first or last card on
the table. When played, it will be added to the table. Then the turn is passed to the other
player. If the card can be played to either first or last card on the table, it will be played to
the last card.
o If the selected card is invalid, it will not be added to the table. The player must also deal a
card from the deck and pass his/her turn.
• The player who has no card left in hand first, wins.
• If all cards have been drawn from the deck and no card can be played, the player who has fewer
cards wins. If both players have the same number of cards, then the result is a draw.
TODO
Code template will be provided. You may see some statements that will be covered later in the course. Don’t
worry. You only have to complete the function in the space provided according to the given description,
using string, list, selection, and repetition.
Testcases are provided for all functions that you need to implement. If your program works correctly, you
should not see any test result from testcase. You will see only normal program outputs. If any function does
not work correctly, you will see the test result for “FAILED” testcase.
For example, the figure above shows the “FAILED” result when execute
peek_kth_card('|2H||4S||TD||AC|', 1).
The correct/expected result should be '|2H|', but your program’s result is '|**|'.
def peek_kth_card(cards, k):
# cards is a string storing cards (may be one card) e.g., "|2H||4S||TD||AC|"
# k is an integer indicating card number that is interested (leftmost is 1)
# TODO:
# assign the variable the_kth_card to store a string that represents
# the kth card in cards
# Example:
# cards stores "|2H||4S||TD||AC|", k is 2
# the_kth_card ---> "|4S|"
return the_kth_card
def remove_kth_card(cards, k):
# cards is a string storing cards (may be one card) e.g., "|2H||4S||TD||AC|"
# k is an integer indicating card number that is interested (leftmost is 1)
# TODO:
# assign the variable new_cards to be the same as cards but removing the kth card,
# the original cards remain unchanged
# Example:
# cards stores "|2H||4S||TD||AC|", k is 2
# new_cards ---> "|2H||TD||AC|"
return new_cards
def deal_n_cards(deck, n):
# deck is a string storing cards (may be one card) e.g. "|2H||4S||TD||AC||AD||AS|"
# n is an integer indicates the number of card deal from the deck (n leftmost cards)
# TODO:
# assign variable cards to store n leftmost cards from deck
# assign variable new_deck to be the same as deck which remove n leftmost cards
# Note that, n always <= number of cards in deck
# Example:
# Example:
# deck stores "|2H||4S||TD||AC|", n stores 3
# cards ---> "|2H||4S||TD|"
# new_deck ---> "|AC|"
return cards, new_deck
def cut(deck, m):
# deck is a string storing cards, e.g. "|2H||4S||TD||AC||3H||4H||5H||6H||7H||8H|"
# m is an integer for m leftmost cards of deck (0 <= m <= number of cards in deck)
# TODO:
# assign variable new_deck to store string result from move m leftmost cards
# of deck append to the right of deck
# Example:
# deck stores "|2H||3H||4H||5H||6H|", m stores 3
# new_deck ---> "|5H||6H||2H||3H||4H|"
return new_deck
def shuffle(deck):
# deck is a string storing cards, e.g. "|2H||4S||TD||AC||3H||4H||5H||6H||7H||8H|"
# TODO:
# assign variable new_deck to store cards taken from left half and right
# half of deck one card at a time
# Example: when the number of cards is odd
# deck stores "|2H||3H||4H||5H||JS||QS||KS|"
# new_deck ---> "|2H||JS||3H||QS||4H||KS||5H|"
# when the number of cards is even
# deck stores "|2H||3H||4H||5H||JS||QS||KS||AS|"
# new_deck ---> "|2H||JS||3H||QS||4H||KS||5H||AS|"
return new_deck
def show_table_cards(cards):
# cards is a string storing cards (may be one card) e.g. "|2H||4S||TD||AC|"
# the maximum number of cards to be display on the table is 5
# TODO:
# If the table is large enough to display all the cards, show them all.
# When there are more cards to be displayed than the table,
# shows 2 leftmost and 2 rightmost cards and show .... in the middle
# This function returns nothing
Example Output
show_table_cards("|2H|")
Note: number of cards < 5
-----------
Table: |2H|
-----------
show_table_cards("|2H||3H||4H||5H|")
Note: number of cards < 5
-----------------------
Table: |2H||3H||4H||5H|
-----------------------
show_table_cards("|2H||3H||4H||5H||6H|")
Note: number of cards == 5
---------------------------
Table: |2H||3H||4H||5H||6H|
---------------------------
show_table_cards("|2H||3H||4H||5H||6H||7H|")
Note: number of cards > 5
---------------------------
Table: |2H||3H|....|6H||7H|
---------------------------
show_table_cards("|2H||3H||4H||5H||6H||7H||8H|")
Note: number of cards > 5
---------------------------
Table: |2H||3H|....|7H||8H|
---------------------------
Click the link to watch the game demo.
https://youtu.be/WxbILEKkfKM
Explanations and Answers
0
No answers posted
Post your Answer - free or at a fee
NB: Post a homework question for free and get answers - free or paid homework help.
Get answers to: Python Card Game To Finish Today Help! or similar questions only at Tutlance.
Related Questions
- Econometrics Exam- Most Of The Topics Are From The Introductory Econometrics Textbook By Jefferey Wooldridge.
- Need A Help For Microeconomics
- Doing An Online Test In Micreconomics. I'm In Need Of Help Asap
- Econ 1010 Microeconomics Test 2
- Online Exam Assistance With Macroeconomics
- Econometrics: Time Series And Panel Data
- International Economics Home Exam 3H
- Demand And Supply In The Housing Market - Suppose The Demand Function For Squared Meters Of Housing In Oslo Is A Standar
- Behavior Of Consumers In The Market For Water - The Residents Of Norway Consume Water (W ) And Spend The Rest Of Their
- Production Of Taxi Rides - Globaltaxi Runs Taxi Services Around The Globe. Globaltaxi Uses Two Inputs In Production
- Pricing In The Electricity Market: Let Households Demand For Electricity Be Given By Q = 100-5P. The Price Of
- Covid-19 Measures In The Labor Market : The Covid-19 Pandemic Has Had Large Effects On The Economy
- Microeconomics L Exam: University Level
- Can Somebody Take My Microeconomics Exam
- Microeconomics 1 Online Little Quiz
- Econ Excel --------------------
- Will This Service Be Able To Get Me A B On This Test ?
- Econ 101, Microeconomics, Consumer Theory, Product Curves And Cost Minimization, Short-Run And Long-Run, Input Markets,
- 3 Python Questions To Be Answered
- R Coding, Along With Corresponding Questions And Graphs Inside R. By 11:00 Pm Eastern Time, The Time Keeps Getting Reset
- Engineering Economics
- What Is Meant By The Debt Crisis Of The Developing World? What Is Being Done To Help Resolve This Crisis?
- 3Rd Year Economics Courses
- Writing Assignment
- Economics Game Theory Final Exam
- Economics Timed Homework Assignment On The Topics Enclosed In The Macroeconomics Syllabus.
- Exam Question
- Economics - Trade And Theory
- Two Questions From Micro Exam
- Exc 35202 Microeconomics: Examination Question Paper - Take-Home Examination
- Economics/Micro. Expecting A B
- Macroeconomics
- Econometrics Assignment
- Ps30 Game Theory And Strategy With 20 Exam Questions
- Microeconomics Exam With Approx 5 Questions
- Micro
- Seeking Help With Microeconomics Test
- Economic Case Study( Payback Analysis And Sensitivity Analysis)
- Case Study
- Solution For Case Study Goats The Green Alternative A
- Assisting Me During 3 Hours For Game Theory (Master's Level) Problem Solving
- Econ 231 Online Final 50 Questions
- Econometrics Homework
- Microeconomic Theory Test
- Help Review The Answers Of A Test (Microeconomic Theory)
- Macro-Economics Professional Needed
- Homework Assignment
- Econometrics Statistics Lab Using R-Studio
- Econ 10A Exam
- Economics Online Exam