tetris: replace PieceStuck() with InvalidMove() #421

pull/426/head
nick black 5 years ago
parent 1665bbad8b
commit cb85e120e0
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -3,7 +3,13 @@ bool MoveDown() {
const std::lock_guard<std::mutex> lock(mtx_); const std::lock_guard<std::mutex> lock(mtx_);
int y, x; int y, x;
if(PrepForMove(&y, &x)){ if(PrepForMove(&y, &x)){
if(PieceStuck()){ if(!curpiece_->move(y + 1, x)){
throw TetrisNotcursesErr("move()");
}
if(InvalidMove()){
if(!curpiece_->move(y, x)){
throw TetrisNotcursesErr("move()");
}
if(y <= board_top_y_ - 1){ if(y <= board_top_y_ - 1){
return true; return true;
} }
@ -11,9 +17,6 @@ bool MoveDown() {
curpiece_ = NewPiece(); curpiece_ = NewPiece();
}else{ }else{
++y; ++y;
if(!curpiece_->move(y, x)){
throw TetrisNotcursesErr("move()");
}
} }
if(!nc_.render()){ if(!nc_.render()){
throw TetrisNotcursesErr("render()"); throw TetrisNotcursesErr("render()");

@ -1,34 +1,3 @@
bool InvalidMove() { // a bit wasteful, but piece are tiny
int dy, dx;
curpiece_->get_dim(&dy, &dx);
while(dy--){
int x = dx;
while(x--){
ncpp::Cell c, b;
if(curpiece_->get_at(dy, x, &c) < 0){
throw TetrisNotcursesErr("get_at()");
}
if(c.is_simple()){
continue;
}
curpiece_->release(c);
int transy = dy, transx = x; // need game area coordinates via translation
curpiece_->translate(*board_, &transy, &transx);
if(transy < 0 || transy >= board_->get_dim_y() || transx < 0 || transx >= board_->get_dim_x()){
return true;
}
if(board_->get_at(transy, transx, &b) < 0){
throw TetrisNotcursesErr("get_at()");
}
if(!b.is_simple()){
return true;
}
board_->release(b);
}
}
return false;
}
void RotateCcw() { void RotateCcw() {
const std::lock_guard<std::mutex> lock(mtx_); const std::lock_guard<std::mutex> lock(mtx_);
int y, x; int y, x;

@ -1,38 +1,29 @@
bool PieceStuck() { bool InvalidMove() { // a bit wasteful, but piece are tiny
if(curpiece_){ int dy, dx;
// check for impact. iterate over bottom row of piece's plane, checking for curpiece_->get_dim(&dy, &dx);
// presence of glyph. if there, check row below. if row below is occupied, while(dy--){
// we're stuck. int x = dx;
int y, dimx, x;
curpiece_->get_dim(&y, &dimx);
std::vector<bool> columns(dimx, false); // bitmap for column verification
int checked = 0;
while(y--){
x = dimx;
while(x--){ while(x--){
if(!columns[x]){ ncpp::Cell c, b;
ncpp::Cell piecec; if(curpiece_->get_at(dy, x, &c) < 0){
if(curpiece_->get_at(y, x, &piecec) < 0){
throw TetrisNotcursesErr("get_at()"); throw TetrisNotcursesErr("get_at()");
} }
if(piecec.is_simple()){ if(c.is_simple()){
continue; continue;
} }
int cmpy = y + 1, cmpx = x; // need game area coordinates via translation curpiece_->release(c);
curpiece_->translate(*board_, &cmpy, &cmpx); int transy = dy, transx = x; // need game area coordinates via translation
ncpp::Cell c; curpiece_->translate(*board_, &transy, &transx);
if(board_->get_at(cmpy, cmpx, &c) < 0){ if(transy < 0 || transy >= board_->get_dim_y() || transx < 0 || transx >= board_->get_dim_x()){
throw TetrisNotcursesErr("get_at()");
}
if(!c.is_simple()){
return true; return true;
} }
columns[x] = true; if(board_->get_at(transy, transx, &b) < 0){
if(++checked == dimx){ throw TetrisNotcursesErr("get_at()");
return false;
}
} }
if(!b.is_simple()){
return true;
} }
board_->release(b);
} }
} }
return false; return false;

Loading…
Cancel
Save