def sums(board): intboard = board.send() for a in range(len(intboard)): for b in range(len(intboard[a])): intboard[a][b] = switch( intboard[a][b], case("O", lambda x=0: 1), case("X", lambda x=0: -1), default=(lambda x=0: x) ) _sums = [] # r1, r2, r3, c1, c2, c3, d1, d2 _sums.append(sum(intboard[0])) _sums.append(sum(intboard[1])) _sums.append(sum(intboard[2])) for a in range(len(board.pic)): if len(_sums) >= 4: _sums[3] += intboard[a][0] else: _sums.append(intboard[a][0]) if len(_sums) >= 5: _sums[4] += intboard[a][1] else: _sums.append(intboard[a][1]) if len(_sums) >= 6: _sums[5] += intboard[a][2] else: _sums.append(intboard[a][2]) _sums.append(intboard[0][0]) _sums[6] += intboard[1][1] _sums[6] += intboard[2][2] _sums.append(intboard[0][2]) _sums[7] += intboard[1][1] _sums[7] += intboard[2][0] return _sums
def load_switch(config, devices): from switches import switch for i in config: item = config[i] if "type" in item and item["type"] == "switch": state_topic = item["state_topic"] command_topic = item["command_topic"] pin = item["pin"] invert = not item["high_on"] device = switch(pin=pin, topic=state_topic, set_topic=command_topic, inverted=invert) devices.append(device) return devices
else: score = 0 children.append([BOARD(newboard), score]) next_player = "O" if (player == "X") else "X" branch_score = 0 for child in children: if child[1] == 0: child[1] = branch_eval(child[0], next_player) branch_score += child[1] return branch_score grid = [] grid.append(["*", "*", "*"]) grid.append(["*", "*", "*"]) grid.append(["*", "*", "*"]) grid = BOARD(grid, True) replay = True while replay: gameplay(grid, computer_move, human_move, game_over) again = input("\nPlay again? (y/n): ") grid.rebuild() replay = switch( again, case("Y", lambda x=0: True), case("y", lambda x=0: True), case("yes", lambda x=0: True), default=lambda x=0: False, )