## How do I create a number of variables in a programme WHILE it is running? - python

### Calculating a game's high score table

```I need to create a function/method ( in python) which calculates a high score "leaderboard". Each player will have played any number of rounds of the game, recieving a score for each round. I want to know what's the best way to sort the top ranking players (accounting for score AND number of rounds played). The possible scores for each round are F, D-, D, D+, C-, C, C+, B-, B, B+, A-, and A.
Obviously a simple average won't work because it doesn't take into account number of rounds played. Whats the best way to set up a fair sorting function?
EDIT: I've been reading some of the really great answers here and I want to try to clear up my question a bit. I want both the players score AND the number of rounds they've played to count towards their ranking in a way that's fair. Meaning a player with 20 B's should be of a higher rank than a player with 5 A's. Basically the high score should reflect general effort and skill, "the number of rounds played PLUS their score" means the higher their ranking should be.
EDIT 2: After reading the answers, I think the best way to do it is a simple total sum of the players points across all rounds. I'm not sure which answer to assign the green check to because you were all correct.
```
```There are many ways that you could do this. Try this for example, let F-A be 0-11 (you can make your own; however try to take difficulty into account), so each score is one higher than the previous. For every game you play, you receive a score (from 0-11). Create a total score and add the game score every time to the total score. That way, if a person receives 7 A's, that's 77, while a person that receives 7 A-'s gets a score of 70, then simply sort them accordingly. Each function has its drawbacks of course. This function is not the "best", consider getting 20 B's would exceed 7 A's even though, 7 A's is a much better score. if you can give me more details about how you want to rank them, then it will be much easier to get the algorithm down.
```
```What you are asking is essentially how we define "good" players and it's not an easy problem. As you mentioned, a simple average score or picking-the-highest-score will not be an ideal answer depending on your game design.
I'd like to recommend that you read about ELO rating system for Chess and other modified versions of it before you design your own player rating system.
One simple and possible way is you can set a window (like 10 most recent games) and use average score from the window. Players who play less games than this window would be "in placement" state. Again, it's not an easy problem and heavily depends on what your game is. Good Luck!
[UPDATE]
I assumed that your game is player vs. player. If not, this is another story. Most games just keep the highest score no matter how many times you play the game and that's going to be your entry in the leaderboard. Since you don't say anything about your game, I have no idea why it wouldn't be fair. As I mentioned earlier, you could set a window for avg. score or the highest score. You can even reset your leaderboard every month or remove players who haven't played for a week. It all depends on your game and what you want. Please remember that no matter what you do, make it sure that the rules are crystal clear for players otherwise they would be easily upset and frustrated.```

### Use already standardized data in different columns to make a new variable

```I have the data set already standardized.
Specifically, I want to get a winning rate using a number of winning and number of playing.
both are already standardized...
I know just division can't work.
What can I do for making a new variable, winning rate using a standardized number of winning and number of playing?```

### what should I modify?

`How would you modify the printer simulation to reflect a larger number of students? Suppose that the number of students was doubled. You make need to make some reasonable assumptions about how this simulation was put together but what would you change? Modify the code. Also suppose that the length of the average print task was cut in half. Change the code to reflect that change. Finally How would you parameterize the number of students, rather than changing the code we would like to make the number of students a parameter of the simulation.`

### how to randomise the image shown in a sketchpad

```I really appreciate any responses. I am thoroughly confused and never knew this experiment design/builder software was so complicated!
I am a fast learner, but still a newbie so please be patient.
Yes I have googled the answers for my question but no answers to similar questions seem to work.
I have an experiment, there are 8 conditions,
each condition needs to show 1 image, chosen at random from a folder of similar images.
Each trial needs to be different (so each participant sits a differently ordered set of conditions) AND each condition's selected image will be different.
So;
Condition- Image
A - 1 to 140
B - 1 to 80
etc..
Recording data is not a problem as this can be done by hand, but I just need the images to be randomly selected from a pre-defined group.
I have tried using code to randomise and shuffle the order, but have got nowhere.
Tom
```
```To generate retrieve the path of a random image in a folder img_folder, it's not too difficult. You can use img_path_array = os.listdir('.../img_folder'). Randomly generate an integer between 0 and len(img_path_array) using random_index = randrange(len(img_path_array)) (import random to use this function), and gain access to the random file url by calling img_path_array[random_index].
```
```You have two things to do: randomize the order of a pool of 8 things some number of times, and make sure that none of the permutations are chosen more than once.
I'll start by pointing out that hopefully you have fewer than factorial(8) participants, because that is the maximum number of unique arrangements.
Here is the brute-force approach which generates all of the possible arrangements of your input list (images), then shuffles it; you can then just take the first n arrangements which are guaranteed to be unique.
import itertools
import random
#images is a list of your images
orderings = list(itertools.permutations(images))
random.shuffle(orderings)
#now just take top n elements of orderings
Now I'll again point out that this is fine for when you have 8 conditions; 8! is only 40320, which is fine to keep in memory. For basically anything larger than 10 conditions you're going to want to take a more sophisticated approach.```

### Where to hold static information for game logic?

