Skip to content

Instantly share code, notes, and snippets.

@mjhea0

mjhea0/runner.py Secret

Last active October 12, 2015 17:42
Show Gist options
  • Save mjhea0/0a6b0bb6cc7557776ab8 to your computer and use it in GitHub Desktop.
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']))
@ramesh2014
Copy link

@danielvatanabe rule clearly says, it can be any integer 1 or greater.

@mahanmarwat
Copy link

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

@rafaelvcaetano
Copy link

@mahanmarwat not "== 5" but ">= 5". One could go from 4 to 6 and skip the conditional branch.

@mahanmarwat
Copy link

@MAGNUMpt Yes, you are right.

@lijenpan
Copy link

I think there will never be tie games even both players have the exactly same strategy given the game runner logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment