我正在为Android编写游戏。游戏的一种风格是让用户与机器对战。在每次游戏迭代中,用户或机器都会在板上放置满足游戏规则的瓷砖。轮到机器时,我在放置瓷砖时使用了一个简单的首次拟合算法。我遍历可用的图块,遍历电路板的行和列,并遍历每个图块的3次旋转,直到找到一个斑点。这可能是一个冗长的操作,所以我正在使用线程来剥离该工作。大约三分之二的时间,它可以完美工作。但是,有时会间歇性地停止搜索
一两个图块之后,没有错误消息。由于我在搜索之前将'found'标志设置为false,因此该线程返回,因为未找到任何内容,因此可以继续进行。但是,只要不间断地穿过瓷砖,就有可用的放置瓷砖的地方。

我的代码启动线程以及搜索循环如下。

-有人在代码中看到任何可疑的东西吗?
-我能做些什么来追踪为什么线程在完成之前和找到位置之前停止循环?我可以看到Log.d沿列/行和旋转以及每个图块的顶点喷出,只是退出了,有时仅经过一两列,有时它会一直遍历数据。

启动线程并等待结果

private void playDeviceTile() {

    if (mDbHelper.isPlayingDevice(game_id)) {

        // make sure I'm seeing all played tiles
        refreshPlayedTiles();

        final Boolean found;



        final ProgressDialog dialog = new ProgressDialog(mcontext);
        dialog.setMessage("Android is thinking...");
        dialog.show();

            new AsyncTask<Void,Void, Boolean>(){
                @Override
                protected void onPostExecute(Boolean isFound) {
                    if (!isFound) {

                            passPlay(1);  // never found a tile to play. We have to pass
                        }
                        else {
                    playTile(currentTile,1);

                    }
                    dialog.dismiss();
                    postInvalidate();
                    invalidate();
                }

                @Override
                protected Boolean doInBackground(Void... params) {
                    try {
                        return doSearchforSpot();
                    } catch (Exception e) {
                        Log.e("DRAW", "Exception find spot for device tile", e);
                    }
                    return null;
                }

            }.execute();

    }
}


进行实际搜索

private Boolean doSearchforSpot(){
    Boolean found = false;
    tileClass tile;
    int tileNum;
    BoardClass spot;
    int rot;
    int maxrot;
    int score = -1;
    int i = 0;

    // totally brain-less, brute-force, 1st fit.  Loop through
    // all open spots on the board, and check each tile in each rotation
    // and return the first time a valid placement and score occurs.

    while ((i < deviceHandNumber) && (!found)) {
        tileNum = deviceHand[i];
        tile = tiles.get(tileNum);
        int row = TileExtents[TOP];
        int col = TileExtents[LEFT];
        while ((row <= TileExtents[BOTTOM]) && (!found)) {
            while ((col <= TileExtents[RIGHT]) && (!found)) {

                spot = board.get(ColRowGrid[col][row]);
                if (!spot.occupied) {
                    if (spot.facedown) {
                        rot = 3;
                        maxrot = 6;
                    }
                    else {
                        rot = 0;
                        maxrot = 3;
                    }
                    while ((rot < maxrot) && (!found)) {
                        // set the rotation and check for a score
                        tile.setRotate(rot);
                        Log.d("WTF","CRT "+col+" "+row+" "+rot+" "+tile.getScore(0)+" "+tile.getScore(1)+" "+tile.getScore(2));
                        score = placeTileOnBoard(spot.index, tileNum);

                        if (score >= 0) {
                            // hey! we found a spot. Play it.
                            turnScore = score;
                            currentTile = tileNum;
                            found = true;
                        }
                        rot++;
                    }

                }
                col++;

            }
            row++;
            col=TileExtents[LEFT];
        }
        i++;

    }
    return found;

}

最佳答案

我前一段时间解决了这个问题。尽管人们的意见与使用异步或线程无关。这只是我的多线程代码覆盖变量的一种情况。我的代码的另一部分检查边界-首先将它们清零。我的搜索代码使用了这些边界。我只是没有意识到他们正在搜索过程中进行更新,因此很难找到。

07-28 01:21
查看更多