Ejemplo n.º 1
0
def play():
    moves = []

    # Get rotation factor from first move
    display.draw_grid(init_board)
    move = input.get_move(players[0]) - 1
    trans = transformer.transformer(move)
    move = trans.transform(move)
    current_board = update_board(init_board, players[0], move)
    moves.append(move)

    # Get rotation / reflection from second move
    move = engine.get_move(current_board)
    trans.set_reflection(move)
    move = trans.rotate(move) if trans.first_move == 4 else trans.reflect(move)
    current_board = update_board(current_board, players[1], move)
    moves.append(move)

    # Continue
    turn = 0
    player = 0
    winner = None
    while turn < 7 and winner == None:
        if player == 0:
            display.draw_grid(trans.undo_transformation(current_board))
            move = trans.transform(input.get_move(players[player]) - 1)
        else:
            move = engine.get_move(current_board)
        current_board = update_board(current_board, players[player], move)
        moves.append(move)
        winner = engine.check_winner(current_board)
        turn += 1
        player = (player + 1) % 2

    display.draw_grid(trans.undo_transformation(current_board))
    if winner != None:
        engine.update_weights(moves, (player + 1) % 2)
        print '\n{0} wins!'.format(winner)
Ejemplo n.º 2
0
def adjacency_fn(grid, model):
    for point in grid.points:
        yield [
            edge.var for edge in point.edges()
            if model[edge.var].as_long() == 1
        ]


m = solve(s, g, adjacency_fn)


def cell_draw(ctx):
    ctx.fill(1, 0.5, 0.5, 1)


def edge_draw(ctx):
    if ctx.val == '1':
        ctx.draw(width=4)


def point_draw(ctx):
    #ctx.draw_square(size=7)
    if givens[ctx.gy][ctx.gx] == 'o':
        ctx.draw_circle(fill=False)
    elif givens[ctx.gy][ctx.gx] == '.':
        ctx.draw_circle(fill=True)


draw_grid(g, m, 64, cell_draw, edge_draw, edge_draw, point_draw)
Ejemplo n.º 3
0
            ]))

for x in range(g.width):
    for y in range(g.height):
        a = givens[y][x]
        if a == '0':
            s.add(g.cell(x, y).var == 0)
        elif a == '1':
            s.add(g.cell(x, y).var == 1)

for x in range(g.width):
    s.add(Sum([g.cell(x, y).var for y in range(g.height)]) == (g.height / 2))

for y in range(g.height):
    s.add(Sum([g.cell(x, y).var for x in range(g.width)]) == (g.width / 2))

s.check()
m = s.model()


def cell_draw(ctx):
    if givens[ctx.gy][ctx.gx] != ' ':
        ctx.fill(1, 0.7, 0.7, 1)
    else:
        ctx.fill(1, 1, 1, 1)

    ctx.circle(fill=(ctx.val == '0'))


draw_grid(g, m, 30, cell_draw)
Ejemplo n.º 4
0
m = s.model()


def cell_draw(ctx):
    ctx.fill(1, 0.5, 0.5, 1)
    given = givens[ctx.gy][ctx.gx]
    if given != ' ':
        ctx.text(str(ord(given) - ord('0')), fontsize=24)


def get_model(m, v):
    if isinstance(v, Invalid):
        return -1
    else:
        return m[v].as_long()


def vert_edge_draw(ctx):
    left = get_model(ctx.model, ctx.edge.cell_left.var)
    right = get_model(ctx.model, ctx.edge.cell_right.var)
    ctx.draw(width=5 if left != right else 1)


def horiz_edge_draw(ctx):
    top = get_model(ctx.model, ctx.edge.cell_above.var)
    bottom = get_model(ctx.model, ctx.edge.cell_below.var)
    ctx.draw(width=5 if top != bottom else 1)


draw_grid(g, m, 64, cell_draw, horiz_edge_draw, vert_edge_draw)