MTG Deck Builder Source Code 5/25
C:\Home
Here is the first iteration of my deck builder source code. My goal here is not to show you proper code, or even good code.
Rather, I want to demonstate working code. At this point, my program allows the user to create a new deck, name it, and designate a commander.
Finally, the user is able to enter cards in one at a time, verify the card information is correct, and submit the card to the decklist.
Each time a card is successfully added, the decklist text file is also updated.
My next goal will be to check that all input for a card is valid before proceeding.
Name must be a string, cost must be an int between 0-16, color must be a combination of WUBRG, and category must be ramp, draw, or removal.
#function to create a card.
def create_card():
name = input("name? ")
cost = input("cost? ")
color = input("color? ")
category = input("category? ")
thisdict = {
"name": name,
"cost": cost,
"color": color,
"category": category
}
return thisdict
#initialize the deck variable, which will either be created or loaded from an existing file.
deck = []
#First input, asking user if they want to create a new deck, or load an existing deck
print("Would you like to create a new deck or load an existing deck?")
loadDeck = input("type new or load \n")
if loadDeck == "new":
#Creates a new deck name and gets the name of the commander.
#Commander is not factored into deck construction.
#As long as there's no textfile with the same deckName, create the new file to eventually store the deck.
deckName = input("What is the name of your new deck? \n")
deckCommander = input("What is the name of your new deck's Commander? \n")
try:
f = open(deckName + ".txt", "x")
input("Great!")
except:
print ("Failed to create deck")
#Ask user for 60 cards to add to their deck.
i = 0
while i != 60:
new_card = create_card()
print(new_card)
#Verify card entry is correct before adding. If card is accepted, write new card to deck txt file.
check = input("Is this card info correct? Type yes or no. ")
if check == "yes":
deck.append(new_card)
file_path = deckName + ".txt"
with open(file_path, "w") as file:
for item in deck:
file.write(str(item) + "\n")
i += 1
print (str(60-i) + " cards left")
else:
print("Please reenter card data")
elif loadDeck == "load":
print("Load Deck")
else:
input("Invalid input")
quit