1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| #include "Game.h" using namespace sf;
Game::Game(std::vector<std::pair<int, int>> initValue) { int width = initValue[0].first, height = initValue[0].second; map = BoardMap(width, height); for (size_t i = 1; i < initValue.size(); i++) { int x = initValue[i].first, y = initValue[i].second; map.setBoxValue(x, y, true); } }
void Game::next() { if (!isRun()) { return; } map = map.gotoNext(); }
bool Game::isRun() { return map.isEmpty(); }
void Game::draw(RenderWindow& window) { int rows = map.getRows(); int cols = map.getCols(); window.clear(Color(234, 255, 208)); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { RectangleShape rectangle(Vector2f(CELL_SIZE, CELL_SIZE)); rectangle.setPosition(j * CELL_SIZE, i * CELL_SIZE); if (map.getBoxValue(i, j)) { rectangle.setFillColor(Color::White); } else { rectangle.setFillColor(Color::Black); } RectangleShape border(Vector2f(CELL_SIZE, CELL_SIZE)); border.setPosition(j * CELL_SIZE, i * CELL_SIZE); border.setFillColor(Color::Transparent); border.setOutlineThickness(1); border.setOutlineColor(Color(128, 128, 128)); window.draw(border); window.draw(rectangle); } } window.display(); }
void Game::run() { int rows = map.getRows(); int cols = map.getCols(); RenderWindow window(VideoMode(1000, 1000), "Board Map"); draw(window); int week = 3; while (window.isOpen()) { Event event; while (window.pollEvent(event)) { if (event.type == Event::Closed) window.close(); if (event.type == Event::KeyPressed) { int rows = map.getRows(); int cols = map.getCols(); if (event.key.code == Keyboard::Space) { map = map.gotoNext(); } } circles ++; if (circles % week == 0) { bool needExpand = false; int rows = map.getRows(); int cols = map.getCols(); for (int i = 0; !needExpand && i < week; i++) { for (int j = 0; j < cols; j++) { if (map.getBoxValue(i, j)) { needExpand = true; std::cout << "(" << i << "," << j << ") boo! " << std::endl; } } } for (int i = rows - week; !needExpand && i < rows; i++) { for (int j = 0; j < cols; j++) { if (map.getBoxValue(i, j)) { needExpand = true; std::cout << "(" << i << "," << j << ") boo! " << std::endl; } } } for (int j = 0; !needExpand && j < week; j++) { for (int i = 0; i < rows; i++) { if (map.getBoxValue(i, j)) { needExpand = true; std::cout << "(" << i << "," << j << ") boo! " << std::endl; } } } for (int j = cols - week; !needExpand && j < cols; j++) { for (int i = 0; i < rows; i++) { if (map.getBoxValue(i, j)) { needExpand = true; } } } if (needExpand) { std::cout << "get a bigger size." << std::endl;
map = map.expand(week);
rows = map.getRows(); cols = map.getCols(); window.create(VideoMode(cols * CELL_SIZE, rows * CELL_SIZE), "Board Map"); window.setSize(Vector2u(cols * CELL_SIZE, rows * CELL_SIZE));
} } } draw(window); } }
|