LightsOut板载随机化器无法解决

时间:2018-12-16 21:51:23

标签: java

我正在尝试重新创建Lights Out,并使用棋盘随机分配器开始游戏。当我使用boardRandomizer时,即使我的代码从一个完全关闭的位置启动该板,然后使用Math.Random随机选取灯光x次数,我仍然发现该板无法解决,< / p>

ionic info

Ionic:

   ionic (Ionic CLI)  : 4.5.0 (/Users/me/node_modules/ionic)
   Ionic Framework    : ionic-angular 3.9.2
   @ionic/app-scripts : 3.2.0

Cordova:

   cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
   Cordova Platforms     : android 7.1.4, ios 4.5.5
   Cordova Plugins       : cordova-plugin-ionic-keyboard 2.0.5, cordova-plugin-ionic-webview 1.1.1, (and 9 other plugins)

System:

   NodeJS : v11.4.0 (/Users/me/.nvm/versions/node/v11.4.0/bin/node)
   npm    : 6.4.1
   OS     : macOS Mojave
   Xcode  : Xcode 10.1 Build version 10B61

package.json: ...

"scripts": {
    "start": "ionic-app-scripts serve",
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "lint": "ionic-app-scripts lint",
    "test": "karma start ./test-config/karma.conf.js",
    "test-ci": "karma start ./test-config/karma.conf.js --single-run",
    "test-coverage": "karma start ./test-config/karma.conf.js --coverage",
    "e2e": "npm run e2e-update && npm run e2e-test",
    "e2e-test": "protractor ./test-config/protractor.conf.js",
    "e2e-update": "webdriver-manager update --standalone false --gecko false"
  },
  "dependencies": {
    "@angular/animations": "5.2.11",
    "@angular/common": "5.2.11",
    "@angular/compiler": "5.2.11",
    "@angular/compiler-cli": "5.2.11",
    "@angular/core": "5.2.11",
    "@angular/forms": "5.2.11",
    "@angular/http": "5.2.11",
    "@angular/platform-browser": "5.2.11",
    "@angular/platform-browser-dynamic": "5.2.11",
    "@ionic-native/core": "^4.11.0",
    "@ionic-native/geolocation": "^4.10.0",
    "@ionic-native/google-analytics": "^4.17.0",
    "@ionic-native/google-maps": "^4.9.1",
    "@ionic-native/social-sharing": "^4.12.0",
    "@ionic-native/splash-screen": "~4.10.0",
    "@ionic-native/status-bar": "^4.10.1",
    "@ionic/storage": "^2.1.3",
    "chart.js": "^2.7.2",
    "cordova-android": "7.1.4",
    "cordova-ios": "4.5.5",
    "cordova-plugin-device": "^2.0.2",
    "cordova-plugin-geolocation": "^4.0.1",
    "cordova-plugin-google-analytics": "^1.8.3",
    "cordova-plugin-googlemaps": "^2.3.8",
    "cordova-plugin-googlemaps-sdk": "git+https://github.com/mapsplugin/cordova-plugin-googlemaps-sdk.git#2.7.0",
    "cordova-plugin-ionic-keyboard": "^2.0.5",
    "cordova-plugin-ionic-webview": "^1.1.19",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-statusbar": "^2.4.2",
    "cordova-plugin-whitelist": "^1.3.3",
    "cordova-plugin-x-socialsharing": "~5.4.0",
    "cordova-sqlite-storage": "^2.3.3",
    "es6-promise-plugin": "^4.2.2",
    "gulp": "^3.9.1",
    "ion2-calendar": "^2.2.0",
    "ionic-angular": "3.9.2",
    "ionicons": "3.0.0",
    "minimist": "^1.2.0",
    "moment": "^2.22.2",
    "ng2-charts": "^1.6.0",
    "rxjs": "5.5.11",
    "sw-toolbox": "3.6.0",
    "webpack": "^4.1.0",
    "xml2js": "^0.4.19",
    "zone.js": "0.8.26"
  },
  "devDependencies": {
    "@ionic/app-scripts": "3.2.0",
    "@types/jasmine": "^3.3.1",
    "@types/node": "^10.12.12",
    "angular2-template-loader": "^0.6.2",
    "html-loader": "^0.5.5",
    "istanbul-instrumenter-loader": "^3.0.1",
    "jasmine": "^3.3.1",
    "jasmine-spec-reporter": "^4.2.1",
    "karma": "^3.1.3",
    "karma-chrome-launcher": "^2.2.0",
    "karma-coverage-istanbul-reporter": "^2.0.4",
    "karma-jasmine": "^2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-webpack": "^3.0.5",
    "null-loader": "^0.1.1",
    "protractor": "^5.4.1",
    "ts-loader": "^5.3.1",
    "ts-node": "^7.0.1",
    "typescript": "^2.8.1"
  },

1 个答案:

答案 0 :(得分:1)

for (int randomCount = 0; randomCount <= 50; randomCount++) {
    randomRow = (int) (Math.random() * intBoardSize);
    randomColumn = (int) (Math.random() * intBoardSize);
    toggleLight(randomRow, randomColumn);
}

应该是

for (int randomCount = 0; randomCount <= 50; randomCount++) {
    randomRow = (int) (Math.random() * intBoardSize);
    randomColumn = (int) (Math.random() * intBoardSize);
    toggleAdjacentLights(randomRow, randomColumn);
} 

否则,随机发生器执行非法移动。

您应该重构代码,以使toggleLightBoard中的私有方法,以防止将来出现此类错误。

请考虑以下内容:

import java.util.Arrays;

class Board {
    public static void main(String[] args) {
        Board board = new Board();
        boardRandomize(board);
        System.out.println(Arrays.deepToString(board.boolLightState));
    }

    public static void boardRandomize(Board board) {
        board.resetBoard();

        int randomRow, randomColumn;

        for (int randomCount = 0; randomCount <= 50; randomCount++) {
            randomRow = (int) (Math.random() * board.intBoardSize);
            randomColumn = (int) (Math.random() * board.intBoardSize);
            board.toggleAdjacentLights(randomRow, randomColumn);
        }
    }

    private final int intBoardSize = 5;
    private boolean[][] boolLightState = new boolean[intBoardSize][intBoardSize];

    public void resetBoard() {
        for (int row = 0; row < intBoardSize; row++)
            for (int column = 0; column < intBoardSize; column++)
                boolLightState[row][column] = false;
    }

    public void toggleAdjacentLights(int row, int column) {

        toggleLight(row, column);

        if (row + 1 >= 0 && row + 1 < intBoardSize)
            toggleLight(row + 1, column);

        if (row - 1 >= 0 && row - 1 < intBoardSize)
            toggleLight(row - 1, column);

        if (column + 1 >= 0 && column + 1 < intBoardSize)
            toggleLight(row, column + 1);

        if (column - 1 >= 0 && column - 1 < intBoardSize)
            toggleLight(row, column - 1);

    }

    //make private to prevent access
    private void toggleLight(int row, int column) {
        if (boolLightState[row][column] == false)
            boolLightState[row][column] = true;
        else
            boolLightState[row][column] = false;
        // all of the above can be simplified to
        // boolLightState[row][column] = !boolLightState[row][column];
        repaint();
    }
}