Esempio n. 1
0
int main(int argc, char *argv[])
{
    /*! blitz window handle */
    HWND hWnd = 0;

    /*! blitz window client rectangle */
    RECT clientRect = { 0 };

    /*! directdraw interfaces */
    IDirectDraw         *pDD = NULL;
    IDirectDraw7        *pDD7 = NULL;

    /*! return code */
    HRESULT hRet;

    /*! advertise */
    {
        printf("autoblitz\n");
        printf("\n");
    }

    /*! ask user for blitz hwnd (found using spyxx, for example) */
    {
        printf("hwnd : ");
        scanf("%X", &hWnd);
    }

    /*! retrieve blitz window rectangle */
    GetWindowRect(hWnd, &windowRect);

    /*! retrieve blitz window client rectangle */
    GetClientRect(hWnd, &clientRect);

    /*! calculate blitz window width/height */
    windowWidth = windowRect.right - windowRect.left;
    windowHeight = windowRect.bottom - windowRect.top;

    /*! allocate rgb data */
    rgb32 = (uint32*)calloc(windowWidth*windowHeight*4, 1);

    /*! initialize capture */
    {
        rp_debug_trace("DirectDrawCreate\n");

        hRet = DirectDrawCreate(NULL, &pDD, NULL);

        if(FAILED(hRet)) { goto cleanup; }

        rp_debug_trace("pDD->QueryInterface\n");

        hRet = pDD->QueryInterface(IID_IDirectDraw7, (void**)&pDD7);

        if(FAILED(hRet)) { goto cleanup; }

        rp_debug_trace("pDD7->SetCooperativeLevel\n");

        hRet = pDD7->SetCooperativeLevel(NULL, DDSCL_NORMAL);

        if(FAILED(hRet)) { goto cleanup; }

        rp_debug_trace("pDD7->CreateSurface\n");

        /*! initialize surfaces */
        {
            DDSURFACEDESC2 ddsd;

            ZeroMemory(&ddsd, sizeof(ddsd));

            ddsd.dwSize = sizeof(ddsd);
            ddsd.dwFlags = DDSD_CAPS;
            ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

            hRet = pDD7->CreateSurface(&ddsd, &pDDSurface7, NULL);

            if(FAILED(hRet)) { goto cleanup; }

            rp_debug_trace("pDD7->CreateSurface (Sys)\n");

            ZeroMemory(&ddsd, sizeof(DDSURFACEDESC2));
            ZeroMemory(&ddsd.ddpfPixelFormat, sizeof(DDPIXELFORMAT));

            ddsd.dwSize = sizeof(ddsd);
            ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_LPSURFACE | DDSD_PITCH | DDSD_PIXELFORMAT | DDSD_CAPS;
            ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
            ddsd.dwWidth = windowWidth;
            ddsd.dwHeight = windowHeight;
            ddsd.lPitch = windowWidth*4;
            ddsd.lpSurface = rgb32;
            ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
            ddsd.ddpfPixelFormat.dwFlags= DDPF_RGB;
            ddsd.ddpfPixelFormat.dwRGBBitCount = (DWORD)4*8;
            ddsd.ddpfPixelFormat.dwRBitMask = 0x00FF0000;
            ddsd.ddpfPixelFormat.dwGBitMask = 0x0000FF00;
            ddsd.ddpfPixelFormat.dwBBitMask = 0x000000FF;

            hRet = pDD7->CreateSurface(&ddsd, &pDDSysSurface7, NULL);

            if(FAILED(hRet)) { goto cleanup; }
        }

        rp_debug_trace("Init success!\n");

        /*! press button to start the game */
        click_window(hWnd, 229, 196);

        while(true)
        {
            /*! wait for user to tell us when to continue on to the next move */
            //int ret = MessageBox(0, L"Press OK to take the next move (wait for animation to stop)", L"autoblitz [blog.caustik.com]", MB_OKCANCEL);

            /*! if cancel was pressed, we're done */
            //if(ret == IDCANCEL) { break; }

            /*! if mouse cursor is within the flash window, wait */
            {
                POINT pt;

                GetCursorPos(&pt);

                if( (pt.x > windowRect.left) && (pt.x < windowRect.right)
                 && (pt.y > windowRect.top)  && (pt.y < windowRect.bottom) )
                {
                    printf("(cursor is inside window..waiting)\n");
                    rp_thread::sleep(100);
                    continue;
                }
            }

            /*! current grid */
            blockColor gameBoard[8][8];

            /*! capture the board */
            capture_board(gameBoard);

            /*! display gameboard */
            {
                printf("\n");
                printf("- current game board -\n");
                printf("\n");

                display_board(gameBoard);
            }

            /*! check for end of game */
            {
                int x;

                for(x=0;x<8;x++)
                {
                    if(gameBoard[7][x] != BC_BLUE) { break; }

                    /*! if last row was entirely blue, click the "play again?" button */
                    if(x == 7)
                    {
                        int clickX = baseX + eachW*1 + halfW;
                        int clickY = baseY + eachH*6 + halfH;

                        click_window(hWnd, clickX, clickY);
                    }
                }
            }

            /*! analyze gameBoard and make the best move */
            {
                int aX=0, aY=0, bX=0, bY=0;

                /*! analyze gameBoard */
                analyze_board(gameBoard, aX, aY, bX, bY);

                /*! click first piece */
                {
                    int clickX = baseX + eachW*aX + halfW;
                    int clickY = baseY + eachH*aY + halfH;

                    click_window(hWnd, clickX, clickY);
                }

                rp_thread::sleep(150);

                /*! click second piece */
                {
                    int clickX = baseX + eachW*bX + halfW;
                    int clickY = baseY + eachH*bY + halfH;

                    click_window(hWnd, clickX, clickY);
                }
            }
#if 0
            /*! wait for animation to start */
            rp_thread::sleep(250);

            /*! wait for board to stabilize */
            while(true)
            {
                /*! current grid */
                blockColor tmpBoard[8][8];

                /*! wait a little */
                rp_thread::sleep(150);

                /*! capture current game board */
                capture_board(tmpBoard);

                /*! if board has not changed, stop waiting */
                if(memcmp(tmpBoard, gameBoard, sizeof(tmpBoard)) == 0) { break; }

                /*! remember this board for next loop */
                memcpy(gameBoard, tmpBoard, sizeof(tmpBoard));
            }
#endif
        }
    }

cleanup:

    /*! cleanup rgb pixels */
    if(rgb32 != 0) { free(rgb32); }

    /*! cleanup system service */
    if(pDDSysSurface7 != 0) { pDDSysSurface7->Release(); }

    /*! cleanup primary surface */
    if(pDDSurface7 != 0) { pDDSurface7->Release(); }

    /*! cleanup directdraw7 */
    if(pDD7 != 0) { pDD7->Release(); }

    /*! cleanup directdraw */
    if(pDD != 0) { pDD->Release(); }

    return 0;
}
Esempio n. 2
0
int main(int argc, char *argv[])
{
    char *descr;
    Board board;
    xmlDocPtr doc;
    xmlNodePtr gameNode, boardNode, analysisNode, node;

    if (argc != 2)
    {
        printf("usage: analyze <game.xml>\n");
        exit(1);
    }

    LIBXML_TEST_VERSION

    /* Parse input file */
    xmlInitParser();
    doc = xmlReadFile(argv[1], NULL, 0);
    if (doc == NULL)
    {
        fprintf(stderr, "Could not read/parse input file.\n");
        exit(1);
    }
    xmlCleanupParser();

    /* Process input data */
    gameNode = xmlDocGetRootElement(doc);
    if (strcmp((char*)gameNode->name, "game") != 0)
    {
        fprintf(stderr, "Root element should be <game>\n");
        exit(1);
    }
    boardNode = NULL;
    for (node = gameNode->children; node != NULL; node = node->next)
    {
        if (node->type == XML_ELEMENT_NODE)
        {
            if (strcmp((char*)node->name, "board") == 0)
            {
                if (boardNode != NULL)
                {
                    fprintf(stderr, "Multiple Mboard> elements found.\n");
                    exit(1);
                }
                boardNode = node;
            }
            if (strcmp((char*)node->name, "analysis") == 0)
            {
                xmlUnlinkNode(node);
                xmlFreeNode(node);
            }
        }
    }
    if (boardNode == NULL)
    {
        fprintf(stderr, "No <board> element found.\n");
        exit(1);
    }
    if ( boardNode->children == NULL ||
         boardNode->children != boardNode->last ||
         boardNode->children->type != XML_TEXT_NODE )
    {
        fprintf(stderr, "<board> should contain only text.\n");
        exit(1);
    }
    descr = (char*)xmlNodeGetContent(boardNode->children);

    /* Decode board */
    if (!board_decode_full(&board, descr))
    {
        fprintf(stderr, "Cannot decode full board description: %s\n", descr);
        exit(1);
    }

    /* Add analysis to data */
    analysisNode = analyze_board(&board);
    xmlAddChild(gameNode, analysisNode);

    /* Write output document */
    xmlDocDump(stdout, doc);
    xmlFreeDoc(doc);

    return 0;
}