Reputation: 1
This is a pig dice game, where I use 2 strategies and the goal is to get to 63 points.
So I got a function play_games(n_games, strategy_a, strategy_b)
.(look bottom of the code)
This function has to play n_games, in this player A has to use strategy_a and player B has to use strategy_b (both arguments are strings). And the function must return a dictionary with keys 'A', 'B' and 'D', where the values says how often A and B have won and how many times it was a draw.
I have tried for two days now, and can't come up with anything, really want to learn this.
This is what I got so far:
from random import randint
def one_round(total, strategy):
round = 0
while True:
value = randint(1,6)
round = round + value
if Value == 1:
round = 0
break
if round + total >= 63:
break
if strategy == 'Sum13':
if round >= 13:
break
if strategy == 'Sum6':
if round >= 6:
break
return round
def one_game(strategy_a, strategy_b):
total_a = 0
total_b = 0
while True:
round_a = one_round(total_a, strategy_a)
round_b = one_round(total_b, strategy_b)
total_a += round_a
total_b += round_b
while total_a >= 63 or total_b >=63:
break
if total_a >= 63:
return 'A'
elif total_b >= 63:
return 'B'
elif total_a == total_b:
return 'D'
def play_games(n_games, strategy_a, strategy_b):
n_games = 100
for i in range(n_games):
Upvotes: 0
Views: 1461
Reputation: 86286
It should work now:
#I made one change in your original part.
from random import randint
def one_round(total, strategy):
round = 0
while True:
value = randint(1,6)
round = round + value
if value == 1:
round = 0
break
if round + total >= 63:
break
if strategy == 'Sum13':
if round >= 13:
break
if strategy == 'Sum6':
if round >= 6:
break
return round
def one_game(strategy_a, strategy_b):
total_a = 0
total_b = 0
while True:
round_a = one_round(total_a, strategy_a)
round_b = one_round(total_b, strategy_b)
total_a += round_a
total_b += round_b
if total_a >= 63 or total_b >=63: # while to if here
break
if total_a >= 63:
return 'A'
elif total_b >= 63:
return 'B'
elif total_a == total_b:
return 'D'
#The additional Part
from collections import defaultdict
def play_games(n_games, strategy_a, strategy_b):
dicto = defaultdict(int)
for i in xrange(n_games):
dicto[one_game(strategy_a, strategy_b)] += 1
return dicto
Result:
>>> play_games(1000,'sum6','sum13')
defaultdict(<type 'int'>, {'A': 495, 'B': 505})
I don't think that the design of the game allows 'D' to ever happen, so you might as well just drop it.
Upvotes: 1
Reputation: 91132
The simple way:
results = {}
for i in range(n_games):
winner = one_game(...)
if not winner in results:
results[winner] = 0
results[winner] += 1
The elegant way:
collections.Counter(one_game(...) for _ in range(n_games))
Another less elegant (but more versatile) way:
results = collections.defaultdict(lambda:0)
for i in range(n_games):
winner = one_game(...)
results[winner] += 1
Upvotes: 0
Reputation: 14873
Just to outline the general steps based on what you have so far, in the first line of play_games I would define your dict with something like:
resultsDict = {A:0, B:0, D:0}
The last line in play_games is of course
return resultsDict
And in your for loop, you would have something like:
resultsDict(one_game(stratA, stratB)) += 1 #increase the count for the victor or draw
Currently, your first line in play_games ets n_games, which doesn't make a lot of sense if you are passing in that value. You either want to just define it as a local value without passing it, or use the passed value. The second is probably the more general and better strategy, but it depends on your class.
Of course, then you need to actually call play_games somewhere with appropriate values for n_games, strategy_a and strategy_b. Unless this is meant to be a library called from somewhere else, in which case the "somewhere else" should call it. The way you could do both is to add a conditional call to play_games if this script is run directly, so that it will not automatically make the call if it is imported. This would look something like:
if __name__ == "__main__":
play_games(1000, 'Sum13', 'Sum6')
One thing I noticed is that you have no comments and no docstrings for your functions. As a general rule, I favor a literate programming style and prefer too many to too few comments. Since this is homework, whether that matters or not depends on how grading is done.
Edit: It occurred to me you could make your setup by just taking a hold value for the strategy instead of the highly specific Sum13 and Sum6 strategies currently. This would make it more versatile and make the code shorter at the same time.
Upvotes: 0