Permalink
Comparing changes
Open a pull request
- 1 commit
- 5 files changed
- 0 commit comments
- 1 contributor
Commits on Mar 12, 2018
Unified
Split
Showing
with
74 additions
and 27 deletions.
- +20 −25 src/Grid.cpp
- +1 −1 src/Grid.h
- +1 −1 src/Makefile
- +46 −0 src/Random.cpp
- +6 −0 src/Random.h
| @@ -1,6 +1,6 @@ | ||
| #include "Grid.h" | ||
|
|
||
| #include <stdlib.h> | ||
| #include "Random.h" | ||
|
|
||
| Grid::Grid(int8 dimension) | ||
| { | ||
| @@ -12,7 +12,6 @@ void Grid::SetDimension(int8 dimension) | ||
| fDimension = dimension; | ||
| fData.clear(); | ||
| fData.resize(dimension * dimension, 0); | ||
| fGrid = fData; | ||
| } | ||
|
|
||
|
|
||
| @@ -24,11 +23,11 @@ void Grid::SetDimension(int8 dimension) | ||
|
|
||
| void Grid::Random(int8 minMoves) | ||
| { | ||
| const int8 numButtons = fGrid.size(); | ||
| const int8 numButtons = fData.size(); | ||
|
|
||
| // start with an empty grid | ||
| for (int8 index = 0; index < numButtons; index++) | ||
| fData[index] = fGrid[index] = 0; | ||
| fData[index] = 0; | ||
|
|
||
| /* | ||
| * Some of the eigenvectors | ||
| @@ -71,35 +70,31 @@ void Grid::Random(int8 minMoves) | ||
| if (minMoves > numButtons) | ||
| minMoves = numButtons; | ||
|
|
||
| for (int8 count = 0; count < minMoves; count++) { | ||
| int8 index; | ||
| int buttonIndices[numButtons]; | ||
|
|
||
| // next random button not already marked | ||
| do | ||
| index = random() % numButtons; | ||
| while (fGrid[index]); | ||
|
|
||
| fGrid[index] = 1; // mark the button to be pressed | ||
| } | ||
| for (int8 index = 0; index < numButtons; index++) | ||
| buttonIndices[index] = index; | ||
|
|
||
| const int8 begin = ChooseRandom(buttonIndices, numButtons, minMoves); | ||
| const int8 n = fDimension; | ||
|
|
||
| for (int8 index = 0; index < numButtons; index++) | ||
| if (fGrid[index]) { | ||
| if (index % n) // not leftmost column | ||
| fData[index - 1] = !fData[index - 1]; // left neighbor | ||
| for (int8 i = 0; i < minMoves; i++) { | ||
| const int8 index = buttonIndices[begin + i]; | ||
|
|
||
| if (index % n) // not leftmost column | ||
| fData[index - 1] = !fData[index - 1]; // left neighbor | ||
|
|
||
| if ((index + 1) % n) // not rightmost column | ||
| fData[index + 1] = !fData[index + 1]; // right neighbor | ||
| if ((index + 1) % n) // not rightmost column | ||
| fData[index + 1] = !fData[index + 1]; // right neighbor | ||
|
|
||
| if (index >= n) // not top row | ||
| fData[index - n] = !fData[index - n]; // neighbor above | ||
| if (index >= n) // not top row | ||
| fData[index - n] = !fData[index - n]; // neighbor above | ||
|
|
||
| if (index < n * (n - 1)) // not bottom row | ||
| fData[index + n] = !fData[index + n]; // neighbor below | ||
| if (index < n * (n - 1)) // not bottom row | ||
| fData[index + n] = !fData[index + n]; // neighbor below | ||
|
|
||
| fData[index] = !fData[index]; | ||
| } | ||
| fData[index] = !fData[index]; | ||
| } | ||
| } | ||
|
|
||
| bool Grid::ValueAt(int8 x, int8 y) | ||
| @@ -27,7 +27,7 @@ class Grid | ||
|
|
||
| private: | ||
| int8 fDimension; | ||
| grid fData, fGrid; | ||
| grid fData; | ||
| }; | ||
|
|
||
| #endif | ||
| @@ -29,7 +29,7 @@ APP_MIME_SIG = application/x-vnd.wgp-LightsOff | ||
| # same name (source.c or source.cpp) are included from different directories. | ||
| # Also note that spaces in folder names do not work well with this Makefile. | ||
| SRCS = AboutWindow.cpp App.cpp Grid.cpp GridView.cpp MainWindow.cpp \ | ||
| Preferences.cpp PuzzlePack.cpp TwoStateDrawButton.cpp | ||
| Preferences.cpp PuzzlePack.cpp TwoStateDrawButton.cpp Random.cpp | ||
|
|
||
| # Specify the resource definition files to use. Full or relative paths can be | ||
| # used. | ||
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright 2018 Haiku, Inc. | ||
| * Distributed under the terms of the MIT License. | ||
| * | ||
| * Author: | ||
| * Owen Pan <owen.pan@yahoo.com> | ||
| */ | ||
|
|
||
| #include "Random.h" | ||
|
|
||
| //#define NDEBUG | ||
| #include <assert.h> // assert | ||
|
|
||
| #include <stdlib.h> // random | ||
|
|
||
|
|
||
| static void | ||
| Swap(int& a, int& b) | ||
| { | ||
| assert (a != b); | ||
|
|
||
| const int tmp = a; | ||
| a = b; | ||
| b = tmp; | ||
| } | ||
|
|
||
|
|
||
| // array size >= n | ||
| int ChooseRandom(int array[], int n, int k) | ||
| { | ||
| assert(k > 0 && k < n); | ||
|
|
||
| const bool chooseComplement = n / k < 2; | ||
|
|
||
| if (chooseComplement) | ||
| k = n - k; | ||
|
|
||
| for (int i = 0; i < k; i++) { | ||
| const int offset = random() % (n - i); | ||
|
|
||
| if (offset > 0) | ||
| Swap(array[i], array[i + offset]); | ||
| } | ||
|
|
||
| return chooseComplement ? k : 0; | ||
| } |
| @@ -0,0 +1,6 @@ | ||
| #ifndef RANDOM_H | ||
| #define RANDOM_H | ||
|
|
||
| int ChooseRandom(int array[], int n, int k); | ||
|
|
||
| #endif |