```The context for this question is:
A Google App Engine backend for a two-person multiplayer turn-based card game
The game revolves around different combinations of cards giving rise to different scores in the game
Obviously, one would store the state of a game in the GAE datastore, but I'm not sure on the approach for the design of the game logic itself. It seems I might have two choices:
Store entries in the datastore with a key that is a sorted list of the valid combinations of cards that can be player. These will then map to the score values. When a player tries to play a combination of cards, the server-side python will sort the combination appropriately and lookup the key. If it succeeds, it can do the necessary updates for the score, if it fails then the combination wasn't valid.
Store the valid combinations as a python dictionary written into the server-side code and perform the same lookups as above to test the validity/get the score but without a trip to the datastore.
From a cost point of view (datastore lookups aren't free), option 2 seems like it would be better. But then there is the performance of the instance itself - will the startup time, processing time, memory usage start to tip me into greater expense?
There's also the code maintanence issue of constructing that Python dictionary, but I can bash together some scripts to help me write the code for that on the infrequently occasions that the logic changes. I think there will be on the order of 1000 card combinations (that can produce a score) of between 2 and 6 cards if that helps anyone who wants to quantify the problem.
I'm starting out with this design, and the summary of the above is whether it is sensible to store the static logic of this kind of game in the datastore, or simply keep it as part of the CPU bound logic? What are the pros and cons of both approaches?
```
```So just for comparison to the information above:
In a standard deck of cards there are 52 unique cards. With 5 cards in a hand, there are 2,598,960 unique hands possible to get dealt.
Here's a break down of the combinatorial math:
n = 52 cards total
r = 5 cards in hand
number of combinations = n! / (r! - (n-r)!)
= 52! / (5! * 47!)
= (52 * 51 * 50 * 49 * 48) / (5 * 4 * 3 * 2 * 1)
= 2,598,960
And to simplify the example a bit more, lets compare the numbers to 5 card stud poker. Poker has 9 different types of "scoring" hands (Royal Flush, Straight Flush, Four of a kind, Full House, Flush, Straight, Three of a kind, Two Pairs, One Pair).
The odds that you have nothing in your hand are 50.11%
The odds that you have any of the above combinations is 49.89%.
The number of possible hand combinations in 5 card stud is huge, there are only 4 suits of cards, 13 numbers on the cards, and 9 different "scoring" combination type
I hope that this example clearly illustrates that it would be a huge undertaking to generate and store all of the possible "scoring" combinations of 5 card stud in a database.
What this means to you:
Since I do not know the rules of your game, the main thing you need to consider is the number of different "scoring" combination types.
You didn't really specify how unique / different your possible scoring combinations are in your game. The closer the number of combination types is to the number of combinations, the more custom "scoring" rules there are.
Using the 52 card deck example above, if each unique hand had it's own unique score You would quickly have upwards of 3 million database entries. In which case I would heavily suggest you get many databases that support a MapReduce query capability (e.g. Cassandra + Hadoop) that would allow you to easily scale your infrastructure to reduce query times. I would imagine that having 3 million unique scoring combinations is very unlikely though. That would make the rules of the game tremendously complex, and probably make your game unplayable.
Since you said that your game will have around 1000 hand combinations of 2-6 cards, let's simplify the example and get a ballpark number. Using the largest hand possible (6 cards in hand), there are 3,003 possible hands in a 12 card deck. Assuming that number of different combination types, suits, and numbered cards scales evenly (there is some fantasy math in here), you are looking at having around ~1,500 "scoring" combinations.
The bottom line:
The application logic needed to "score" winning hands for your game is the very same logic that the players of your game will need to understand in order to play (assuming that this game requires any skill / understanding at all, and it does not purely rely on luck). The more complicated it is, the harder the game will be for the players. I can only assume that the game logic isn't that complicated.
I would find it unlikely that you only have 16 cards in your deck. It would seem reasonable to have a couple hundred cards with several grouping types used for uniqueness (e.g. suit on a poker card, or mana type on a Magic the Gathering card). Assuming that you have more cards and combination types than a basic poker game, then it would seem very reasonable to conclude that it would take far more effort to store the various combinations than to include the logic of your game in the code. Also, every time you add a new rule your storage requirements would jump up in orders of magnitude, rather than scale linearly.
Since you will inevitably develop the code necessary to implement the rules of the card game, you might as well see how long it takes to "score" an arbitrary hand of cards. I would caution against premature optimization, and suggest that you prototype your design.
I would suggest you have configurable logic module that allows you to easily alter the rules of your game as needed. once the rules are solidified and unlikely to change, then I would look at optimizing your application code as needed. From the maintainability perspective, storing all of your application logic in a database is nuts (I think most maintenance programmers would agree with me on this). After you try to "fix" (e.g. normalize, migrate, transform) the data generated by a few scripts you just "bashed together" (your words) you will end up bashing your head into your keyboard.
As far as GAE pricing is concerned, the number of instances you will need will be based on your users / demand. Generally the limiting factor on scaling systems is Disk IOPS not the CPU. In the long run, I bet you would take a much bigger hit on performance as well as pricing by storing all of your application logic in a database.
Sources:
1) A Combinatorial Calculator: http://www.calculatorsoup.com/calculators/discretemathematics/combinations.php
B) Poker Odds: http://www.durangobill.com/Poker.html
```
`If the logic is fixed, keep it in your code. Maybe you can procedurally generate the dicts on startup. If there is a dynamic component to the logic (something you want to update frequently), a data store might be a better bet, but it sounds like that's not applicable here. Unless the number of combinations runs over the millions, and you'd want to trade speed in favour of a lower memory footprint, stick with putting it in the application itself.`