Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <random>
  4. #include "timer.cpp"
  5.  
  6. // std::chrono::steady_clock?
  7. std::mt19937_64 mt_rand(time(0));
  8.  
  9. class Board
  10. {
  11. int * board;
  12. int width, height;
  13. int binMask;
  14. void reset(int);
  15. public:
  16. Board(int, int);
  17. void display();
  18. void newRandomBoard(int);
  19. void newRandomBoard2(int);
  20. int getSquare(int, int);
  21. int totalCallsToRand;
  22. };
  23.  
  24. Board::Board(int width, int height){
  25. // std::unique_pointer<int> ?
  26. std::cout << "In Board constructor" << std::endl;
  27. board = new int[height*width];
  28. for(int i=0; i<height*width; i++){
  29. board[i] = i%10;
  30. }
  31. binMask = 1;
  32. while(binMask < height*width) {
  33. binMask <<= 1;
  34. binMask++;
  35. }
  36. this->width = width;
  37. this->height = height;
  38. totalCallsToRand = 0;
  39. }
  40.  
  41. void Board::display() {
  42. int i, j;
  43.  
  44. std::cout << "+";
  45. for (i=0; i<width*2-1; i++){
  46. std::cout << "-";
  47. }
  48. std::cout << "+" << std::endl;
  49. for (i=0; i<height; i++){
  50. std::cout << "|";
  51. for (j=0; j<width; j++) {
  52. std::cout << getSquare(i, j);
  53. if (j < width-1){std::cout << " ";}
  54. }
  55. std::cout << "|" << std::endl;
  56. }
  57. std::cout << "+";
  58. for (i=0; i<width*2-1; i++){
  59. std::cout << "-";
  60. }
  61. std::cout << "+" << std::endl;
  62. }
  63.  
  64. int Board::getSquare(int row, int column){
  65. if (0 <= row && 0 <= column){
  66. if (row < height && column < width) {
  67. return board[row*width + column];
  68. }
  69. }
  70. std::cout << "Warning. Out of bounds." << std::endl;
  71. return -1;
  72. }
  73.  
  74. void Board::reset(int val){
  75. for (int i=0;i<width*height;i++){
  76. board[i] = val;
  77. }
  78. }
  79.  
  80. void Board::newRandomBoard(int mines){
  81. int area = width*height;
  82. if(mines > area/2){
  83. std::cout << "Too many mines" << std::endl;
  84. return;
  85. }
  86. reset(0);
  87. for (int i=0;i<mines;i++) {
  88. int done = 0;
  89. while (done == 0){
  90. int xy = mt_rand() & binMask;
  91. totalCallsToRand++;
  92. if (xy < area && board[xy] == 0){
  93. done++;
  94. board[xy] = 1;
  95. }
  96. }
  97. }
  98. }
  99.  
  100. void Board::newRandomBoard2(int mines) {
  101. int area = width*height;
  102. mines = (mines<0) ? 0 : ((mines>area) ? area : mines);
  103. int initialState = 0;
  104. int changedState = 1;
  105. if (mines > area/2) {
  106. initialState = 1;
  107. changedState = 0;
  108. mines = area - mines;
  109. }
  110. reset(initialState);
  111. int mask = 1;
  112. while(mask < area-mines){
  113. mask<<=1; mask++;
  114. }
  115. // Now we do the shuffle
  116. for (int i=area-mines; i<area; i++) {
  117. int gotRand = 0;
  118. int index;
  119. while(gotRand==0){
  120. index = mt_rand() & mask; // Take a power of 2 for good randomness
  121. totalCallsToRand++;
  122. if(index <= i){
  123. gotRand++;
  124. }
  125. }
  126. board[i] = board[index];
  127. board[index] = changedState;
  128. if (mask == i){
  129. mask<<=1; mask++;
  130. }
  131. }
  132. }
  133.  
  134. void test(Board board) {
  135. for(int i=0;i<1000;i++){
  136. board.newRandomBoard2(99);
  137. }
  138. std::cout << "Total rand calls: " << board.totalCallsToRand << std::endl;
  139. }
  140.  
  141. int main()
  142. {
  143. Board board(30, 16);
  144. Timer timer;
  145. timer.start();
  146. test(board);
  147. double elapsed = timer.stop();
  148. std::cout << elapsed << std::endl;
  149. std::cout << "Total rand calls: " << board.totalCallsToRand << std::endl;
  150. return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement