Skip to content

Instantly share code, notes, and snippets.

@mjhea0 mjhea0/runner.py Secret

Last active Oct 12, 2015
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

idefux commented Sep 21, 2015

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

@mjhea0

This comment has been minimized.

Copy link
Owner Author

mjhea0 commented Sep 21, 2015

@idefux updated - thanks!

@Amarchuk

This comment has been minimized.

Copy link

Amarchuk commented Sep 22, 2015

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)

@mjhea0

This comment has been minimized.

Copy link
Owner Author

mjhea0 commented Sep 22, 2015

@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
@danielvatanabe

This comment has been minimized.

Copy link

danielvatanabe commented Sep 22, 2015

The main should check if the computer_answer is valid. Otherwise one could just play -1 or 0.5 and always win.

@ramesh2014

This comment has been minimized.

Copy link

ramesh2014 commented Sep 23, 2015

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

@mahanmarwat

This comment has been minimized.

Copy link

mahanmarwat commented Sep 23, 2015

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

This comment has been minimized.

Copy link

rafaelvcaetano commented Sep 23, 2015

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

@mahanmarwat

This comment has been minimized.

Copy link

mahanmarwat commented Sep 26, 2015

@MAGNUMpt Yes, you are right.

@lijenpan

This comment has been minimized.

Copy link

lijenpan commented Oct 12, 2015

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
You can’t perform that action at this time.