Created
October 28, 2019 11:32
-
-
Save SiLiKhon/2678655342d2234736fdc0c9e525b511 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import math | |
import random | |
# Auto-generated code below aims at helping you parse | |
# the standard input according to the problem statement. | |
import numpy as np | |
my = np.zeros(shape=(3, 3), dtype=bool) | |
his = np.zeros(shape=(3, 3), dtype=bool) | |
def opt_to_field(opt): | |
return np.fromfunction(lambda x0, x1: (x0 == opt[0]) & (x1 == opt[1]), shape=(3, 3)) | |
def check_win(field): | |
return ( | |
field.all(axis=0).any() or | |
field.all(axis=1).any() or | |
field .diagonal().all() or | |
field.T.diagonal().all() | |
) | |
# game loop | |
while True: | |
opponent_row, opponent_col = [int(i) for i in input().split()] | |
if opponent_row >= 0: | |
his[opponent_row, opponent_col] = True | |
valid_action_count = int(input()) | |
options = [] | |
for i in range(valid_action_count): | |
options.append(tuple(int(j) for j in input().split())) | |
# Write an action using print | |
# To debug: print("Debug messages...", file=sys.stderr) | |
print(options) | |
best_opts = [] | |
for opt in options: | |
if check_win(my | opt_to_field(opt)): | |
best_opts.append(opt) | |
break | |
if check_win(his | opt_to_field(opt)): | |
best_opts.append(opt) | |
if opt == (1, 1): | |
best_opts.insert(0, opt) | |
if best_opts: | |
best_opt = best_opts[-1] | |
else: | |
best_opt = options[random.randint(0, valid_action_count - 1)] | |
my[best_opt] = True | |
print("{} {}".format(*best_opt)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment