ctictactoe_lambda.py
1 # state = [[0 for _ in range(3)] for _ in range(3)] 2 3 # num_to_cell = lambda num: ((num - 1) % 3, 2 - (num - 1) // 3) 4 # cell_to_num = lambda x, y: (2 - y) * 3 + x + 1 5 # display = lambda numbers: print("\033c", end='') or ([print(("---+---+---\n" if i > 0 else "") + "|".join( 6 # [" " + ("\033[36m" + str(cell_to_num(j, i)) + "\033[0m" if numbers and x == 0 else " XO"[x]) + " " for j, x in 7 # enumerate(state[i])])) for i in range(3)] and None) 8 # userinput = lambda: [ 9 # (list(iter(lambda: (display(True) or xin.__setitem__(0, input(">> ")) or True) and xin[0] in valid, 10 # True)) and False) or [state[y].__setitem__(x, 1) for x, y in [num_to_cell(int(xin[0]))]] 11 # for xin, valid in [([""], [str(cell_to_num(j, i)) for i in range(3) for j in range(3) if state[i][j] == 0])] 12 # ] 13 # check_state = lambda: \ 14 # ([row[0] for row in state if row == [1] * 3 or row == [2] * 3] or 15 # [col[0] for col in zip(*state) if col == (1,) * 3 or col == (2,) * 3] or 16 # ([state[0][0]] if state[0][0] == state[1][1] == state[2][2] != 0 else []) or 17 # ([state[2][0]] if state[2][0] == state[1][1] == state[0][2] != 0 else []) or 18 # [-1 if any([state[y][x] == 0 for x in range(3) for y in range(3)]) else 0])[0] 19 20 # old_aimove = lambda: [ 21 # list(iter(lambda: ( 22 # value[0] is not None and state[value[0][1]][value[0][0]] == 0 or 23 # value.__setitem__(0, (random(), random())) and False), True)) 24 # and False or value[0] 25 # for value, random in [([None], lambda: __import__("random").randint(0, 2))] 26 # ][0] 27 28 # aimove = lambda: ([ 29 # (k, j) 30 # for i in [2, 1] for j in range(3) for k in range(3) for l in range(4) 31 # if (l == 0 and state[j][k] == 0 and state[j][(k+1)%3] == i and state[j][(k+2)%3] == i) or 32 # (l == 1 and state[j][k] == 0 and state[(j+1)%3][k] == i and state[(j+2)%3][k] == i) or 33 # (l == 2 and j == k and state[j][k] == 0 and state[(j+1)%3][(k+1)%3] == i and state[(j+2)%3][(k+2)%3] == i) or 34 # (l == 3 and j + k == 2 and state[j][k] == 0 and state[(j+1)%3][(k+2)%3] == i and state[(j+2)%3][(k+1)%3] == i) 35 # ] + ([(1, 1)] if True and state[1][1] == 0 else []) + [ 36 # list(iter(lambda: ( 37 # value[0] is not None and state[value[0][1]][value[0][0]] == 0 or 38 # value.__setitem__(0, (random(), random())) and False), True)) 39 # and False or value[0] 40 # for value, random in [([None], lambda: __import__("random").randint(0, 2))] 41 # ] 42 # )[0] 43 44 45 (lambda: [ 46 [ 47 [ 48 list(iter(lambda: ( 49 (userinput() and False or check_state() == -1 and [state[y].__setitem__(x, 2) for x, y in 50 [aimove()]]) and False or 51 check_state() != -1 52 ), True)) and 53 display(False) or 54 print(["Draw!", "You won!", "Computer won!"][check_state()]) 55 for userinput, aimove in [[ 56 lambda: [ 57 (list(iter(lambda: (display(True) or xin.__setitem__(0, input(">> ")) or True) and xin[0] in valid, 58 True)) and False) or [state[y].__setitem__(x, 1) for x, y in [num_to_cell(int(xin[0]))]] 59 for xin, valid in [([""], [str(cell_to_num(j, i)) for i in range(3) for j in range(3) if state[i][j] == 0])] 60 ], 61 lambda: ([ 62 (k, j) 63 for i in [2, 1] for j in range(3) for k in range(3) for l in range(4) 64 if (l == 0 and state[j][k] == 0 and state[j][(k+1)%3] == i and state[j][(k+2)%3] == i) or 65 (l == 1 and state[j][k] == 0 and state[(j+1)%3][k] == i and state[(j+2)%3][k] == i) or 66 (l == 2 and j == k and state[j][k] == 0 and state[(j+1)%3][(k+1)%3] == i and state[(j+2)%3][(k+2)%3] == i) or 67 (l == 3 and j + k == 2 and state[j][k] == 0 and state[(j+1)%3][(k+2)%3] == i and state[(j+2)%3][(k+1)%3] == i) 68 ] + ([(1, 1)] if True and state[1][1] == 0 else []) + [ 69 list(iter(lambda: ( 70 value[0] is not None and state[value[0][1]][value[0][0]] == 0 or 71 value.__setitem__(0, (random(), random())) and False), True)) 72 and False or value[0] 73 for value, random in [([None], lambda: __import__("random").randint(0, 2))] 74 ] 75 )[0] 76 ]] 77 ] 78 for display, check_state in [[lambda numbers: print("\033c" if ansi else "", end='') or ([print(("---+---+---\n" if i > 0 else "") + "|".join( 79 [" " + (("\033[36m" if ansi else "") + str(cell_to_num(j, i)) + ("\033[0m" if ansi else "") if numbers and x == 0 else " XO"[x]) + " " for j, x in 80 enumerate(state[i])])) for i in range(3)] and None), 81 lambda: ([row[0] for row in state if row == [1] * 3 or row == [2] * 3] or 82 [col[0] for col in zip(*state) if col == (1,) * 3 or col == (2,) * 3] or 83 ([state[0][0]] if state[0][0] == state[1][1] == state[2][2] != 0 else []) or 84 ([state[2][0]] if state[2][0] == state[1][1] == state[0][2] != 0 else []) or 85 [-1 if any([state[y][x] == 0 for x in range(3) for y in range(3)]) else 0])[0]]] 86 ] 87 for state, num_to_cell, cell_to_num, ansi in [( 88 [[0 for _ in range(3)] for _ in range(3)], 89 (lambda num: ((num - 1) % 3, 2 - (num - 1) // 3)), 90 (lambda x, y: (2 - y) * 3 + x + 1), 91 True 92 )] 93 ] and None)() 94 95 # list(iter(lambda: ( 96 # (userinput() and False or check_state() == -1 and [state[y].__setitem__(x, 2) for x, y in 97 # [aimove()]]) and False or 98 # check_state() != -1 99 # ), True)) and 100 # display(False) or 101 # print(["Draw!", "You won!", "Computer won!"][check_state()]) 102