-
-
Save mjhea0/0a6b0bb6cc7557776ab8 to your computer and use it in GitHub Desktop.
import random | |
def base_strategy(): | |
return random.randrange(1, 10) | |
def computer_strategy(scores, player_answers, computer_answers, | |
total_iterations, current_iteration): | |
# ADD YOUR SOLUTION HERE | |
def compute_score(player_answer, computer_answer, scores): | |
if player_answer + 1 == computer_answer: | |
scores['computer'] += 2 | |
elif computer_answer + 1 == player_answer: | |
scores['player'] += 2 | |
elif player_answer < computer_answer: | |
scores['player'] += 1 | |
elif player_answer > computer_answer: | |
scores['computer'] += 1 | |
return scores | |
def calculate_winner(scores, outcomes): | |
if scores['player'] > scores['computer']: | |
outcomes['player'] += 1 | |
elif scores['computer'] > scores['player']: | |
outcomes['computer'] += 1 | |
else: | |
outcomes['ties'] += 1 | |
return outcomes | |
# run! | |
if __name__ == '__main__': | |
iterations = 100 | |
scores = {'player': 0, 'computer': 0} | |
outcomes = {'player': 0, 'computer': 0, 'ties': 0} | |
player_nums = [] | |
computer_nums = [] | |
for x in range(0, iterations): | |
while scores['player'] < 5 and scores['computer'] < 5: | |
player_answer = base_strategy() | |
computer_answer = computer_strategy( | |
scores, player_nums, computer_nums, iterations, x) | |
player_nums.append(player_answer) | |
computer_nums.append(computer_answer) | |
compute_score(player_answer, computer_answer, scores) | |
calculate_winner(scores, outcomes) | |
scores = {'player': 0, 'computer': 0} | |
player_nums = [] | |
computer_nums = [] | |
print('Results - Player {0}, Computer {1}, Tie {2}'.format( | |
outcomes['player'], outcomes['computer'], outcomes['ties'])) |
@idefux updated - thanks!
The rules says "Knowing ... all your opponent’s previous numbers". As I understand this condition, player_strategy
must be player_strategy(scores, player_answers, computer_answers, total_iterations, current_iteration)
@Amarchuck - yes, you are correct. You are creating the computer strategy, so you need to add your code here:
def computer_strategy(scores, player_answers, computer_answers,
total_iterations, current_iteration):
# ADD YOUR SOLUTION HERE
The main should check if the computer_answer is valid. Otherwise one could just play -1 or 0.5 and always win.
@danielvatanabe rule clearly says, it can be any integer 1 or greater.
I believe line 44 is buggy while scores['player'] <= 5 and scores['computer'] <= 5:
. I think as soon as a player win 5 points, the loop should end. But due to the above condition, the loop would not break even a player have 5 points. It will give another chance to the loser. The condition will still be true even both player have 5 points. The condition will be only false, when one user win 6 points.
So, I think the condition should be:
while True: # instead of "while scores['player'] <= 5 and scores['computer'] <= 5:"
... # code first, condition at the end.
if scores['player'] == 5 or scores['computer'] == 5:
break
@mahanmarwat not "== 5" but ">= 5". One could go from 4 to 6 and skip the conditional branch.
@MAGNUMpt Yes, you are right.
I think there will never be tie games even both players have the exactly same strategy given the game runner logic.
I believe there is a bug in line 46. Parameter ordering is wrong. See here for a fixed version: https://gist.github.com/Idefux/f89353a3983cf746d31c