Created
September 28, 2018 13:10
-
-
Save rurumimic/82ce668890acadcb327f940beb564daa to your computer and use it in GitHub Desktop.
C++ Board
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <deque> | |
using namespace std; | |
template <typename T> | |
struct Board { | |
int w; | |
int h; | |
vector<T> board; | |
Board(int _w, int _h, T _v) : w(_w), h(_h) { | |
board.assign(w * h, _v); | |
} | |
T get(int _w, int _h) { | |
return board[_h * w + _w]; | |
} | |
void set(int _w, int _h, T newValue) { | |
board[_h * w + _w] = newValue; | |
} | |
void print() { | |
for (int y = 0; y < h; y++) { | |
for (int x = 0; x < w; x++) { | |
cout << get(x, y) << " "; | |
} | |
cout << endl; | |
} | |
} | |
}; | |
template <typename T> | |
void print(Board<T> *board) { | |
for (int y = 0; y < board->h; y++) { | |
for (int x = 0; x < board->w; x++) { | |
cout << board->get(x, y) << " "; | |
} | |
cout << endl; | |
} | |
} | |
int main(int argc, const char * argv[]) { | |
Board<int> * intBoard = new Board<int>(3, 4, 0); | |
// 0 0 0 | |
// 0 0 0 | |
// 0 0 0 | |
// 0 0 0 | |
intBoard->set(0, 0, 1); | |
intBoard->set(1, 2, 2); | |
intBoard->set(2, 3, 3); | |
// 1 0 0 | |
// 0 0 0 | |
// 0 2 0 | |
// 0 0 3 | |
// 3 way of printing | |
// 1. template | |
print(intBoard); | |
cout << endl; | |
// 2. in struct | |
intBoard->print(); | |
cout << endl; | |
// 3. direct | |
for (int y = 0; y < intBoard->h; y++) { | |
for (int x = 0; x < intBoard->w; x++) { | |
cout << intBoard->get(x, y) << " "; | |
} | |
cout << endl; | |
} | |
cout << endl; | |
/////////////////////////////////////////////////////// | |
Board<char> * charBoard = new Board<char>(3, 4, ' '); | |
// | |
// | |
// | |
// | |
charBoard->set(0, 0, 'O'); | |
charBoard->set(1, 2, 'O'); | |
charBoard->set(2, 3, 'O'); | |
// O | |
// | |
// O | |
// O | |
print(charBoard); | |
/////////////////////////////////////////////////////// | |
Board<string> * strBoard = new Board<string>(3, 4, "."); | |
// | |
// | |
// | |
// | |
strBoard->set(0, 0, "X"); | |
strBoard->set(1, 2, "X"); | |
strBoard->set(2, 3, "X"); | |
// X | |
// | |
// X | |
// X | |
print(strBoard); | |
/////////////////////////////////////////////////////// | |
// copy board | |
cout << "copy board before" << endl; | |
Board<int> * copiedBoard = new Board<int>(intBoard->w, intBoard->h, 0); | |
copiedBoard->print(); | |
cout << "copy board after" << endl; | |
copy(intBoard->board.begin(), intBoard->board.end(), copiedBoard->board.begin()); | |
copiedBoard->print(); | |
// check board | |
cout << "checkboard" << endl; | |
intBoard->set(0, 0, 1); | |
intBoard->set(0, 1, 1); | |
intBoard->set(1, 0, 1); | |
intBoard->set(1, 1, 1); | |
intBoard->print(); | |
cout << endl; | |
Board<bool> * checkBoard = new Board<bool>(intBoard->w, intBoard->h, false); | |
for (int y = 0; y < (intBoard->h - 1); y++) { | |
for (int x = 0; x < (intBoard->w - 1); x++) { | |
if (intBoard->get(x, y) == 0) { | |
continue; | |
} | |
if (intBoard->get(x+1, y) == 1 | |
&& intBoard->get(x, y+1) == 1 | |
&& intBoard->get(x+1, y+1) == 1) { | |
checkBoard->set(x, y, true); | |
checkBoard->set(x+1, y, true); | |
checkBoard->set(x, y+1, true); | |
checkBoard->set(x+1, y+1, true); | |
} | |
} | |
} | |
checkBoard->print(); | |
cout << endl; | |
// remove dupli | |
cout << "remove dupli of intBoard" << endl; | |
for (int y = 0; y < checkBoard->h; y++) { | |
for (int x = 0; x < checkBoard->w; x++) { | |
if (checkBoard->get(x, y) == true) { | |
intBoard->set(x, y, 0); | |
} | |
} | |
} | |
intBoard->print(); | |
cout << endl; | |
// down blocks | |
cout << "down intBoard" << endl; | |
for (int x = 0; x < intBoard->w; x++) { | |
deque<int> buffer; | |
buffer.assign(intBoard->h, 0); | |
for (int y = 0; y < intBoard->h; y++) { | |
if (intBoard->get(x, y) != 0) { | |
buffer.push_front(intBoard->get(x, y)); // insert front, count = height + 1 | |
buffer.pop_back(); // count = height | |
} | |
} | |
for (int y = 0; y < intBoard->h; y++) { | |
intBoard->set(x, y, buffer.back()); | |
buffer.pop_back(); | |
} | |
} | |
intBoard->print(); | |
cout << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment