How can I allow users to access variables within a function? I'm working on a python implementation of FreeCell. Cards, stacks of cards, etc are all objects that are initialized as variables by the program. I'm trying to make a function that asks a user a source stack and destination stack, checks if that's a legal move and if so, moves the top card from the source to the destination. Here's a snippet of the code: http://pastebin.com/ajUM81RZ
The issue is how to turn user input into a function call. Of course there are other ways to skin the same cat - I could build up a tedious logic system, I could have the user input a card name and search through available cards to find the card name - but the most straightforward way, it seems to me, would be to simply prompt for the variable name of the source and destination stack, and then call the move function with the user input as variables. But I don't know how to do this in python. In a somewhat parallel similar problem, so I might as well ask it here - I don't know how to make functions that generate arbitrary numbers of variables with arbitrary names. In other words, how do you make a program make variables instead of you having to define every one of them yourself? Defining everything manually is fine for small numbers of variables, but for example in this program I had to manually type in the information for 52 individual card objects. Blech.
Can you link to the pastebin of how you defined your variables?
Okay. Do you know what arrays are?
I'm aware of them; that's why I added numpy. But I haven't really used them at all.
Here's what you want to do: - keep a variable to count what card you're currently assigning, start at 0 - declare an array of size 52 (one spot for each card). - have a loop within a loop; one loop will iterate through the values 1 to 13 and the other through the suits (C, H, S, D) - inside the innermost for loop, do the assignment with whatever the loops are at right now - the index will be whatever the counter is at - after it is assigned add one to the counter Sounds complicated, but it's really only a few lines. I don't know python that well, but I could give you can example in java if you want. It wouldn't be hard for you to translate it to python. Once you have this, accessing the variables given user input will be easy. You just need to know what index you want to access.
Oo, wait. I forgot something, in python they use lists a lot right? You can probably just use that instead of an array.
Yeah perhaps you could give me an example in Java, because I'm not quite sure what you mean. Is this to address my first question (user accessing variables) or my second (declaring variables automatically)?
I think if you have an array the first part will be a simple fix.
http://ideone.com/HsFe6 This probably looks confusing. I'll look at some python docs and write it real quick in python.
Well if I understand you correctly, the purpose of the array would be so that a user could choose a given card based on its position in the array (ex, if you wanted to move the queen of hearts, you'd type H12 or something). But the issue isn't the user selecting the card - If that were my goal, I could have the user input a string and just test each available card to see if its string value matched what the user inputted. What I want to do is choose a source list and a destination list. And like I said, there are other ways to do it, but what I want to know is, is it possible to take a user input and turn it into a variable name as input to a function? Maybe I'm not making much sense...
It isn't that you're not making sense, I don't understand your approach (I don't know the game either). If I understand things correctly, there are piles of cards. Each pile has a top card. The use chooses a source and destination pile, and you want to access those cards and swap them? You can do this with an array.
If you want the user to input H12 and access the variable named H12, what you could do is turn H12 into the index number, say H12 is the 38th element, and feed the number 38 into the function. Then the function just accesses that element for the same effect.
I believe I understand your code: it will output an array where each element is a string, eg ['1 of Spades', '1 of Hearts',...,'13 of Diamonds']. I understand how to create these strings. The key is, a card isn't just a string. It's an object with data, namely the number and the suit. Strings alone won't give my program enough information to smoothly operate (technically I could parse the data from those strings to access that information, but that's unwieldy). I need to create 52 card objects, which can be passed around freely. But hmm, on second thought, maybe actually a similar loop could work, just instead of making strings it makes cards... maybe I'm thinking about this in a bit of the wrong way... (goes off to think)
That was my intention. Didn't think it was necessary to include your code for the card object. Also, did some research and your original goal is possible: user input to variable name. Wrote this: http://ideone.com/clmEN Although, I wouldn't use that if I were you. Arrays/lists would be better imo.
Actually I realized that the variable name for the card is completely irrelevant - I can simply write a loop that just generates a bunch of card objects, and it really doesn't matter what their variable names are. I've got a bit more thinking to do, but I believe I'm on the way here. Thanks a bunch!
And I just remembered, python has dictionarys (I think they're called). Look those up, they can act like arrays except you won't even have to transform the input to a number.
yeah, the dictionaries are great, but I'm not sure they're especially useful in this case, or at least they aren't required. But thanks
They would have let you access the variable with the same name as the input (your original goal)
Join our real-time social learning platform and learn together with your friends!