Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Winners, in no particular order...

def computer_strategy(scores, player_answers, computer_answers,
total_iterations, current_iteration):
v = [] # counter of values
expected = [] # expected values of scores
for i in range(10):
v.append(1)
expected.append(0)
for i in player_answers:
i = i - 1
if i < 0:
i = 0
if i > 9:
i = 9
v[i] = v[i] + 1
# compute expected value
# computer chooses i
# adversary chooses j
for i in range(10):
for j in range(10):
score = 1
if i + 1 == j:
score = -2
elif i - 1 == j:
score = 2
elif i == j:
score = 0
elif j < i:
score = -1
expected[i] = expected[i] + score * v[j]
best = 1
maxi = -10000000 # -INF
for i in range(10):
if expected[i] > maxi:
maxi = expected[i]
best = i + 1
return best
def computer_strategy(scores, player_answers, computer_answers,
total_iterations, current_iteration):
import random
offset = random.randrange(0, 2)
scale = random.randrange(0, player_answers[-1] if player_answers else 1)
avg = sum(player_answers) // (current_iteration + 1)
aggression = scores['player'] - scores['computer']
return 1 if aggression > scale else random.randrange(1, avg+2) + offset
def f2f_computer_strategy(player_answers):
import random
# Strategy begining
if not player_answers:
return random.randint(1, 3)
else:
X = dict()
P = dict()
for x in xrange(0, len(player_answers)):
r = player_answers[x]
if r not in X:
X[r] = 1
else:
X[r] += 1
iters = x + 1
for v in X:
P[v] = float(X[v])/float(iters)
major = float(0)
major_values = []
for (v, p) in P.items():
if major < p:
major = p
major_values = []
if major == p:
major_values.append(v)
posible_answers = []
for v in major_values:
if 1 == v:
posible_answers.append(2)
if 2 == v:
posible_answers.append(3)
if 2 < v:
try:
posible_answers += range(1, v-1)
posible_answers.append(v+1)
except MemoryError, e:
posible_answers.append(random.randint(1, 3))
proposal = random.choice(posible_answers)
# Strategy ending
return proposal
def computer_strategy(scores, player_answers, computer_answers,
total_iterations, current_iteration):
import random
# Computer's (my) strategy
mod_5 = current_iteration % 5
if (mod_5 == 0):
return random.randrange(1, 5)
elif (mod_5 == 1 or mod_5 == 3):
return 2
else:
return 3
def computer_strategy(scores, player_answers, computer_answers,
total_iterations, current_iteration):
import random
def pattern_matcher(their_plays, player_answers):
patterns = [
[0, 2, 3, 1], [0, 2, 3, 4, 1], [0, 3, 1, 2], [0, 4, 1, 2, 3]
]
if len(player_answers) > 2:
for pattern in patterns:
match_len = 1
i = 1
while i < len(player_answers) and \
player_answers[i - 1] < len(pattern) and \
pattern[player_answers[i - 1]] == player_answers[i]:
match_len += 1
i += 1
if match_len == len(player_answers):
their_plays[pattern[player_answers[-1]]] += \
len(player_answers) * 2
def get_best_play(their_plays, my_points_in, their_points_in):
def point_change(my_play, their_play):
if my_play == their_play + 1:
return 2
if my_play < their_play - 1:
return 1
return 0
def win_percentage(my_play, my_points, their_points):
top = 0
bot = 0
if my_points >= 5:
return 1
if their_points >= 5:
return 0
for i in range(1, 6):
if i == my_play:
continue
if their_plays[i] > 0:
top += their_plays[i] * \
win_percentage(my_play, my_points +
point_change(my_play, i), their_points +
point_change(i, my_play))
bot += their_plays[i]
if bot == 0:
return 0
return top / bot
def point_change_simple(my_play, their_play):
if my_play == their_play + 1:
return 2
if their_play == my_play + 1:
return -2
if my_play < their_play - 1:
return 1
return -1
def win_percentage_simple(my_play):
top = 0
bot = 0
for i in range(1, 6):
if i == my_play:
continue
if their_plays[i] > 0:
top += their_plays[i] * point_change_simple(my_play, i)
bot += their_plays[i]
if bot == 0:
return 0
return top / bot
max_percentage = 0
max_percentage_simple = -3
best_play = 1
for i in range(1, 5):
current_percentage = win_percentage(
i, my_points_in, their_points_in)
current_percentage_simple = win_percentage_simple(i)
if current_percentage > max_percentage or \
(current_percentage == max_percentage and
current_percentage_simple > max_percentage_simple):
best_play = i
max_percentage = current_percentage
max_percentage_simple = current_percentage_simple
return best_play
def get_plays(player_answers):
plays = [0, 0, 0, 0, 0, 0]
for answer in player_answers:
plays[max(0, min(answer, 5))] += 1
return plays
their_plays_1 = get_plays(player_answers)
their_plays_2 = get_plays(player_answers)
pattern_matcher(their_plays_2, player_answers)
for i in range(0, 3):
their_plays_1[random.randrange(1, 4)] += 1
their_plays_2[random.randrange(1, 4)] += 1
my_best_play_1 = get_best_play(their_plays_1,
scores['computer'], scores['player'])
their_plays_2[my_best_play_1] += 6
return get_best_play(their_plays_2, scores['player'], scores['computer'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment